Dll Injection Using SetWindowsHookEx()

On viernes, 28 de enero de 2011 0 comentarios

Description // Info




Source Code

  1. #define PROC_NAME \"target.exe\"
  2. #define DLL_NAME \"injected.dll\"
  3.  
  4. void LoadDll(char *procName, char *dllName);
  5. unsigned long GetTargetThreadIdFromProcname(char *procName);
  6.  
  7. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
  8. {
  9.     LoadDll(PROC_NAME, DLL_NAME);
  10.  
  11.     return 0;
  12. }
  13.  
  14. void LoadDll(char *procName, char *dllName)
  15. {
  16.     HMODULE hDll;
  17.     unsigned long cbtProcAddr;
  18.  
  19.     hDll        = LoadLibrary(dllName);
  20.     cbtProcAddr = GetProcAddress(hDll, \"CBTProc\");
  21.  
  22.     SetWindowsHookEx(WH_CBT, cbtProcAddr, hDll, GetTargetThreadIdFromProcName(procName));
  23.    
  24.     return TRUE;
  25. }
  26.  
  27. unsigned long GetTargetThreadIdFromProcname(char *procName)
  28. {
  29.    PROCESSENTRY32 pe;
  30.    HANDLE thSnapshot, hProcess;
  31.    BOOL retval, ProcFound = false;
  32.    unsigned long pTID, threadID;
  33.  
  34.    thSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  35.  
  36.    if(thSnapshot == INVALID_HANDLE_VALUE)
  37.    {
  38.       MessageBox(NULL, \"Error: unable to create toolhelp snapshot\", \"Loader\", NULL);
  39.       return false;
  40.    }
  41.  
  42.    pe.dwSize = sizeof(PROCESSENTRY32);
  43.  
  44.     retval = Process32First(thSnapshot, &pe);
  45.  
  46.    while(retval)
  47.    {
  48.       if(StrStrI(pe.szExeFile, procName) )
  49.       {
  50.          ProcFound = true;
  51.          break;
  52.       }
  53.  
  54.       retval    = Process32Next(thSnapshot,&pe);
  55.       pe.dwSize = sizeof(PROCESSENTRY32);
  56.    }
  57.  
  58.    CloseHandle(thSnapshot);
  59.    
  60.    _asm {
  61.       mov eax, fs:[0x18]
  62.       add eax, 36
  63.       mov [pTID], eax
  64.    }
  65.  
  66.    hProcess = OpenProcess(PROCESS_VM_READ, false, pe.th32ProcessID);
  67.    ReadProcessMemory(hProcess, (const void *)pTID, &threadID, 4, NULL);
  68.    CloseHandle(hProcess);
  69.  
  70.  
  71.    return threadID;
  72. }

0 comentarios:

Publicar un comentario