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 


[Help] How do you get the ret address from a function?
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
dnsi0
I post too much
Reputation: 0

Joined: 04 Jan 2007
Posts: 2674

PostPosted: Tue Jan 06, 2009 6:40 pm    Post subject: [Help] How do you get the ret address from a function? Reply with quote

WHen you call a function you would use "call address" and it would push eip to the stack or something so when you use ret it would do pop that outa the stack and jmp back there.

Am I right...?
Back to top
View user's profile Send private message
sponge
I'm a spammer
Reputation: 1

Joined: 07 Nov 2006
Posts: 6009

PostPosted: Tue Jan 06, 2009 7:36 pm    Post subject: Reply with quote

Yes.
_________________
Back to top
View user's profile Send private message
dnsi0
I post too much
Reputation: 0

Joined: 04 Jan 2007
Posts: 2674

PostPosted: Tue Jan 06, 2009 7:39 pm    Post subject: Reply with quote

what part of the stack is that EIP? I because I see a push ebp and mov ebp,esp I think that the EIP is stored in ebp but when I try it, it doesn't return to the call func that called it.
Back to top
View user's profile Send private message
sponge
I'm a spammer
Reputation: 1

Joined: 07 Nov 2006
Posts: 6009

PostPosted: Tue Jan 06, 2009 7:40 pm    Post subject: Reply with quote

It's in [ESP] before the stack preservation header.
_________________
Back to top
View user's profile Send private message
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Tue Jan 06, 2009 7:41 pm    Post subject: Reply with quote

http://msdn.microsoft.com/en-us/library/64ez38eh(VS.71).aspx
Back to top
View user's profile Send private message
dnsi0
I post too much
Reputation: 0

Joined: 04 Jan 2007
Posts: 2674

PostPosted: Tue Jan 06, 2009 7:44 pm    Post subject: Reply with quote

_void_ wrote:
http://msdn.microsoft.com/en-us/library/64ez38eh(VS.71).aspx


... Im using delphi...
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Wed Jan 07, 2009 7:44 pm    Post subject: Reply with quote

the EIP is not pushed onto stack when CALL is done. it is the address of the next instruction. so assuming the function has a epilogue/preamble that means [ebp] will have the old ebp because of the 'push ebp' and so the return address is at [ebp+4]. just hook the function and do the preamble in your hook function then read off [ebp+4] into eax for example and move that into a buffer. inline asm for that.

it is possible to do it in a higher level language as well but it's a bit trickier.
Back to top
View user's profile Send private message
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Wed Jan 07, 2009 8:20 pm    Post subject: Reply with quote

dnsi0 wrote:
_void_ wrote:
http://msdn.microsoft.com/en-us/library/64ez38eh(VS.71).aspx


... Im using delphi...

Lol how am I suppose to know?
Back to top
View user's profile Send private message
dnsi0
I post too much
Reputation: 0

Joined: 04 Jan 2007
Posts: 2674

PostPosted: Thu Jan 08, 2009 5:25 pm    Post subject: Reply with quote

Slugsnack wrote:
the EIP is not pushed onto stack when CALL is done. it is the address of the next instruction. so assuming the function has a epilogue/preamble that means [ebp] will have the old ebp because of the 'push ebp' and so the return address is at [ebp+4]. just hook the function and do the preamble in your hook function then read off [ebp+4] into eax for example and move that into a buffer. inline asm for that.

it is possible to do it in a higher level language as well but it's a bit trickier.


I thought that all push did was push the value into the stack not destroy the value after the stack.

So do I really have to get the adr before the push ebp mov ebp,esp?
woulden't right after where ebp is still intack work too?
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 Jan 08, 2009 6:02 pm    Post subject: Reply with quote

dnsi0 wrote:
Slugsnack wrote:
the EIP is not pushed onto stack when CALL is done. it is the address of the next instruction. so assuming the function has a epilogue/preamble that means [ebp] will have the old ebp because of the 'push ebp' and so the return address is at [ebp+4]. just hook the function and do the preamble in your hook function then read off [ebp+4] into eax for example and move that into a buffer. inline asm for that.

