【发布时间】:2014-05-10 02:46:19
【问题描述】:
如果您看一下以下简单 DLL 注入的工作代码:
//Open the target process with read , write and execute priviledges
Process = OpenProcess(PROCESS_CREATE_THREAD|PROCESS_QUERY_INFORMATION|PROCESS_VM_READ|PROCESS_VM_WRITE|PROCESS_VM_OPERATION, FALSE, ID);
//Get the address of LoadLibraryA
LoadLibrary = (LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
// Allocate space in the process for our DLL
Memory = (LPVOID)VirtualAllocEx(Process, NULL, strlen(dll)+1, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
// Write the string name of our DLL in the memory allocated
WriteProcessMemory(Process, (LPVOID)Memory, dll, strlen(dll)+1, NULL);
// Load our DLL
CreateRemoteThread(Process, NULL, NULL, (LPTHREAD_START_ROUTINE)LoadLibrary, (LPVOID)Memory, NULL, NULL);
//Let the program regain control of itself
CloseHandle(Process);
让我困惑的是GetProcAddress返回当前进程的LoadLibraryA函数地址,你怎么能把它作为参数传递给CreateRemoteThread并期望目标进程运行它?
【问题讨论】:
-
因为 CreateRemoteThread 将
LoadLibrary作为参数并调用它。由于它也将Memory作为参数,所以它传递给LoadLibrary的参数。 -
这仍然无法解释为什么你需要当前进程的
LoadLibrary函数地址。Memory是 dll 名称的地址,如果你只是想调用它,为什么不直接将LoadLibrary作为字符串传递? -
因为偏移量在其他进程中将完全相同。如果另一个进程是 x32 而你的进程是 x32,那么与 kernel32 的偏移量是相同的。如果您的进程是 x64 而另一个进程是 x64,则偏移量再次相同。如果另一个进程是 x32 而你的进程是 x64 或反之亦然,则偏移量将不同,注入将失败。我相信 User32.dll 也总是以相同的偏移量加载。类似于 Kernel32.dll
标签: c++ visual-studio visual-c++ dll dll-injection