远程注入进程

  
  
  
  
  1. #include <Windows.h> 
  2. #include <tchar.h> 
  3. #include <TlHelp32.h> 
  4.  
  5. BOOL LoadRemoteDll(DWORD dwProcessId,LPTSTR lpszLibName); 
  6. DWORD EnablePrivilege (PCSTR name); 
  7. BOOL GetProcessIdByName(LPSTR szProcessName, LPDWORD lpPID); 
  8.  
  9. DWORD EnablePrivilege (PCSTR name) 
  10.     HANDLE hToken; 
  11.     BOOL rv; 
  12.     TOKEN_PRIVILEGES priv = { 1, {0, 0, SE_PRIVILEGE_ENABLED} }; 
  13.     LookupPrivilegeValue ( 
  14.         0, 
  15.         name, 
  16.         &priv.Privileges[0].Luid 
  17.         ); 
  18.     OpenProcessToken( 
  19.         GetCurrentProcess (), 
  20.         TOKEN_ADJUST_PRIVILEGES, 
  21.         &hToken 
  22.         ); 
  23.     AdjustTokenPrivileges ( 
  24.         hToken, 
  25.         FALSE, 
  26.         &priv, 
  27.         sizeof priv, 
  28.         0, 
  29.         0 
  30.         ); 
  31.     rv = GetLastError(); 
  32.     CloseHandle (hToken); 
  33.     return rv; 
  34.  
  35. BOOL GetProcessIdByName(LPSTR szProcessName, LPDWORD lpPID) 
  36.     STARTUPINFO st; 
  37.     PROCESS_INFORMATION pi; 
  38.     PROCESSENTRY32 ps; 
  39.     HANDLE hSnapshot; 
  40.     ZeroMemory(&st, sizeof(STARTUPINFO)); 
  41.     ZeroMemory(&pi, sizeof(PROCESS_INFORMATION)); 
  42.     st.cb = sizeof(STARTUPINFO); 
  43.     ZeroMemory(&ps,sizeof(PROCESSENTRY32)); 
  44.     ps.dwSize = sizeof(PROCESSENTRY32); 
  45.  
  46.     hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0); 
  47.     if(hSnapshot == INVALID_HANDLE_VALUE) 
  48.     { 
  49.         return FALSE; 
  50.     } 
  51.  
  52.     if(!Process32First(hSnapshot,&ps)) 
  53.     { 
  54.         return FALSE; 
  55.     } 
  56.     do 
  57.     { 
  58.  
  59.         if(lstrcmpi(ps.szExeFile,"explorer.exe")==0) 
  60.         { 
  61.  
  62.             *lpPID = ps.th32ProcessID; 
  63.             CloseHandle(hSnapshot); 
  64.             return TRUE; 
  65.         } 
  66.     } 
  67.     while(Process32Next(hSnapshot,&ps)); 
  68.  
  69.     CloseHandle(hSnapshot); 
  70.     return FALSE; 
  71.  
  72. BOOL LoadRemoteDll(DWORD dwProcessId,LPTSTR lpszLibName){ 
  73.     BOOL bResult = FALSE; 
  74.     HANDLE hProcess = NULL; 
  75.     HANDLE hThread = NULL; 
  76.     PSTR pszLibFileRemote = NULL; 
  77.     DWORD cch; 
  78.     PTHREAD_START_ROUTINE pfnThreadRtn; 
  79.  
  80.     __try
  81.         hProcess = OpenProcess(PROCESS_ALL_ACCESS,FALSE,dwProcessId); 
  82.         if(hProcess == NULL){ 
  83.             __leave; 
  84.         } 
  85.         cch = 1 + lstrlen(lpszLibName); 
  86.         pszLibFileRemote = (PSTR)VirtualAllocEx(hProcess,NULL,cch,MEM_COMMIT,PAGE_READWRITE); 
  87.         if(pszLibFileRemote == NULL){ 
  88.             __leave; 
  89.         } 
  90.         if(!WriteProcessMemory(hProcess,(LPVOID)pszLibFileRemote,(LPVOID)lpszLibName,cch,NULL)){ 
  91.             __leave; 
  92.         } 
  93.         pfnThreadRtn = (PTHREAD_START_ROUTINE)GetProcAddress(GetModuleHandle(TEXT("Kernel32")),TEXT("LoadLibraryA")); 
  94.         if(pfnThreadRtn == NULL){ 
  95.             __leave; 
  96.         } 
  97.         hThread = CreateRemoteThread(hProcess,NULL,0,pfnThreadRtn,(PVOID)pszLibFileRemote,0,NULL); 
  98.         if(hThread == NULL){ 
  99.             __leave; 
  100.         } 
  101.         WaitForSingleObject(hThread,INFINITE); 
  102.         bResult = TRUE; 
  103.     }__finally
  104.         if(pszLibFileRemote != NULL){ 
  105.             VirtualFreeEx(hProcess,(PVOID)pszLibFileRemote,0,MEM_RELEASE); 
  106.         } 
  107.         if(hThread != NULL){ 
  108.             CloseHandle(hThread); 
  109.         } 
  110.         if(hProcess != NULL){ 
  111.             CloseHandle(hProcess); 
  112.         } 
  113.     } 
  114.     return bResult; 
  115.  
  116. int WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow){ 
  117.     DWORD dwPID; 
  118.     if(0!=EnablePrivilege(SE_DEBUG_NAME)); 
  119.     return 0; 
  120.     if(!GetProcessIdByName("explorer.exe",&dwPID)) 
  121.         return 0; 
  122.     if(!LoadRemoteDll(dwPID,"msg.dll")) 
  123.         return 0; 

 

你可能感兴趣的:(线程,职场,sdk,休闲)