How to edit an executable to call certain WinApi functions

Viewed 33

First, sorry for the bad title just let me explain what I mean.
So let's say i have got a simple C++ program like this:

#include <windows.h>
// Compiled to x86
int main() {
    DWORD add = (DWORD)GetProcAddress(GetModuleHandleA("kernel32.dll"), "LoadLibraryA");
}

if I take a look at the assembly in x64dbg it will show this:
enter image description here

Due to the aslr of the windows dlls the address of GetProcAddress will be a different one after restarting the PC. You may have noticed that the opcode: 08707D00 is underlined. After restarting the PC, the opcode changes to something different (obviously that has to happen because the address of GetProcAddress changes too).

So I tried to copy the opcode from above into a codecave just to see if the opcode there would also change after restarting the PC. It turns out that it does not change the opcode so the call in the codecave becomes invalid after restarting the PC.

Now my question: How does the program know which opcodes need to be changed to always call the right function and why does my codecave call not change like the one shown in the picture?

0 Answers
Related