Question

I have simple dll code:

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
      if(ul_reason_for_call == DLL_PROCESS_ATTACH) 
      {
            MessageBox(0, L"Hello!", L"Hello!", 0);          

      }

return TRUE;
}

And python code for injecting dll to another process:

import ctypes

PAGE_READWRITE = 0x04
PROCESS_ALL_ACCESS = (0x000F0000 | 0x00100000 | 0xFFF)
VIRTUAL_MEM = (0x1000 | 0x2000)

def InjectDLL(DLLPath, PID):

    kernel = ctypes.windll.kernel32
    dllLen = len(DLLPath)

    hProcs  = ctypes.windll.kernel32.OpenProcess(PROCESS_ALL_ACCESS, False, PID)
    if not hProcs:
        print 'OpenProcess failed.'
        return None


    argAddy = ctypes.windll.kernel32.VirtualAllocEx(hProcs, 0, dllLen, VIRTUAL_MEM, PAGE_READWRITE)
    if not argAddy:
        print 'VirtualAllocEx failed.'
        return None

    wrote = ctypes.c_int(0)
    kernel.WriteProcessMemory(hProcs, argAddy, DLLPath, dllLen, ctypes.byref(wrote))

    hKernel = kernel.GetModuleHandleA("kernel32.dll")
    hLib = kernel.GetProcAddress(hKernel, "LoadLibraryA")
    t_Id = ctypes.c_ulong(0)
    kernel.CreateRemoteThread(hProcs, None, 0, hLib, argAddy, 0, ctypes.byref(t_Id))
    return t_Id

result = InjectDLL("injdll.dll", 564)
print result

Injecting works for some processes. For example it works for gvim. I can see messagebox. But I have created simple application in C#. And after trying to inject my dll to this application nothing happens.

Python injector don't reports any errors with OpenProcess or CirtualAllocEx and return c_ulong(3968L). So why I can't see messagebox?

EDIT:

My operating system: Windows XP professional SP3 32-bit.

EDIT 2:

I did the same thing with C code and get the same result. Injection works for example for gvim. But not for my process in c#. I did check with debugger and it looks like DllMain function isn't executed.

DWORD pid;
cout << "PID: ";
cin >> pid;


HANDLE hproc = OpenProcess(PROCESS_ALL_ACCESS,0,pid);

LPVOID adr = 
VirtualAllocEx (
                hproc,
               (LPVOID)0,
               (SIZE_T)0x1000,
               MEM_COMMIT | MEM_RESERVE,
               PAGE_EXECUTE_READWRITE
               );

WriteProcessMemory (hproc, adr, "injdll.dll", 256, NULL);

HANDLE hthread = CreateRemoteThread 
(hproc,
 NULL,
 0,
 (LPTHREAD_START_ROUTINE)
 GetProcAddress(GetModuleHandle(L"kernel32"),"LoadLibraryA"),
 adr,
 0,
 NULL);

CloseHandle(hthread);
CloseHandle(hproc);

return 0;
Was it helpful?

Solution

Can't leave comments yet, so, what OS version you are using?

In most cases, your Dll must be 64-bit for .net applications in 64-bit OS.

I'm not sure in pyton, but LoadLibrary requires AsciiZ string. So, for safety reason, you may use "dllLen = len(DLLPath)+1"?

Also, try to change default stack size

kernel.CreateRemoteThread(hProcs, None, 4096*16, hLib, argAddy, 0, ctypes.byref(t_Id))

Or, mayby, there is exception occured in you DllMain (for some reason?). Attach debugger to target process before executing CreateRemoteThread

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top