Results 1 to 6 of 6
  1. #1
          ( ° ͜ʖ͡°)╭∩╮
    Former Staff
    MarkHC's Avatar
    Join Date
    Nov 2011
    Gender
    male
    Location
    127.0.0.1
    Posts
    2,750
    Reputation
    66
    Thanks
    14,530
    My Mood
    Angelic

    Mid Function Hook

    Well, I'm bored as hell now, so I decided to do something useful and write this down...

    First of all, if you don't have a good understanding of C++ and ASM. Don't bother reading the rest of this...

    Well, if you're still reading that means you do know C++/ASM so let's get started

    Let's begin with our hooking function, it's pretty straight foward:
    Code:
    void PlaceJMP(BYTE *pAddress, DWORD dwJumpTo, DWORD dwLen){
         DWORD dwOldProtect, dwBkup, dwRelAddr;
    
         // Basic VirtualProtect... y'all should know this
         VirtualProtect(pAddress, dwLen, PAGE_EXECUTE_READWRITE, &dwOldProtect);
        
         // Calculate the "distance" we're gonna have to jump - the size of the JMP instruction
         dwRelAddr = (DWORD) (dwJumpTo - (DWORD) pAddress) - 5;    
       
         // Write the JMP opcode @ our jump position...
         *pAddress = 0xE9;
    
          // Write the offset to where we're gonna jump
         *((DWORD *)(pAddress + 0x1)) = dwRelAddr; 
    
         // Overwrite the rest of the bytes with NOPs
         for(DWORD x = 0x5; x < dwLen; x++)
              *(pAddress + x) = 0x90; 
    
         // Restore the default permissions
         VirtualProtect(pAddress, dwLen, dwOldProtect, &dwBkup);
    
    }
    This function will place a JMP to dwJumpTo at the pAddress. dwLen is the byte count we're gonna overwrite...

    For this example, and since I'm a CoD hacker.. we're gonna hook CG_Draw2D. ALso, we're gonna be using TeknoMW3 (1.4.382) offsets if you want to try it yourself
    On 1.4.382 Version, the CG_Draw2D function is located at 0x430430 and its assembly looks like this:


    For this tutorial we're gonna hook the beginning of the function, but you can easily hook at any place...

    So let's take the first 3 instructions, they will be the instructions we're gonna overwrite...
    Code:
    push esi
    mov esi, [esp + 4 + arg_0]
    push esi
    if we look at the opcode, those 3 instructions use a total of 6 bytes. We need a minimum space of 5 bytes to do a Mid Function Hook (the size of the JMP) so that is ok.

    push esi = 1 (56)
    mov esi, [esp + 4 + arg_0] = 4 (8B 74 24 08)
    push esi = 1 (56)

    So we know the dwLen that we're gonna use on our PlaceJMP function will be 6. Therefore, the call of the PlaceJMP will be like this:
    Code:
    PlaceJMP((BYTE*)0x430430, (DWORD)hkDraw2D, 6);
    Where hkDraw2D will be our hooked Draw2D

    With this our hook is almost done. We just need to write our hooked Draw2D now

    Code:
    DWORD dwJMPback = 0x430436; //The Jump Back address is where we're gonna return after our hooked function ends.
                                    //0x430430 is where we're gonna hook... 6 is the length. 0x430430 + 6 = 0x430436
    
     __declspec(naked) void hkDraw2D(){
              __asm PUSHAD   //Push general registers onto the stack
              __asm PUSHFD   //Push EFLAGS Register onto the stack
        
    	  //Do ya Hax magicz here :P
    
              __asm POPFD     //Pop EFLAGS Register off the stack
              __asm POPAD    //Pop registers off the stack
    
            //Here we have to write the intructions we have overwritten with our JMP
    	__asm PUSH ESI
    	__asm MOV ESI, [ESP + 8]
    	__asm PUSH ESI
    
            //Now we jump back to the rest of the function
            //So the game can keep executing without issues :D
    	__asm JMP [dwJMPback]
    }
    And that's all. Hope this can be useful to someone. Any comment would be really appreciated. Thanks to learn_more and his original tutorial that helped me understand Mid Function Hooks and be able to write this.

    Regards
    Last edited by MarkHC; 10-03-2012 at 01:58 AM.


    CoD Minion from 09/19/2012 to 01/10/2013

  2. The Following 8 Users Say Thank You to MarkHC For This Useful Post:

    affe2626 (07-23-2016),base187 (01-13-2014),fredo121 (12-29-2013),Lovroman (10-18-2013),mwxplayer (04-06-2013),Nimboso (11-04-2015),xVIRUZx (10-28-2013),_PuRe.LucK* (12-11-2013)

  3. #2
    Kenshin13's Avatar
    Join Date
    May 2011
    Gender
    male
    Location
    Cloud 9
    Posts
    3,470
    Reputation
    564
    Thanks
    6,169
    My Mood
    Psychedelic
    Gotta start looking in this section more often.
    Found this through google
    Thx bro. (y) Coding god.

  4. #3
    New Day's Avatar
    Join Date
    Oct 2013
    Gender
    male
    Posts
    1
    Reputation
    10
    Thanks
    0
    Hey, i know this post is old, but i wanted to ask you something bout it.

    When you do

    PlaceJMP((BYTE*)0x430430, (DWORD)hkDraw2D, 6);

    In particular (DWORD)hkDraw2D



    what exactly are you doing? ive tried it in my own code, and it says some kind of identificator missing error. Thx in advance!

  5. #4
    abuckau907's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Location
    other side of the wire
    Posts
    1,342
    Reputation
    162
    Thanks
    239
    My Mood
    Cold
    Quote Originally Posted by New Day View Post
    When you do

    PlaceJMP((BYTE*)0x430430, (DWORD)hkDraw2D, 6);

    In particular (DWORD)hkDraw2D


    what exactly are you doing? ive tried it in my own code, and it says some kind of identificator missing error. Thx in advance!
    It's a function pointer. Being passed in as the value for 'jumpTo' address.

    dwJumpTo = his own function

    So the 'detour' will be a jump to his function called hkDraw2D.

    The function itself doesn't do anything except run the game's original code and stack prep/cleanup.
     

    DWORD dwJMPback = 0x430436; //The Jump Back address is where we're gonna return after our hooked function ends.
    //0x430430 is where we're gonna hook... 6 is the length. 0x430430 + 6 = 0x430436

    __declspec(naked) void hkDraw2D(){
    __asm PUSHAD //Push general registers onto the stack
    __asm PUSHFD //Push EFLAGS Register onto the stack

    //Do ya Hax magicz here :P

    __asm POPFD //Pop EFLAGS Register off the stack
    __asm POPAD //Pop registers off the stack

    //Here we have to write the intructions we have overwritten with our JMP
    __asm PUSH ESI
    __asm MOV ESI, [ESP + 8]
    __asm PUSH ESI

    //Now we jump back to the rest of the function
    //So the game can keep executing without issues
    __asm JMP [dwJMPback]
    }
    Last edited by abuckau907; 10-20-2013 at 06:49 AM.
    'Some things that can be counted, don't matter. And some things that matter, can't be counted' - A.E.
    --
     

    My posts have some inaccuracies/are wrong/wrong keyword(s) used.
    They're (maybe) pretty close, and I hope they helped you, not created confusion. Take with grain of salt.

    -if you give rep, please leave a comment, else it means less.

  6. #5
    I'm not lazy, I just really enjoy doing nothing.
    Donator
    _PuRe.LucK*'s Avatar
    Join Date
    Apr 2013
    Gender
    male
    Location
    idk bruh.
    Posts
    521
    Reputation
    71
    Thanks
    5,650
    My Mood
    Bored
    Quote Originally Posted by -InSaNe- View Post
    Well, I'm bored as hell now, so I decided to do something useful and write this down...

    First of all, if you don't have a good understanding of C++ and ASM. Don't bother reading the rest of this...

    Well, if you're still reading that means you do know C++/ASM so let's get started

    Let's begin with our hooking function, it's pretty straight foward:
    Code:
    void PlaceJMP(BYTE *pAddress, DWORD dwJumpTo, DWORD dwLen){
         DWORD dwOldProtect, dwBkup, dwRelAddr;
    
         // Basic VirtualProtect... y'all should know this
         VirtualProtect(pAddress, dwLen, PAGE_EXECUTE_READWRITE, &dwOldProtect);
        
         // Calculate the "distance" we're gonna have to jump - the size of the JMP instruction
         dwRelAddr = (DWORD) (dwJumpTo - (DWORD) pAddress) - 5;    
       
         // Write the JMP opcode @ our jump position...
         *pAddress = 0xE9;
    
          // Write the offset to where we're gonna jump
         *((DWORD *)(pAddress + 0x1)) = dwRelAddr; 
    
         // Overwrite the rest of the bytes with NOPs
         for(DWORD x = 0x5; x < dwLen; x++)
              *(pAddress + x) = 0x90; 
    
         // Restore the default permissions
         VirtualProtect(pAddress, dwLen, dwOldProtect, &dwBkup);
    
    }
    This function will place a JMP to dwJumpTo at the pAddress. dwLen is the byte count we're gonna overwrite...

    For this example, and since I'm a CoD hacker.. we're gonna hook CG_Draw2D. ALso, we're gonna be using TeknoMW3 (1.4.382) offsets if you want to try it yourself
    On 1.4.382 Version, the CG_Draw2D function is located at 0x430430 and its assembly looks like this:


    For this tutorial we're gonna hook the beginning of the function, but you can easily hook at any place...

    So let's take the first 3 instructions, they will be the instructions we're gonna overwrite...
    Code:
    push esi
    mov esi, [esp + 4 + arg_0]
    push esi
    if we look at the opcode, those 3 instructions use a total of 6 bytes. We need a minimum space of 5 bytes to do a Mid Function Hook (the size of the JMP) so that is ok.

    push esi = 1 (56)
    mov esi, [esp + 4 + arg_0] = 4 (8B 74 24 08)
    push esi = 1 (56)

    So we know the dwLen that we're gonna use on our PlaceJMP function will be 6. Therefore, the call of the PlaceJMP will be like this:
    Code:
    PlaceJMP((BYTE*)0x430430, (DWORD)hkDraw2D, 6);
    Where hkDraw2D will be our hooked Draw2D

    With this our hook is almost done. We just need to write our hooked Draw2D now

    Code:
    DWORD dwJMPback = 0x430436; //The Jump Back address is where we're gonna return after our hooked function ends.
                                    //0x430430 is where we're gonna hook... 6 is the length. 0x430430 + 6 = 0x430436
    
     __declspec(naked) void hkDraw2D(){
              __asm PUSHAD   //Push general registers onto the stack
              __asm PUSHFD   //Push EFLAGS Register onto the stack
        
    	  //Do ya Hax magicz here :P
    
              __asm POPFD     //Pop EFLAGS Register off the stack
              __asm POPAD    //Pop registers off the stack
    
            //Here we have to write the intructions we have overwritten with our JMP
    	__asm PUSH ESI
    	__asm MOV ESI, [ESP + 8]
    	__asm PUSH ESI
    
            //Now we jump back to the rest of the function
            //So the game can keep executing without issues :D
    	__asm JMP [dwJMPback]
    }
    And that's all. Hope this can be useful to someone. Any comment would be really appreciated. Thanks to learn_more and his original tutorial that helped me understand Mid Function Hooks and be able to write this.

    Regards
    nice tutorial. but most people of this forum understand nothing of this

  7. #6
    medo.soleman's Avatar
    Join Date
    Jul 2015
    Gender
    male
    Posts
    16
    Reputation
    10
    Thanks
    0
    fantastic tutorial

Similar Threads

  1. [Release] Menu Selection v4 Mid Function
    By luizimloko in forum Combat Arms BR Hack Coding/Source Code
    Replies: 65
    Last Post: 08-23-2012, 01:44 PM
  2. [Discussion] Mid Function
    By DiogoMAT in forum Piercing Blow Hack Coding/Source Code
    Replies: 4
    Last Post: 05-30-2012, 01:28 PM
  3. [Source Code] DIP / Present / SetTransform Mid Functions
    By DirecTX_ in forum WarRock Hack Source Code
    Replies: 2
    Last Post: 11-23-2011, 02:07 PM
  4. [Help] Mid function hook
    By giniyat202 in forum CrossFire Hack Coding / Programming / Source Code
    Replies: 17
    Last Post: 08-07-2011, 04:33 AM
  5. [Release] DIP Mid-Func Hook
    By MasterXxX in forum CrossFire Hack Coding / Programming / Source Code
    Replies: 2
    Last Post: 08-02-2011, 10:11 AM