Like Tree1Likes
  • 1 Post By anduril462

Asm code hook/ modification problem

This is a discussion on Asm code hook/ modification problem within the C Programming forums, part of the General Programming Boards category; I have never used Asm before, so I've had a little bit of trouble trying to write a Hook in ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    55

    Asm code hook/ modification problem

    I have never used Asm before, so I've had a little bit of trouble trying to write a Hook in Asm for a process. I've also had trouble with the function FlushInstructionCache. Here is my problematic code snippet ( the real project is 1,000+ lines, but the rest of the code executes with no problems ).

    ( The code I posted below begins at line 627 )

    Code:
                        Newmem = VirtualAllocEx( RobloxHandle, NULL, 32, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE );
    
    
                        if ( Newmem == NULL )
                        {
                            return;
                        } else
                        {
                            __asm
                            {
                                Newmem
                                movss xmm3,[edx]
                                addss xmm3,xmm3
                                addss xmm3,xmm3
                                ret
    
    
                                "MyProcess.exe"+0x3BC6AE
                                call Newmem
                                ret
                            };
    
    
                            if ( FlushInstructionCache( ProcessHandle, NULL, NULL) == 0 )
                            {
                                return;
                            }
                        }
    Here was what I wanted the above code to do :
    Overall goal : Create a hook in MyProcess.exe and let the application be able to call the memory I allocated for execution.
    Step 1 : Allocate code in memory
    Step 2 : Return to main if the Allocation failed ( and exit )
    Step 3 : If the allocation did not fail, do the following :
    - Put the code in the asm bracket after Newmem at Newmem's address
    - Overwrite the code at the offset of "MyProcess.exe"+0x3BC6AE with a call to the allocated memory's address and return back.
    - Use FlushInstructionCache() to insure the above code was added and executed properly, and return to main if the function fails ( and exit )
    Appendage : Newmem is declared as a void *

    Of course, this code executes with 2 warnings and errors which are listed below :

    Code:
    |In function 'input_loop':|
    |635|error: expected '(' before '{' token|
    |637|warning: statement with no effect|
    |637|error: expected ';' before 'movss'|
    |647|warning: passing argument 3 of 'FlushInstructionCache' makes integer from pointer without a cast|
    include\winbase.h|1382|note: expected 'DWORD' but argument is of type 'void *'|
    ||=== Build finished: 2 errors, 2 warnings ===|
    Please help me get the code written properly.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Assembly ("Asm") is not one language. It is a general term for a language that is 1:1 (or nearly 1:1) with the actual instruction set of the processor you are programming for. It uses mnemonics for instructions and registers to help people read/write it. As such, assembly has little to do with the C language, except that typically, when you compile C, it gets turned into assembly before being turned into machine code. Furthermore, the C language itself does not provide support for inline assembly. That ability is an extension provided by the compiler. Also, the C language is processor independent, i.e. it can work on Intel, Motorola, ARM, etc processors, which all have different assembly languages, so long as there is a compiler that can translate C into that assembly/machine language.

    In light of all these facts, it's near impossible for us to say with certainty what your problems are. It would help if we knew:
    1. processor - which assembly language should you be using
    2. OS - what system calls, etc might you need
    3. compiler - so we can help you figure out the exact syntax for inline assembly in that compiler

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    55
    1) AMD Athlon(tm) II X2 250 Processor 3.00 GHz
    2) Windows 7 - 64 bit
    3) Code::Blocks - Full version

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Quote Originally Posted by BatchProgrammer View Post
    3) Code::Blocks - Full version
    Code::Blocks is an IDE. What compiler are you using with it? MinGW or something else?
    stahta01 likes this.

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    55
    MinGW

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I don't see how you'll be able to assemble the asm code at a dynamic memory location. Think about it: in order for the program to execute (and therefore be able to retrieve the dynamic address) the program must be compiled, which includes the asm part. But you don't have the address yet!

    Instead, you'll have to determine the actual machine code for the asm and write that data into newmem.

    And the assembly syntax for gcc (assuming that's what you're using) is more like this:

    Code:
    __asm__ (
        "movss %xmm3,(%edx)\n\t"
        "addss %xmm3,%xmm3\n\t"
        "addss %xmm3,%xmm3\n\t"
        "ret"
    );
    GCC-Inline-Assembly-HOWTO

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    55
    ^ What? I stored the address in the Newmem pointer though. Not exactly sure what you're trying to tell me.

  8. #8
    Registered User
    Join Date
    Apr 2013
    Posts
    1,340
    Quote Originally Posted by BatchProgrammer View Post
    I stored the address in the Newmem pointer though. Not exactly sure what you're trying to tell me.
    The issue is getting the address of the asm code that you want to copy.

    Probably the best option here is to write the assembly code as a separate program, then use a utility like exe2bin to produce a binary, then extract the part of the binary you need and convert it into an array of characters to use in your program that you want to inject code with (assuming you're writing something similar to a trainer or debugger).

    Another but more difficult option is to create a function with the assembly code, but add a detectable pattern before and after the code (special instructions, like "mov eax,12345678h" (which is stored as b8,78,56,34,12)). Then cast a pointer to that function to a pointer to character, find the starting and ending patterns, then move the asm code in between those patterns. The problem with this is that some compilers will create a jump table to functions, so the pointer to function is a pointer to a jump instruction, and you'd have to compensate for that.
    Last edited by rcgldr; 06-13-2013 at 08:02 PM.

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by BatchProgrammer View Post
    ^ What? I stored the address in the Newmem pointer though. Not exactly sure what you're trying to tell me.
    Inline assembly code assembles at compile time, but at compile time you won't have the dynamic address. You need to poke the actual machine code bytes into the new memory at runtime if you want to create your asm program dynamically.

    For instance, your code assembles to:
    Code:
    f3 0f 11 1a          	movss  %xmm3,(%edx)
    f3 0f 58 db          	addss  %xmm3,%xmm3
    f3 0f 58 db          	addss  %xmm3,%xmm3
    c3                   	ret
    So you need to poke those bytes (0xf3, 0x0f, etc.) into your new memory.

    Depending on what you're trying to do, I forsee other troubles. For example, are you sure edx will contain a proper address?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. modification of C programming code.
    By engr_waqar290 in forum C Programming
    Replies: 7
    Last Post: 04-01-2013, 03:13 AM
  2. File modification problem
    By led1090 in forum C Programming
    Replies: 14
    Last Post: 09-26-2010, 03:09 AM
  3. Help with code modification
    By DCMann2 in forum C Programming
    Replies: 7
    Last Post: 05-06-2008, 11:33 PM
  4. Code modification and errors
    By ganesh143 in forum C Programming
    Replies: 9
    Last Post: 12-21-2007, 05:42 PM
  5. ~ Help with my code modification ~
    By indy in forum C Programming
    Replies: 2
    Last Post: 12-03-2003, 09:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21