Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Dynamic function argument counting

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Banjo Patterson
Cheater
Reputation: 1

Joined: 15 May 2018
Posts: 36

PostPosted: Sun Jun 21, 2020 6:17 pm    Post subject: Dynamic function argument counting Reply with quote

Hey everyone,

I'm working on a project at the moment and I'm trying to figure out the best way to dynamically learn the amount of arguments that a particular function takes with respect to x86 32 bit processes.

This project attaches to a process and hooks a particular set of functions so it can run some code before jumping back to the original function. As a note these functions are all thiscalls.

I've had a few ideas so far, but none of these are particularly reliable without a slew of hard-coded conditions. My ideas so far:

-Read the stack for pointers to executable code. Doesn't really help to guess the argument count though because of local variables on the stack in the calling function.

-Scan the original function for ret instructions (c2 / c3).
---If C2, check the following byte to grab the argument count and ensure the following byte is 00 (no functions I've seen should have more than 63 arguments so that 2nd following byte should be 00).
---If C3, check for a preceding pop ebp, or an 0xCC after the instruction so that the scan doesn't return incorrect results if C3 constitutes an operand of some unrelated instruction. Wouldn't catch inline functions though or functions who don't have unused memory between itself and the next.

Does anyone know of any nice tricks to get the argument count of a function?

Thanks!
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 90

Joined: 06 Jul 2014
Posts: 2878

PostPosted: Sun Jun 21, 2020 7:06 pm    Post subject: Reply with quote

In general, you look at accesses to stack locations outside the local frame and accesses to registers that haven't been written to first.
Code:
sub esp,34
push esi
mov esi,[ecx+1C8]  // ecx is a parameter
mov edx,[esi+14]
cmp edx,[esp+40] // at least 2 parameters on the stack
//...
This isn't easy to do both correctly and generally.
_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
Banjo Patterson
Cheater
Reputation: 1

Joined: 15 May 2018
Posts: 36

PostPosted: Sun Jun 21, 2020 8:16 pm    Post subject: Reply with quote

Hey Parkour,

Thanks for your reply. I didn't consider that option. It does look like it's going to be a matter of trial and error and would never be 100% reliable if I were to try and implement it.

I think what I'll do instead is use a 3rd party disassembler to display the function, so it's argument count can be determined with a human set of eyes, rather than trying to code it for automatic discovery.

Cheers!
Back to top
View user's profile Send private message
mariodarín
How do I cheat?
Reputation: 0

Joined: 24 May 2020
Posts: 2

PostPosted: Thu Jul 02, 2020 9:09 pm    Post subject: Reply with quote

Thanks for your reply.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites