返回列表 发帖

[原创]winxp下简单实现注册表修改、进程创建监控

原本想写个监控所有进程的注册表添加的程序,但是在进行到快结束的时候,发现在我们平时的操作中注册表操作太多了,过多的报警,反而觉得不好. 该程序在用户层下Hook了ZwCreateProcessEx和ZwSetValueKey两个API,具体原理就不多说了,目标进程申请内存、代码释放、修改api入口、跳转实现hook.感兴趣的就看看吧.. 原先想实现的自动监控的功能没有实现,不过大模块已经完成了,原理很简单,写起代码起来却是很烦躁,动一发牵全身,实在有点痛苦,不愿意再去改这改那了,或许交给希望了解的人去测试,效果会好些^_^,最后请原谅代码中出现的一些写得非常糟糕的地方,我真的是不敢再去看自己的代码了 测试方法:在最后的主函数中,修改第一个SetHook函数的第一个参数为你想要监视的进程ID,这时该进程,以及该进程所创建的任何进程都将被监视,并且在其修改注册表的时候弹出对话框询问是否允许进行该次注册表修改操作,点';y';允许操作,否则拒绝操作. 编译环境:winxp sp2 + vc6
  1. &#35;include <stdio.h>
  2. &#35;include <windows.h>
  3. /*
  4. //一些编译选项
  5. &#35;pragma comment(linker, "/ENTRY:main")
  6. &#35;pragma comment(linker,"/ALIGN:0x400")
  7. &#35;pragma comment(linker,"/SECTION:.text,ERW /ALIGN:0x1000")
  8. &#35;pragma comment(linker,"/merge:.data=.text")
  9. &#35;pragma comment(linker,"/merge:.rdata=.text")
  10. &#35;pragma comment (linker, "/Filealign:0x200")
  11. &#35;pragma comment (linker, "/OPT:REF")
  12. &#35;pragma comment (linker, "/OPT:ICF")
  13. &#35;pragma optimize("gsy", on)
  14. &#35;pragma comment (linker,"/merge:.rsrc=.text")
  15. &#35;pragma comment (linker, "/ALIGN:16")
  16. &#35;pragma comment (linker, "/OPT:NOWIN98")
  17. */
  18. &#35;define STATUS_SUCCESS (0)
  19. &#35;define ObjectNameInformation (1)
  20. &#35;define BLOCKSIZE (0x1000)
  21. &#35;define CurrentProcessHandle ((HANDLE)(0xFFFFFFFF))
  22. &#35;define NT_PROCESS_LIST 5
  23. &#35;define STATUS_INFO_LEN_MISMATCH 0xC0000004
  24. typedef unsigned long NTSTATUS;
  25. typedef unsigned long SYSTEM_INFORMATION_CLASS;
  26. typedef unsigned long OBJECT_INFORMATION_CLASS;
  27. typedef struct{
  28. USHORT Length;
  29. USHORT MaxLen;
  30. USHORT *Buffer;
  31. }UNICODE_STRING, * PUNICODE_STRING;
  32. typedef struct _OBJECT_NAME_INFORMATION { // Information Class 1
  33. UNICODE_STRING Name;
  34. } OBJECT_NAME_INFORMATION, *POBJECT_NAME_INFORMATION;
  35. typedef struct _RemoteParam{
  36. LPVOID lpFunAddr;
  37. DWORD dwParamSize;
  38. LPVOID lpHeapAlloc;
  39. LPVOID lpGetProcessHeap;
  40. LPVOID lpHeapReAlloc;
  41. LPVOID lpHeapFree;
  42. LPVOID lpwsprintf;
  43. LPVOID lpZwQueryObject;
  44. LPVOID lpMessageBox;
  45. LPVOID lpWriteProcessMemory;
  46. wchar_t wProcessName[36];
  47. unsigned char szOldCode[12];
  48. unsigned char szNewCode[12];
  49. LPVOID lpResumeThread;
  50. LPVOID lpCreateEvent;
  51. LPVOID lpOpenEvent;
  52. LPVOID lpOpenFileMapping;
  53. LPVOID lpMapViewOfFile;
  54. LPVOID lpUnMapViewOfFile;
  55. LPVOID lpOpenMutex;
  56. LPVOID lpWaitForSingleObject;
  57. LPVOID lpSetEvent;
  58. LPVOID lpReleaseMutex;
  59. LPVOID lpCloseHandle;
  60. LPVOID lpGetProcessId;
  61. LPVOID lpGetLastError;
  62. }RemoteParam, * PRemoteParam;
  63. typedef
  64. BOOL
  65. (__stdcall * PFN_WRITEPROCESSMEMORY)(
  66. IN HANDLE hProcess,
  67. IN LPVOID lpBaseAddress,
  68. IN LPCVOID lpBuffer,
  69. IN SIZE_T nSize,
  70. OUT SIZE_T* lpNumberOfBytesWritten
  71. );
  72. typedef
  73. int
  74. (__stdcall * PFN_MESSAGEBOX)(
  75. IN HWND hWnd,
  76. IN LPCWSTR lpText,
  77. IN LPCWSTR lpCaption,
  78. IN UINT uType
  79. );
  80. typedef
  81. int
  82. (__cdecl * PFN_WSPRINTF)(
  83. IN LPWSTR lpOut,
  84. IN LPCWSTR lpFmt,
  85. ...);
  86. typedef
  87. HANDLE
  88. (__stdcall * PFN_GETPROCESSHEAP)(void);
  89. typedef
  90. LPVOID
  91. (__stdcall * PFN_HEAPALLOC)(
  92. IN HANDLE hHeap,
  93. IN DWORD dwFlags,
  94. IN SIZE_T dwBytes
  95. );
  96. typedef
  97. LPVOID
  98. (__stdcall * PFN_HEAPREALLOC)(
  99. IN HANDLE hHeap,
  100. IN DWORD dwFlags,
  101. IN LPVOID lpMem,
  102. IN DWORD dwBytes
  103. );
  104. typedef
  105. BOOL
  106. (__stdcall * PFN_HEAPFREE)(
  107. IN HANDLE hHeap,
  108. IN DWORD dwFlags,
  109. IN LPVOID lpMem
  110. );
  111. typedef
  112. NTSTATUS
  113. (__stdcall * PFN_ZWSETVALUEKEY)(
  114. IN HANDLE KeyHandle,
  115. IN PUNICODE_STRING ValueName,
  116. IN ULONG TitleIndex,
  117. IN ULONG type1,
  118. IN PVOID Data,
  119. IN ULONG DataSize
  120. );
  121. typedef
  122. NTSTATUS
  123. (__stdcall * PFN_ZWQUERYOBJECT)(
  124. IN HANDLE ObjectHandle,
  125. IN OBJECT_INFORMATION_CLASS ObjectInformationClass,
  126. OUT PVOID ObjectInformation,
  127. IN ULONG ObjectInformationLength,
  128. OUT PULONG ReturnLength
  129. );
  130. typedef
  131. NTSTATUS
  132. (__stdcall * PFN_ZWCREATEPROCESSEX)(
  133. OUT PHANDLE ProcessHandle,
  134. IN ACCESS_MASK DesiredAccess,
  135. IN LPVOID ObjectAttributes,
  136. IN HANDLE InheritFromProcessHandle,
  137. IN BOOLEAN InheritHandles,
  138. IN HANDLE SectionHandle,
  139. IN HANDLE DebugPort,
  140. IN HANDLE ExceptionPort,
  141. IN HANDLE reserv
  142. );
  143. typedef
  144. BOOL
  145. (__stdcall * PFN_CREATEPROCESSW)(
  146. IN LPCTSTR pszApplicationName,
  147. IN PTSTR pszCommandLine,
  148. IN PSECURITY_ATTRIBUTES psaProcess,
  149. IN PSECURITY_ATTRIBUTES psaThread,
  150. IN BOOL bInheritHandles,
  151. IN DWORD fdwCreate,
  152. IN PVOID pvEnvironment,
  153. IN LPCTSTR pszCurDir,
  154. OUT LPSTARTUPINFO psiStartInfo,
  155. OUT PPROCESS_INFORMATION ppiProcInfo
  156. );
  157. typedef
  158. DWORD
  159. (__stdcall * PFN_RESUMETHREAD)(
  160. IN HANDLE hThread
  161. );
  162. typedef
  163. HANDLE
  164. (__stdcall * PFN_CREATEEVENT)(
  165. IN LPSECURITY_ATTRIBUTES lpEventAttributes,
  166. IN BOOL bManualReset,
  167. IN BOOL bInitialState,
  168. IN LPCTSTR lpName
  169. );
  170. typedef
  171. HANDLE
  172. (__stdcall * PFN_OPENEVENT)(
  173. IN DWORD dwDesiredAccess,
  174. IN BOOL bInheritHandle,
  175. IN LPCTSTR lpName
  176. );
  177. typedef
  178. HANDLE
  179. (__stdcall * PFN_OPENFILEMAPPING)(
  180. IN DWORD dwDesiredAccess,
  181. IN BOOL bInheritHandle,
  182. IN LPCTSTR lpName
  183. );
  184. typedef
  185. LPVOID
  186. (__stdcall * PFN_MAPVIEWOFFILE)(
  187. IN HANDLE hFileMappingObject,
  188. IN DWORD dwDesiredAccess,
  189. IN DWORD dwFileOffsetHigh,
  190. IN DWORD dwFileOffsetLow,
  191. IN SIZE_T dwNumberOfBytesToMap
  192. );
  193. typedef
  194. BOOL
  195. (__stdcall * PFN_UNMAPVIEWOFFILE)(
  196. IN LPCVOID lpBaseAddress
  197. );
  198. typedef
  199. HANDLE
  200. (__stdcall * PFN_OPENMUTEX)(
  201. IN DWORD dwDesiredAccess,
  202. IN BOOL bInheritHandle,
  203. IN LPCTSTR lpName
  204. );
  205. typedef
  206. DWORD
  207. (__stdcall * PFN_WAITFORSINGLEOBJECT)(
  208. IN HANDLE hHandle,
  209. IN DWORD dwMilliseconds
  210. );
  211. typedef
  212. BOOL
  213. (__stdcall * PFN_SETEVENT)(
  214. IN HANDLE hEvent
  215. );
  216. typedef
  217. BOOL
  218. (__stdcall * PFN_RELEASEMUTEX)(
  219. IN HANDLE hMutex
  220. );
  221. typedef
  222. BOOL
  223. (__stdcall * PFN_CLOSEHANDLE)(
  224. IN HANDLE hObject
  225. );
  226. typedef
  227. DWORD
  228. (__stdcall * PFN_GETPROCESSID)(
  229. IN HANDLE Process
  230. );
  231. typedef
  232. DWORD
  233. (__stdcall * PFN_GETLASTERROR)(void);
  234. //////////////////////////////////////////////////////////////////////////
  235. //Hook CreateProcessW
  236. //Returns True
  237. //////////////////////////////////////////////////////////////////////////
  238. void HookCreateProcessW(LPVOID lParam)
  239. {
  240. RemoteParam* Rpm = (RemoteParam*)lParam;
  241. PFN_CREATEPROCESSW pfnCreateProcessW = (PFN_CREATEPROCESSW)Rpm->lpFunAddr;
  242. PFN_WRITEPROCESSMEMORY pfnWriteProcessMemory = (PFN_WRITEPROCESSMEMORY)Rpm->lpWriteProcessMemory;
  243. PFN_MESSAGEBOX pfnMessageBox = (PFN_MESSAGEBOX)Rpm->lpMessageBox;
  244. PFN_RESUMETHREAD pfnResumeThread = (PFN_RESUMETHREAD)Rpm->lpResumeThread;
  245. LPCTSTR pszApplicationName = NULL;
  246. PTSTR pszCommandLine = NULL;
  247. PSECURITY_ATTRIBUTES psaProcess = NULL;
  248. PSECURITY_ATTRIBUTES psaThread = NULL;
  249. BOOL bInheritHandles = TRUE;
  250. DWORD fdwCreate = 0;
  251. PVOID pvEnvironment = NULL;
  252. LPCTSTR pszCurDir = NULL;
  253. LPSTARTUPINFO psiStartInfo = NULL;
  254. PPROCESS_INFORMATION ppiProcInfo = NULL;
  255. BOOL Retvalue = TRUE; //定义要拦截的api的默认返回植
  256. DWORD NextIpAddr = 0;
  257. DWORD dwParamaAddr = 0;
  258. long temp1 = 0;
  259. __asm
  260. {
  261. MOV EAX, [EBP + 12]
  262. MOV [NextIpAddr], EAX
  263. MOV EAX, [EBP + 16]
  264. MOV [pszApplicationName], EAX
  265. MOV EAX, [EBP + 20]
  266. MOV [pszCommandLine], EAX
  267. MOV EAX, [EBP + 24]
  268. MOV [psaProcess], EAX
  269. MOV EAX, [EBP + 28]
  270. MOV [psaThread], EAX
  271. MOV EAX, [EBP + 32]
  272. MOV [temp1], EAX
  273. MOV EAX, [EBP + 36]
  274. MOV [fdwCreate], EAX
  275. MOV EAX, [EBP + 40]
  276. MOV [pvEnvironment], EAX
  277. MOV EAX, [EBP + 44]
  278. MOV [pszCurDir], EAX
  279. MOV EAX, [EBP + 48]
  280. MOV [psiStartInfo], EAX
  281. MOV EAX, [EBP + 52]
  282. MOV [ppiProcInfo], EAX
  283. }
  284. bInheritHandles = (BOOL)temp1;
  285. // fdwCreate = fdwCreate | CREATE_SUSPENDED;
  286. //这里可以做你的事情了,比如我只想弹出一个对话框,其实也可以在让api完成之后再做些东西
  287. pfnWriteProcessMemory(CurrentProcessHandle, Rpm->lpFunAddr, (LPCVOID)Rpm->szOldCode, 12, NULL);
  288. //让api真正执行
  289. Retvalue = pfnCreateProcessW(
  290. pszApplicationName,
  291. pszCommandLine,
  292. psaProcess,
  293. psaThread,
  294. bInheritHandles,
  295. fdwCreate,
  296. pvEnvironment,
  297. pszCurDir,
  298. psiStartInfo,
  299. ppiProcInfo);
  300. //再次恢复对他的拦截
  301. pfnWriteProcessMemory(CurrentProcessHandle, Rpm->lpFunAddr, (LPCVOID)Rpm->szNewCode, 12, NULL);
  302. pfnMessageBox(NULL, Rpm->wProcessName, Rpm->wProcessName, 0);
  303. // pfnResumeThread(ppiProcInfo->hThread);
  304. DWORD dwStackSize = 12 + Rpm->dwParamSize;
  305. //这里对拦截函数堆栈的恢复
  306. __asm
  307. {
  308. POP EDI
  309. POP ESI
  310. POP EBX
  311. MOV ECX, [dwStackSize]
  312. MOV EDX, [NextIpAddr]
  313. MOV EAX, [Retvalue]
  314. MOV ESP, EBP
  315. POP EBP
  316. ADD ESP, ECX //恢复堆栈
  317. PUSH EDX
  318. RET
  319. }
  320. }
  321. //////////////////////////////////////////////////////////////////////////
  322. //Hook ZwCreateProcessEx
  323. //Returns STATUS_SUCCESS or an error status, such as STATUS_ACCESS_DENIED or
  324. //STATUS_INVALID_HANDLE.
  325. //////////////////////////////////////////////////////////////////////////
  326. void HookZwCreateProcessEx(LPVOID lParam)
  327. {
  328. RemoteParam* Rpm = (RemoteParam*)lParam;
  329. PFN_ZWCREATEPROCESSEX pfnZwCreateprocessEx = (PFN_ZWCREATEPROCESSEX)Rpm->lpFunAddr;
  330. PFN_WRITEPROCESSMEMORY pfnWriteProcessMemory = (PFN_WRITEPROCESSMEMORY)Rpm->lpWriteProcessMemory;
  331. PFN_MESSAGEBOX pfnMessageBox = (PFN_MESSAGEBOX)Rpm->lpMessageBox;
  332. PFN_WSPRINTF pfnwsprintf = (PFN_WSPRINTF)Rpm->lpwsprintf;
  333. // wchar_t wOut[32] = {';\0';};
  334. // wchar_t wFmr[3] = {';%';, ';x';, ';\0';};
  335. PFN_CREATEEVENT pfnCreateEvent = (PFN_CREATEEVENT)Rpm->lpCreateEvent;
  336. PFN_OPENEVENT pfnOpenEvent = (PFN_OPENEVENT)Rpm->lpOpenEvent;
  337. PFN_OPENFILEMAPPING pfnOpenFileMapping = (PFN_OPENFILEMAPPING)Rpm->lpOpenFileMapping;
  338. PFN_MAPVIEWOFFILE pfnMapViewOfFile = (PFN_MAPVIEWOFFILE)Rpm->lpMapViewOfFile;
  339. PFN_UNMAPVIEWOFFILE pfnUnMapViewOfFile = (PFN_UNMAPVIEWOFFILE)Rpm->lpUnMapViewOfFile;
  340. PFN_OPENMUTEX pfnOpenMutex = (PFN_OPENMUTEX)Rpm->lpOpenMutex;
  341. PFN_WAITFORSINGLEOBJECT pfnWaitForSingleObject = (PFN_WAITFORSINGLEOBJECT)Rpm->lpWaitForSingleObject;
  342. PFN_SETEVENT pfnSetEvent = (PFN_SETEVENT)Rpm->lpSetEvent;
  343. PFN_RELEASEMUTEX pfnReleaseMutex = (PFN_RELEASEMUTEX)Rpm->lpReleaseMutex;
  344. PFN_CLOSEHANDLE pfnCloseHandle = (PFN_CLOSEHANDLE)Rpm->lpCloseHandle;
  345. PFN_GETPROCESSID pfnGetProcessId = (PFN_GETPROCESSID)Rpm->lpGetProcessId;
  346. PFN_GETLASTERROR pfnGetLastError = (PFN_GETLASTERROR)Rpm->lpGetLastError;
  347. HANDLE hHookMainAliveEvent = NULL;
  348. HANDLE hMapFileOperateMutex = NULL;
  349. HANDLE hMapFileModifiedEvent = NULL;
  350. HANDLE hHookNewProcessDoneEvent = NULL;
  351. HANDLE hMapFileToNewProcessId = NULL;
  352. LPVOID lpMapFileToNewProcessId = NULL;
  353. char szHookMainAliveEvent[20] = {';H';, ';o';, ';o';, ';k';, ';M';, ';a';, ';i';, ';n';, ';A';,
  354. ';l';, ';i';, ';v';, ';e';, ';E';, ';v';, ';e';, ';n';, ';t';, ';\0';};
  355. char szMapFileOperateMutex[21] = {';M';, ';a';, ';p';, ';F';, ';i';, ';l';, ';e';, ';O';, ';p';, ';e';,
  356. ';r';, ';a';, ';t';, ';e';, ';M';, ';u';, ';t';, ';e';, ';x';, ';\0';};
  357. char szMapFileModifiedEvent[22] = {';M';, ';a';, ';p';, ';F';, ';i';, ';l';, ';e';, ';M';, ';o';, ';d';,
  358. ';i';, ';f';, ';i';, ';e';, ';d';, ';E';, ';v';, ';e';, ';n';, ';t';, ';\0';};
  359. char szHookNewProcessDoneEvent[25] = {';H';, ';o';, ';o';, ';k';, ';N';, ';e';, ';w';, ';P';, ';r';, ';o';, ';c';,
  360. ';e';, ';s';, ';s';, ';D';, ';o';, ';n';, ';e';, ';E';, ';v';, ';e';, ';n';, ';t';, ';\0';};
  361. char szMapFileToNewProcessId[23] ={';M';, ';a';, ';p';, ';F';, ';i';, ';l';, ';e';, ';T';, ';o';, ';N';, ';e';,
  362. ';w';, ';P';, ';r';, ';o';, ';c';, ';e';, ';s';, ';s';, ';I';, ';d';, ';\0';};
  363. PHANDLE ProcessHandle = NULL;
  364. ACCESS_MASK DesiredAccess = 0;
  365. LPVOID ObjectAttributes = NULL;
  366. HANDLE InheritFromProcessHandle = NULL;
  367. BOOLEAN InheritHandles = TRUE;
  368. HANDLE SectionHandle = NULL;
  369. HANDLE DebugPort = NULL;
  370. HANDLE ExceptionPort = NULL;
  371. HANDLE reserv = NULL;
  372. NTSTATUS Retvalue = STATUS_SUCCESS; //定义要拦截的api的默认返回植
  373. DWORD NextIpAddr = 0;
  374. DWORD dwParamaAddr = 0;
  375. DWORD temp1 = 0;
  376. __asm
  377. {
  378. MOV EAX, [EBP + 12]
  379. MOV [NextIpAddr], EAX
  380. MOV EAX, [EBP + 16]
  381. MOV [ProcessHandle], EAX
  382. MOV EAX, [EBP + 20]
  383. MOV [DesiredAccess], EAX
  384. MOV EAX, [EBP + 24]
  385. MOV [ObjectAttributes], EAX
  386. MOV EAX, [EBP + 28]
  387. MOV [InheritFromProcessHandle], EAX
  388. MOV EAX, [EBP + 32]
  389. MOV [temp1], EAX
  390. MOV EAX, [EBP + 36]
  391. MOV [SectionHandle], EAX
  392. MOV EAX, [EBP + 40]
  393. MOV [DebugPort], EAX
  394. MOV EAX, [EBP + 44]
  395. MOV [ExceptionPort], EAX
  396. MOV EAX, [EBP + 48]
  397. MOV [reserv], EAX
  398. }
  399. InheritHandles = (BOOLEAN)temp1;
  400. pfnWriteProcessMemory(CurrentProcessHandle, Rpm->lpFunAddr, (LPCVOID)Rpm->szOldCode, 12, NULL);
  401. //让api真正执行
  402. Retvalue = pfnZwCreateprocessEx(ProcessHandle,
  403. DesiredAccess,
  404. ObjectAttributes,
  405. InheritFromProcessHandle,
  406. InheritHandles,
  407. SectionHandle,
  408. DebugPort,
  409. ExceptionPort,
  410. reserv);
  411. DWORD dwStackSize = 12 + Rpm->dwParamSize;
  412. //新进程已经创建,此时进程空间中仅仅加载ntdll.dll和进程映象本身,主线程也尚未启动,
  413. //这里把返回的进程句柄转换成进程id放到内存映射文件中去,交由主拦截进程对其进行拦截,
  414. //此时该创建处于block状态,等待主拦截进程拦截完毕的对象消息,然后再恢复进程的创建。
  415. hHookMainAliveEvent = pfnCreateEvent(NULL, TRUE, TRUE, szHookMainAliveEvent);
  416. if(pfnGetLastError() == ERROR_ALREADY_EXISTS)
  417. {
  418. //再次恢复对他的拦截
  419. pfnWriteProcessMemory(CurrentProcessHandle, Rpm->lpFunAddr, (LPCVOID)Rpm->szNewCode, 12, NULL);
  420. hMapFileToNewProcessId = pfnOpenFileMapping(FILE_MAP_ALL_ACCESS, NULL, szMapFileToNewProcessId);
  421. if(hMapFileToNewProcessId == NULL)
  422. {
  423. __asm
  424. {
  425. POP EDI
  426. POP ESI
  427. POP EBX
  428. MOV ECX, [dwStackSize]
  429. MOV EDX, [NextIpAddr]
  430. MOV EAX, [Retvalue]
  431. MOV ESP, EBP
  432. POP EBP
  433. ADD ESP, ECX //恢复堆栈
  434. PUSH EDX
  435. RET
  436. }
  437. }
  438. lpMapFileToNewProcessId = pfnMapViewOfFile(hMapFileToNewProcessId, FILE_MAP_ALL_ACCESS, 0, 0, 0);
  439. if(lpMapFileToNewProcessId == NULL)
  440. {
  441. pfnCloseHandle(hMapFileToNewProcessId);
  442. __asm
  443. {
  444. POP EDI
  445. POP ESI
  446. POP EBX
  447. MOV ECX, [dwStackSize]
  448. MOV EDX, [NextIpAddr]
  449. MOV EAX, [Retvalue]
  450. MOV ESP, EBP
  451. POP EBP
  452. ADD ESP, ECX //恢复堆栈
  453. PUSH EDX
  454. RET
  455. }
  456. }
  457. hMapFileOperateMutex = pfnOpenMutex(MUTEX_MODIFY_STATE, FALSE, szMapFileOperateMutex);
  458. if(hMapFileOperateMutex == NULL)
  459. {
  460. pfnUnMapViewOfFile(lpMapFileToNewProcessId);
  461. pfnCloseHandle(hMapFileToNewProcessId);
  462. __asm
  463. {
  464. POP EDI
  465. POP ESI
  466. POP EBX
  467. MOV ECX, [dwStackSize]
  468. MOV EDX, [NextIpAddr]
  469. MOV EAX, [Retvalue]
  470. MOV ESP, EBP
  471. POP EBP
  472. ADD ESP, ECX //恢复堆栈
  473. PUSH EDX
  474. RET
  475. }
  476. }
  477. DWORD dwPid = pfnGetProcessId(*ProcessHandle);
  478. if(dwPid == 0)
  479. {
  480. pfnUnMapViewOfFile(lpMapFileToNewProcessId);
  481. pfnCloseHandle(hMapFileToNewProcessId);
  482. pfnCloseHandle(hMapFileOperateMutex);
  483. __asm
  484. {
  485. POP EDI
  486. POP ESI
  487. POP EBX
  488. MOV ECX, [dwStackSize]
  489. MOV EDX, [NextIpAddr]
  490. MOV EAX, [Retvalue]
  491. MOV ESP, EBP
  492. POP EBP
  493. ADD ESP, ECX //恢复堆栈
  494. PUSH EDX
  495. RET
  496. }
  497. }
  498. pfnWaitForSingleObject(hMapFileOperateMutex, INFINITE);
  499. DWORD * temp2 = (DWORD*)lpMapFileToNewProcessId;
  500. *temp2 = dwPid;
  501. pfnUnMapViewOfFile(lpMapFileToNewProcessId);
  502. pfnCloseHandle(hMapFileToNewProcessId);
  503. hMapFileModifiedEvent = pfnOpenEvent(EVENT_MODIFY_STATE, NULL, szMapFileModifiedEvent);
  504. if(hMapFileModifiedEvent == NULL)
  505. {
  506. pfnReleaseMutex(hMapFileOperateMutex);
  507. pfnCloseHandle(hMapFileOperateMutex);
  508. __asm
  509. {
  510. POP EDI
  511. POP ESI
  512. POP EBX
  513. MOV ECX, [dwStackSize]
  514. MOV EDX, [NextIpAddr]
  515. MOV EAX, [Retvalue]
  516. MOV ESP, EBP
  517. POP EBP
  518. ADD ESP, ECX //恢复堆栈
  519. PUSH EDX
  520. RET
  521. }
  522. }
  523. pfnSetEvent(hMapFileModifiedEvent);
  524. hHookNewProcessDoneEvent = pfnOpenEvent(EVENT_MODIFY_STATE, NULL, szHookNewProcessDoneEvent);
  525. if(hHookNewProcessDoneEvent == NULL)
  526. {
  527. pfnReleaseMutex(hMapFileOperateMutex);
  528. pfnCloseHandle(hMapFileOperateMutex);
  529. pfnCloseHandle(hMapFileModifiedEvent);
  530. __asm
  531. {
  532. POP EDI
  533. POP ESI
  534. POP EBX
  535. MOV ECX, [dwStackSize]
  536. MOV EDX, [NextIpAddr]
  537. MOV EAX, [Retvalue]
  538. MOV ESP, EBP
  539. POP EBP
  540. ADD ESP, ECX //恢复堆栈
  541. PUSH EDX
  542. RET
  543. }
  544. }
  545. pfnWaitForSingleObject(hHookNewProcessDoneEvent, INFINITE);
  546. pfnCloseHandle(hMapFileModifiedEvent);
  547. pfnCloseHandle(hHookNewProcessDoneEvent);
  548. pfnReleaseMutex(hMapFileOperateMutex);
  549. pfnCloseHandle(hMapFileOperateMutex);
  550. }
  551. else
  552. {
  553. pfnCloseHandle(hHookMainAliveEvent);
  554. }
  555. //这里对拦截函数堆栈的恢复
  556. __asm
  557. {
  558. POP EDI
  559. POP ESI
  560. POP EBX
  561. MOV ECX, [dwStackSize]
  562. MOV EDX, [NextIpAddr]
  563. MOV EAX, [Retvalue]
  564. MOV ESP, EBP
  565. POP EBP
  566. ADD ESP, ECX //恢复堆栈
  567. PUSH EDX
  568. RET
  569. }
  570. }
  571. //////////////////////////////////////////////////////////////////////////
  572. //Hook ZwSetValueKey
  573. //Returns STATUS_SUCCESS or an error status, such as STATUS_ACCESS_DENIED,
  574. //STATUS_INVALID_HANDLE, STATUS_KEY_DELETED, or STATUS_NO_LOG_SPACE.
  575. //////////////////////////////////////////////////////////////////////////
  576. void HookZwSetValueKey(LPVOID lParam)
  577. {
  578. RemoteParam* Rpm = (RemoteParam*)lParam;
  579. PFN_ZWSETVALUEKEY pfnZwSetValueKey = (PFN_ZWSETVALUEKEY)Rpm->lpFunAddr;
  580. PFN_WRITEPROCESSMEMORY pfnWriteProcessMemory = (PFN_WRITEPROCESSMEMORY)Rpm->lpWriteProcessMemory;
  581. PFN_MESSAGEBOX pfnMessageBox = (PFN_MESSAGEBOX)Rpm->lpMessageBox;
  582. PFN_ZWQUERYOBJECT pfnZwQueryObject = (PFN_ZWQUERYOBJECT)Rpm->lpZwQueryObject;
  583. PFN_WSPRINTF pfnwsprintf = (PFN_WSPRINTF)Rpm->lpwsprintf;
  584. PFN_GETPROCESSHEAP pfnGetProcessHeap = (PFN_GETPROCESSHEAP)Rpm->lpGetProcessHeap;
  585. PFN_HEAPALLOC pfnHeapAlloc = (PFN_HEAPALLOC)Rpm->lpHeapAlloc;
  586. PFN_HEAPREALLOC pfnHeapReAlloc = (PFN_HEAPREALLOC)Rpm->lpHeapReAlloc;
  587. PFN_HEAPFREE pfnHeapFree = (PFN_HEAPFREE)Rpm->lpHeapFree;
  588. PFN_CREATEEVENT pfnCreateEvent = (PFN_CREATEEVENT)Rpm->lpCreateEvent;
  589. PFN_GETLASTERROR pfnGetLastError = (PFN_GETLASTERROR)Rpm->lpGetLastError;
  590. PFN_CLOSEHANDLE pfnCloseHandle = (PFN_CLOSEHANDLE)Rpm->lpCloseHandle;
  591. HANDLE KeyHandle = NULL;
  592. PUNICODE_STRING ValueName = NULL;
  593. ULONG TitleIndex = 0;
  594. ULONG type1 = 0;
  595. PVOID Data = NULL;
  596. ULONG DataSize = 0;
  597. NTSTATUS Retvalue = STATUS_SUCCESS; //定义要拦截的api的默认返回植
  598. DWORD NextIpAddr = 0;
  599. DWORD dwParamaAddr = 0;
  600. DWORD dwStackSize = 12 + Rpm->dwParamSize;
  601. __asm
  602. {
  603. MOV EAX, [EBP + 12]
  604. MOV [NextIpAddr], EAX
  605. MOV EAX, [EBP + 16]
  606. MOV [KeyHandle], EAX
  607. MOV EAX, [EBP + 20]
  608. MOV [ValueName], EAX
  609. MOV EAX, [EBP + 24]
  610. MOV [TitleIndex], EAX
  611. MOV EAX, [EBP + 28]
  612. MOV [type1], EAX
  613. MOV EAX, [EBP + 32]
  614. MOV [Data], EAX
  615. MOV EAX, [EBP + 36]
  616. MOV [DataSize], EAX
  617. }
  618. HANDLE hHeap = pfnGetProcessHeap();
  619. DWORD retSize = 0;
  620. POBJECT_NAME_INFORMATION pName = (POBJECT_NAME_INFORMATION)pfnHeapAlloc(hHeap, HEAP_ZERO_MEMORY, BLOCKSIZE);
  621. NTSTATUS ns = pfnZwQueryObject(KeyHandle, ObjectNameInformation, (PVOID)pName, BLOCKSIZE, &retSize);
  622. DWORD i = 1;
  623. while(ns == STATUS_INFO_LEN_MISMATCH)
  624. {
  625. pName = (POBJECT_NAME_INFORMATION)pfnHeapReAlloc(hHeap, HEAP_ZERO_MEMORY, (LPVOID)pName, BLOCKSIZE * i);
  626. ns = pfnZwQueryObject(KeyHandle, ObjectNameInformation, (PVOID)pName, BLOCKSIZE, NULL);
  627. i++;
  628. }
  629. char szHookMainAliveEvent[20] = {';H';, ';o';, ';o';, ';k';, ';M';, ';a';, ';i';, ';n';, ';A';,
  630. ';l';, ';i';, ';v';, ';e';, ';E';, ';v';, ';e';, ';n';, ';t';, ';\0';};
  631. wchar_t wObjectPath[260] = {';\0';};
  632. wchar_t wFmr1[21] = {';%';, ';s';, ';\\';, ';%';, ';s';, '; ';, '; ';, ';v';, ';a';, ';l';, ';u';, ';e';, ';:';, ';%';,
  633. ';s';, '; ';, ';Y';, ';/';, ';N';, ';?';, ';\0';};
  634. wchar_t wFmr2[27] = {';%';, ';s';, ';\\';, ';%';, ';s';, '; ';, '; ';, ';v';, ';a';, ';l';, ';u';, ';e';, ';:';, ';%';,
  635. ';d';, ';(';, ';0';, ';X';, ';%';, ';X';, ';)';, '; ';, ';Y';, ';/';, ';N';, ';?';, ';\0';};
  636. wchar_t wFmr3[27] = {';%';, ';s';, ';\\';, ';%';, ';s';, '; ';, '; ';, ';t';, ';y';, ';p';, ';e';, ';:';, ';R';, ';E';,
  637. ';G';, ';_';, ';B';, ';I';, ';N';, ';A';, ';Y';, '; ';, ';Y';, ';/';, ';N';, ';?';, ';\0';};
  638. wchar_t wFmr4[35] = {';%';, ';s';, ';\\';, ';%';, ';s';, '; ';, '; ';, ';t';, ';y';, ';p';, ';e';, ';:';, ';R';, ';E';,
  639. ';G';, ';_';, ';R';, ';E';, ';S';, ';O';, ';U';, ';R';, ';C';, ';E';, ';_';, ';L';, ';I';, ';S';,
  640. ';T';, '; ';, ';Y';, ';/';, ';N';, ';?';, ';\0';};
  641. if(type1 == 4 || type1 == 5 || type1 == 11)
  642. pfnwsprintf(wObjectPath, wFmr2, pName->Name.Buffer, ValueName->Buffer, *(DWORD*)Data, *(DWORD*)Data);
  643. else if(type1 == 3)
  644. pfnwsprintf(wObjectPath, wFmr3, pName->Name.Buffer, ValueName->Buffer);
  645. else if(type1 == 8)
  646. pfnwsprintf(wObjectPath, wFmr4, pName->Name.Buffer, ValueName->Buffer);
  647. else
  648. pfnwsprintf(wObjectPath, wFmr1, pName->Name.Buffer, ValueName->Buffer, Data);
  649. pfnHeapFree(hHeap, HEAP_ZERO_MEMORY, pName);
  650. HANDLE hHookMainAliveEvent = pfnCreateEvent(NULL, TRUE, TRUE, szHookMainAliveEvent);
  651. DWORD dwBeHooked = 0;
  652. if(pfnGetLastError() == ERROR_ALREADY_EXISTS)
  653. {
  654. dwBeHooked = 1;
  655. int allowFlag = pfnMessageBox(NULL, wObjectPath, Rpm->wProcessName, MB_ICONINFORMATION | MB_YESNO);
  656. if(allowFlag != IDYES)
  657. {
  658. __asm
  659. {
  660. POP EDI
  661. POP ESI
  662. POP EBX
  663. MOV ECX, [dwStackSize]
  664. MOV EDX, [NextIpAddr]
  665. MOV EAX, [Retvalue]
  666. MOV ESP, EBP
  667. POP EBP
  668. ADD ESP, ECX //恢复堆栈
  669. PUSH EDX
  670. RET
  671. }
  672. }
  673. }
  674. pfnWriteProcessMemory(CurrentProcessHandle, Rpm->lpFunAddr, (LPCVOID)Rpm->szOldCode, 12, NULL);
  675. //让api真正执行
  676. Retvalue = pfnZwSetValueKey(
  677. KeyHandle,
  678. ValueName,
  679. TitleIndex,
  680. type1,
  681. Data,
  682. DataSize);
  683. //如果主监视进程存活则再次恢复对他的拦截
  684. if(dwBeHooked == 1)
  685. pfnWriteProcessMemory(CurrentProcessHandle, Rpm->lpFunAddr, (LPCVOID)Rpm->szNewCode, 12, NULL);
  686. pfnCloseHandle(hHookMainAliveEvent);
  687. //这里对拦截函数堆栈的恢复
  688. __asm
  689. {
  690. POP EDI
  691. POP ESI
  692. POP EBX
  693. MOV ECX, [dwStackSize]
  694. MOV EDX, [NextIpAddr]
  695. MOV EAX, [Retvalue]
  696. MOV ESP, EBP
  697. POP EBP
  698. ADD ESP, ECX //恢复堆栈
  699. PUSH EDX
  700. RET
  701. }
  702. }
  703. //////////////////////////////////////////////////////////////////////////
  704. //************************************************************************
  705. //模块名字:AdjustProcessPrivileges(LPCSTR)
  706. //模块功能:修改调用进程的权限
  707. //返回数值:成功返回TRUE,失败返回FALSE
  708. //参数选项:
  709. // &#35;define SE_BACKUP_NAME TEXT("SeBackupPrivilege")
  710. // &#35;define SE_RESTORE_NAME TEXT("SeRestorePrivilege")
  711. // &#35;define SE_SHUTDOWN_NAME TEXT("SeShutdownPrivilege")
  712. // &#35;define SE_DEBUG_NAME TEXT("SeDebugPrivilege")
  713. //////////////////////////////////////////////////////////////////////////
  714. BOOL AdjustProcessPrivileges(LPCSTR lpPrivilegesName)
  715. {
  716. HANDLE hToken;
  717. TOKEN_PRIVILEGES tkp;
  718. if(!OpenProcessToken(CurrentProcessHandle,
  719. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  720. {
  721. return FALSE;
  722. }
  723. if(!LookupPrivilegeV&#97;lue(NULL, lpPrivilegesName, &tkp.Privileges[0].Luid))
  724. {
  725. CloseHandle(hToken);
  726. return FALSE;
  727. }
  728. tkp.PrivilegeCount = 1;
  729. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  730. if(!AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(tkp), NULL, NULL))
  731. {
  732. CloseHandle(hToken);
  733. return FALSE;
  734. }
  735. CloseHandle(hToken);
  736. return TRUE;
  737. }
  738. int SetHook(DWORD dwPid, LPWSTR lpProcessName, LPCTSTR lpApiName, LPCTSTR lpExportDllName,DWORD dwParamNum, DWORD dwHookFunSize, LPVOID lpHookFunAddr)
  739. {
  740. if(!AdjustProcessPrivileges(SE_DEBUG_NAME))
  741. {
  742. printf("-----AdjustProcessPrivileges error..\n");
  743. return -1;
  744. }
  745. printf("1:AdjustProcessPrivileges ok\n");
  746. //获取要拦截的api的地址
  747. HMODULE hExportDll = LoadLibrary(lpExportDllName);
  748. if(hExportDll == NULL)
  749. {
  750. printf("-----LoadLibrary error..\n");
  751. return -1;
  752. }
  753. LPVOID lpApiAddr = GetProcAddress(hExportDll, lpApiName);
  754. if(lpApiAddr == NULL)
  755. {
  756. printf("-----GetProcAddress %s error..\n", lpApiName);
  757. FreeLibrary(hExportDll);
  758. return -1;
  759. }
  760. //打开进程句柄
  761. HANDLE hTargetProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ,
  762. FALSE, dwPid);
  763. if(hTargetProcess == NULL)
  764. {
  765. printf("-----OpenProcess %d error..\n", dwPid);
  766. FreeLibrary(hExportDll);
  767. return -1;
  768. }
  769. //申请拦截函数内存空间
  770. LPVOID lpFunAddr = VirtualAllocEx(hTargetProcess, NULL, dwHookFunSize,
  771. MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
  772. if(lpFunAddr == NULL)
  773. {
  774. printf("-----VirtualAllocEx for remotefuction error..\n");
  775. FreeLibrary(hExportDll);
  776. CloseHandle(hTargetProcess);
  777. return -1;
  778. }
  779. //申请远程参数空间
  780. LPVOID lpParamaAddr = VirtualAllocEx(hTargetProcess, NULL, sizeof(RemoteParam),
  781. MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
  782. if(lpParamaAddr == NULL)
  783. {
  784. printf("-----VirtualAllocEx for remoteparam error..\n");
  785. FreeLibrary(hExportDll);
  786. VirtualFreeEx(hTargetProcess, lpFunAddr, 0, MEM_RELEASE);
  787. CloseHandle(hTargetProcess);
  788. return -1;
  789. }
  790. printf("2:Alloc remote memory for the hook fuction and param ok\n");
  791. //设置远程参数,下一步要把参数写入要拦截的远程进程地址空间
  792. RemoteParam RParam;
  793. ZeroMemory(&RParam, sizeof(RParam));
  794. wcscpy(RParam.wProcessName, lpProcessName);
  795. unsigned char oldcode[12];
  796. unsigned char newcode[12];
  797. DWORD dwError = 0;
  798. if(!ReadProcessMemory(CurrentProcessHandle,
  799. lpApiAddr,
  800. oldcode,
  801. 12,
  802. &dwError))
  803. {
  804. printf("-----ReadProcessMemory from lpApiName error..\n");
  805. FreeLibrary(hExportDll);
  806. VirtualFreeEx(hTargetProcess, lpFunAddr, 0, MEM_RELEASE);
  807. VirtualFreeEx(hTargetProcess, lpParamaAddr, 0, MEM_RELEASE);
  808. CloseHandle(hTargetProcess);
  809. return -1;
  810. }
  811. //生成跳转指令,这将覆盖要拦截的api的前十个字节
  812. int praadd = (int)lpParamaAddr;
  813. int threadadd = (int)lpFunAddr;
  814. newcode[4] = praadd>>24;
  815. newcode[3] = (praadd<<8)>>24;
  816. newcode[2] = (praadd<<16)>>24;
  817. newcode[1] = (praadd<<24)>>24;
  818. newcode[0] = 0x68; //PUSH lpParamaAddr
  819. int offsetaddr = threadadd - (int)lpApiAddr - 10 ;
  820. newcode[9] = offsetaddr>>24;
  821. newcode[8] = (offsetaddr<<8)>>24;
  822. newcode[7] = (offsetaddr<<16)>>24;
  823. newcode[6] = (offsetaddr<<24)>>24;
  824. newcode[5] = 0xE8; //CALL lpFunAddr
  825. newcode[10] = 0x90;
  826. newcode[11] = 0x90;
  827. for(int j = 0; j < 12; j++)
  828. {
  829. RParam.szOldCode[j] = oldcode[j];
  830. RParam.szNewCode[j] = newcode[j];
  831. }
  832. RParam.lpFunAddr = lpApiAddr;
  833. RParam.dwParamSize = dwParamNum * 4;
  834. HMODULE hKernel32 = LoadLibrary("Kernel32.dll");
  835. if(hKernel32 == NULL)
  836. {
  837. printf("-----LoadLibrary Kernel32.dll error..\n");
  838. FreeLibrary(hExportDll);
  839. VirtualFreeEx(hTargetProcess, lpFunAddr, 0, MEM_RELEASE);
  840. VirtualFreeEx(hTargetProcess, lpParamaAddr, 0, MEM_RELEASE);
  841. CloseHandle(hTargetProcess);
  842. return -1;
  843. }
  844. RParam.lpWriteProcessMemory = GetProcAddress(hKernel32, "WriteProcessMemory");
  845. if(RParam.lpWriteProcessMemory == NULL)
  846. {
  847. printf("-----GetProcAddress WriteProcessMemory from Kernel32.dll error..\n");
  848. FreeLibrary(hExportDll);
  849. VirtualFreeEx(hTargetProcess, lpFunAddr, 0, MEM_RELEASE);
  850. VirtualFreeEx(hTargetProcess, lpParamaAddr, 0, MEM_RELEASE);
  851. CloseHandle(hTargetProcess);
  852. FreeLibrary(hKernel32);
  853. return -1;
  854. }
  855. RParam.lpGetProcessHeap = GetProcAddress(hKernel32, "GetProcessHeap");
  856. if(RParam.lpGetProcessHeap == NULL)
  857. {
  858. printf("-----GetProcAddress GetProcessHeap from Kernel32.dll error..\n");
  859. FreeLibrary(hExportDll);
  860. VirtualFreeEx(hTargetProcess, lpFunAddr, 0, MEM_RELEASE);
  861. VirtualFreeEx(hTargetProcess, lpParamaAddr, 0, MEM_RELEASE);
  862. CloseHandle(hTargetProcess);
  863. FreeLibrary(hKernel32);
  864. return -1;
  865. }
  866. RParam.lpHeapAlloc = GetProcAddress(hKernel32, "HeapAlloc");
  867. if(RParam.lpHeapAlloc == NULL)
  868. {
  869. printf("-----GetProcAddress HeapAlloc from Kernel32.dll error..\n");
  870. FreeLibrary(hExportDll);
  871. VirtualFreeEx(hTargetProcess, lpFunAddr, 0, MEM_RELEASE);
  872. VirtualFreeEx(hTargetProcess, lpParamaAddr, 0, MEM_RELEASE);
  873. CloseHandle(hTargetProcess);
  874. FreeLibrary(hKernel32);
  875. return -1;
  876. }
  877. RParam.lpHeapReAlloc = GetProcAddress(hKernel32, "HeapReAlloc");
  878. if(RParam.lpHeapReAlloc == NULL)
  879. {
  880. printf("-----GetProcAddress HeapReAlloc from Kernel32.dll error..\n");
  881. FreeLibrary(hExportDll);
  882. VirtualFreeEx(hTargetProcess, lpFunAddr, 0, MEM_RELEASE);
  883. VirtualFreeEx(hTargetProcess, lpParamaAddr, 0, MEM_RELEASE);
  884. CloseHandle(hTargetProcess);
  885. FreeLibrary(hKernel32);
  886. return -1;
  887. }
  888. RParam.lpHeapFree = GetProcAddress(hKernel32, "HeapFree");
  889. if(RParam.lpHeapFree == NULL)
  890. {
  891. printf("-----GetProcAddress HeapFree from Kernel32.dll error..\n");
  892. FreeLibrary(hExportDll);
  893. VirtualFreeEx(hTargetProcess, lpFunAddr, 0, MEM_RELEASE);
  894. VirtualFreeEx(hTargetProcess, lpParamaAddr, 0, MEM_RELEASE);
  895. CloseHandle(hTargetProcess);
  896. FreeLibrary(hKernel32);
  897. return -1;
  898. }
  899. RParam.lpResumeThread = GetProcAddress(hKernel32, "ResumeThread");
  900. if(RParam.lpHeapFree == NULL)
  901. {
  902. printf("-----GetProcAddress ResumeThread from Kernel32.dll error..\n");
  903. FreeLibrary(hExportDll);
  904. VirtualFreeEx(hTargetProcess, lpFunAddr, 0, MEM_RELEASE);
  905. VirtualFreeEx(hTargetProcess, lpParamaAddr, 0, MEM_RELEASE);
  906. CloseHandle(hTargetProcess);
  907. FreeLibrary(hKernel32);
  908. return -1;
  909. }
  910. RParam.lpCreateEvent = GetProcAddress(hKernel32, "CreateEventA");
  911. if(RParam.lpCreateEvent == NULL)
  912. {
  913. printf("-----GetProcAddress CreateEventA from Kernel32.dll error..\n");
  914. FreeLibrary(hExportDll);
  915. VirtualFreeEx(hTargetProcess, lpFunAddr, 0, MEM_RELEASE);
  916. VirtualFreeEx(hTargetProcess, lpParamaAddr, 0, MEM_RELEASE);
  917. CloseHandle(hTargetProcess);
  918. FreeLibrary(hKernel32);
  919. return -1;
  920. }
  921. RParam.lpOpenEvent = GetProcAddress(hKernel32, "OpenEventA");
  922. if(RParam.lpOpenEvent == NULL)
  923. {
  924. printf("-----GetProcAddress OpenEventA from Kernel32.dll error..\n");
  925. FreeLibrary(hExportDll);
  926. VirtualFreeEx(hTargetProcess, lpFunAddr, 0, MEM_RELEASE);
  927. VirtualFreeEx(hTargetProcess, lpParamaAddr, 0, MEM_RELEASE);
  928. CloseHandle(hTargetProcess);
  929. FreeLibrary(hKernel32);
  930. return -1;
  931. }
  932. RParam.lpOpenFileMapping = GetProcAddress(hKernel32, "OpenFileMappingA");
  933. if(RParam.lpOpenFileMapping == NULL)
  934. {
  935. printf("-----GetProcAddress OpenFileMappingA from Kernel32.dll error..\n");
  936. FreeLibrary(hExportDll);
  937. VirtualFreeEx(hTargetProcess, lpFunAddr, 0, MEM_RELEASE);
  938. VirtualFreeEx(hTargetProcess, lpParamaAddr, 0, MEM_RELEASE);
  939. CloseHandle(hTargetProcess);
  940. FreeLibrary(hKernel32);
  941. return -1;
  942. }
  943. RParam.lpMapViewOfFile = GetProcAddress(hKernel32, "MapViewOfFile");
  944. if(RParam.lpMapViewOfFile == NULL)
  945. {
  946. printf("-----GetProcAddress MapViewOfFile from Kernel32.dll error..\n");
  947. FreeLibrary(hExportDll);
  948. VirtualFreeEx(hTargetProcess, lpFunAddr, 0, MEM_RELEASE);
  949. VirtualFreeEx(hTargetProcess, lpParamaAddr, 0, MEM_RELEASE);
  950. CloseHandle(hTargetProcess);
  951. FreeLibrary(hKernel32);
  952. return -1;
  953. }
  954. RParam.lpUnMapViewOfFile = GetProcAddress(hKernel32, "UnmapViewOfFile");
  955. if(RParam.lpUnMapViewOfFile == NULL)
  956. {
  957. printf("-----GetProcAddress UnMapViewOfFile from Kernel32.dll error..\n");
  958. FreeLibrary(hExportDll);
  959. VirtualFreeEx(hTargetProcess, lpFunAddr, 0, MEM_RELEASE);
  960. VirtualFreeEx(hTargetProcess, lpParamaAddr, 0, MEM_RELEASE);
  961. CloseHandle(hTargetProcess);
  962. FreeLibrary(hKernel32);
  963. return -1;
  964. }
  965. RParam.lpOpenMutex = GetProcAddress(hKernel32, "OpenMutexA");
  966. if(RParam.lpOpenMutex == NULL)
  967. {
  968. printf("-----GetProcAddress OpenMutexA from Kernel32.dll error..\n");
  969. FreeLibrary(hExportDll);
  970. VirtualFreeEx(hTargetProcess, lpFunAddr, 0, MEM_RELEASE);
  971. VirtualFreeEx(hTargetProcess, lpParamaAddr, 0, MEM_RELEASE);
  972. CloseHandle(hTargetProcess);
  973. FreeLibrary(hKernel32);
  974. return -1;
  975. }
  976. RParam.lpWaitForSingleObject = GetProcAddress(hKernel32, "WaitForSingleObject");
  977. if(RParam.lpWaitForSingleObject == NULL)
  978. {
  979. printf("-----GetProcAddress WaitForSingleObject from Kernel32.dll error..\n");
  980. FreeLibrary(hExportDll);
  981. VirtualFreeEx(hTargetProcess, lpFunAddr, 0, MEM_RELEASE);
  982. VirtualFreeEx(hTargetProcess, lpParamaAddr, 0, MEM_RELEASE);
  983. CloseHandle(hTargetProcess);
  984. FreeLibrary(hKernel32);
  985. return -1;
  986. }
  987. RParam.lpSetEvent = GetProcAddress(hKernel32, "SetEvent");
  988. if(RParam.lpSetEvent == NULL)
  989. {
  990. printf("-----GetProcAddress SetEvent from Kernel32.dll error..\n");
  991. FreeLibrary(hExportDll);
  992. VirtualFreeEx(hTargetProcess, lpFunAddr, 0, MEM_RELEASE);
  993. VirtualFreeEx(hTargetProcess, lpParamaAddr, 0, MEM_RELEASE);
  994. CloseHandle(hTargetProcess);
  995. FreeLibrary(hKernel32);
  996. return -1;
  997. }
  998. RParam.lpReleaseMutex = GetProcAddress(hKernel32, "ReleaseMutex");
  999. if(RParam.lpReleaseMutex == NULL)
  1000. {
  1001. printf("-----GetProcAddress ReleaseMutex from Kernel32.dll error..\n");
  1002. FreeLibrary(hExportDll);
  1003. VirtualFreeEx(hTargetProcess, lpFunAddr, 0, MEM_RELEASE);
  1004. VirtualFreeEx(hTargetProcess, lpParamaAddr, 0, MEM_RELEASE);
  1005. CloseHandle(hTargetProcess);
  1006. FreeLibrary(hKernel32);
  1007. return -1;
  1008. }
  1009. RParam.lpCloseHandle = GetProcAddress(hKernel32, "CloseHandle");
  1010. if(RParam.lpCloseHandle == NULL)
  1011. {
  1012. printf("-----GetProcAddress CloseHandle from Kernel32.dll error..\n");
  1013. FreeLibrary(hExportDll);
  1014. VirtualFreeEx(hTargetProcess, lpFunAddr, 0, MEM_RELEASE);
  1015. VirtualFreeEx(hTargetProcess, lpParamaAddr, 0, MEM_RELEASE);
  1016. CloseHandle(hTargetProcess);
  1017. FreeLibrary(hKernel32);
  1018. return -1;
  1019. }
  1020. RParam.lpGetProcessId = GetProcAddress(hKernel32, "GetProcessId");
  1021. if(RParam.lpGetProcessId == NULL)
  1022. {
  1023. printf("-----GetProcAddress GetProcessId from Kernel32.dll error..\n");
  1024. FreeLibrary(hExportDll);
  1025. VirtualFreeEx(hTargetProcess, lpFunAddr, 0, MEM_RELEASE);
  1026. VirtualFreeEx(hTargetProcess, lpParamaAddr, 0, MEM_RELEASE);
  1027. CloseHandle(hTargetProcess);
  1028. FreeLibrary(hKernel32);
  1029. return -1;
  1030. }
  1031. RParam.lpGetLastError = GetProcAddress(hKernel32, "GetLastError");
  1032. if(RParam.lpGetLastError == NULL)
  1033. {
  1034. printf("-----GetProcAddress GetLastError from Kernel32.dll error..\n");
  1035. FreeLibrary(hExportDll);
  1036. VirtualFreeEx(hTargetProcess, lpFunAddr, 0, MEM_RELEASE);
  1037. VirtualFreeEx(hTargetProcess, lpParamaAddr, 0, MEM_RELEASE);
  1038. CloseHandle(hTargetProcess);
  1039. FreeLibrary(hKernel32);
  1040. return -1;
  1041. }
  1042. FreeLibrary(hKernel32);
  1043. //以上皆为必须,下面可以为自己的拦截函数中所需要用的一些变量以及系统api放到参数中去
  1044. HMODULE hUser32 = LoadLibrary("User32.dll");
  1045. if(hUser32 == NULL)
  1046. {
  1047. printf("-----LoadLibrary User32.dll error..\n");
  1048. FreeLibrary(hExportDll);
  1049. VirtualFreeEx(hTargetProcess, lpFunAddr, 0, MEM_RELEASE);
  1050. VirtualFreeEx(hTargetProcess, lpParamaAddr, 0, MEM_RELEASE);
  1051. CloseHandle(hTargetProcess);
  1052. return -1;
  1053. }
  1054. RParam.lpMessageBox = GetProcAddress(hUser32, "MessageBoxW");
  1055. if(RParam.lpMessageBox == NULL)
  1056. {
  1057. printf("-----GetProcAddress MessageBoxA from User32.dll error..\n");
  1058. FreeLibrary(hExportDll);
  1059. VirtualFreeEx(hTargetProcess, lpFunAddr, 0, MEM_RELEASE);
  1060. VirtualFreeEx(hTargetProcess, lpParamaAddr, 0, MEM_RELEASE);
  1061. CloseHandle(hTargetProcess);
  1062. FreeLibrary(hUser32);
  1063. return -1;
  1064. }
  1065. RParam.lpwsprintf = GetProcAddress(hUser32, "wsprintfW");
  1066. if(RParam.lpwsprintf == NULL)
  1067. {
  1068. printf("-----GetProcAddress wsprintf from User32.dll error..\n");
  1069. FreeLibrary(hExportDll);
  1070. VirtualFreeEx(hTargetProcess, lpFunAddr, 0, MEM_RELEASE);
  1071. VirtualFreeEx(hTargetProcess, lpParamaAddr, 0, MEM_RELEASE);
  1072. CloseHandle(hTargetProcess);
  1073. FreeLibrary(hUser32);
  1074. return -1;
  1075. }
  1076. FreeLibrary(hUser32);
  1077. HMODULE hNtdll = LoadLibrary("ntdll.dll");
  1078. if(hNtdll == NULL)
  1079. {
  1080. printf("-----LoadLibrary ntdll.dll error..\n");
  1081. FreeLibrary(hExportDll);
  1082. VirtualFreeEx(hTargetProcess, lpFunAddr, 0, MEM_RELEASE);
  1083. VirtualFreeEx(hTargetProcess, lpParamaAddr, 0, MEM_RELEASE);
  1084. CloseHandle(hTargetProcess);
  1085. return -1;
  1086. }
  1087. RParam.lpZwQueryObject = GetProcAddress(hNtdll, "ZwQueryObject");
  1088. if(RParam.lpwsprintf == NULL)
  1089. {
  1090. printf("-----GetProcAddress ZwQueryObject from ntdll.dll error..\n");
  1091. FreeLibrary(hExportDll);
  1092. VirtualFreeEx(hTargetProcess, lpFunAddr, 0, MEM_RELEASE);
  1093. VirtualFreeEx(hTargetProcess, lpParamaAddr, 0, MEM_RELEASE);
  1094. CloseHandle(hTargetProcess);
  1095. FreeLibrary(hNtdll);
  1096. return -1;
  1097. }
  1098. FreeLibrary(hNtdll);
  1099. printf("3:Generate remoteparam ok\n");
  1100. //下面为必须部分
  1101. //把参数写入要拦截的远程进程地址空间
  1102. if(!WriteProcessMemory(hTargetProcess, lpParamaAddr, (LPVOID)&RParam, sizeof(RParam), &dwError))
  1103. {
  1104. printf("-----WriteProcessMemory for remoteparam error..\n");
  1105. FreeLibrary(hExportDll);
  1106. VirtualFreeEx(hTargetProcess, lpFunAddr, 0, MEM_RELEASE);
  1107. VirtualFreeEx(hTargetProcess, lpParamaAddr, 0, MEM_RELEASE);
  1108. CloseHandle(hTargetProcess);
  1109. return -1;
  1110. }
  1111. printf("4:WriteProcessMemory for remoteparam ok\n");
  1112. //把拦截函数体写入目标进程地址空间
  1113. if(!WriteProcessMemory(hTargetProcess, lpFunAddr, lpHookFunAddr, dwHookFunSize, &dwError))
  1114. {
  1115. printf("-----WriteProcessMemory for remotefuction error..\n");
  1116. FreeLibrary(hExportDll);
  1117. VirtualFreeEx(hTargetProcess, lpFunAddr, 0, MEM_RELEASE);
  1118. VirtualFreeEx(hTargetProcess, lpParamaAddr, 0, MEM_RELEASE);
  1119. CloseHandle(hTargetProcess);
  1120. return -1;
  1121. }
  1122. printf("5:WriteProcessMemory for remotefuction ok\n");
  1123. //把新的跳转指令写入要拦截的目标进程的要拦截的api的函数的前十字节
  1124. if(!WriteProcessMemory(hTargetProcess, lpApiAddr , (LPVOID)newcode, 12, &dwError))
  1125. {
  1126. printf("-----Modify remote api error..\n");
  1127. FreeLibrary(hExportDll);
  1128. VirtualFreeEx(hTargetProcess, lpFunAddr, 0, MEM_RELEASE);
  1129. VirtualFreeEx(hTargetProcess, lpParamaAddr, 0, MEM_RELEASE);
  1130. CloseHandle(hTargetProcess);
  1131. return -1;
  1132. }
  1133. printf("6:Modify remote api %s ok\n", lpApiName);
  1134. //show..
  1135. printf("---------------------------------------------------------------------------\n"
  1136. "You hava hooked pid: %d ,and the infomation of this hooking are as follows:\n"
  1137. "API name: %s\n"
  1138. "API addr: 0x%.8x\n"
  1139. "API Parm: %d\n"
  1140. "Module name: %s\n"
  1141. "Module addr: 0x%.8x\n"
  1142. "Remote parama addr: 0x%.8x\n"
  1143. "Remote thread addr: 0x%.8x\n"
  1144. "RemoteParam.lpFunAddr: 0x%.8x\n"
  1145. "RemoteParam.lpWriteProcessMemory: 0x%.8x\n"
  1146. "RemoteParam.lpMessageBox: 0x%.8x\n"
  1147. "RemoteParam.lpGetProcessHeap: 0x%.8x\n"
  1148. "RemoteParam.lpHeapAlloc: 0x%.8x\n"
  1149. "RemoteParam.lpHeapReAlloc: 0x%.8x\n"
  1150. "RemoteParam.lpHeapFree: 0x%.8x\n"
  1151. "RemoteParam.lpwsprintf: 0x%.8x\n"
  1152. "RemoteParam.lpZwQueryObject: 0x%.8x\n"
  1153. "RemoteParam.lpResumeThread: 0x%.8x\n"
  1154. "RemoteParam.lpCreateEvent: 0x%.8x\n"
  1155. "RemoteParam.lpOpenEvent: 0x%.8x\n"
  1156. "RemoteParam.lpOpenFileMapping: 0x%.8x\n"
  1157. "RemoteParam.lpMapViewOf文件: 0x%.8x\n"
  1158. "RemoteParam.lpUnMapViewOf文件: 0x%.8x\n"
  1159. "RemoteParam.lpOpenMutex: 0x%.8x\n"
  1160. "RemoteParam.lpWaitForSingleObject: 0x%.8x\n"
  1161. "RemoteParam.lpSetEvent: 0x%.8x\n"
  1162. "RemoteParam.lpReleaseMutex: 0x%.8x\n"
  1163. "RemoteParam.lpCloseHandle: 0x%.8x\n"
  1164. "RemoteParam.lpGetProcessId: 0x%.8x\n"
  1165. "RemoteParam.lpGetLastError: 0x%.8x\n",
  1166. dwPid,
  1167. lpApiName,
  1168. lpApiAddr,
  1169. RParam.dwParamSize,
  1170. lpExportDllName,
  1171. hExportDll,
  1172. lpParamaAddr,
  1173. lpFunAddr,
  1174. RParam.lpFunAddr,
  1175. RParam.lpWriteProcessMemory,
  1176. RParam.lpMessageBox,
  1177. RParam.lpGetProcessHeap,
  1178. RParam.lpHeapAlloc,
  1179. RParam.lpHeapReAlloc,
  1180. RParam.lpHeapFree,
  1181. RParam.lpwsprintf,
  1182. RParam.lpZwQueryObject,
  1183. RParam.lpResumeThread,
  1184. RParam.lpCreateEvent,
  1185. RParam.lpOpenEvent,
  1186. RParam.lpOpenFileMapping,
  1187. RParam.lpMapViewOfFile,
  1188. RParam.lpUnMapViewOfFile,
  1189. RParam.lpOpenMutex,
  1190. RParam.lpWaitForSingleObject,
  1191. RParam.lpSetEvent,
  1192. RParam.lpReleaseMutex,
  1193. RParam.lpCloseHandle,
  1194. RParam.lpGetProcessId,
  1195. RParam.lpGetLastError);
  1196. wprintf(L"RemoteParam.wProcessName: %s\n", RParam.wProcessName);
  1197. printf("OldCode:");
  1198. for(int i = 0; i < 12; i++)
  1199. {
  1200. printf("0x%.2x ", RParam.szOldCode[i]);
  1201. }
  1202. printf("\nNewCode:");
  1203. for(i = 0; i < 12; i++)
  1204. {
  1205. printf("0x%.2x ", RParam.szNewCode[i]);
  1206. }
  1207. printf("\n");
  1208. printf("---------------------------------------------------------------------------\n\n\n");
  1209. //收工,清理资源
  1210. FreeLibrary(hExportDll);
  1211. CloseHandle(hTargetProcess);
  1212. return 0;
  1213. }
  1214. DWORD dwRunning = 1;
  1215. DWORD WINAPI getExit(PHANDLE hExitEvent)
  1216. {
  1217. char exitCode = ';\0';;
  1218. DWORD dwCount = 0;
  1219. while(TRUE)
  1220. {
  1221. printf("Input q to exit the program...\n");
  1222. scanf("%c", &exitCode);
  1223. if(exitCode == ';q';)
  1224. {
  1225. dwRunning = 0;
  1226. SetEvent(*hExitEvent);
  1227. break;
  1228. }
  1229. }
  1230. return 0;
  1231. }
  1232. int main(void)
  1233. {
  1234. HANDLE hHookMainAliveEvent = CreateEvent(NULL, TRUE, TRUE, "HookMainAliveEvent");
  1235. if(hHookMainAliveEvent == NULL)
  1236. {
  1237. printf("CreateEvent hHookMainAliveEvent error..\n");
  1238. return -1;
  1239. }
  1240. HANDLE hMapFileOperateMutex = CreateMutex(NULL, FALSE, "MapFileOperateMutex");
  1241. if(hMapFileOperateMutex == NULL)
  1242. {
  1243. printf("CreateMutex hMapFileOperateMutex error..\n");
  1244. CloseHandle(hHookMainAliveEvent);
  1245. return -1;
  1246. }
  1247. HANDLE hMapFileModifiedEvent = CreateEvent(NULL, FALSE, FALSE, "MapFileModifiedEvent");
  1248. if(hMapFileModifiedEvent == NULL)
  1249. {
  1250. printf("CreateEvent hMapFileModifiedEvent error..\n");
  1251. CloseHandle(hHookMainAliveEvent);
  1252. CloseHandle(hMapFileOperateMutex);
  1253. return -1;
  1254. }
  1255. HANDLE hHookNewProcessDoneEvent = CreateEvent(NULL, FALSE, FALSE, "HookNewProcessDoneEvent");
  1256. if(hHookNewProcessDoneEvent == NULL)
  1257. {
  1258. printf("CreateEvent hHookNewProcessDoneEvent error..\n");
  1259. CloseHandle(hHookMainAliveEvent);
  1260. CloseHandle(hMapFileOperateMutex);
  1261. CloseHandle(hMapFileModifiedEvent);
  1262. return -1;
  1263. }
  1264. HANDLE hMapFileToNewProcessId = CreateFileMapping(
  1265. (HANDLE)0xFFFFFFFF,
  1266. NULL,
  1267. PAGE_READWRITE,
  1268. 0,
  1269. 4,
  1270. "MapFileToNewProcessId");
  1271. if(hMapFileToNewProcessId == NULL)
  1272. {
  1273. printf("CreateFileMapping NewCreatedProcessId error..\n");
  1274. CloseHandle(hHookMainAliveEvent);
  1275. CloseHandle(hMapFileOperateMutex);
  1276. CloseHandle(hMapFileModifiedEvent);
  1277. CloseHandle(hHookNewProcessDoneEvent);
  1278. return -1;
  1279. }
  1280. LPVOID lpMapFileToNewProcessId = MapViewOfFile(hMapFileToNewProcessId, FILE_MAP_READ, 0, 0, 0);
  1281. if(lpMapFileToNewProcessId == NULL)
  1282. {
  1283. printf("MapViewOfFile NewCreatedProcessId error..\n");
  1284. CloseHandle(hMapFileToNewProcessId);
  1285. CloseHandle(hHookMainAliveEvent);
  1286. CloseHandle(hMapFileOperateMutex);
  1287. CloseHandle(hMapFileModifiedEvent);
  1288. CloseHandle(hHookNewProcessDoneEvent);
  1289. return -1;
  1290. }
  1291. DWORD * Ppid = (PDWORD)lpMapFileToNewProcessId;
  1292. //在这里填入想要监控的进程id,稍作修改,可以监控所有进程,程序没有作好,请原谅.
  1293. //这里还是觉得就这样,大家如果有兴趣可以在自己的机器上测试,因为做到现在,忽然发现似乎功能的
  1294. //真正实现不是那么重要了。
  1295. SetHook(2560, L"explorer.exe\0", "ZwCreateProcessEx", "ntdll.dll", 9, 4096 * 10, &HookZwCreateProcessEx);
  1296. DWORD dwExitThread = 0;
  1297. HANDLE hExitThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&getExit, &hMapFileModifiedEvent, 0, &dwExitThread);
  1298. while(dwRunning == 1)
  1299. {
  1300. WaitForSingleObject(hMapFileModifiedEvent, INFINITE);
  1301. if(dwRunning == 0)
  1302. break;
  1303. DWORD dwPid = * Ppid;
  1304. //这里的传递的进程名字只是用来测试用,可以编程实现获取进程名,并传递,注意这里为宽字符,
  1305. //抱歉本人没有完成这里的自动获取进程名字功能,写这个代码已经太烦躁了,请原谅.
  1306. SetHook(dwPid, L"explorer.exe\0", "ZwCreateProcessEx", "ntdll.dll", 9, 4096 * 10, &HookZwCreateProcessEx);
  1307. SetHook(dwPid, L"regedit.exe\0", "ZwSetValueKey", "ntdll.dll", 6, 4096 * 5, &HookZwSetValueKey);
  1308. SetEvent(hHookNewProcessDoneEvent);
  1309. }
  1310. UnmapViewOfFile(lpMapFileToNewProcessId);
  1311. CloseHandle(hMapFileToNewProcessId);
  1312. CloseHandle(hHookMainAliveEvent);
  1313. CloseHandle(hMapFileOperateMutex);
  1314. CloseHandle(hMapFileModifiedEvent);
  1315. CloseHandle(hHookNewProcessDoneEvent);
  1316. CloseHandle(hExitThread);
  1317. return 0;
  1318. }
复制代码

[原创]winxp下简单实现注册表修改、进程创建监控

支持原创!

TOP

[原创]winxp下简单实现注册表修改、进程创建监控

如果能做到内核层效果会更好,不是吗?

TOP

[原创]winxp下简单实现注册表修改、进程创建监控

能力有限,眼看着但是不能入门,如果能修改SSDT那是更好,当然也许有更好的方法.
ring 3 太耗资源了.而且不彻底.
楼上可否留下联系方式,或者加群:16189699,期待您的指点啊

TOP

返回列表 回复 发帖