Main.cpp
上传用户:nbcables
上传日期:2007-01-11
资源大小:1243k
文件大小:4k
源码类别:

钩子与API截获

开发平台:

Visual C++

  1. #include <windows.h>
  2. #include "hookapi.h"
  3. #include "ProcessModule.h"
  4. #include "util.h"
  5. #ifdef WINNT
  6. #include "injlib.h"
  7. #endif
  8. CHookAPI g_hook_api;
  9. extern char g_szDllPath[128];
  10. #ifdef WINNT
  11. BOOL ObtainSeDebugPrivilege();
  12. #endif
  13. #ifdef WINNT
  14. BOOL IfCanHook(DWORD process_id)
  15. {
  16. DWORD cur_pid =GetCurrentProcessId();
  17. if(process_id ==0 || process_id ==cur_pid) return false;
  18. CProcessModule pm;
  19. if(pm.GetProcessModuleHandle(process_id, "smss.exe")
  20. //|| pm.GetProcessModuleHandle(process_id, "csrss.exe")
  21. //|| pm.GetProcessModuleHandle(process_id, "lsass.exe")
  22. )
  23. return false;
  24. return true;
  25. }
  26. #endif
  27. int WINAPI HookOneProcess(DWORD process_id)
  28. {
  29. #ifdef WINNT
  30. char fname[128];
  31. ObtainSeDebugPrivilege();
  32. wsprintf(fname, "%s\HookAPINT.dll", g_szDllPath);
  33. InjectLib(process_id, fname);
  34. #endif
  35. return 0;
  36. }
  37. int WINAPI UnhookOneProcess(DWORD process_id)
  38. {
  39. #ifdef WINNT
  40. ObtainSeDebugPrivilege();
  41. EjectLib(process_id, "HookAPINT.dll");
  42. #endif
  43. return 0;
  44. }
  45. char g_exe_to_hook[256];
  46. int g_f_stop =0;
  47. DWORD g_pid =0;
  48. DWORD WINAPI hook_thread(void *lpvoid)
  49. {
  50. HWND hwndNotify =(HWND)lpvoid;
  51. WriteLog("hook exe:%s", g_exe_to_hook);
  52. #ifdef WINNT
  53. ObtainSeDebugPrivilege();
  54. #endif
  55. CProcessModule pm;
  56. while(!g_f_stop && (g_pid=pm.GetProcessID(g_exe_to_hook)) ==0)
  57. {
  58. Sleep(1000);
  59. }
  60. WriteLog("hook....");
  61. if(!g_f_stop)
  62. {
  63. char fname[128];
  64. wsprintf(fname, "%s\HookAPINT.dll", g_szDllPath);
  65. if(hwndNotify) SendMessage(hwndNotify, WM_APP+2004, g_pid, 0L);
  66. InjectLib(g_pid, fname);
  67. }
  68. return 0;
  69. }
  70. int WINAPI HookOneProcess2(HWND hwndNotify, char *exe_name)
  71. {
  72. #ifdef WINNT
  73. DWORD dw;
  74. strcpy(g_exe_to_hook, exe_name);
  75. HANDLE hThread =CreateThread(NULL, 0, hook_thread, hwndNotify, 0, &dw);
  76. if(hThread ==NULL)
  77. return -1;
  78. #endif
  79. return 0;
  80. }
  81. int WINAPI UnhookOneProcess2(char *exe_name)
  82. {
  83. g_f_stop =1;
  84. #ifdef WINNT
  85. if(g_pid)
  86. EjectLib(g_pid, "HookAPINT.dll");
  87. #endif
  88. return 0;
  89. }
  90. int WINAPI HookAllProcess()
  91. {
  92. #ifdef WINNT
  93. char fname[128];
  94. wsprintf(fname, "%s\HookAPINT.dll", g_szDllPath);
  95. ObtainSeDebugPrivilege();
  96. CProcessModule pm;
  97. pm.EnumProcess();
  98. for(int i =0; i<(int)pm.m_dwProcessCount; i++)
  99. {
  100. if(IfCanHook(pm.m_dwProcessIDs[i]))
  101. InjectLib(pm.m_dwProcessIDs[i], fname);
  102. }
  103. #endif
  104. return 0;
  105. }
  106. int WINAPI UnhookAllProcess()
  107. {
  108. g_f_stop =1;
  109. #ifdef WINNT
  110. CProcessModule pm;
  111. pm.EnumProcess();
  112. for(int i =0; i<(int)pm.m_dwProcessCount; i++)
  113. {
  114. if(IfCanHook(pm.m_dwProcessIDs[i]))
  115. EjectLib(pm.m_dwProcessIDs[i], "HookAPINT.dll");
  116. }
  117. #endif
  118. return 0;
  119. }
  120. #ifdef WINNT
  121. BOOL ObtainSeDebugPrivilege()
  122. {
  123. TOKEN_PRIVILEGES TokenPrivileges;
  124. TOKEN_PRIVILEGES PreviousTokenPrivileges;
  125. LUID luid;
  126. HANDLE hToken;
  127. DWORD dwPreviousTokenPrivilegesSize = sizeof(TOKEN_PRIVILEGES);
  128. if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  129. {
  130. WriteLog("debug 001");
  131. return false;
  132. }
  133. if(!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid))
  134. {
  135. WriteLog("debug 002");
  136. return false;
  137. }
  138.   
  139. TokenPrivileges.PrivilegeCount            = 1;
  140. TokenPrivileges.Privileges[0].Luid        = luid;
  141. TokenPrivileges.Privileges[0].Attributes  = 0;
  142. if(!AdjustTokenPrivileges(hToken, FALSE, &TokenPrivileges, sizeof(TOKEN_PRIVILEGES),
  143. &PreviousTokenPrivileges, &dwPreviousTokenPrivilegesSize))
  144. {
  145. WriteLog("debug 003");
  146. return false;
  147. }
  148. PreviousTokenPrivileges.PrivilegeCount             = 1;
  149. PreviousTokenPrivileges.Privileges[0].Luid         = luid;
  150. PreviousTokenPrivileges.Privileges[0].Attributes  |= SE_PRIVILEGE_ENABLED;
  151. if(!AdjustTokenPrivileges(hToken, FALSE, &PreviousTokenPrivileges,
  152. dwPreviousTokenPrivilegesSize, NULL, NULL))
  153. {
  154. WriteLog("debug 004");
  155. return false;
  156. }
  157. //WriteLog("debug ok");
  158. return true;
  159. }
  160. #endif
  161. BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD dwReason, PVOID pvReserved)
  162. {
  163. switch (dwReason)
  164. {
  165. case DLL_PROCESS_ATTACH:
  166. break;
  167. case DLL_PROCESS_DETACH:
  168. break;
  169. }
  170. return TRUE;
  171. }