VC++ 实现线程注入

在基于黑客软件、杀毒软件、系统应用软件开发过程中,需要使用一种方式:将外部DLL通过线程形式注入到其他进程中。这样的过程就叫注入线程或者叫线程注入。

  1. #include "stdafx.h"    
  2. #include "windows.h"    
  3.    
  4.    
  5. // ========== 定义一个代码结构,本例为一个对话框============    
  6. struct MyData   
  7. {   
  8.  char sz[64]; // 对话框显示内容    
  9.  DWORD dwMessageBox; // 对话框的地址    
  10. };   
  11.    
  12. // ========== 远程线程的函数 ==============================    
  13. DWORD __stdcall RMTFunc(MyData *pData)   
  14. {   
  15.  typedef int(__stdcall*MMessageBox)(HWND,LPCTSTR,LPCTSTR,UINT);   
  16.  MMessageBox MsgBox = (MMessageBox)pData->dwMessageBox;   
  17.  MsgBox(NULL, pData->sz, NULL, MB_OK);   
  18.  return 0;   
  19. }   
  20. int main(int argc, char* argv[])   
  21. {   
  22. // ===== 获得需要创建REMOTETHREAD的进程句柄 ===============================    
  23.  HWND hWnd = FindWindow("notepad", NULL); // 以NOTEPAD为例    
  24.  DWORD dwProcessId;   
  25.  ::GetWindowThreadProcessId(hWnd, &dwProcessId);   
  26.  HANDLE hProcess = OpenProcess(   
  27.         PROCESS_ALL_ACCESS,   
  28.         FALSE,   
  29.         dwProcessId);   
  30.    
  31. // ========= 代码结构 ================================================    
  32.  MyData data;   
  33.  ZeroMemory(&data, sizeof (MyData));   
  34.  strcat(data.sz, "对话框的内容.");   
  35.  HINSTANCE hUser = LoadLibrary("user32.dll");   
  36.  if (! hUser)   
  37.  {   
  38.   printf("Can not load library.\n");   
  39.   return 0;   
  40.  }   
  41.  data.dwMessageBox = (DWORD)GetProcAddress(hUser, "MessageBoxA");   
  42.  FreeLibrary(hUser);   
  43.  if (! data.dwMessageBox)   
  44.   return 0;   
  45.    
  46. // ======= 分配空间 ===================================================    
  47.  void *pRemoteThread   
  48.   = VirtualAllocEx(hProcess, 0,   
  49.       1024*4, MEM_COMMIT|MEM_RESERVE,   
  50.       PAGE_EXECUTE_READWRITE);   
  51.  if (! pRemoteThread)   
  52.   return 0;   
  53.  if (! WriteProcessMemory(hProcess, pRemoteThread, &RMTFunc, 1024*4, 0))   
  54.   return 0;   
  55.    
  56.  MyData *pData   
  57.   = (MyData*)VirtualAllocEx(hProcess, 0,   
  58.       sizeof (MyData), MEM_COMMIT,   
  59.       PAGE_READWRITE);   
  60.  if (!pData)   
  61.   return 0;   
  62.    
  63.  if (! WriteProcessMemory(hProcess, pData, &data, sizeof (MyData), 0))   
  64.   return 0;   
  65.    
  66. // =========== 创建远程线程 ===========================================    
  67.  HANDLE hThread   
  68.   = CreateRemoteThread(hProcess, 0,   
  69.        0, (LPTHREAD_START_ROUTINE)pRemoteThread,   
  70.        pData, 0, 0);   
  71.  if (! hThread)   
  72.  {   
  73.   printf("远程线程创建失败");   
  74.   return 0;   
  75.  }   
  76.  CloseHandle(hThread);   
  77.  VirtualFreeEx(hProcess, pRemoteThread, 1024*3, MEM_RELEASE);   
  78.  VirtualFreeEx(hProcess, pData, sizeof (MyData), MEM_RELEASE);   
  79.  CloseHandle(hProcess);   
  80.  printf("Hello World!\n");   
  81.  return 0;   
  82. }   
原文地址:https://www.cnblogs.com/new0801/p/6177742.html