Advertisement
Guest User

Untitled

a guest
Oct 29th, 2012
691
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.07 KB | None | 0 0
  1. unsigned char codeToInject[] =
  2. {
  3.     // Push a dummy value for the return address
  4.     0x50, // push rax
  5.     // Save the flags
  6.     0x9c,                                                                   // pushfq                
  7.     // Save the registers
  8.     0x50,                                                                   // push rax
  9.     // rax is saved, now overwrite the return address we pushed earlier
  10.     0x48, 0xB8, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, // mov rax, 0CCCCCCCCCCCCCCCCh
  11.     0x48, 0x89, 0x84, 0x24, 0x10, 0x00, 0x00, 0x00,             // mov         qword ptr [rsp+16],rax  
  12.     0x51,                                                                   // push rcx
  13.     0x52,                                                                   // push rdx
  14.     0x53,                                                                   // push rbx
  15.     0x55,                                                                   // push rbp
  16.     0x56,                                                                   // push rsi
  17.     0x57,                                                                   // push rdi
  18.     0x41, 0x50,                                                             // push r8
  19.     0x41, 0x51,                                                             // push r9
  20.     0x41, 0x52,                                                             // push r10
  21.     0x41, 0x53,                                                             // push r11
  22.     0x41, 0x54,                                                             // push r12
  23.     0x41, 0x55,                                                             // push r13
  24.     0x41, 0x56,                                                             // push r14
  25.     0x41, 0x57,                                                             // push r15
  26.     // Placeholder for the string address and LoadLibrary
  27.     0x48, 0xB9, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, // mov rcx, 0CCCCCCCCCCCCCCCCh
  28.     0x48, 0xB8, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, // mov rax, 0CCCCCCCCCCCCCCCCh
  29.     // Call LoadLibrary with the string parameter
  30.     0xFF, 0xD0,                // call rax
  31.     // Restore the registers
  32.     0x41, 0x5F,                                                             // pop r15
  33.     0x41, 0x5E,                                                             // pop r14
  34.     0x41, 0x5D,                                                             // pop r13
  35.     0x41, 0x5C,                                                             // pop r12
  36.     0x41, 0x5B,                                                             // pop r11
  37.     0x41, 0x5A,                                                             // pop r10
  38.     0x41, 0x59,                                                             // pop r9
  39.     0x41, 0x58,                                                             // pop r8
  40.     0x5F,                                                                   // pop rdi
  41.     0x5E,                                                                   // pop rsi
  42.     0x5D,                                                                   // pop rbp
  43.     0x5B,                                                                   // pop rbx
  44.     0x5A,                                                                   // pop rdx
  45.     0x59,                                                                   // pop rcx
  46.     0x58,                                                                   // pop rax
  47.     // Restore the flags
  48.     0x9D,                                                                   // popfq
  49.     0xC3                                                                    // ret
  50. };
  51.  
  52. bool InjectDLL64(__in const DWORD& dwProcessID, __in const WCHAR* const pwszDLLName)
  53. {
  54.     HANDLE hProcess = NULL;
  55.     LPVOID pRemoteMemDllName = NULL;
  56.     LPVOID pRemoteMemFunction = NULL;
  57.     SIZE_T nDllNameBuffSize = NULL;
  58.     DWORD64 nFunctionBuffSize = sizeof(codeToInject2);
  59.     HANDLE hThread = NULL;
  60.     DWORD  dwThreadSuspendCount = -1;
  61.  
  62.     std::wstring strModuleFilePath = pwszDLLName;
  63.     bool bRet = false;
  64.  
  65.     hProcess = pi.hProcess;
  66.        
  67.     // Allocate memory on the target process with current DLL path
  68.     nDllNameBuffSize = (strModuleFilePath.size()+1) * sizeof(WCHAR);
  69.     pRemoteMemDllName = ::VirtualAllocEx(hProcess, NULL, nDllNameBuffSize, MEM_COMMIT,
  70.                                              PAGE_READWRITE);
  71.     if(!pRemoteMemDllName)
  72.     {
  73.         std::wcout << L"Error: Failed to allocate data on target process" << std::endl;
  74.         goto cleanup;
  75.     }
  76.  
  77.     SIZE_T nNumBytesWritten = 0;
  78.     ::WriteProcessMemory(hProcess, pRemoteMemDllName, (void*)strModuleFilePath.data(),
  79.                              nDllNameBuffSize, &nNumBytesWritten);
  80.     if(nNumBytesWritten != nDllNameBuffSize)
  81.     {
  82.         std::wcout << L"Error: Failed to write data on target process" << std::endl;
  83.         goto cleanup;
  84.     }
  85.  
  86.     // Allocate memory for the stub
  87.     pRemoteMemFunction = ::VirtualAllocEx(hProcess, NULL, nFunctionBuffSize, MEM_COMMIT,
  88.                                               PAGE_EXECUTE_READWRITE);
  89.  
  90.     // Get proc address of LoadLibrary
  91.     HMODULE hKernel32 = ::GetModuleHandleW(L"Kernel32");
  92.     if(!hKernel32)
  93.     {
  94.         std::wcout << L"Error : Failed to load kernel32" << std::endl;
  95.         goto cleanup;
  96.     }
  97.  
  98.     DWORD64 fnLocLoadLibrary = (DWORD64)::GetProcAddress(hKernel32, "LoadLibraryW");
  99.     if(!fnLocLoadLibrary)
  100.     {
  101.         std::wcout << L"Error : Failed to get LoadLibraryW proc address" << std::endl;
  102.         goto cleanup;
  103.     }
  104.  
  105.     hThread = pi.hThread;
  106.        
  107.     // Set the instruction pointer to point to our function
  108.     CONTEXT ctx;
  109.     ctx.ContextFlags = CONTEXT_ALL;
  110.     if(!GetThreadContext(hThread, &ctx))
  111.     {
  112.         std::wcout << L"Error : Failed to get the thread context" << std::endl;
  113.         goto cleanup;
  114.     }
  115.  
  116.     DWORD64 dwOldIP = ctx.Rip;
  117.     // Jump ahead the stack a little bit so we don't accidentally overwrite something
  118.     ctx.Rsp -= 128;
  119.     // Make sure the stack will be aligned to 16 bytes right at the LoadLibrary call
  120.     ctx.Rsp = ctx.Rsp & ~15;
  121.     ctx.Rsp -= 8;
  122.     ctx.Rip = (DWORD64) pRemoteMemFunction;
  123.     ctx.ContextFlags = CONTEXT_ALL;
  124.  
  125.     // Replace placeholders
  126.     memcpy(codeToInject + 5, &dwOldIP, sizeof(dwOldIP));
  127.     memcpy(codeToInject + 45, &pRemoteMemDllName, sizeof(pRemoteMemDllName));
  128.     memcpy(codeToInject + 55, &fnLocLoadLibrary, sizeof(fnLocLoadLibrary));
  129.     if(!WriteProcessMemory(hProcess, pRemoteMemFunction, codeToInject, nFunctionBuffSize, NULL))
  130.     {
  131.         std::wcout << L"Error: Failed to write the code cave" << std::endl;
  132.         goto cleanup;
  133.     }
  134.  
  135.     HMODULE h = LoadLibraryW(INJECTED);
  136.  
  137.     if(!SetThreadContext(hThread, &ctx))
  138.     {
  139.         std::wcout << L"Error: Failed to modify the thread context" << std::endl;
  140.         goto cleanup;
  141.     }
  142.  
  143.     // DLL Injection is successful
  144.     bRet = true;
  145.  
  146. cleanup:
  147.     // Cleanup allocated data
  148.     //if(dwThreadSuspendCount != -1)
  149.     ResumeThread(hThread);
  150.        
  151.     if(pRemoteMemDllName)
  152.         VirtualFreeEx(hProcess, pRemoteMemDllName, nDllNameBuffSize, MEM_RELEASE);
  153.  
  154.     if(pRemoteMemFunction)
  155.         VirtualFreeEx(hProcess, pRemoteMemFunction, nFunctionBuffSize, MEM_RELEASE);
  156.  
  157.     ::CloseHandle(hProcess);
  158.     ::CloseHandle(hThread);
  159.  
  160.     return bRet;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement