返回列表 发帖

查找加载某个windows标准动态库的进程

[这个贴子最后由x86在 2006/03/22 00:26am 第 4 次编辑] 一段查找加载某个windows标准动态库的进程id的代码,没有显示进程名字,只有id winxp+vc6.0编译通过 以后还会增加一些功能..
  1. &#35;include <stdio.h>
  2. &#35;include <windows.h>
  3. &#35;include <Psapi.h>
  4. &#35;pragma comment(lib, "psapi.lib")
  5. &#35;pragma comment(lib, "ws2_32.lib")
  6. &#35;define PROCESSNUM 128
  7. //////////////////////////////////////////////////////////////////
  8. //进程权限提升函数
  9. //////////////////////////////////////////////////////////////////
  10. BOOL AdjustProcessPrivileges ( LPCSTR szPrivilegesName)
  11. {
  12. HANDLE hToken;
  13. TOKEN_PRIVILEGES tkp;
  14. if(!OpenProcessToken(GetCurrentProcess(),
  15. TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken))
  16. {
  17. return FALSE;
  18. }
  19. if(!LookupPrivilegeV&#97;lue(NULL,szPrivilegesName,
  20. &tkp.Privileges[0].Luid))
  21. {
  22. CloseHandle(hToken);
  23. return FALSE;
  24. }
  25. tkp.PrivilegeCount = 1;
  26. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  27. if(!AdjustTokenPrivileges(hToken,FALSE,&tkp,sizeof(tkp),NULL,NULL))
  28. {
  29. CloseHandle(hToken);
  30. return 0;
  31. }
  32. CloseHandle(hToken);
  33. return TRUE;
  34. }
  35. //////////////////////////////////////////////////////////////////
  36. //查找系统中加载某一特定模块的进程,并返回其进程id到一数组
  37. //////////////////////////////////////////////////////////////////
  38. BOOL FindProcessByModuleName(DWORD * lpidProcesses,DWORD numOfProcess,LPCSTR FindModuleName,
  39. DWORD * lpoutProcessBuffer, DWORD cb, DWORD * dwNeed)
  40. {
  41. *dwNeed = 0;
  42. CHAR MapFileName[MAX_PATH] = "unknown";
  43. CHAR ModuleName[MAX_PATH];
  44. memset(ModuleName, 0, sizeof(ModuleName));
  45. DWORD FileNameLength = 0;
  46. DWORD ModuleNameLength = 0;
  47. DWORD ModuleAddr = (DWORD)LoadLibrary(FindModuleName);
  48. if((HMODULE)ModuleName == NULL)
  49. {
  50. return FALSE;
  51. }
  52. HANDLE pHd;
  53. DWORD ProcessBufferId = 0;
  54. for (DWORD i = 1; i < numOfProcess; i++)
  55. {
  56. pHd = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
  57. FALSE, lpidProcesses[i] );
  58. if (pHd == NULL)
  59. {
  60. continue;
  61. }
  62. FileNameLength = GetMappedFileName( pHd, (LPVOID)ModuleAddr,MapFileName, sizeof(MapFileName) );
  63. if(FileNameLength == 0)
  64. {
  65. CloseHandle(pHd);
  66. continue;
  67. }
  68. for(DWORD j = (FileNameLength - 1); MapFileName[j] != ';\\';; j--)
  69. {
  70. ModuleName[FileNameLength - j - 1] = MapFileName[j];
  71. }
  72. char temp;
  73. DWORD Starti = 0;
  74. DWORD Endi = strlen(ModuleName) - 1;
  75. while( Starti != Endi && Starti < Endi )
  76. {
  77. temp = ModuleName[Starti];
  78. ModuleName[Starti] = ModuleName[Endi];
  79. ModuleName[Endi] = temp;
  80. Starti++;
  81. Endi--;
  82. }
  83. if(0 != strcmp(ModuleName,FindModuleName))
  84. {
  85. CloseHandle(pHd);
  86. continue;
  87. }
  88. CloseHandle(pHd);
  89. if(*dwNeed >= (cb / sizeof(DWORD)))
  90. return FALSE;
  91. lpoutProcessBuffer[ProcessBufferId] = lpidProcesses[i];
  92. printf("Find %d:%s\n", lpoutProcessBuffer[ProcessBufferId], ModuleName);
  93. ProcessBufferId++;
  94. (*dwNeed)++;
  95. }
  96. return TRUE;
  97. }
  98. //////////////////////////////////////////////////////////////////
  99. //在一组进程空间中保留和提交一段指定大小和保护属性的内存空间
  100. //////////////////////////////////////////////////////////////////
  101. BOOL ReserveAndCommitForProcesses(DWORD * lpidProcesses, DWORD numOfProcess, DWORD dwThreadSize,
  102. DWORD * lpCommitAddrs, DWORD cb, DWORD * dwNeeded,
  103. DWORD * lpCresPondPids,DWORD cbOfCres)
  104. {
  105. (*dwNeeded) = 0;
  106. HANDLE hTargetProcess;
  107. DWORD dwTargetAddr = 0;
  108. for (DWORD i = 0; i < numOfProcess; i++)
  109. {
  110. hTargetProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, lpidProcesses[i] );
  111. if (hTargetProcess == NULL)
  112. {
  113. continue;
  114. }
  115. if(*dwNeeded >= (cb/sizeof(DWORD)) || *dwNeeded >= (cbOfCres/sizeof(DWORD)))
  116. {
  117. CloseHandle(hTargetProcess);
  118. return FALSE;
  119. }
  120. dwTargetAddr = (DWORD)VirtualAllocEx(hTargetProcess, NULL, dwThreadSize,
  121. MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
  122. if((LPVOID)dwTargetAddr == NULL)
  123. {
  124. CloseHandle(hTargetProcess);
  125. continue;
  126. }
  127. CloseHandle(hTargetProcess);
  128. lpCommitAddrs[*dwNeeded] = dwTargetAddr;
  129. lpCresPondPids[*dwNeeded] = lpidProcesses[i];
  130. (*dwNeeded)++;
  131. }
  132. return TRUE;
  133. }
  134. int main(int argc, char *argv[])
  135. {
  136. DWORD ProcessIds[PROCESSNUM];
  137. DWORD AccordProcessIds[PROCESSNUM];
  138. DWORD CommitAddres[PROCESSNUM];
  139. DWORD MatchAddrPids[PROCESSNUM];
  140. DWORD cPids = 0;
  141. DWORD cAccordPids = 0;
  142. DWORD cCommitAddr = 0;
  143. if(!AdjustProcessPrivileges(SE_DEBUG_NAME))
  144. {
  145. printf("AdjustProcessPrivileges Error!\n");
  146. return 1;
  147. }
  148. if (!EnumProcesses( ProcessIds, sizeof(ProcessIds), &cPids))
  149. {
  150. printf("EnumProcess Error!\n");
  151. return 1;
  152. }
  153. if(!FindProcessByModuleName(ProcessIds, cPids/sizeof(DWORD), "ws2_32.dll", AccordProcessIds, sizeof(AccordProcessIds), &cAccordPids))
  154. {
  155. printf("FindProcessByModuleName Error!\n");
  156. return 1;
  157. }
  158. printf("%d Processes Load The ws2_32.dll\n",cAccordPids);
  159. if(!ReserveAndCommitForProcesses(AccordProcessIds, cAccordPids, 4096,
  160. CommitAddres, sizeof(CommitAddres), &cCommitAddr,
  161. MatchAddrPids, sizeof(MatchAddrPids)))
  162. {
  163. printf("ReserveAndCommitForProcesses Error!\n");
  164. return 1;
  165. }
  166. for (DWORD i = 0; i < cCommitAddr; i++)
  167. {
  168. printf("PID:%d,Commit Addr:0x%x\n", MatchAddrPids[i], CommitAddres[i]);
  169. }
  170. printf("Commit %d addresses!\n", cCommitAddr);
  171. return 0;
  172. }
