C ++ and FULLY dynamic functions

I have a problem with workarounds. Bypass, as you all know, can only move between 5 bytes of space (for example, "jmp" and a 4-byte address). Because of this, it is impossible to have a “hook” function in a class (method), you cannot specify a 'this' pointer because there is simply not enough space ( the problem is explained in more detail here ). Thus, I have been collecting brains all day for a solution, and now I need your thoughts on this issue, so I do not start the project for 3-5 days, not knowing whether this is possible or not.

At first I had 3 goals, I wanted the hook functions to be cool methods, I wanted the whole approach to be object oriented (no static functions or global objects), and the worst / most difficult part was completely dynamic. This is my (theoretically) decision; with assembly, you can change functions at runtime (any workaround is an ideal example). Since I can change functions dynamically, shouldn't I also create them dynamically? For example; I allocate memory, say ~ 30 bytes (via malloc / new). It would be impossible to simply replace all bytes with binary numbers corresponding to different assembly operators (for example, 0xE9 is "jmp"), and then call the address directly (since it will contain the function)?

NOTE. I know in advance the return value and all the arguments to all the functions that I want to combine, and since I use GCC, thiscall convention is almost identical to _cdecl.

So this is my thought / future implementation; I create a class "Function". This constructor accepts a variable number of arguments (except for the first argument, which describes the return value of the objective function).

Each argument is a description of the arguments the hook will receive (size, whether it is a pointer or not). So let me say that I want to create a Function class for int * RandomClass::IntCheckNum(short arg1); . Then I just needed to do this: Function func(Type(4, true), Type(4, true), Type(2, false)); . Where "Type" is defined as Type(uint size, bool pointer) . Then, through the assembly, I could dynamically create a function (note: all this will use the _cdecl calling convention), since I can calculate the number of arguments and the total size.

EDIT: in the example, Type(4, true) is the return value (int *), scond Type(4, true) is the RandomClass 'this' pointer, and Type(2, false) describes the first argument (short arg1).

With this implementation, I could easily use class methods as callbacks, but this would require an extensive amount of build code (which I didn't even really feel). In the end, the only non-dynamic thing will be the methods in my callback class (which will also require preliminary and subsequent callbacks).

So, I wanted to know; Is it possible? How much work will it take and am I here above my head?

EDIT: Sorry if I presented everything a little fuzzy, but if there is anything you want to explain in more detail, ask!

EDIT2: I would also like to know if I can find the hexadecimal values ​​somewhere for all the assembly statements? The list will help a ton! And / or if you can somehow "save" asm (""); code at memory address (which I highly doubt).

+6
source share
1 answer

What you describe is usually called "thunking" and is commonly used. Historically, the most common goal was to compare between 16-bit and 32-bit code (by auto-generating a new 32-bit function that calls an existing 16-bit or vice versa). I believe that some C ++ compilers generate similar functions for setting base class pointers to subclass pointers in multiple inheritance.

This certainly looks like a viable solution to your problem, and I do not foresee any serious problems. Just make sure that you allocate memory with any flags necessary on your operating system to make sure that the memory is complete (most modern operating systems default to non-executable memory).

You may find this link useful, especially if it works in Win32: http://www.codeproject.com/Articles/16785/Thunking-in-Win32-Simplifying-Callbacks-to-Non-sta

Regarding the search for hexadecimal values ​​of assembly operations, the best reference that I know of is the NASM Assembler Guide Appendix (and I'm not just saying this because I helped write it). There is a copy here: http://www.posix.nl/linuxassembly/nasmdochtml/nasmdoca.html

+4
source

All Articles