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 


hooking program defined functions
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
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Fri Jul 25, 2008 12:13 pm    Post subject: hooking program defined functions Reply with quote

How do you hook program defined functions? Can you use the hotpatching method? Otherwise how can you hook them?
_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
HalfPrime
Grandmaster Cheater
Reputation: 0

Joined: 12 Mar 2008
Posts: 532
Location: Right there...On your monitor

PostPosted: Fri Jul 25, 2008 1:11 pm    Post subject: Reply with quote

It's the same thing. Just make sure you know what the first 5 bytes are. It's pretty much just the same thing as a code-cave except you'll probably be routing into your program instead of the host.
_________________
Back to top
View user's profile Send private message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Fri Jul 25, 2008 1:35 pm    Post subject: Reply with quote

How exactly do I figure out what the first five bytes are? I know I probably have to step into the function call and look at its disassembled code. But how do I figure out how many lines of it are the first five bytes?
_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
HalfPrime
Grandmaster Cheater
Reputation: 0

Joined: 12 Mar 2008
Posts: 532
Location: Right there...On your monitor

PostPosted: Fri Jul 25, 2008 2:03 pm    Post subject: Reply with quote

You'll just have to figure it out. The first byte is going to be the command, so look it up and see what is and how many bytes it's going to take up.
If you can attach with CE to see what it is, it would make it a lot easier for you.

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

Joined: 06 Feb 2008
Posts: 54

PostPosted: Fri Jul 25, 2008 2:08 pm    Post subject: Reply with quote



find these initial 5 bytes in other function.

_________________
I wanna hack, but I don't know how...
Back to top
View user's profile Send private message
HalfPrime
Grandmaster Cheater
Reputation: 0

Joined: 12 Mar 2008
Posts: 532
Location: Right there...On your monitor

PostPosted: Fri Jul 25, 2008 2:32 pm    Post subject: Reply with quote

That's the first 5 bytes for most API to make hotpatching easier. We're talking about functions inside the program.
_________________
Back to top
View user's profile Send private message
Ferocious
Advanced Cheater
Reputation: 0

Joined: 06 Feb 2008
Posts: 54

PostPosted: Fri Jul 25, 2008 3:02 pm    Post subject: Reply with quote

yes, they are the same actually,

lets take this routine for example (it prints string to a buffer) :
Code:
.text:00401000                   sub_401000      proc near     ; CODE XREF: _main+36p
.text:00401000                   DstBuf          = dword ptr  4
.text:00401000                   Format          = dword ptr  8
.text:00401000                   ArgList         = byte ptr  0Ch
.text:00401000 8B 4C 24 08                       mov     ecx, [esp+Format]
.text:00401004 8D 44 24 0C                       lea     eax, [esp+ArgList]
.text:00401008 50                                push    eax   ; ArgList
.text:00401009 8B 44 24 08                       mov     eax, [esp+4+DstBuf]
.text:0040100D 6A 00                             push    0     ; Locale
.text:0040100F 51                                push    ecx   ; Format
.text:00401010 52                                push    edx   ; MaxCount
.text:00401011 50                                push    eax   ; DstBuf
.text:00401012 FF 15 9C 20 40 00                 call    ds:__imp___vswprintf_c_l
.text:00401018 83 C4 14                          add     esp, 14h
.text:0040101B C3                                retn
.text:0040101B                   sub_401000      endp


as you can see the initial 5 bytes are :

Quote:
.text:00401000 sub_401000 proc near ; CODE XREF: _main+36p
.text:00401000 DstBuf = dword ptr 4
.text:00401000 Format = dword ptr 8
.text:00401000 ArgList = byte ptr 0Ch
.text:00401000 8B 4C 24 08 mov ecx, [esp+Format]
.text:00401004 8D 44 24 0C lea eax, [esp+ArgList]
.text:00401008 50 push eax ; ArgList
.text:00401009 8B 44 24 08 mov eax, [esp+4+DstBuf]
.text:0040100D 6A 00 push 0 ; Locale
.text:0040100F 51 push ecx ; Format
.text:00401010 52 push edx ; MaxCount
.text:00401011 50 push eax ; DstBuf
.text:00401012 FF 15 9C 20 40 00 call ds:__imp___vswprintf_c_l
.text:00401018 83 C4 14 add esp, 14h
.text:0040101B C3 retn
.text:0040101B sub_401000 endp


those are the initial 5 bytes.
as you can see here, if you replace the 5 bytes, you are going to screw up the operation sequence thus crashing the application.

to overcome this issue, IAT hooks could do the job Smile

EDIT: if you are going to hook program defined functions you will not get thr function's name because function names that are obvious like MessageBox, SendMessage, etc. etc is loaded from Microsoft's debug symbol a.k.a PDB files - http://msdn.microsoft.com/en-us/library/yd4f8bd1(VS.71).aspx

_________________
I wanna hack, but I don't know how...
Back to top
View user's profile Send private message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Fri Jul 25, 2008 3:12 pm    Post subject: Reply with quote

Doesn't IAT mean Import Address Table. And doesn't that mean that you can only use IAT hooks on functions in the Import Table, which would really only be APIs or program and or defined functions in a DLL.

