Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

314 lines
8.5 KiB

  1. #include <windows.h>
  2. #include <stdlib.h>
  3. #include <psapi.h>
  4. #include <tlhelp32.h>
  5. #include <imagehlp.h>
  6. #include <stdio.h>
  7. #include "shimdb.h"
  8. #include "shim2.h"
  9. #include "hooks.h"
  10. #include "dump.h"
  11. #include "view.h"
  12. #include "filter.h"
  13. #include "except.h"
  14. #include "profiler.h"
  15. //
  16. // API hook externs
  17. //
  18. extern BOOL g_bIsWin9X;
  19. extern HANDLE g_hSnapshot;
  20. extern HANDLE g_hValidationSnapshot;
  21. extern LONG g_nShimDllCount;
  22. extern LONG g_nHookedModuleCount;
  23. extern HMODULE g_hHookedModules[MAX_MODULES];
  24. extern HMODULE g_hShimDlls[MAX_MODULES];
  25. extern PHOOKAPI g_rgpHookAPIs[MAX_MODULES];
  26. extern LONG g_rgnHookAPICount[MAX_MODULES];
  27. //
  28. // Global array of hooked functions
  29. //
  30. HOOKAPI g_rgBaseHookAPIs[SHIM_BASE_APIHOOK_COUNT];
  31. PVOID StubGetProcAddress(
  32. HMODULE hMod,
  33. char* pszProc)
  34. {
  35. char szModName[MAX_PATH];
  36. char* pszShortName;
  37. UINT ind;
  38. DWORD dwSize;
  39. PVOID pfn;
  40. LONG i,j;
  41. PHOOKAPI pTopHookAPI = NULL;
  42. PFNGETPROCADDRESS pfnOld;
  43. pfnOld = g_rgBaseHookAPIs[ hookGetProcAddress ].pfnOld;
  44. if( pfn = (*pfnOld)(hMod, pszProc) )
  45. {
  46. for (i = 0; i < g_nShimDllCount; i++)
  47. {
  48. for (j = 0; j < g_rgnHookAPICount[i]; j++)
  49. {
  50. if( g_rgpHookAPIs[i][j].pfnOld == pfn)
  51. {
  52. pTopHookAPI = ConstructChain( pfn, &dwSize );
  53. //maybe use the include exclude function here as well
  54. return pTopHookAPI->pfnNew;
  55. }
  56. }
  57. }
  58. }
  59. return pfn;
  60. } // StubGetProcAddress
  61. HMODULE StubLoadLibraryA(
  62. LPCSTR pszModule)
  63. {
  64. HMODULE hMod;
  65. PFNLOADLIBRARYA pfnOld;
  66. PIMAGE_NT_HEADERS pHeaders;
  67. pfnOld = g_rgBaseHookAPIs[ hookLoadLibraryA ].pfnOld;
  68. hMod = (*pfnOld)(pszModule);
  69. if (hMod != NULL) {
  70. RefreshFilterList();
  71. Shim2PatchNewModules();
  72. //
  73. // Rescan DLLs and add updated base information
  74. //
  75. WriteImportDLLTableInfo();
  76. }
  77. return hMod;
  78. } // StubLoadLibraryA
  79. HMODULE StubLoadLibraryW(
  80. WCHAR* pwszModule)
  81. {
  82. HMODULE hMod;
  83. CHAR szModuleName[MAX_PATH];
  84. PIMAGE_NT_HEADERS pHeaders;
  85. INT nResult;
  86. PFNLOADLIBRARYW pfnOld;
  87. pfnOld = g_rgBaseHookAPIs[ hookLoadLibraryW ].pfnOld;
  88. hMod = (*pfnOld)(pwszModule);
  89. if (hMod != NULL) {
  90. RefreshFilterList();
  91. Shim2PatchNewModules();
  92. //
  93. // Rescan DLLs and add updated base information
  94. //
  95. WriteImportDLLTableInfo();
  96. }
  97. return hMod;
  98. } // StubLoadLibraryW
  99. HMODULE StubLoadLibraryExA(
  100. LPCSTR pszModule,
  101. HANDLE hFile,
  102. DWORD dwFlags)
  103. {
  104. HMODULE hMod;
  105. PFNLOADLIBRARYEXA pfnOld;
  106. PIMAGE_NT_HEADERS pHeaders;
  107. pfnOld = g_rgBaseHookAPIs[ hookLoadLibraryExA ].pfnOld;
  108. hMod = (*pfnOld)(pszModule, hFile, dwFlags);
  109. if (hMod != NULL) {
  110. RefreshFilterList();
  111. Shim2PatchNewModules();
  112. //
  113. // Rescan DLLs and add updated base information
  114. //
  115. WriteImportDLLTableInfo();
  116. }
  117. return hMod;
  118. } // StubLoadLibraryExA
  119. HMODULE StubLoadLibraryExW(
  120. WCHAR* pwszModule,
  121. HANDLE hFile,
  122. DWORD dwFlags)
  123. {
  124. HMODULE hMod;
  125. PIMAGE_NT_HEADERS pHeaders;
  126. INT nResult;
  127. PFNLOADLIBRARYEXW pfnOld;
  128. pfnOld = g_rgBaseHookAPIs[ hookLoadLibraryExW ].pfnOld;
  129. hMod = (*pfnOld)(pwszModule, hFile, dwFlags);
  130. if (hMod != NULL) {
  131. RefreshFilterList();
  132. Shim2PatchNewModules();
  133. //
  134. // Rescan DLLs and add updated base information
  135. //
  136. WriteImportDLLTableInfo();
  137. }
  138. return hMod;
  139. } // StubLoadLibraryExW
  140. BOOL StubFreeLibrary(
  141. HMODULE hLibModule // handle to loaded library module
  142. )
  143. {
  144. BOOL bRet, bFound;
  145. PFNFREELIBRARY pfnOld;
  146. MODULEENTRY32 ModuleEntry32;
  147. long i, j;
  148. pfnOld = (PFNFREELIBRARY) g_rgBaseHookAPIs[ hookFreeLibrary ].pfnOld;
  149. bRet = (*pfnOld)(hLibModule);
  150. g_hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, 0 );
  151. ModuleEntry32.dwSize = sizeof( ModuleEntry32 );
  152. for( i = 0; i < g_nHookedModuleCount; i++ )
  153. {
  154. bFound = FALSE;
  155. bRet = Module32First( g_hSnapshot, &ModuleEntry32 );
  156. while( bRet )
  157. {
  158. if( g_hHookedModules[i] == ModuleEntry32.hModule )
  159. {
  160. bFound = TRUE;
  161. break;
  162. }
  163. bRet = Module32Next( g_hSnapshot, &ModuleEntry32 );
  164. }
  165. if( ! bFound )
  166. {
  167. // Take out of list
  168. for( j = i; j < g_nHookedModuleCount - 1; j++ )
  169. g_hHookedModules[j] = g_hHookedModules[j+1];
  170. g_hHookedModules[j] = NULL;
  171. g_nHookedModuleCount--;
  172. }
  173. }
  174. if( g_hSnapshot )
  175. {
  176. CloseHandle( g_hSnapshot );
  177. g_hSnapshot = NULL;
  178. }
  179. return bRet;
  180. }
  181. VOID StubExitProcess(UINT uExitCode)
  182. {
  183. PFNEXITPROCESS pfnOld;
  184. //
  185. // Process is terminating - flush ourselves
  186. //
  187. FlushForTermination();
  188. pfnOld = g_rgBaseHookAPIs[ hookExitProcess ].pfnOld;
  189. (*pfnOld)(uExitCode);
  190. } // StubExitProcess
  191. HANDLE StubCreateThread(
  192. LPSECURITY_ATTRIBUTES lpThreadAttributes,
  193. DWORD dwStackSize,
  194. LPTHREAD_START_ROUTINE lpStartAddress,
  195. LPVOID lpParameter,
  196. DWORD dwCreationFlags,
  197. LPDWORD lpThreadId)
  198. {
  199. PFNCREATETHREAD pfnOld;
  200. PVIEWCHAIN pvTemp;
  201. pfnOld = g_rgBaseHookAPIs[ hookCreateThread ].pfnOld;
  202. //
  203. // Add a mapping breakpoint for the thread entrypoint
  204. //
  205. pvTemp = AddViewToMonitor((DWORD)lpStartAddress,
  206. ThreadStart);
  207. return (*pfnOld)(lpThreadAttributes,
  208. dwStackSize,
  209. lpStartAddress,
  210. lpParameter,
  211. dwCreationFlags,
  212. lpThreadId);
  213. } // StubCreateThread
  214. VOID
  215. InitializeBaseHooks(HINSTANCE hInstance)
  216. {
  217. g_hSnapshot = NULL;
  218. g_hValidationSnapshot = NULL;
  219. g_nShimDllCount = 0;
  220. g_nHookedModuleCount = 0;
  221. ZeroMemory( g_hHookedModules, sizeof( g_hHookedModules ) );
  222. ZeroMemory( g_hShimDlls, sizeof( g_hShimDlls ) );
  223. ZeroMemory( g_rgpHookAPIs, sizeof( g_rgpHookAPIs ) );
  224. ZeroMemory( g_rgnHookAPICount, sizeof( g_rgnHookAPICount ) );
  225. ZeroMemory( g_rgBaseHookAPIs, sizeof( g_rgBaseHookAPIs ) );
  226. g_rgBaseHookAPIs[ hookGetProcAddress ].pszModule = "kernel32.dll";
  227. g_rgBaseHookAPIs[ hookGetProcAddress ].pszFunctionName = "GetProcAddress";
  228. g_rgBaseHookAPIs[ hookGetProcAddress ].pfnNew = (PVOID)StubGetProcAddress;
  229. g_rgBaseHookAPIs[ hookLoadLibraryA ].pszModule = "kernel32.dll";
  230. g_rgBaseHookAPIs[ hookLoadLibraryA ].pszFunctionName = "LoadLibraryA";
  231. g_rgBaseHookAPIs[ hookLoadLibraryA ].pfnNew = (PVOID)StubLoadLibraryA;
  232. g_rgBaseHookAPIs[ hookLoadLibraryW ].pszModule = "kernel32.dll";
  233. g_rgBaseHookAPIs[ hookLoadLibraryW ].pszFunctionName = "LoadLibraryW";
  234. g_rgBaseHookAPIs[ hookLoadLibraryW ].pfnNew = (PVOID)StubLoadLibraryW;
  235. g_rgBaseHookAPIs[ hookLoadLibraryExA ].pszModule = "kernel32.dll";
  236. g_rgBaseHookAPIs[ hookLoadLibraryExA ].pszFunctionName = "LoadLibraryExA";
  237. g_rgBaseHookAPIs[ hookLoadLibraryExA ].pfnNew = (PVOID)StubLoadLibraryExA;
  238. g_rgBaseHookAPIs[ hookLoadLibraryExW ].pszModule = "kernel32.dll";
  239. g_rgBaseHookAPIs[ hookLoadLibraryExW ].pszFunctionName = "LoadLibraryExW";
  240. g_rgBaseHookAPIs[ hookLoadLibraryExW ].pfnNew = (PVOID)StubLoadLibraryExW;
  241. g_rgBaseHookAPIs[ hookFreeLibrary ].pszModule = "kernel32.dll";
  242. g_rgBaseHookAPIs[ hookFreeLibrary ].pszFunctionName = "FreeLibrary";
  243. g_rgBaseHookAPIs[ hookFreeLibrary ].pfnNew = (PVOID)StubFreeLibrary;
  244. g_rgBaseHookAPIs[ hookExitProcess ].pszModule = "kernel32.dll";
  245. g_rgBaseHookAPIs[ hookExitProcess ].pszFunctionName = "ExitProcess";
  246. g_rgBaseHookAPIs[ hookExitProcess ].pfnNew = (PVOID)StubExitProcess;
  247. g_rgBaseHookAPIs[ hookCreateThread ].pszModule = "kernel32.dll";
  248. g_rgBaseHookAPIs[ hookCreateThread ].pszFunctionName = "CreateThread";
  249. g_rgBaseHookAPIs[ hookCreateThread ].pfnNew = (PVOID)StubCreateThread;
  250. AddHookAPIs(hInstance, g_rgBaseHookAPIs, SHIM_BASE_APIHOOK_COUNT, NULL);
  251. Shim2PatchNewModules();
  252. }