C# Theoretical: Write a JMP to a codecave in asm

后端 未结 2 868
暗喜
暗喜 2021-01-18 12:59

Lets assume I\'ve allocated the address where my codecave is placed using VirtualAllocEx (it returns the address) and I write my code into that address using

相关标签:
2条回答
  • 2021-01-18 13:15

    to get the relative offset just subtract the addresses:

    uint32_t patch_address = (uint32_t) VirtualAlloc(...);
    uint32_t jmp_offset = patch_address - (current_offset + current_len);
    

    note: current_len is 5 bytes on x86 E9 JMP instruction. see my post on this thread for more information:

    VirtualAlloc C++ , injected dll, asm

    0 讨论(0)
  • 2021-01-18 13:17

    E9 is a relative jump so the later 32 bits are just an offset to the current instruction pointer. See Intel® 64 and IA-32 Architectures Software Developer’s Manual Volume 2A: Instruction Set Reference, A-M pages 549ff for details. For more information see Intel® 64 and IA-32 Architectures Software Developer's Manuals.

    So the opcode to jump from 00402020 to 004028CF should be the following.

        E9  00 00 08 AA
    
    Offset   = DestinationAddress - CurrentInstructionPointer
    000008AA = 004028CF           - 00402025
    

    When the jump instruction is executed, the instruction pointer is already set to the next instruction. So the offset of the jump instruction and the current instruction pointer value differ by 5.

    CurrentInstructionPointer = AddressOfJumpInstruction + 5
    

    UPDATE

    Corrected error about the current instruction pointer value. Thanks jn.

    0 讨论(0)
提交回复
热议问题