I am playing with some test application to try to patch its code.
here is original code from IDA pro
movzx eax, byte ptr word_F3BB4A
and eax, 2
jz short loc_62300F
here is my patch
push ax
xor ax, ax
mov byte ptr word_F3BB4A, al
nop
pop ax
jmp short loc_62300F
The problem is that after launching application, address in my code does not change base. I am getting the exception accessing F3BB4A.
In the code you are patching, namely
there are two instructions that reference memory: the
movzx
and thejz
.Of the twos only the latter is position independent (it's destination is encoded as a relative offset), the former use an absolute address.
Absolute addresses are fixed up (relocated) by the loader based on the metadata the linker have generated. These metadata works by instruction address.
For example if we patch this program
by moving the move down 8 bytes (that's the reason of all the
nop
I used) and step again through the executable we have:where we see:
nop
in place of the original move has been changed by the loader when performing the relocation of the, now non existent, original move.If you want to patch the original code so that it overwrites the location of the move with a zero before doing its original work, then replace
with
Note how the immediates of both moves have the same address and how at the end
eax
is zero as expected.