Results 1 to 8 of 8
  1. #1
    kibbles18's Avatar
    Join Date
    Oct 2008
    Gender
    male
    Location
    US
    Posts
    860
    Reputation
    5
    Thanks
    127

    Stub Injection Fails when Process is executed

    Im trying to inject a DLL by writing a stub to allocated memory in the target process and changing the EIP register in the thread. It works fine when it is written after the process has already executed, but if I try and do it when the process first starts up it crashes the process.
    Code:
    void injectproxy(char szDll[MAX_PATH])
    {
    	szDll = szGetDirFile(szDll);
    	stubLen = sizeof(stub);
    	DWORD dwProcId = NULL, threadID = NULL, oEIP = NULL, oldprot = NULL, dwLoadLibrary = NULL;
    	out("waiting for process");
    	CONTEXT ctx;
    	do
    	{
    		GetWindowTextA(g_hwEdit2, buf, sizeof(buf));
    		dwProcId = dwProcessID(buf);
    	}
    	while(dwProcId == NULL);
    	do
    	{
    	dwLoadLibrary = (DWORD)GetProcAddress(GetModuleHandleA("kernel32.dll"), "LoadLibraryA");
    	}
    	while(dwLoadLibrary == NULL);
    
    	hProcess = OpenProcess((PROCESS_VM_WRITE | PROCESS_VM_OPERATION), false, dwProcId);
    	if(hProcess == NULL)
    	{
    		out("unable to open process!");
    	}
    	dllLen = strlen(szDll)+1;
    	addrDllPath = VirtualAllocEx(hProcess, NULL, dllLen, MEM_COMMIT, PAGE_READWRITE);
    	addrStub = VirtualAllocEx(hProcess, NULL, stubLen, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
    	if(addrStub == NULL)
    	{
    		sprintf(buf, "error code: %d", GetLastError());
    		out(buf);
    		return;
    	}
    	if(WriteProcessMemory(hProcess, addrDllPath, szDll, strlen(szDll), NULL) == 0)
    	{
    		out("WPM fail");
    		return;
    	}
    	do
    	threadID = GetMainThreadId(dwProcId);
    	while(threadID == 0);
    	hThread = OpenThread((THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME | SYNCHRONIZE ), false, threadID);
    	if(hThread == NULL)
    	{
    		sprintf(buf, "unable to open thread. error code:%d", GetLastError());
    		out(buf);
    		return;
    	}
    	SuspendThread(hThread);
    	ct*****ntextFlags = CONTEXT_CONTROL;
    	if(GetThreadContext(hThread, &ctx) == 0)
    	{
    		sprintf(buf,"failed to get thread context. error code:%d", GetLastError());
    		out(buf);
    		return;
    	}
    	oEIP = ctx.Eip;
    	ctx.Eip = (DWORD)addrStub;
    	ct*****ntextFlags = CONTEXT_CONTROL;
    	VirtualProtect(stub, stubLen, PAGE_EXECUTE_READWRITE, &oldprot); 
    	memcpy((void *)((unsigned long)stub + 0x1), (void*)&oEIP, 4);   
    	memcpy((void *)((unsigned long)stub + 0x8), (void*)&addrDllPath, 4);
    	memcpy((void *)((unsigned long)stub + 0xD), (void*)&dwLoadLibrary, 4);
    	if(WriteProcessMemory(hProcess, addrStub, (LPCVOID)stub, stubLen, NULL) == 0)
    	{
    		out("WPM fail");
    		return;
    	}
    	if(SetThreadContext(hThread, &ctx) == NULL)
    	{
    		out("unable to setthreadcontext");
    	}
    	if(ResumeThread(hThread) == 0xFFFFFFFF)
    	{
    		out("unable to resume the thread!");
    	}
    }
    Way she fuckin goes boys

  2. #2
    0xB4DF00D's Avatar
    Join Date
    Mar 2012
    Gender
    male
    Posts
    46
    Reputation
    10
    Thanks
    28
    My Mood
    Bored
    Check if Kernel32.dll is loaded at the target process cause if it's not it'll crash when LoadLibraryA is called.
    You can do this by RPM read 1 byte of dwLoadLibrary and cmp with 0x8B, remember 8BFF = mov edi,edi standard in APIs.

    Check if it is returning to the original EIP in the stub.
    And at last check if the main thread is resuming.

  3. The Following User Says Thank You to 0xB4DF00D For This Useful Post:

    kibbles18 (11-15-2013)

  4. #3
    kibbles18's Avatar
    Join Date
    Oct 2008
    Gender
    male
    Location
    US
    Posts
    860
    Reputation
    5
    Thanks
    127
    I added the check but it is still crashing. Kernel32.dll is loaded because the DLL is injected successfully and LoadLibrary'd. The EIP is always correctly changed and restored. What seems to be crashing it is another thread trying to write to a memory location that it cannot write to. That memory location for some reason is the start of the injected dll module. Must be a problem with a register being overwritten?
    Last edited by kibbles18; 11-15-2013 at 07:32 PM.
    Way she fuckin goes boys

  5. #4
    0xB4DF00D's Avatar
    Join Date
    Mar 2012
    Gender
    male
    Posts
    46
    Reputation
    10
    Thanks
    28
    My Mood
    Bored
    Quote Originally Posted by kibbles18 View Post
    What seems to be crashing it is another thread trying to write to a memory location that it cannot write to. That memory location for some reason is the start of the injected dll module. Must be a problem with a register being overwritten?
    Well then it must be a Deadlock, try load a blank dll with only DllMain and see if the problem continues.

    You could stop all threads if it has more than one and resume after loadlib, but first see if a blank dll crash too.

  6. #5
    kibbles18's Avatar
    Join Date
    Oct 2008
    Gender
    male
    Location
    US
    Posts
    860
    Reputation
    5
    Thanks
    127
    It works fine with a blank DLL.
    Way she fuckin goes boys

  7. #6
    0xB4DF00D's Avatar
    Join Date
    Mar 2012
    Gender
    male
    Posts
    46
    Reputation
    10
    Thanks
    28
    My Mood
    Bored
    Quote Originally Posted by kibbles18 View Post
    It works fine with a blank DLL.
    Best Practices for Creating DLLs something in your DllMain is causing deadlock or maybe u are trying to read/write an addr that is not yet loaded.

  8. #7
    kibbles18's Avatar
    Join Date
    Oct 2008
    Gender
    male
    Location
    US
    Posts
    860
    Reputation
    5
    Thanks
    127
    Code:
    BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved) 
    { 
        if (reason == DLL_PROCESS_ATTACH) 
        {
    		CreateThread(0, 0, (LPTHREAD_START_ROUTINE)hook, 0, 0, 0);
        } 
    
        else if (reason == DLL_PROCESS_DETACH) 
        { 
        }
        return true; 
    }
    Just your standard dllmain.
    Way she fuckin goes boys

  9. #8
    kibbles18's Avatar
    Join Date
    Oct 2008
    Gender
    male
    Location
    US
    Posts
    860
    Reputation
    5
    Thanks
    127
    Edit: Issue fixed, was a small mistake in the DLL
    Way she fuckin goes boys

Similar Threads

  1. [Help Request] Open Process fails when not running in visual studio
    By _corn_ in forum Crossfire Coding Help & Discussion
    Replies: 5
    Last Post: 01-02-2013, 02:39 PM
  2. [Help]Creation of the target process and/or injection failed.
    By azuredreamer in forum Mission Against Terror Discussions
    Replies: 11
    Last Post: 02-24-2011, 02:49 PM
  3. PROBLEM of INJECT FAILED
    By killerld in forum WarRock - International Hacks
    Replies: 11
    Last Post: 07-31-2009, 02:12 PM
  4. 7-16-09 I got the X-Fire Hacks to work (Injection Failed Error)
    By Grim in forum Combat Arms Hacks & Cheats
    Replies: 15
    Last Post: 07-19-2009, 02:09 PM
  5. injecting failed
    By noime1989 in forum Soldier Front Hacks
    Replies: 4
    Last Post: 03-07-2009, 07:01 AM