This is my first post, and I am new to assembly and debugging, so bear with me please.
I was trying to inject some code (a little call to MessageBoxA) into the Windows 7 Notepad executable. However, I ran into the problem of addresses. First, I opened up the exe in OllyDbg, then I went to the line that contains the ASCII text of “notepad.pdb.” Then I put an ASCII string below that (For example, “INJECTED NOTEPAD”). Next, below that, I entered this asm code:
PUSH 0
PUSH address_of_ASCII_string ; In this case, 00A6B668C
PUSH address_of_ASCII_string ; In this case, 00A6B668C
PUSH 0
CALL MessageBoxA
Next, I went to the first line of code in the program (simply by right clicking and pressing Go to Origin (or just press * on the number pad)) Then I replaced the first line with a JMP instruction to the address of the first PUSH 0 in my injected code. Then, I put the instruction that I replaced at the end of my injected code. After that, I put a JMP instruction to the line of code after my JMP instruction that jumps to my injected code (yes, I just described a codecave or sort of). It all works fine when I run it. However, when I save the modified code to a new executable and run it with OllyDbg again, it doesn’t work. When I try to reference the ASCII string that I put in, the address is completely wrong. Example is in pictures below:
As you can see, I push the string onto the stack, but when I reload the modified program into the debugger again, the address of the string changes, but my code doesn’t. So when I call the MessageBoxA function, it errors out because I have loaded the wrong address for the Text and Caption arguments. How do I fix this?