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 


Get original call addr?
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Wed Dec 17, 2008 8:40 am    Post subject: Get original call addr? Reply with quote

I have hooked both send and recv APIs and I'm wondering how I can get the original address caller. Thanks.
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Wed Dec 17, 2008 9:06 am    Post subject: Reply with quote

move the return address into a variable. when a function is called, the return address (address of instruction directly after the call) is pushed onto the stack. then the stack frame is set up (push ebp). therefore at the new ebp, there will be the old ebp and at ebp+4 will be the return address. so at your hook function, after setting up stack frame yourself, just do something like mov eax, dword ptr ss:[ebp+4].
Back to top
View user's profile Send private message
Zand
Master Cheater
Reputation: 0

Joined: 21 Jul 2006
Posts: 424

PostPosted: Wed Dec 17, 2008 9:32 pm    Post subject: Reply with quote

Slugsnack wrote:
move the return address into a variable. when a function is called, the return address (address of instruction directly after the call) is pushed onto the stack. then the stack frame is set up (push ebp). therefore at the new ebp, there will be the old ebp and at ebp+4 will be the return address. so at your hook function, after setting up stack frame yourself, just do something like mov eax, dword ptr ss:[ebp+4].


That will be the return address. For the address of the call, subtract 4(or 5, not sure).
Back to top
View user's profile Send private message
Bizarro
I post too much
Reputation: 0

Joined: 01 May 2007
Posts: 2648

PostPosted: Wed Dec 17, 2008 9:39 pm    Post subject: Reply with quote

original call = esp - 4
_________________

w8 baby.com Banner contest, Come join NOW!!
Check us out for Prize deatils
Back to top
View user's profile Send private message
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Wed Dec 17, 2008 10:47 pm    Post subject: Reply with quote

Bizarro wrote:
original call = esp - 4

so..

DWORD addr;

mov eax, dword ptr ss:[esp-4]
mov [addr], eax

To get the address right?
Back to top
View user's profile Send private message
Bizarro
I post too much
Reputation: 0

Joined: 01 May 2007
Posts: 2648

PostPosted: Wed Dec 17, 2008 10:52 pm    Post subject: Reply with quote

mov calladdr, esp


calladdr= calladdr -4;

_________________

w8 baby.com Banner contest, Come join NOW!!
Check us out for Prize deatils
Back to top
View user's profile Send private message
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Wed Dec 17, 2008 10:54 pm    Post subject: Reply with quote

Bizarro wrote:
mov calladdr, esp


calladdr= calladdr -4;


Thanks Smile

Can't I do

Code:

mov calladdr, [ebp-4]
Back to top
View user's profile Send private message
sponge
I'm a spammer
Reputation: 1

Joined: 07 Nov 2006
Posts: 6009

PostPosted: Thu Dec 18, 2008 12:35 am    Post subject: Reply with quote

no. ebp != esp. this is assuming you hook before stack preserving header.
Code:
push ebp
mov ebp, esp

_________________
Back to top
View user's profile Send private message
nog_lorp
Grandmaster Cheater
Reputation: 0

Joined: 26 Feb 2006
Posts: 743

PostPosted: Thu Dec 18, 2008 1:55 am    Post subject: Reply with quote

If you do it before the function prologue, then it is [esp].
_________________
Mutilated lips give a kiss on the wrist of the worm-like tips of tentacles expanding in my mind
I'm fine accepting only fresh brine you can get another drop of this yeah you wish
Back to top
View user's profile Send private message
Trucido
Moderator
Reputation: 6

Joined: 08 Sep 2007
Posts: 2792

PostPosted: Thu Dec 18, 2008 2:17 am    Post subject: Reply with quote

You can't explicitly get the caller address due to you not knowing exactly what kind of call is made (SHORT/FAR/PTR/etc), you're better off just using the _ReturnAddress() intrinsic function to get the return address, then work your way from there.
_________________
I'm out.
Back to top
View user's profile Send private message
nog_lorp
Grandmaster Cheater
Reputation: 0

Joined: 26 Feb 2006
Posts: 743

PostPosted: Thu Dec 18, 2008 3:27 am    Post subject: Reply with quote

