Status update:
The injection itself is working fine now. Here are the patches needed for the dll loader.
In ntdll!LdrpFindOrMapDll
Code:
00000000`771eab4c 488d152da20c00 lea rdx,[ntdll!LdrpKernel32DllName (00000000`772b4d80)]
00000000`771eab53 488d8c2498000000 lea rcx,[rsp+98h]
00000000`771eab5b 41b001 mov r8b,1
00000000`771eab5e e86da40100 call ntdll!RtlEqualUnicodeString (00000000`77204fd0)
00000000`771eab63 84c0 test al,al
00000000`771eab65 0f8561e80000 jne ntdll!LdrpFindOrMapDll+0x824 (00000000`771f93cc)
00000000`771eab6b 4032ed xor bpl,bpl
Get rid of the jne to make sure kernel32 loads
bpl is tested a bit later in the beginning of ntdll!LdrpRelocateImage and if it's not zero it fails, so I think it's some kind of "don't relocate this dll" flag.
Set the byte ntdll!LdrpLdrDatabaseIsSetup to 1
Call LdrLoadDll
And now we get this in the debugger:
Code:
1c48:22e0 @ 275166171 - LdrpRunInitializeRoutines - INFO: Calling init routine 00000000005633E0 for DLL "C:\Windows\system32\KERNELBASE.dll"
1c48:22e0 @ 275166171 - LdrpRunInitializeRoutines - INFO: Calling init routine 000000000045EFF0 for DLL "C:\Windows\system32\KERNEL32.dll"
1c48:22e0 @ 275166234 - LdrpRunInitializeRoutines - INFO: Calling init routine 0000000055475C10 for DLL "C:\Windows\SYSTEM32\MSVCR100D.dll"
1c48:22e0 @ 275166234 - LdrpRunInitializeRoutines - INFO: Calling init routine 0000000000031520 for DLL "C:\Users\Micke\Documents\Visual Studio 2010\Projects\Injector\x64\Debug\test64.dll"
1c48:22e0 @ 275166234 - LdrpLoadDll - RETURN: Status: 0x00000000
1c48:22e0 @ 275166250 - LdrLoadDll - RETURN: Status: 0x00000000
But now the part where I'm stuck at the moment..
My DllMain is a simple
Code:
BOOL WINAPI DllMain(HINSTANCE hDll, DWORD dwReason, LPVOID reserved)
{
OutputDebugString(L"In 64bit DllMain!\n");
return TRUE;
}
If I run it through windbg things work fine, but when running the injector directly and trying to view the output in sysinternals' dbgview OutputDebugString causes an unhandled exception crash for exception 40010006 (DBG_PRINTEXCEPTION_C)
I can stop the crash by installing my own exception handler with RtlAddVectoredExceptionHandler but this also stops the debug string from being caught by dbgview.
I've searched for the default exception handler to find out how to handle that exception because OutputDebugString works from the 32bit code, but I can't find it because AddVectoredExceptionHandler never gets called by the crt. Are there any other ways to install exception handlers?