View Single Post
  #1  
Old 10-21-2021, 16:34
Succubus Succubus is offline
Friend
 
Join Date: Oct 2021
Location: Japan
Posts: 6
Rept. Given: 0
Rept. Rcvd 0 Times in 0 Posts
Thanks Given: 1
Thanks Rcvd at 12 Times in 4 Posts
Succubus Reputation: 1
Lightbulb [C++] Calling any function dynamically without typedef

CODE

PHP Code:
template<typename DATA_TYPEtypename Ttypename... PARAMS>
DATA_TYPE dynamic_call(T addressPARAMS... args)
{
    return ((
DATA_TYPE(__fastcall *)(...))reinterpret_cast<void*>(address))(args...);

USAGE

PHP Code:
auto sum dynamic_call<int>(0xdeadbeef12);
printf("\nsum: %d\n"sum); 
MORE INFO

PHP Code:
// <int> = return type 
// 1st param = target address
// 2nd param = arguments
// return type can be anything like <const char*> <char*> etc 
OTHER

This is kinda useful if you don't like to write typedef at all or if you have ida pro and u want to copy the source code of the whole function without typing the typedef of each call inside that function.

Sometimes the function source code in IDA PRO got weird calls like the first param is a 'this' and it does this + any dword. and it seems like ida treat it as a function like

v31 = *(__int64* (this + 0x31))(v1);

something or similar like that.. you can now just do that also

PHP Code:
// xD well you cant call it this because thats reserve
// I asume you have like void* _this as your first param for example                    
auto set_hp dynamic_call<__int64*>(_this 0x31100);
printf("\ntest: %d\n"set_hp); 
This help also to make your source cleaner, less typedef for those functions you really don't care about and you just wanna try it or experiment with it.

OTHER USAGE

I also uses this on hook

PHP Code:
DWORD64 osample;

zend_op_array__fastcall sample(__int64 a1DWORD *a2int *a3) {

    
auto ok dynamic_call<zend_op_array*>(osamplea1a2a3);

    return 
ok;
}

....
MH_CreateHook(reinterpret_cast<void*>(0xdeadbeef), &samplereinterpret_cast<void**>(&osample));
MH_EnableHook(reinterpret_cast<void*>(0xdeadbeef)); 
no need typedef

Last edited by Succubus; 10-21-2021 at 17:07. Reason: added more example
Reply With Quote
The Following 5 Users Say Thank You to Succubus For This Useful Post:
b30wulf (10-22-2021), Fyyre (10-27-2021), niculaita (10-22-2021), tonyweb (09-08-2022)