If the function get far calls it will have to use RETF, otherwise it will ignore the segment selector on the stack. [esp] is perfectly reliable - if you are actually dealing with a function designed for far calls, you can get the segment selector too if you want, [esp-4].
_________________
Mutilated lips give a kiss on the wrist of the worm-like tips of tentacles expanding in my mind
I'm fine accepting only fresh brine you can get another drop of this yeah you wish
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Thu Dec 18, 2008 4:29 am    Post subject: Reply with quote

just use the method i suggested.. it will get the return address which is pretty much just as good. subtract the size of the call instruction from that address and you have the caller address.
Back to top
View user's profile Send private message
Zand
Master Cheater
Reputation: 0

Joined: 21 Jul 2006
Posts: 424

PostPosted: Thu Dec 18, 2008 5:18 am    Post subject: Reply with quote

Slugsnack wrote:
just use the method i suggested.. it will get the return address which is pretty much just as good. subtract the size of the call instruction from that address and you have the caller address.


Well yeah if only you could know for certain the size of the call instruction.
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Thu Dec 18, 2008 5:25 am    Post subject: Reply with quote

Quote:
E8 cw CALL rel16 Call near, relative, displacement relative to next instruction
E8 cd CALL rel32 Call near, relative, displacement relative to next instruction
FF /2 CALL r/m16 Call near, absolute indirect, address given in r/m16
FF /2 CALL r/m32 Call near, absolute indirect, address given in r/m32
9A cd CALL ptr16:16 Call far, absolute, address given in operand
9A cp CALL ptr16:32 Call far, absolute, address given in operand
FF /3 CALL m16:16 Call far, absolute indirect, address given in m16:16
FF /3 CALL m16:32 Call far, absolute indirect, address given in m16:32

a scan function could be used to find the type of call to determine size of it. you'd have to be a bit careful about how you go about doing it though. i guess the alternative is to write a bit of debugging code to do that.
Back to top
View user's profile Send private message
Trucido
Moderator
Reputation: 6

Joined: 08 Sep 2007
Posts: 2792

PostPosted: Thu Dec 18, 2008 7:47 am    Post subject: Reply with quote

nog_lorp wrote:
If the function get far calls it will have to use RETF, otherwise it will ignore the segment selector on the stack. [esp] is perfectly reliable - if you are actually dealing with a function designed for far calls, you can get the segment selector too if you want, [esp-4].

Now you're fucking annoying me, directly accessing ESP in this case is not only volatile but it is not supported and is now technically a hack due to Microsoft's new inline assembler policy (assumption that he is coding this in C because everything else he has posted has been in C). Furthermore, in the context specified, there are many manners in which those APIs could be called (e.g. a direct 'CALL EAX/RAX' after a GetProcAddress call) in the 'wild'. There is simply no sure or safe way to assume how the call has been made.

Slugsnack wrote:
Code:
E8 cw CALL rel16 Call near, relative, displacement relative to next instruction
E8 cd CALL rel32 Call near, relative, displacement relative to next instruction
FF /2 CALL r/m16 Call near, absolute indirect, address given in r/m16
FF /2 CALL r/m32 Call near, absolute indirect, address given in r/m32
9A cd CALL ptr16:16 Call far, absolute, address given in operand
9A cp CALL ptr16:32 Call far, absolute, address given in operand
FF /3 CALL m16:16 Call far, absolute indirect, address given in m16:16
FF /3 CALL m16:32 Call far, absolute indirect, address given in m16:32

a scan function could be used to find the type of call to determine size of it. you'd have to be a bit careful about how you go about doing it though. i guess the alternative is to write a bit of debugging code to do that.

A scan function could easily fail and get a false trigger, let me show you a few examples:
Code:
; checking for a call to a relative address would return true
00400000 | 81F1 00E80000 | xor  eax, 0x0E800
00400006 | FFD1          | call ecx
00400008 | C3            | retn
 
; checking for a call call to an immediate register would return true
00400000 | E8   0000FFD1 | call 0xD32F0005
00400005 | C3            | retn

Mind you, you could decrease the chance of getting a false trigger but the amount of code required for such a frivolous function would only indicate that the programmer is probably brain-dead.

_________________
I'm out.
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
Goto page 1, 2  Next
Page 1 of 2

 
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