复制代码

查找加载某个windows标准动态库的进程

  1. &#35;include <stdio.h>
  2. &#35;include <windows.h>
  3. &#35;include <Psapi.h>
  4. &#35;pragma comment(lib, "psapi.lib")
  5. &#35;pragma comment(lib, "ws2_32.lib")
  6. &#35;define PROCESSNUM 128
  7. //线程参数结构体定义
  8. typedef struct _RemoteParam {
  9. char szMsg[12]; //MessageBox函数中显示的字符提示
  10. DWORD dwMessageBox;//MessageBox函数的入口地址
  11. char szModulename[12]; //加载的动态库名
  12. DWORD dwLoadLibrary;
  13. HMODULE ModuleAddr;
  14. DWORD dwFreeLibrary;
  15. } RemoteParam, * PRemoteParam;
  16. //定义MessageBox类型的函数指针
  17. typedef int (__stdcall * PFN_MESSAGEBOX)(HWND, LPCTSTR, LPCTSTR, DWORD);
  18. typedef int (__stdcall * PFN_LOADLIBRARY)(LPCTSTR);
  19. typedef int (__stdcall * PFN_FREELIBRARY)(HMODULE);
  20. //线程函数定义
  21. DWORD __stdcall threadProc(LPVOID lParam)
  22. {
  23. RemoteParam* pRP = (RemoteParam*)lParam;
  24. PFN_MESSAGEBOX pfnMessageBox;
  25. PFN_LOADLIBRARY pfnLoadLibrary;
  26. PFN_FREELIBRARY pfnFreeLibrary;
  27. pfnMessageBox = (PFN_MESSAGEBOX)pRP->dwMessageBox;
  28. pfnLoadLibrary = (PFN_LOADLIBRARY)pRP->dwLoadLibrary;
  29. pfnFreeLibrary = (PFN_FREELIBRARY)pRP->dwFreeLibrary;
  30. pfnLoadLibrary(pRP->szModulename);
  31. pfnMessageBox(NULL, pRP->szMsg, pRP->szMsg, 0);
  32. pfnFreeLibrary(pRP->ModuleAddr);
  33. return 0;
  34. }
  35. //////////////////////////////////////////////////////////////////
  36. //进程权限提升函数
  37. //////////////////////////////////////////////////////////////////
  38. BOOL AdjustProcessPrivileges ( LPCSTR szPrivilegesName)
  39. {
  40. HANDLE hToken;
  41. TOKEN_PRIVILEGES tkp;
  42. if(!OpenProcessToken(GetCurrentProcess(),
  43. TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken))
  44. {
  45. return FALSE;
  46. }
  47. if(!LookupPrivilegeValue(NULL,szPrivilegesName,
  48. &tkp.Privileges[0].Luid))
  49. {
  50. CloseHandle(hToken);
  51. return FALSE;
  52. }
  53. tkp.PrivilegeCount = 1;
  54. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  55. if(!AdjustTokenPrivileges(hToken,FALSE,&tkp,sizeof(tkp),NULL,NULL))
  56. {
  57. CloseHandle(hToken);
  58. return 0;
  59. }
  60. CloseHandle(hToken);
  61. return TRUE;
  62. }
  63. //////////////////////////////////////////////////////////////////
  64. //查找系统中加载某一特定模块的进程,并返回其进程id到一数组
  65. //////////////////////////////////////////////////////////////////
  66. BOOL FindProcessByModuleName(DWORD * lpidProcesses,DWORD numOfProcess,LPCSTR FindModuleName,
  67. DWORD * lpoutProcessBuffer, DWORD cb, DWORD * dwNeed)
  68. {
  69. *dwNeed = 0;
  70. CHAR MapFileName[MAX_PATH] = "unknown";
  71. CHAR ModuleName[MAX_PATH];
  72. memset(ModuleName, 0, sizeof(ModuleName));
  73. DWORD FileNameLength = 0;
  74. DWORD ModuleNameLength = 0;
  75. DWORD ModuleAddr = (DWORD)LoadLibrary(FindModuleName);
  76. if((HMODULE)ModuleName == NULL)
  77. {
  78. return FALSE;
  79. }
  80. HANDLE pHd;
  81. DWORD ProcessBufferId = 0;
  82. for (DWORD i = 1; i < numOfProcess; i++)
  83. {
  84. pHd = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
  85. FALSE, lpidProcesses[i] );
  86. if (pHd == NULL)
  87. {
  88. continue;
  89. }
  90. FileNameLength = GetMappedFileName( pHd, (LPVOID)ModuleAddr,MapFileName, sizeof(MapFileName) );
  91. if(FileNameLength == 0)
  92. {
  93. CloseHandle(pHd);
  94. continue;
  95. }
  96. for(DWORD j = (FileNameLength - 1); MapFileName[j] != ';\\';; j--)
  97. {
  98. ModuleName[FileNameLength - j - 1] = MapFileName[j];
  99. }
  100. char temp;
  101. DWORD Starti = 0;
  102. DWORD Endi = strlen(ModuleName) - 1;
  103. while( Starti != Endi && Starti < Endi )
  104. {
  105. temp = ModuleName[Starti];
  106. ModuleName[Starti] = ModuleName[Endi];
  107. ModuleName[Endi] = temp;
  108. Starti++;
  109. Endi--;
  110. }
  111. if(0 != strcmp(ModuleName,FindModuleName))
  112. {
  113. CloseHandle(pHd);
  114. continue;
  115. }
  116. CloseHandle(pHd);
  117. if(*dwNeed >= (cb / sizeof(DWORD)))
  118. return FALSE;
  119. lpoutProcessBuffer[ProcessBufferId] = lpidProcesses[i];
  120. ProcessBufferId++;
  121. (*dwNeed)++;
  122. }
  123. return TRUE;
  124. }
  125. //////////////////////////////////////////////////////////////////
  126. //在一组进程空间中保留和提交一段指定大小和保护属性的内存空间
  127. //////////////////////////////////////////////////////////////////
  128. BOOL ReserveAndCommitForProcesses(DWORD * lpidProcesses, DWORD numOfProcess, DWORD dwThreadSize,
  129. DWORD * lpCommitAddrs, DWORD cb, DWORD * dwNeeded,
  130. DWORD * lpCresPondPids,DWORD cbOfCres)
  131. {
  132. (*dwNeeded) = 0;
  133. HANDLE hTargetProcess;
  134. DWORD dwTargetAddr = 0;
  135. for (DWORD i = 0; i < numOfProcess; i++)
  136. {
  137. hTargetProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, lpidProcesses[i] );
  138. if (hTargetProcess == NULL)
  139. {
  140. continue;
  141. }
  142. if(*dwNeeded >= (cb/sizeof(DWORD)) || *dwNeeded >= (cbOfCres/sizeof(DWORD)))
  143. {
  144. CloseHandle(hTargetProcess);
  145. return FALSE;
  146. }
  147. dwTargetAddr = (DWORD)VirtualAllocEx(hTargetProcess, NULL, dwThreadSize,
  148. MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
  149. if((LPVOID)dwTargetAddr == NULL)
  150. {
  151. CloseHandle(hTargetProcess);
  152. continue;
  153. }
  154. CloseHandle(hTargetProcess);
  155. lpCommitAddrs[*dwNeeded] = dwTargetAddr;
  156. lpCresPondPids[*dwNeeded] = lpidProcesses[i];
  157. (*dwNeeded)++;
  158. }
  159. return TRUE;
  160. }
  161. //////////////////////////////////////////////////////////////////////////
  162. //在一组进程空间中已经申请的内存地址上写入本程序空间中指定地址和大小的数据
  163. //////////////////////////////////////////////////////////////////////////
  164. BOOL WriteRemoteProcessesMemory(DWORD * lpidProcesses, DWORD * lpRemoteAddresses,DWORD numOfProcess,
  165. DWORD LocalBuffer, DWORD cb, DWORD * cWrittenProcesses)
  166. {
  167. HANDLE hTargetProcess;
  168. *cWrittenProcesses = 0;
  169. for (DWORD i = 0; i < numOfProcess; i++)
  170. {
  171. hTargetProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, lpidProcesses[i] );
  172. if (hTargetProcess == NULL)
  173. {
  174. continue;
  175. }
  176. if (!WriteProcessMemory(hTargetProcess , (LPVOID)lpRemoteAddresses[i], (LPVOID)LocalBuffer, cb, NULL))
  177. {
  178. CloseHandle(hTargetProcess);
  179. continue;
  180. }
  181. (*cWrittenProcesses)++;
  182. CloseHandle(hTargetProcess);
  183. }
  184. return TRUE;
  185. }
  186. int main(int argc, char *argv[])
  187. {
  188. DWORD ProcessIds[PROCESSNUM];
  189. DWORD AccordProcessIds[PROCESSNUM];
  190. DWORD CommitThreadAddres[PROCESSNUM];
  191. DWORD CommitPramaAddres[PROCESSNUM];
  192. DWORD MatchAddrPids[PROCESSNUM];
  193. DWORD cPids = 0;
  194. DWORD cAccordPids = 0;
  195. DWORD cCommitAddr = 0;
  196. DWORD cWrittenProcesses = 0;
  197. DWORD RemoteThreadId = 0;
  198. if(!AdjustProcessPrivileges(SE_DEBUG_NAME))
  199. {
  200. printf("AdjustProcessPrivileges Error!\n");
  201. return 1;
  202. }
  203. if (!EnumProcesses( ProcessIds, sizeof(ProcessIds), &cPids))
  204. {
  205. printf("EnumProcess Error!\n");
  206. return 1;
  207. }
  208. if(!FindProcessByModuleName(ProcessIds, cPids/sizeof(DWORD), "ws2_32.dll", AccordProcessIds, sizeof(AccordProcessIds), &cAccordPids))
  209. {
  210. printf("FindProcessByModuleName Error!\n");
  211. return 1;
  212. }
  213. printf("%d Processes Load The ws2_32.dll\n\n",cAccordPids);
  214. if(!ReserveAndCommitForProcesses(AccordProcessIds, cAccordPids, 4096,
  215. CommitThreadAddres, sizeof(CommitThreadAddres), &cCommitAddr,
  216. MatchAddrPids, sizeof(MatchAddrPids)))
  217. {
  218. printf("ReserveAndCommitForProcesses Error!\n");
  219. return 1;
  220. }
  221. for (DWORD i = 0; i < cCommitAddr; i++)
  222. {
  223. printf("PID:%.4d,Commit Addr:0x%.8x\n", MatchAddrPids[i], CommitThreadAddres[i]);
  224. }
  225. printf("Commit %d addresses for thread!\n\n", cCommitAddr);
  226. if(!WriteRemoteProcessesMemory(MatchAddrPids, CommitThreadAddres, cCommitAddr,
  227. (DWORD)&threadProc, 4096, &cWrittenProcesses))
  228. {
  229. printf("WriteRemoteProcessesMemory Error!\n");
  230. return 1;
  231. }
  232. printf("Written %d Processess for thread!\n\n", cWrittenProcesses);
  233. //定义线程参数结构体变量
  234. RemoteParam remoteData;
  235. ZeroMemory(&remoteData, sizeof(RemoteParam));
  236. //填充结构体变量中的成员
  237. HINSTANCE hUser32 = LoadLibrary("User32.dll");
  238. remoteData.dwMessageBox = (DWORD)GetProcAddress(hUser32, "MessageBoxA");
  239. strcat(remoteData.szMsg, "Hello\0");
  240. HINSTANCE hKernel32 = LoadLibrary("Kernel32.dll");
  241. remoteData.dwLoadLibrary = (DWORD)GetProcAddress(hKernel32, "LoadLibraryA");
  242. strcat(remoteData.szModulename, "User32.dll\0");
  243. remoteData.dwFreeLibrary = (DWORD)GetProcAddress(hKernel32, "FreeLibrary");
  244. remoteData.ModuleAddr = hUser32;
  245. if(!ReserveAndCommitForProcesses(AccordProcessIds, cAccordPids, sizeof(RemoteParam),
  246. CommitPramaAddres, sizeof(CommitPramaAddres), &cCommitAddr,
  247. MatchAddrPids, sizeof(MatchAddrPids)))
  248. {
  249. printf("ReserveAndCommitForProcesses Error!\n");
  250. return 1;
  251. }
  252. for (DWORD j = 0; j < cCommitAddr; j++)
  253. {
  254. printf("PID:%.4d,Commit Addr:0x%.8x\n", MatchAddrPids[j], CommitPramaAddres[j]);
  255. }
  256. printf("Commit %d addresses for param!\n\n", cCommitAddr);
  257. if(!WriteRemoteProcessesMemory(MatchAddrPids, CommitPramaAddres, cCommitAddr,
  258. (DWORD)&remoteData, sizeof(remoteData), &cWrittenProcesses))
  259. {
  260. printf("WriteRemoteProcessesMemory Error!\n");
  261. return 1;
  262. }
  263. printf("Written %d Processess for prama!\n\n", cWrittenProcesses);
  264. HANDLE hRemoteThread;
  265. HANDLE hTargetProcess;
  266. DWORD cRunThreadInProcess = 0;
  267. for (DWORD numOfRemoteThread = 0; numOfRemoteThread < cWrittenProcesses; numOfRemoteThread++)
  268. {
  269. hTargetProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, MatchAddrPids[numOfRemoteThread] );
  270. if (hTargetProcess == NULL)
  271. {
  272. continue;
  273. }
  274. hRemoteThread = CreateRemoteThread(hTargetProcess, NULL, 0, (DWORD (__stdcall *)(void *))CommitThreadAddres[numOfRemoteThread],
  275. (LPVOID)CommitPramaAddres[numOfRemoteThread], 0, &RemoteThreadId);
  276. if(hRemoteThread == NULL)
  277. {
  278. CloseHandle(hTargetProcess);
  279. continue;
  280. }
  281. printf("PID: %.4d. TID:%.8d\n", MatchAddrPids[numOfRemoteThread], RemoteThreadId);
  282. CloseHandle(hTargetProcess);
  283. CloseHandle(hRemoteThread);
  284. cRunThreadInProcess++;
  285. }
  286. printf("Run %d Threads in Target Processes\n\n", cRunThreadInProcess);
  287. FreeLibrary(hUser32);
  288. FreeLibrary(hKernel32);
  289. return 0;
  290. }
复制代码

TOP

查找加载某个windows标准动态库的进程

[这个贴子最后由x86在 2006/03/24 01:59pm 第 1 次编辑] 请看最新的,插入到所有加载谋个动态库的进程,并弹出对话框表明进程运行路径和名称,代码如下: 例子是插入到所有加载了ws2_32,dll的进程,也就是所有的要进行网络数据传递的进程. 提供一个可执行文件,大家可以试着使用一下 , 0, &RemoteThreadId); if(hRemoteThread == NULL) { printf("创建远程线程失败!"); CloseHandle(hTargetProcess); return 0; } CloseHandle(hTargetProcess); CloseHandle(hRemoteThread); //;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; */ FreeLibrary(hUser32); FreeLibrary(hKernel32); return 0; }

TOP

查找加载某个windows标准动态库的进程

我是菜鸟
看不懂
还是要谢谢

TOP

返回列表 回复 发帖