I'm trying to inject my executable payload into remote process. I allocated memory in remote process, I converted raw payload using RVA addressing. I applied relocations and imports table.
When I'm testing my solution in Windows 10 environment it works fine. Exe is injected and it runs properly, and shows me message box.
But when I'm trying to do the same on Windows 7 64bit (loader, payload and target are compiled as x86 code), I have an error:
Access violation executing location 0x7698FD1E
I checked, and this address is an MessageBoxA function's address from user32.dll library
Here is my main code to inject pe to remote process:
char* target_n = "InjectTarget.exe";
char* payload_path = "C:\\Users\\pb\\source\\repos\\pe-dumper\\Debug\\DummyApp.exe";
FILE* raw_payload = get_file_buffer(payload_path);
PIMAGE_NT_HEADERS inth = get_nt_headers(raw_payload);
DWORD kImageSize = inth->OptionalHeader.SizeOfImage;
DWORD kTargetProcId = get_process_id(target_n);
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, NULL, kTargetProcId);
if (hProcess == NULL) {
printf("Error: Process handle is NULL\n");
}
LPVOID imageBaseRemote = VirtualAllocEx(hProcess, NULL, kImageSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if (imageBaseRemote == NULL) {
printf("Error: Image base remote is NULL\n");
}
LPVOID imageBaseLocal = VirtualAlloc(NULL, kImageSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
copy_raw_to_image_local(imageBaseLocal, raw_payload);
adjust_relocations(imageBaseRemote, imageBaseLocal);
adjust_imports(imageBaseLocal);
DWORD bytesWritten;
if (!WriteProcessMemory(hProcess, imageBaseRemote, imageBaseLocal, kImageSize, &bytesWritten)) {
printf("Cannot write to remote process!\n");
}
LPTHREAD_START_ROUTINE routine = ((ULONG_PTR)imageBaseRemote + inth->OptionalHeader.AddressOfEntryPoint);
DWORD threadId;
HANDLE hThread = CreateRemoteThread(hProcess, NULL, NULL, routine, NULL, NULL, &threadId);
if (hThread == NULL) {
printf("%d", GetLastError());
}
VirtualFree(imageBaseLocal, kImageSize, MEM_RELEASE);
fclose(raw_payload);
Why these differences between windows 10 and windows 7 appears?
MessageBoxA
s address? I would assume since you're not loading your image, that it won't be resolved for you. If you do anything to resolve it yourself, I assume the problem is there. Additionally, how did you check0x7698FD1E
is the address ofMessageBoxA
? Did you check the target process when it failed?copy_raw_to_image_local
,adjust_relocations
andadjust_imports
do even though the their names suggest particular functionality. But given that any of us would probably come up with a slightly different variation on the theme when tasked to implement said functions, it would make sense if you gave the details NirIzr was inquiring about.