Btw, I thought with hotpatching, the original 5 bytes are:

Code:

mov edi, edi
push ebp
mov ebp, esp

_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Ferocious
Advanced Cheater
Reputation: 0

Joined: 06 Feb 2008
Posts: 54

PostPosted: Fri Jul 25, 2008 3:17 pm    Post subject: Reply with quote

yes you are correct. I am saying this in term of imported functions, Detouring is the answer then.

here's a simple tutorial by DrDeath of the GameDeception : http://forum.gamedeception.net/showthread.php?t=11581

EDIT : sorry for the misleading information about IAT, you will have to use other method to overcome the issue.

_________________
I wanna hack, but I don't know how...
Back to top
View user's profile Send private message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Fri Jul 25, 2008 3:51 pm    Post subject: Reply with quote

Any way to do it without using the detours library?
_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
HalfPrime
Grandmaster Cheater
Reputation: 0

Joined: 12 Mar 2008
Posts: 532
Location: Right there...On your monitor

PostPosted: Fri Jul 25, 2008 3:59 pm    Post subject: Reply with quote

Quote:
yes, they are the same actually,

Quote:
as you can see here, if you replace the 5 bytes, you are going to screw up the operation sequence thus crashing the application.

You're contradicting yourself.

If you look at a function inside a program, they're all going to start differently.

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

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Fri Jul 25, 2008 4:04 pm    Post subject: Reply with quote

Code:

#define MakeJmp(from,to) to-from+5 //I forget this part, just guessing
#define Address 0xDEADBEEF

static BYTE[5] bOriginalBytes;

void PatchFunction() {
    for (BYTE *bAddr = Address, DWORD dwCount = 0; dwCount < 5; bAddr++, dwCount++) {
        bOriginalBytes[i] = *bAddr;
    }

    *((BYTE *)Address) = 0xE8; //jmp instruction
    *((DWORD *)(Address + 1)) = MakeJmp((Address+1), NewFunction); //I think
}

void FixFunction() {
    for (BYTE *bAddr = Address, DWORD dwCount = 0; dwCount < 5; bAddr++, dwCount++) {
        *bAddr = bOriginalBytes[i];
    }
}

_________________
Wiccaan wrote:

Oh jeez, watchout I'm a bias person! Locked.


Auto Assembly Tuts:
In Depth Tutorial on AA
Extended
Back to top
View user's profile Send private message
lurc
Grandmaster Cheater Supreme
Reputation: 2

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Fri Jul 25, 2008 4:10 pm    Post subject: Reply with quote

Code:
#define MakeJmp( to, from )    to - from - 5

Just a little fix.

_________________
Back to top
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Fri Jul 25, 2008 4:15 pm    Post subject: Reply with quote

samuri25404 wrote:
Code:

#define MakeJmp(from,to) to-from+5 //I forget this part, just guessing
#define Address 0xDEADBEEF

static BYTE[5] bOriginalBytes;

void PatchFunction() {
    for (BYTE *bAddr = Address, DWORD dwCount = 0; dwCount < 5; bAddr++, dwCount++) {
        bOriginalBytes[i] = *bAddr;
    }

    *((BYTE *)Address) = 0xE8; //jmp instruction
    *((DWORD *)(Address + 1)) = MakeJmp((Address+1), NewFunction); //I think
}

void FixFunction() {
    for (BYTE *bAddr = Address, DWORD dwCount = 0; dwCount < 5; bAddr++, dwCount++) {
        *bAddr = bOriginalBytes[i];
    }
}


As lurc already stated, that jmp formula is wrong. Also, 0xE8 the call opcode, 0xE9 is a far jmp. When using MakeJmp, you shouldn't add anything to the address, and that address doesn't need to be a global as it can be passed to the function. Also, you need to use VirtualProtect() first otherwise you will come across access violations. Not to mention there is a lot of superfluous use of variables in your for loops.

See how C# teaches you bad habits?
Back to top
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Fri Jul 25, 2008 4:22 pm    Post subject: Reply with quote

Flyte wrote:
As lurc already stated, that jmp formula is wrong. Also, 0xE8 the call opcode, 0xE9 is a far jmp. When using MakeJmp, you shouldn't add anything to the address, and that address doesn't need to be a global as it can be passed to the function. Also, you need to use VirtualProtect() first otherwise you will come across access violations. Not to mention there is a lot of superfluous use of variables in your for loops.

See how C# teaches you bad habits?


Yeah, alright. My ASM is rusty. k, haven't used MakeJmp in a long time, didn't quite remember what it was, nor how it was used. The address was just assuming that he was only patching that one function, and it honestly doesn't matter. He could have easily made it dynamic, and I would have done the same had there been the need.

The variables were just personal coding style, and I was throwing it together really quickly, I'm kind of tired anyway, just got back from a long ass movie with some friends (Dark Knight).

As for VirtualProtect(), you've found one thing that's come over from C#... so what? =P

_________________
Wiccaan wrote:

Oh jeez, watchout I'm a bias person! Locked.


Auto Assembly Tuts:
In Depth Tutorial on AA
Extended
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