Out-of-process hooking. menu

Shout-Out

User Tag List

Results 1 to 8 of 8
  1. #1
    wraithZX's Avatar Active Member
    Reputation
    43
    Join Date
    May 2007
    Posts
    122
    Thanks G/R
    0/1
    Trade Feedback
    1 (100%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Out-of-process hooking.

    Okay, so I figured I'd actually take my own advice and add a bit of info.

    This "tutorial" is going to be a bit light on the specifics, as I'm assuming you know what you're doing.
    If you blatantly copy/paste this code without understanding, then come begging for people to fix your mess, you'll be met with laughter and ridicule. You have been warned.

    So, out-of-process injection. I've used this successfully in a previous version of my mapping program - specifically hooking into IDirect3DDevice9::EndScene.

    A few things you'll need.

    1. A brain.
    2. Basic knowledge of assembly.
    3. Knowledge of reading and writing to remote processes.

    The simple pseudocode for my tool for out-of-process hooking was as follows:

    1. Pause all running threads in the target process.
    2. Find the location of IDirect3DDevice9::EndScene in the remote processes address space.
    3. Allocate memory and copy over the function I wish to run.
    4. Apply the hook based on the above two locations.
    5. Resume all threads.

    Simple eh?

    Now because I'm going to be nice, I'll put the two stubs I used:

    Code:
    // The detour stub
    BYTE detour_stub[] = {
        0xB8, 0xAA, 0xAA, 0xAA, 0xAA,      // mov eax, 0xAAAAAAAA
        0xFF, 0xE0,                        // jmp eax
    };
    Code:
    // The loader func we are going to use in order to execute the user code
    BYTE loader_stub[] =
    {
        0x9C,                            // pushfd
        0x60,                            // pushad
        0x68, 0xBB, 0xBB, 0xBB, 0xBB,    // push 0xBBBBBBBB (function arg)
        0xB8, 0xCC, 0xCC, 0xCC, 0xCC,    // mov eax,0xCCCCCCCC (function address - void Func(void*) )
        0xFF, 0xD0,                      // call eax
        0x83, 0xC4, 0x04,                // add esp,4 (fix up the stack from the function arg)
        0x61,                            // popad
        0x9D,                            // popfd
    };
    So how do those two interact with each other?

    The detour stub replaces the code that we want to hook.
    The 0xAAAAAAAA gets replaced with the location of the loader_stub, sitting in the remote process.
    The 0xBBBBBBBB gets replaced with a pointer to a block of memory, which I used for RPC.
    The 0xCCCCCCCC gets replaced with the location of the actual function we wish to execute.
    ....and another detour_stub is created with 0xAAAAAAAA replaced with the location we want to jump back to.

    So, in the remote process, I'd allocate enough space for:
    Code:
    (loader_stub)
    original hooked code
    (detour_stub)
    payload function we wish to execute
    function data - memory used for RPC
    I'll assume you can figure out the offsets on all of the above pieces used for allocating memory.

    As far as applying the hook is concerned, the sequence then becomes:

    1. Write the function payload to the required offset in memory.
    2. Copy out the original executable code, then write that data to the required offset in the block of memory.
    3. Replace 0xBBBBBBBB with the function data offset from the allocated memory.
    4. Replace 0xCCCCCCCC with the payload function offset in the allocated memory.
    5. Write the loader stub to the required location.
    6. Replace 0xAAAAAAAA with the location of the instruction AFTER the copied bytes in memory.
    7. Write the detour stub to the required location in the allocated memory.
    8. Replace 0xAAAAAAAA with the location of the loader stub in memory.
    9. Write the detour stub to the original code location.

    Of course, you'll say: "but I'm not getting the function parameters or anything useful!"
    That's what the RPC block of data is for. Put your offsets into a structure from your loader, and write the structure to the allocated memory location.

    Out-of-process hooking.
  2. #2
    flo8464's Avatar Active Member
    Reputation
    30
    Join Date
    Apr 2009
    Posts
    434
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very nice.
    I kinda dislike there is no way to hook a function without using Assembly code but well, gotta live with it.

  3. #3
    furang's Avatar Member
    Reputation
    19
    Join Date
    Jul 2009
    Posts
    84
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great.
    Is this techique's name called splicing (as i remember)?
    2flo8464: You need just a little asm. You don't have to learn it perfectly fine for this.
    i did it 4 lulz

  4. #4
    flo8464's Avatar Active Member
    Reputation
    30
    Join Date
    Apr 2009
    Posts
    434
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by furang View Post
    Great.
    Is this techique's name called splicing (as i remember)?
    2flo8464: You need just a little asm. You don't have to learn it perfectly fine for this.
    No, using ASm is no problem, I just don't like ASM in my C++-application

  5. #5
    Apoc's Avatar Angry Penguin
    Reputation
    1388
    Join Date
    Jan 2008
    Posts
    2,750
    Thanks G/R
    0/13
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by flo8464 View Post
    No, using ASm is no problem, I just don't like ASM in my C++-application
    Then your programs must suck...

  6. #6
    flo8464's Avatar Active Member
    Reputation
    30
    Join Date
    Apr 2009
    Posts
    434
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Apoc View Post
    Then your programs must suck...
    I guess I don't have to understand what you mean.
    Of course I mean I don't like having ASM in my source code.

  7. #7
    bigtimt's Avatar Active Member
    Reputation
    41
    Join Date
    Mar 2008
    Posts
    100
    Thanks G/R
    2/4
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the library i wrote (Voodoo) can call functions OOP without any asm required, you guys should check it out.

  8. #8
    Cypher's Avatar Kynox's Sister's Pimp
    Reputation
    1358
    Join Date
    Apr 2006
    Posts
    5,368
    Thanks G/R
    0/6
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Apoc View Post
    Then your programs must suck...
    I don't use inline ASM in any of my programs either. There's pretty much always a way to work around it (like rewriting RtlRemoteCall instead of just dumping bytes into the remote process and starting a new thread), or using compiler intrinsics rather than inline asm, etc.

    One exception I do make is the use of AsmJit, but that is an architecture independent ASM generator, so it doesn't have the same restrictions as inline ASM. (As inline ASM is disabled entirely under MSVC when compiling for x64, you have to use FASM to compile ASM files manually then link them into your project, major pain in the ass)

Similar Threads

  1. [Preview] Out-Of-Process hooking made easy.
    By bigtimt in forum WoW Memory Editing
    Replies: 1
    Last Post: 09-21-2009, 06:45 AM
  2. [C++]CSyringe - Out of Process Mem Manager
    By cenron in forum WoW Memory Editing
    Replies: 1
    Last Post: 01-21-2009, 08:48 PM
  3. [Out of Process] GetNumLootItems()
    By hypnodok in forum WoW Memory Editing
    Replies: 8
    Last Post: 12-16-2008, 02:51 PM
  4. [Help] Accessing a function Out of Process
    By cenron in forum WoW Memory Editing
    Replies: 18
    Last Post: 10-14-2008, 05:49 AM
  5. Can you read player names out of process?
    By sweeper18 in forum WoW Memory Editing
    Replies: 10
    Last Post: 07-06-2008, 08:54 PM
All times are GMT -5. The time now is 07:17 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search