it is possible to do it in a higher level language as well but it's a bit trickier.


I thought that all push did was push the value into the stack not destroy the value after the stack.

So do I really have to get the adr before the push ebp mov ebp,esp?
woulden't right after where ebp is still intack work too?
You can get it anywhere. The retrieval offset will be different however.
_________________
Back to top
View user's profile Send private message
rapion124
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Mar 2007
Posts: 1095

PostPosted: Thu Jan 08, 2009 6:22 pm    Post subject: Reply with quote

It depends where you are hooking. If it's before the prologue, then it's [esp]. If it's after the prologue, then it's [ebp+4]. After that, it's function specific.
Back to top
View user's profile Send private message
dnsi0
I post too much
Reputation: 0

Joined: 04 Jan 2007
Posts: 2674

PostPosted: Thu Jan 08, 2009 6:47 pm    Post subject: Reply with quote

rapion124 wrote:
It depends where you are hooking. If it's before the prologue, then it's [esp]. If it's after the prologue, then it's [ebp+4]. After that, it's function specific.


Thanks.

And yea its right after the function.
So its:
push ebp
mov ebp,esp
*code here*
Back to top
View user's profile Send private message
tombana
Master Cheater
Reputation: 2

Joined: 14 Jun 2007
Posts: 456
Location: The Netherlands

PostPosted: Fri Jan 09, 2009 9:53 am    Post subject: Reply with quote

And if you hook at the return (so if you replaced the RETN with a JMP) then it's also [esp].
Back to top
View user's profile Send private message
dnsi0
I post too much
Reputation: 0

Joined: 04 Jan 2007
Posts: 2674

PostPosted: Fri Jan 09, 2009 2:41 pm    Post subject: Reply with quote

tombana wrote:
And if you hook at the return (so if you replaced the RETN with a JMP) then it's also [esp].


I don't think that works... You still have to pop all those values from the stack.
Back to top
View user's profile Send private message
BanMe
Master Cheater
Reputation: 0

Joined: 29 Nov 2005
Posts: 375
Location: Farmington NH, USA

PostPosted: Fri Jan 09, 2009 8:59 pm    Post subject: Reply with quote

in a sense tombana is actually correct for most usual function flow goes like this
Code:

mov edi,edi \
push ebp      |-=Enter
mov ebp,esp/
here comes the pushs to save certain regs
xyzrealcode
here is the pops that restore registers
mov esp,ebp\
                   |-=Leave
pop ebp       /
ret/retn | by placing a hook in this classical style function the ret address should be [esp]


also hooking the preamble(designated by Enter up top) one code formulate code similiar to hook hopping take OpenThreadToken as a some what conformant example...
Code:
 
             MOV EDI,EDI
             PUSH EBP
             MOV EBP,ESP
             PUSH DWORD PTR SS:[EBP+14]
             PUSH DWORD PTR SS:[EBP+10]
             PUSH DWORD PTR SS:[EBP+0C]
             PUSH DWORD PTR SS:[EBP+8]
             CALL DWORD PTR DS:[<&ntdll.NtOpenThreadToken>]
             TEST EAX,EAX
             JGE SHORT ADVAPI32.77DD7B9F
             PUSH EAX
             CALL ADVAPI32.77DD6D24
             XOR EAX,EAX
             POP EBP
             RETN 10

by writeing a jmp hook to the first 5 bytes
one could have hook Code that looks like this
Code:

__declspec(naked)void Hk_OpenThreadToken()
{
        DWORD RetAddress;     
        __asm
        {
              mov RetAddress,[esp]
              mov edi,edi
              push ebp
              mov ebp,esp
              jmp OpenThreadToken+5
        }
}


now to get this working in all cases, shouldnt be to hard.. if anyone sees any mistakes in my code I emplore you to point them out..

as always regards BanMe
Back to top
View user's profile Send private message MSN Messenger
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