Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 23:00

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Automated function hooking for any calling convention in runtime?

Discussion on Automated function hooking for any calling convention in runtime? within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #1

 
Mega Byte's Avatar
 
elite*gold: 0
Join Date: Dec 2009
Posts: 1,777
Received Thanks: 1,003
Lightbulb Automated function hooking for any calling convention in runtime?

Okay so this is a crazy thought I had.
Not quite sure how to implement it assume a C++ compiled target win32 x86.


A DLL once injected and given a memory range in the code section will locate functions, identify their calling convention and args, hook them dynamically and log when they are called, what the args contain and returned value.
All at runtime, okay static analysis assisted might be fine too.

Yes I know we can just use a static analyzer or debugger and do them on our own one by one but how might a program be developed to help automate this tedious process?

With a disassembly engine it could process the bytes back into opcodes and run scans for patterns of interest there are also some nifty ones that have good attempts at "decompiling" it back into code.

Even better if it can auto locate vtables, generate signatures to scan for the function addresses in modified versions of the executable target and provide an API to hook them.


Ideally the end product scenario is, once a user has a list of all functions and have seen some interesting things in the args they could simply select the ones they are interested in from a list and click a button to generate C++ source for a dll base to further build on containing all of the typedefs and hook functions that just call the original method by default.


Also I can't seem to get the hang of hooking vardic functions either (variable number of args e.g. think sprintf with the ... and a format string) getting the number of arguments seems tricky?


TLDR; How could a program know how many arguments a function has at and where those arguments come from, construct the relevant hook and log them at runtime then use that information to generate C++ hook template source.

Or am I just being silly?
Mega Byte is offline  
Old 09/26/2019, 14:27   #2
 
elite*gold: 0
Join Date: Feb 2009
Posts: 1,137
Received Thanks: 573
Quote:
Originally Posted by Mega Byte View Post
TLDR; How could a program know how many arguments a function has at and where those arguments come from, construct the relevant hook and log them at runtime then use that information to generate C++ hook template source.
You might want to take a look at which is an open source reverse engineering tool, with quite a decent decompiler. If you where lazy you could basically just copy their code for detecting functions, but I don't know if this really satisfies you.
What you are trying to archive is not really trivial, but let's get through this step by step

Quote:
Originally Posted by Mega Byte View Post
A DLL once injected and given a memory range in the code section will locate functions, identify their calling convention and args, hook them dynamically and log when they are called, what the args contain and returned value.
All at runtime, okay static analysis assisted might be fine too.
Let's start off with the obvious, how does a call work on x86. Im by no means an assembly pro, so this is very superficial, but I think it should be enough to answer your question
Whenever calling a function the parameter are pushed onto the stack or written into register (or both) on x86_64 you have much more registers, giving you more possibilities. The called function basically can address the arguments than by accessing the values below (well technically above, the stack grows down in the addressspace) the return address (well theres also a stack cookie, but for this explaination we will ignore it) on the stack. The calling convention than basically explains how these values are pushed on the stack (order, etc.), which registers are used (usually not so many under x86, but under x86_64 this is a whole different story) and how the stack in general is prepared for the call, and who (and how) this is cleaned up after the call. The details are pretty neat explained in . Also you don't need to deduce the original calling convention, you just need to find a calling convention that fits (i.e. afaik pascal and stdcall calling conventions are basically the same except for the order in which the arguments are pushed onto the stack, therefore you can replace one with the other by just excanging the order of arguments)

Basically your best chance to deduce this is to search in the code page for call instructions to a given location, and than analyze how the call is prepared (i.e. what is pushed onto the stack, and how). I.e. scan the whole binary, for each call instruction look at the preparation just before the call and deduce thereby the (or better a) signature of the called function

Quote:
Originally Posted by Mega Byte View Post
With a disassembly engine it could process the bytes back into opcodes and run scans for patterns of interest there are also some nifty ones that have good attempts at "decompiling" it back into code.
Basically how Ghidra works.

Quote:
Originally Posted by Mega Byte View Post
Even better if it can auto locate vtables, generate signatures to scan for the function addresses in modified versions of the executable target and provide an API to hook them.
Thats more or less impossible. vtables are a high level construct, which are soly handled on the frontend. E.g. assume you have a programm written in Rust using gcc C++, intel C++ and Fortran Libraries. Fortran doesn't have vtables at all, and I don't know much about rust, but it's safe to assume that gcc C++, rust and intel C++ might completely use different vtable formats, making it impossible to recognize vtables from just the resulting assembly. Even different versions of the same compiler might be completely incompatible with each other.
And than there is also the Optimizer. If the optimizer can proof that you don't need a vtable, you won't have a vtable, simple as that. In general does optimization makes it usually harder to disassemble


Quote:
Originally Posted by Mega Byte View Post
Ideally the end product scenario is, once a user has a list of all functions and have seen some interesting things in the args they could simply select the ones they are interested in from a list and click a button to generate C++ source for a dll base to further build on containing all of the typedefs and hook functions that just call the original method by default.
I think this is not ase usefull as you might think. In a non trivial project there are hundreds of functions that have the same signature, and having a list of them will just be completely useless. I just fired up ghidra and looked into the function list, and having a whole list of functions like:
Code:
void FUN_100002b30(undefined8 uParm1,code *pcParm2)
undefined8 FUN_100002b40(void)
void FUN_100002b50(undefined8 uParm1)
ulonglong FUN_100002be0(void)
void FUN_100002c20(void)
undefined8 FUN_100002c30(void)
void FUN_100002c40(undefined8 *puParm1,undefined8 *puParm2,ulonglong uParm3)
void FUN_100002f40(longlong *plParm1,ulonglong uParm2,byte bParm3)
undefined * FUN_100003050(ulonglong uParm1,undefined *puParm2,char cParm3)
ulonglong FUN_1000030c0(ulonglong uParm1,ulonglong uParm2,short sParm3)
...
Doesn't tell you shit without closer inspection of these functions. And if you analyze these functions to see whether or not it is what you are searching for, you can also easiely deduce the signature, meaning you don't gain shit from fully automated signature recognicion

Quote:
Originally Posted by Mega Byte View Post
Also I can't seem to get the hang of hooking vardic functions either (variable number of args e.g. think sprintf with the ... and a format string) getting the number of arguments seems tricky?
You simply can't get the number of arguments, thats the fucking joke.
Varargs are an abomination, as they basically just say: push as many arguments on the stack as you like. It's the callees job to somehow make sense of it.
Thats why you can use printf to read arbitrary memory locations. e.g. take a look at the following Code
Code:
char buff[1024];
scanf("%s", buff);
printf(buff);
if you supply as string %d%d%d%d, it will start dumping the stack, even though no variable was given to printf, because there is no way to check the amount of variables. By using printf you make the "promise" that you won't have more formatters in there than variables you pass. If you don't... well printf doesn't care. This get's especially interesting if you write adresses into the string, because after a few formatters, the next address read will be buffer itself, meaning you can print things from your buffer. Using %s it also can chase pointers, this means by writing an arbitrary address into the buffer and reading it via %s, you can literally read any arbitrary memory location. It is even worse if you use %n, as this writes the number of printed charactes into the address provided, meaning with the code above, you can read and write arbitrary memory locations.
warfley is offline  
Reply

Tags
callingconvention, class, hook, idea, vtable


Similar Threads Similar Threads
[Selling] 2014 Guan YU Convention Skin/Red Kali Convention Skin
06/15/2016 - Smite Trading - 4 Replies
Hello, first of all I cannot always say these work because they have been in a box for a year/longer. If a Code doesn't work ill send a new one. I gathered them at the Gamescom in the year 2014/2013. I have no clue how much they are worth so I'm open for offers Payment Method: Offline PSC/Paypal Offers/CSGO Skins. Trade will be done with Teamviewer while I record the giving of the skin. The Code has to be redeemed ingame. If 1 Skin Code doesnt work I'll give out the next one. Proof:...
Calling Convention Problem bei Hook
05/02/2015 - C/C++ - 3 Replies
Hey, ich versuche derzeit die Argumente einer Funktion von einem Spiel zu bekommen mithilfe eines MS Detours. Also wenn ich z.B die MessageBoxW Funktion detouren möchte, mache ich es folgendermaßen: #include <windows.h> #include <detours.h> #pragma comment(lib, "detours.lib") typedef int (WINAPI *MessageBoxType) (HWND, LPCTSTR, LPCTSTR, UINT);
Parameter und Calling Convention finden
08/21/2014 - C/C++ - 14 Replies
Hey, habe in einem Spiel mit ollydbg eine Funktion gefunden, von der ich glaube, dass sie Text auf dem Bildschirm ausgibt: Address Hex dump Command Comments 013CDB28 F30F1085 6CFFFF MOVSS XMM0,DWORD PTR SS: ; FLOAT 42.00000 013CDB30 8D85 78FFFFFF LEA EAX, 013CDB36 F30F5805 5850CE ADDSS XMM0,DWORD PTR DS: ; FLOAT 20.00000 (y coord?) 013CDB3E D985 70FFFFFF FLD DWORD PTR SS: ; FLOAT 1760.00000 (x coord?)
std::function of a function returning an std::function
11/11/2013 - C/C++ - 19 Replies
Nun muss ich nach langer Zeit auch mal wieder einen Thread erstellen, weil mir Google nicht mehr weiterhelfen kann. Ich verzweifle an Folgendem Vorhaben: #include <Windows.h> #include <string> #include <iostream> using namespace std;
Wrong Calling Convention! (Run-Time Check Failure #0)
01/11/2010 - General Coding - 4 Replies
Greetings, hijax



All times are GMT +1. The time now is 23:02.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.