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.

954 lines
23 KiB

  1. #include <windows.h>
  2. //#include <wdm.h>
  3. #include <tchar.h>
  4. #include <stdio.h>
  5. #include <conio.h>
  6. /*
  7. namespace NT {
  8. extern "C" {
  9. #pragma warning(disable: 4005) // macro redefinition
  10. #include <wdm.h>
  11. #pragma warning(default: 4005)
  12. }
  13. }
  14. using NT::NTSTATUS;
  15. */
  16. FILE* g_OutFile;
  17. #define RESTORE_FUNCTION(x, y) {for(int i = 0; i < 2; ((DWORD *)x)[i] = y[i], i++);}
  18. #define INTERCEPT_FUNCTION(x, y) {for(int i = 0; i < 2; ((DWORD *)x)[i] = y[i], i++);}
  19. #define MYAPI NTAPI
  20. /////////////////////////////////////////////////////////////////////
  21. typedef struct _UNICODE_STRING {
  22. USHORT Length;
  23. USHORT MaximumLength;
  24. #ifdef MIDL_PASS
  25. [size_is(MaximumLength / 2), length_is((Length) / 2) ] USHORT * Buffer;
  26. #else // MIDL_PASS
  27. PWSTR Buffer;
  28. #endif // MIDL_PASS
  29. } UNICODE_STRING;
  30. typedef UNICODE_STRING *PUNICODE_STRING;
  31. typedef const UNICODE_STRING *PCUNICODE_STRING;
  32. #define UNICODE_NULL ((WCHAR)0) // winnt
  33. typedef struct _OBJECT_ATTRIBUTES {
  34. ULONG Length;
  35. HANDLE RootDirectory;
  36. PUNICODE_STRING ObjectName;
  37. ULONG Attributes;
  38. PVOID SecurityDescriptor; // Points to type SECURITY_DESCRIPTOR
  39. PVOID SecurityQualityOfService; // Points to type SECURITY_QUALITY_OF_SERVICE
  40. } OBJECT_ATTRIBUTES;
  41. typedef OBJECT_ATTRIBUTES *POBJECT_ATTRIBUTES;
  42. typedef CONST OBJECT_ATTRIBUTES *PCOBJECT_ATTRIBUTES;
  43. typedef LONG NTSTATUS;
  44. typedef PVOID POBJECT;
  45. ///////////////////////////////////////////////////////////
  46. typedef HRESULT (CALLBACK* ExcludeRegistryKeyT) (HANDLE,LPCTSTR,LPCTSTR);
  47. typedef struct _OBJECT_TYPE *POBJECT_TYPE;
  48. typedef CCHAR KPROCESSOR_MODE;
  49. typedef struct _OBJECT_HANDLE_INFORMATION {
  50. ULONG HandleAttributes;
  51. ACCESS_MASK GrantedAccess;
  52. } OBJECT_HANDLE_INFORMATION, *POBJECT_HANDLE_INFORMATION;
  53. #define KernelMode 0x0
  54. #define UserMode 0x1
  55. typedef LONG (MYAPI *ObReferenceObjectByHandleT)(
  56. IN HANDLE Handle,
  57. IN ACCESS_MASK DesiredAccess,
  58. IN POBJECT_TYPE ObjectType OPTIONAL,
  59. IN KPROCESSOR_MODE AccessMode,
  60. OUT PVOID *Object,
  61. OUT POBJECT_HANDLE_INFORMATION HandleInformation OPTIONAL
  62. );
  63. ObReferenceObjectByHandleT ObReferenceObjectByHandle=0;
  64. //----------------------------------------------------------------------
  65. //
  66. // GetPointer
  67. //
  68. // Translates a handle to an object pointer.
  69. //
  70. //----------------------------------------------------------------------
  71. POBJECT
  72. GetPointer(
  73. HANDLE handle
  74. )
  75. {
  76. POBJECT pKey;
  77. //
  78. // Ignore null handles
  79. //
  80. if( !handle ) return NULL;
  81. //
  82. // Get the pointer the handle refers to
  83. //
  84. ObReferenceObjectByHandle( handle, 0, NULL, UserMode, &pKey, NULL );
  85. return pKey;
  86. }
  87. /////////////////////////////////////////////////////////////////////
  88. #define BEGIN_NEW_FUNC1(FuncName, t1, p1)\
  89. typedef LONG (MYAPI *INTERCEPTED_##FuncName)(t1 p1);\
  90. \
  91. LONG MYAPI New##FuncName(t1 p1);\
  92. \
  93. LONG gl_ResultOf##FuncName = NULL;\
  94. \
  95. DWORD gl_Backup##FuncName[2] = {0, 0},\
  96. gl_Intercept##FuncName[2] = {0, 0};\
  97. \
  98. INTERCEPTED_##FuncName gl_p##FuncName = NULL; \
  99. \
  100. \
  101. LONG MYAPI New##FuncName(t1 p1) \
  102. {\
  103. RESTORE_FUNCTION(gl_p##FuncName, gl_Backup##FuncName);\
  104. \
  105. gl_ResultOf##FuncName = gl_p##FuncName(p1);
  106. #define BEGIN_NEW_FUNC2(FuncName, t1, p1, t2, p2)\
  107. typedef LONG (MYAPI *INTERCEPTED_##FuncName)(t1 p1, t2 p2);\
  108. \
  109. LONG MYAPI New##FuncName(t1 p1, t2 p2);\
  110. \
  111. LONG gl_ResultOf##FuncName = NULL;\
  112. \
  113. DWORD gl_Backup##FuncName[2] = {0, 0},\
  114. gl_Intercept##FuncName[2] = {0, 0};\
  115. \
  116. INTERCEPTED_##FuncName gl_p##FuncName = NULL; \
  117. \
  118. \
  119. LONG MYAPI New##FuncName(t1 p1, t2 p2) \
  120. {\
  121. RESTORE_FUNCTION(gl_p##FuncName, gl_Backup##FuncName);\
  122. \
  123. gl_ResultOf##FuncName = gl_p##FuncName(p1, p2);
  124. #define BEGIN_NEW_FUNC3(FuncName, t1, p1, t2, p2, t3, p3)\
  125. typedef LONG (MYAPI *INTERCEPTED_##FuncName)(t1 p1, t2 p2, t3 p3);\
  126. \
  127. LONG MYAPI New##FuncName(t1 p1, t2 p2, t3 p3);\
  128. \
  129. LONG gl_ResultOf##FuncName = NULL;\
  130. \
  131. DWORD gl_Backup##FuncName[2] = {0, 0},\
  132. gl_Intercept##FuncName[2] = {0, 0};\
  133. \
  134. INTERCEPTED_##FuncName gl_p##FuncName = NULL; \
  135. \
  136. \
  137. LONG MYAPI New##FuncName(t1 p1, t2 p2, t3 p3) \
  138. {\
  139. RESTORE_FUNCTION(gl_p##FuncName, gl_Backup##FuncName);\
  140. \
  141. gl_ResultOf##FuncName = gl_p##FuncName(p1, p2, p3);
  142. #define BEGIN_NEW_FUNC4(FuncName, t1, p1, t2, p2, t3, p3, t4, p4)\
  143. typedef LONG (MYAPI *INTERCEPTED_##FuncName)(t1 p1, t2 p2, t3 p3, t4 p4);\
  144. \
  145. LONG MYAPI New##FuncName(t1 p1, t2 p2, t3 p3, t4 p4);\
  146. \
  147. LONG gl_ResultOf##FuncName = NULL;\
  148. \
  149. DWORD gl_Backup##FuncName[2] = {0, 0},\
  150. gl_Intercept##FuncName[2] = {0, 0};\
  151. \
  152. INTERCEPTED_##FuncName gl_p##FuncName = NULL; \
  153. \
  154. \
  155. LONG MYAPI New##FuncName(t1 p1, t2 p2, t3 p3, t4 p4) \
  156. {\
  157. RESTORE_FUNCTION(gl_p##FuncName, gl_Backup##FuncName);\
  158. \
  159. gl_ResultOf##FuncName = gl_p##FuncName(p1, p2, p3, p4);
  160. #define BEGIN_NEW_FUNC5(FuncName, t1, p1, t2, p2, t3, p3, t4, p4, t5, p5)\
  161. typedef LONG (MYAPI *INTERCEPTED_##FuncName)(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5);\
  162. \
  163. LONG MYAPI New##FuncName(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5);\
  164. \
  165. LONG gl_ResultOf##FuncName = NULL;\
  166. \
  167. DWORD gl_Backup##FuncName[2] = {0, 0},\
  168. gl_Intercept##FuncName[2] = {0, 0};\
  169. \
  170. INTERCEPTED_##FuncName gl_p##FuncName = NULL; \
  171. \
  172. \
  173. LONG MYAPI New##FuncName(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5) \
  174. {\
  175. RESTORE_FUNCTION(gl_p##FuncName, gl_Backup##FuncName);\
  176. \
  177. gl_ResultOf##FuncName = gl_p##FuncName(p1, p2, p3, p4, p5);
  178. #define BEGIN_NEW_FUNC6(FuncName, t1, p1, t2, p2, t3, p3, t4, p4, t5, p5, t6, p6)\
  179. typedef LONG (MYAPI *INTERCEPTED_##FuncName)(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6);\
  180. \
  181. LONG MYAPI New##FuncName(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6);\
  182. \
  183. LONG gl_ResultOf##FuncName = NULL;\
  184. \
  185. DWORD gl_Backup##FuncName[2] = {0, 0},\
  186. gl_Intercept##FuncName[2] = {0, 0};\
  187. \
  188. INTERCEPTED_##FuncName gl_p##FuncName = NULL; \
  189. \
  190. \
  191. LONG MYAPI New##FuncName(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6) \
  192. {\
  193. RESTORE_FUNCTION(gl_p##FuncName, gl_Backup##FuncName);\
  194. \
  195. gl_ResultOf##FuncName = gl_p##FuncName(p1, p2, p3, p4, p5, p6);
  196. #define BEGIN_NEW_FUNC7(FuncName, t1, p1, t2, p2, t3, p3, t4, p4, t5, p5, t6, p6, t7, p7)\
  197. typedef LONG (MYAPI *INTERCEPTED_##FuncName)(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7);\
  198. \
  199. LONG MYAPI New##FuncName(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7);\
  200. \
  201. LONG gl_ResultOf##FuncName = NULL;\
  202. \
  203. DWORD gl_Backup##FuncName[2] = {0, 0},\
  204. gl_Intercept##FuncName[2] = {0, 0};\
  205. \
  206. INTERCEPTED_##FuncName gl_p##FuncName = NULL; \
  207. \
  208. \
  209. LONG MYAPI New##FuncName(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7) \
  210. {\
  211. RESTORE_FUNCTION(gl_p##FuncName, gl_Backup##FuncName);\
  212. \
  213. gl_ResultOf##FuncName = gl_p##FuncName(p1, p2, p3, p4, p5, p6, p7);
  214. #define BEGIN_NEW_FUNC8(FuncName, t1, p1, t2, p2, t3, p3, t4, p4, t5, p5, t6, p6, t7, p7, t8, p8)\
  215. typedef LONG (MYAPI *INTERCEPTED_##FuncName)(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8);\
  216. \
  217. LONG MYAPI New##FuncName(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8);\
  218. \
  219. LONG gl_ResultOf##FuncName = NULL;\
  220. \
  221. DWORD gl_Backup##FuncName[2] = {0, 0},\
  222. gl_Intercept##FuncName[2] = {0, 0};\
  223. \
  224. INTERCEPTED_##FuncName gl_p##FuncName = NULL; \
  225. \
  226. \
  227. LONG MYAPI New##FuncName(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8) \
  228. {\
  229. RESTORE_FUNCTION(gl_p##FuncName, gl_Backup##FuncName);\
  230. \
  231. gl_ResultOf##FuncName = gl_p##FuncName(p1, p2, p3, p4, p5, p6, p7, p8);
  232. #define BEGIN_NEW_FUNC9(FuncName, t1, p1, t2, p2, t3, p3, t4, p4, t5, p5, t6, p6, t7, p7, t8, p8, t9, p9)\
  233. typedef LONG (MYAPI *INTERCEPTED_##FuncName)(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8, t9 p9);\
  234. \
  235. LONG MYAPI New##FuncName(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8, t9 p9);\
  236. \
  237. LONG gl_ResultOf##FuncName = NULL;\
  238. \
  239. DWORD gl_Backup##FuncName[2] = {0, 0},\
  240. gl_Intercept##FuncName[2] = {0, 0};\
  241. \
  242. INTERCEPTED_##FuncName gl_p##FuncName = NULL; \
  243. \
  244. \
  245. LONG MYAPI New##FuncName(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8, t9 p9) \
  246. {\
  247. RESTORE_FUNCTION(gl_p##FuncName, gl_Backup##FuncName);\
  248. \
  249. gl_ResultOf##FuncName = gl_p##FuncName(p1, p2, p3, p4, p5, p6, p7, p8, p9);
  250. #define BEGIN_NEW_FUNC12(FuncName, t1, p1, t2, p2, t3, p3, t4, p4, t5, p5, t6, p6, t7, p7, t8, p8, t9, p9, t10, p10, t11, p11, t12, p12)\
  251. typedef LONG (MYAPI *INTERCEPTED_##FuncName)(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8, t9 p9, t10 p10, t11 p11, t12 p12);\
  252. \
  253. LONG MYAPI New##FuncName(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8, t9 p9, t10 p10, t11 p11, t12 p12);\
  254. \
  255. LONG gl_ResultOf##FuncName = NULL;\
  256. \
  257. DWORD gl_Backup##FuncName[2] = {0, 0},\
  258. gl_Intercept##FuncName[2] = {0, 0};\
  259. \
  260. INTERCEPTED_##FuncName gl_p##FuncName = NULL; \
  261. \
  262. \
  263. LONG MYAPI New##FuncName(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8, t9 p9, t10 p10, t11 p11, t12 p12) \
  264. {\
  265. RESTORE_FUNCTION(gl_p##FuncName, gl_Backup##FuncName);\
  266. \
  267. gl_ResultOf##FuncName = gl_p##FuncName(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12);
  268. /////////////////////////////////////////////////////////////////////
  269. #define END_NEW_FUNC(FuncName) \
  270. INTERCEPT_FUNCTION(gl_p##FuncName, gl_Intercept##FuncName);\
  271. return gl_ResultOf##FuncName;\
  272. }
  273. /////////////////////////////////////////////////////////////////////
  274. #define INTERCEPT(FuncName) \
  275. gl_p##FuncName = (INTERCEPTED_##FuncName)GetProcAddress(hKernel32, #FuncName);\
  276. if(!gl_p##FuncName)\
  277. return FALSE;\
  278. \
  279. ::VirtualProtect(gl_p##FuncName, 10, PAGE_EXECUTE_READWRITE, &dwResult);\
  280. \
  281. ((BYTE *)gl_Intercept##FuncName)[0] = 0xE9;\
  282. ((DWORD *)(((BYTE *)gl_Intercept##FuncName) + 1))[0] = DWORD(New##FuncName) - (DWORD(gl_p##FuncName) + 5);\
  283. \
  284. for(int i = 0; i < 2; gl_Backup##FuncName[i] = ((DWORD *)gl_p##FuncName)[i], \
  285. ((DWORD *)gl_p##FuncName)[i] = gl_Intercept##FuncName[i], i++)
  286. #define RESTORE(FuncName) RESTORE_FUNCTION(gl_p##FuncName, gl_Backup##FuncName)
  287. /////////////////////////////////////////////////////////////////////
  288. #define LOG(X) _fputts(X, g_OutFile);
  289. #define LOGN(X) _fputts(X L"\n", g_OutFile);
  290. #define LOGNL() _fputts(L"\n", g_OutFile);
  291. void LOGSTR(LPCTSTR ValueName, LPCTSTR Value)
  292. {
  293. _ftprintf(g_OutFile, L" (%s: %s)", ValueName, Value);
  294. }
  295. void LOGKEY(HANDLE key)
  296. {
  297. TCHAR buf[256];
  298. buf[0] = 0;
  299. /* switch ((int)key)
  300. {
  301. case HKEY_LOCAL_MACHINE:
  302. _tcscpy(buf, L"HKEY_LOCAL_MACHINE");
  303. break;
  304. case HKEY_CLASSES_ROOT:
  305. _tcscpy(buf, L"HKEY_CLASSES_ROOT");
  306. break;
  307. case HKEY_CURRENT_CONFIG:
  308. _tcscpy(buf, L"HKEY_CURRENT_CONFIG");
  309. break;
  310. case HKEY_CURRENT_USER:
  311. _tcscpy(buf, L"HKEY_CURRENT_USER");
  312. break;
  313. case HKEY_USERS:
  314. _tcscpy(buf, L"HKEY_USERS");
  315. break;
  316. case HKEY_PERFORMANCE_DATA:
  317. _tcscpy(buf, L"HKEY_PERFORMANCE_DATA");
  318. break;
  319. };
  320. if (buf[0] != 0)
  321. _ftprintf(g_OutFile, L" (Key: %s)", buf);
  322. else*/
  323. _ftprintf(g_OutFile, L" (Key: %u)", key);
  324. }
  325. /*
  326. BEGIN_NEW_FUNC1(RegCloseKey, HKEY, hkey)
  327. LOG(L"RegCloseKey");
  328. LOGKEY(hkey);
  329. LOGNL();
  330. END_NEW_FUNC(RegCloseKey)
  331. BEGIN_NEW_FUNC2(RegOverridePredefKey, HKEY, hKey, HKEY, hNewHKey)
  332. LOGN(L"RegOverridePredefKey");
  333. END_NEW_FUNC(RegOverridePredefKey)
  334. BEGIN_NEW_FUNC4(RegOpenUserClassesRoot, HANDLE, hToken, DWORD, dwOptions, REGSAM, samDesired, PHKEY, phkResult)
  335. LOGN(L"RegOpenUserClassesRoot");
  336. END_NEW_FUNC(RegOpenUserClassesRoot)
  337. BEGIN_NEW_FUNC2(RegOpenCurrentUser, REGSAM, samDesired, PHKEY, phkResult)
  338. LOGN(L"RegOpenCurrentUser");
  339. END_NEW_FUNC(RegOpenCurrentUser)
  340. BEGIN_NEW_FUNC3(RegConnectRegistryW, LPCWSTR, lpMachineName, HKEY, hKey, PHKEY, phkResult)
  341. LOGN(L"RegConnectRegistryW");
  342. END_NEW_FUNC(RegConnectRegistryW)
  343. BEGIN_NEW_FUNC3(RegCreateKeyW, HKEY, hKey, LPCWSTR, lpSubKey, PHKEY, phkResult)
  344. LOGN(L"RegCreateKeyW");
  345. END_NEW_FUNC(RegCreateKeyW)
  346. BEGIN_NEW_FUNC9(RegCreateKeyExW,
  347. HKEY, hKey,
  348. LPCWSTR, lpSubKey,
  349. DWORD, Reserved,
  350. LPWSTR, lpClass,
  351. DWORD, dwOptions,
  352. REGSAM, samDesired,
  353. LPSECURITY_ATTRIBUTES, lpSecurityAttributes,
  354. PHKEY, phkResult,
  355. LPDWORD, lpdwDisposition)
  356. LOGN(L"RegCreateKeyExW");
  357. LOGKEY(hKey);
  358. LOGSTR(L"SubKey", lpSubKey);
  359. if ((phkResult != NULL) && (gl_ResultOfRegCreateKeyExW == ERROR_SUCCESS))
  360. LOGKEY(*phkResult);
  361. else
  362. LOGKEY(0);
  363. LOGNL();
  364. END_NEW_FUNC(RegCreateKeyExW)
  365. BEGIN_NEW_FUNC2(RegDeleteKeyW, HKEY, hKey, LPCWSTR, lpSubKey)
  366. LOG(L"RegDeleteKeyW");
  367. LOGKEY(hKey);
  368. LOGSTR(L"SubKey", lpSubKey);
  369. LOGNL();
  370. END_NEW_FUNC(RegDeleteKeyW)
  371. BEGIN_NEW_FUNC2(RegDeleteValueW, HKEY, hKey, LPCWSTR, lpValueName)
  372. LOG(L"RegDeleteValueW");
  373. LOGKEY(hKey);
  374. LOGSTR(L"Value", lpValueName);
  375. LOGNL();
  376. END_NEW_FUNC(RegDeleteValueW)
  377. BEGIN_NEW_FUNC4(RegEnumKeyW, HKEY, hKey, DWORD, dwIndex, LPWSTR, lpName, DWORD, cbName)
  378. LOGN(L"RegEnumKeyW");
  379. END_NEW_FUNC(RegEnumKeyW)
  380. BEGIN_NEW_FUNC8(RegEnumKeyExW,
  381. HKEY, hKey,
  382. DWORD, dwIndex,
  383. LPWSTR, lpName,
  384. LPDWORD, lpcbName,
  385. LPDWORD, lpReserved,
  386. LPWSTR, lpClass,
  387. LPDWORD, lpcbClass,
  388. PFILETIME, lpftLastWriteTime)
  389. LOG(L"RegEnumKeyExW");
  390. LOGKEY(hKey);
  391. LOGNL();
  392. END_NEW_FUNC(RegEnumKeyExW)
  393. BEGIN_NEW_FUNC8(RegEnumValueW,
  394. HKEY, hKey,
  395. DWORD, dwIndex,
  396. LPWSTR, lpValueName,
  397. LPDWORD, lpcbValueName,
  398. LPDWORD, lpReserved,
  399. LPDWORD, lpType,
  400. LPBYTE, lpData,
  401. LPDWORD, lpcbData)
  402. LOG(L"RegEnumValueW");
  403. LOGKEY(hKey);
  404. LOGNL();
  405. END_NEW_FUNC(RegEnumValueW)
  406. BEGIN_NEW_FUNC1(RegFlushKey, HKEY, hKey)
  407. LOGN(L"RegFlushKey");
  408. END_NEW_FUNC(RegFlushKey)
  409. BEGIN_NEW_FUNC4(RegGetKeySecurity, HKEY, hKey, SECURITY_INFORMATION, SecurityInformation, PSECURITY_DESCRIPTOR, pSecurityDescriptor, LPDWORD, lpcbSecurityDescriptor)
  410. LOG(L"RegGetKeySecurity");
  411. LOGKEY(hKey);
  412. LOGNL();
  413. END_NEW_FUNC(RegGetKeySecurity)
  414. BEGIN_NEW_FUNC3(RegLoadKeyW, HKEY, hKey, LPCWSTR, lpSubKey, LPCWSTR, lpFile)
  415. LOGN(L"RegLoadKeyW");
  416. END_NEW_FUNC(RegLoadKeyW)
  417. BEGIN_NEW_FUNC5(RegNotifyChangeKeyValue,
  418. HKEY, hKey,
  419. BOOL, bWatchSubtree,
  420. DWORD, dwNotifyFilter,
  421. HANDLE, hEvent,
  422. BOOL, fAsynchronus)
  423. LOGN(L"RegNotifyChangeKeyValue");
  424. END_NEW_FUNC(RegNotifyChangeKeyValue)
  425. BEGIN_NEW_FUNC3(RegOpenKeyW, HKEY, hKey, LPCWSTR, lpSubKey, PHKEY, phkResult)
  426. LOGN(L"RegOpenKeyW");
  427. END_NEW_FUNC(RegOpenKeyW)
  428. BEGIN_NEW_FUNC5(RegOpenKeyExW,
  429. HKEY, hKey,
  430. LPCWSTR, lpSubKey,
  431. DWORD, ulOptions,
  432. REGSAM, samDesired,
  433. PHKEY, phkResult)
  434. LOG(L"RegOpenKeyExW");
  435. LOGKEY(hKey);
  436. LOGSTR(L"SubKey", lpSubKey);
  437. if ((phkResult != NULL) && (gl_ResultOfRegOpenKeyExW == ERROR_SUCCESS))
  438. LOGKEY(*phkResult);
  439. else
  440. LOGKEY(0);
  441. LOGNL();
  442. END_NEW_FUNC(RegOpenKeyExW)
  443. BEGIN_NEW_FUNC12(RegQueryInfoKeyW,
  444. HKEY, hKey,
  445. LPWSTR, lpClass,
  446. LPDWORD, lpcbClass,
  447. LPDWORD, lpReserved,
  448. LPDWORD, lpcSubKeys,
  449. LPDWORD, lpcbMaxSubKeyLen,
  450. LPDWORD, lpcbMaxClassLen,
  451. LPDWORD, lpcValues,
  452. LPDWORD, lpcbMaxValueNameLen,
  453. LPDWORD, lpcbMaxValueLen,
  454. LPDWORD, lpcbSecurityDescriptor,
  455. PFILETIME, lpftLastWriteTime)
  456. LOG(L"RegQueryInfoKeyW");
  457. LOGKEY(hKey);
  458. LOGNL();
  459. END_NEW_FUNC(RegQueryInfoKeyW)
  460. BEGIN_NEW_FUNC4(RegQueryValueW,
  461. HKEY, hKey,
  462. LPCWSTR, lpSubKey,
  463. LPWSTR, lpValue,
  464. PLONG, lpcbValue)
  465. LOGN(L"RegQueryValueW");
  466. END_NEW_FUNC(RegQueryValueW)
  467. BEGIN_NEW_FUNC5(RegQueryMultipleValuesW,
  468. HKEY, hKey,
  469. PVALENTW, val_list,
  470. DWORD, num_vals,
  471. LPWSTR, lpValueBuf,
  472. LPDWORD, ldwTotsize)
  473. LOG(L"RegQueryMultipleValuesW");
  474. LOGKEY(hKey);
  475. LOGNL();
  476. END_NEW_FUNC(RegQueryMultipleValuesW)
  477. BEGIN_NEW_FUNC6(RegQueryValueExW,
  478. HKEY, hKey,
  479. LPCWSTR, lpValueName,
  480. LPDWORD, lpReserved,
  481. LPDWORD, lpType,
  482. LPBYTE, lpData,
  483. LPDWORD, lpcbData)
  484. LOG(L"RegQueryValueExW");
  485. LOGKEY(hKey);
  486. if (lpValueName != NULL)
  487. LOGSTR(L"ValueName", lpValueName);
  488. else
  489. LOGSTR(L"ValueName", L"");
  490. LOGNL();
  491. END_NEW_FUNC(RegQueryValueExW)
  492. BEGIN_NEW_FUNC4(RegReplaceKeyW,
  493. HKEY, hKey,
  494. LPCWSTR, lpSubKey,
  495. LPCWSTR, lpNewFile,
  496. LPCWSTR, lpOldFile)
  497. LOGN(L"RegReplaceKeyW");
  498. END_NEW_FUNC(RegReplaceKeyW)
  499. BEGIN_NEW_FUNC3(RegRestoreKeyW, HKEY, hKey, LPCWSTR, lpFile, DWORD, dwFlags)
  500. LOGN(L"RegRestoreKeyW");
  501. END_NEW_FUNC(RegRestoreKeyW)
  502. BEGIN_NEW_FUNC3(RegSaveKeyW, HKEY, hKey, LPCWSTR, lpFile, LPSECURITY_ATTRIBUTES, lpSecurityAttributes)
  503. LOGN(L"RegSaveKeyW");
  504. END_NEW_FUNC(RegSaveKeyW)
  505. BEGIN_NEW_FUNC3(RegSetKeySecurity, HKEY, hKey, SECURITY_INFORMATION, SecurityInformation, PSECURITY_DESCRIPTOR, pSecurityDescriptor)
  506. LOGN(L"RegSetKeySecurity");
  507. END_NEW_FUNC(RegSetKeySecurity)
  508. BEGIN_NEW_FUNC5(RegSetValueW,
  509. HKEY, hKey,
  510. LPCWSTR, lpSubKey,
  511. DWORD, dwType,
  512. LPCWSTR, lpData,
  513. DWORD, cbData)
  514. LOGN(L"RegSetValueW");
  515. END_NEW_FUNC(RegSetValueW)
  516. BEGIN_NEW_FUNC6(RegSetValueExW,
  517. HKEY, hKey,
  518. LPCWSTR, lpValueName,
  519. DWORD, Reserved,
  520. DWORD, dwType,
  521. CONST BYTE*, lpData,
  522. DWORD, cbData)
  523. LOGN(L"RegSetValueExW");
  524. LOGKEY(hKey);
  525. if (lpValueName != NULL)
  526. LOGSTR(L"ValueName", lpValueName);
  527. else
  528. LOGSTR(L"ValueName", L"");
  529. LOGNL();
  530. END_NEW_FUNC(RegSetValueExW)
  531. BEGIN_NEW_FUNC2(RegUnLoadKeyW, HKEY, hKey, LPCWSTR, lpSubKey)
  532. LOGN(L"RegUnLoadKeyW");
  533. END_NEW_FUNC(RegUnLoadKeyW)
  534. */
  535. //NTSYSCALLAPI
  536. LONG
  537. NTAPI
  538. NtOpenKey(
  539. PHANDLE KeyHandle,
  540. ACCESS_MASK DesiredAccess,
  541. POBJECT_ATTRIBUTES ObjectAttributes
  542. );
  543. BEGIN_NEW_FUNC3(NtOpenKey, PHANDLE, KeyHandle, ACCESS_MASK, DesiredAccess, \
  544. POBJECT_ATTRIBUTES, ObjectAttributes)
  545. LOG(L"NtOpenKey");
  546. LOGKEY(ObjectAttributes->RootDirectory);
  547. LOGSTR(L"SubKey", (LPWSTR)ObjectAttributes->ObjectName->Buffer);
  548. LOGKEY(*KeyHandle);
  549. LOGNL();
  550. END_NEW_FUNC(NtOpenKey)
  551. //NTSYSCALLAPI
  552. NTSTATUS
  553. NTAPI
  554. NtCreateKey(
  555. OUT PHANDLE KeyHandle,
  556. IN ACCESS_MASK DesiredAccess,
  557. IN POBJECT_ATTRIBUTES ObjectAttributes,
  558. IN ULONG TitleIndex,
  559. IN PUNICODE_STRING Class OPTIONAL,
  560. IN ULONG CreateOptions,
  561. OUT PULONG Disposition OPTIONAL
  562. );
  563. BEGIN_NEW_FUNC7(NtCreateKey,
  564. PHANDLE, KeyHandle,
  565. ACCESS_MASK, DesiredAccess,
  566. POBJECT_ATTRIBUTES, ObjectAttributes,
  567. ULONG, TitleIndex,
  568. PUNICODE_STRING, Class,
  569. ULONG, CreateOptions,
  570. PULONG, Disposition)
  571. LOG(L"NtCreateKey");
  572. LOGKEY(ObjectAttributes->RootDirectory);
  573. LOGSTR(L"SubKey", (LPWSTR)ObjectAttributes->ObjectName->Buffer);
  574. LOGKEY(*KeyHandle);
  575. LOGNL();
  576. END_NEW_FUNC(NtCreateKey)
  577. /////////////////////////////////////////////////////////////////////////////////
  578. BOOL InterceptSystemFunctions()
  579. {
  580. DWORD dwResult;
  581. HINSTANCE hKernel32;
  582. // hKernel32 = LoadLibrary(L"advapi32.DLL");
  583. hKernel32 = LoadLibrary(L"ntdll.DLL");
  584. // ObReferenceObjectByHandle = (ObReferenceObjectByHandleT) GetProcAddress (hKernel32, "ObReferenceObjectByHandle");
  585. //////////////////
  586. /*
  587. INTERCEPT(RegCloseKey);
  588. INTERCEPT(RegOverridePredefKey);
  589. INTERCEPT(RegOpenUserClassesRoot);
  590. INTERCEPT(RegOpenCurrentUser);
  591. INTERCEPT(RegConnectRegistryW);
  592. INTERCEPT(RegCreateKeyW);
  593. INTERCEPT(RegCreateKeyExW);
  594. INTERCEPT(RegDeleteKeyW);
  595. INTERCEPT(RegDeleteValueW);
  596. INTERCEPT(RegEnumKeyW);
  597. INTERCEPT(RegEnumKeyExW);
  598. INTERCEPT(RegEnumValueW);
  599. INTERCEPT(RegFlushKey);
  600. INTERCEPT(RegGetKeySecurity);
  601. INTERCEPT(RegLoadKeyW);
  602. INTERCEPT(RegNotifyChangeKeyValue);
  603. INTERCEPT(RegOpenKeyW);
  604. INTERCEPT(RegOpenKeyExW);
  605. INTERCEPT(RegQueryInfoKeyW);
  606. INTERCEPT(RegQueryValueW);
  607. INTERCEPT(RegQueryMultipleValuesW);
  608. INTERCEPT(RegQueryValueExW);
  609. INTERCEPT(RegReplaceKeyW);
  610. INTERCEPT(RegRestoreKeyW);
  611. INTERCEPT(RegSaveKeyW);
  612. INTERCEPT(RegSetKeySecurity);
  613. INTERCEPT(RegSetValueW);
  614. INTERCEPT(RegSetValueExW);
  615. INTERCEPT(RegUnLoadKeyW);
  616. */
  617. INTERCEPT(NtOpenKey);
  618. INTERCEPT(NtCreateKey);
  619. // CloseHandle(hKernel32);
  620. //////////////////
  621. return TRUE;
  622. }
  623. void RestoreSystemFunctions()
  624. {
  625. /* RESTORE(RegCloseKey);
  626. RESTORE(RegOverridePredefKey);
  627. RESTORE(RegOpenUserClassesRoot);
  628. RESTORE(RegOpenCurrentUser);
  629. RESTORE(RegConnectRegistryW);
  630. RESTORE(RegCreateKeyW);
  631. RESTORE(RegCreateKeyExW);
  632. RESTORE(RegDeleteKeyW);
  633. RESTORE(RegDeleteValueW);
  634. RESTORE(RegEnumKeyW);
  635. RESTORE(RegEnumKeyExW);
  636. RESTORE(RegEnumValueW);
  637. RESTORE(RegFlushKey);
  638. RESTORE(RegGetKeySecurity);
  639. RESTORE(RegLoadKeyW);
  640. RESTORE(RegNotifyChangeKeyValue);
  641. RESTORE(RegOpenKeyW);
  642. RESTORE(RegOpenKeyExW);
  643. RESTORE(RegQueryInfoKeyW);
  644. RESTORE(RegQueryValueW);
  645. RESTORE(RegQueryMultipleValuesW);
  646. RESTORE(RegQueryValueExW);
  647. RESTORE(RegReplaceKeyW);
  648. RESTORE(RegRestoreKeyW);
  649. RESTORE(RegSaveKeyW);
  650. RESTORE(RegSetKeySecurity);
  651. RESTORE(RegSetValueW);
  652. RESTORE(RegSetValueExW);
  653. RESTORE(RegUnLoadKeyW);
  654. */
  655. RESTORE(NtOpenKey);
  656. RESTORE(NtCreateKey);
  657. }
  658. typedef HRESULT (CALLBACK* TempDllRegisterServerT) ();
  659. TempDllRegisterServerT TempDllRegisterServer=0;
  660. void RegisterAndLogAllDlls(FILE* Dlls)
  661. {
  662. TCHAR DllFileName[MAX_PATH];
  663. DllFileName[0] = 0;
  664. while(_fgetts(DllFileName, MAX_PATH, Dlls) != NULL)
  665. {
  666. int len = _tcslen(DllFileName);
  667. DllFileName[len-1]=0;
  668. HMODULE hLibrary = LoadLibrary (DllFileName);
  669. if (hLibrary)
  670. {
  671. LOG(L"********** Loaded: ");
  672. LOG(DllFileName);
  673. LOGNL();
  674. TempDllRegisterServer = (TempDllRegisterServerT) GetProcAddress (hLibrary, "DllRegisterServer");
  675. if (TempDllRegisterServer != 0)
  676. {
  677. LOG(L"Loaded DllRegisterServer, calling it now");
  678. LOGNL();
  679. InterceptSystemFunctions();
  680. TempDllRegisterServer();
  681. RestoreSystemFunctions();
  682. }
  683. else
  684. {
  685. LOG(L"Could not load DllRegisterServer");
  686. LOGNL();
  687. }
  688. FreeLibrary(hLibrary);
  689. }
  690. else
  691. {
  692. LOG(L"********** Could not load: ");
  693. LOG(DllFileName);
  694. LOGNL();
  695. }
  696. LOGNL();
  697. }
  698. }
  699. int __cdecl wmain(int argc, WCHAR* argv[])
  700. {
  701. HKEY temp;
  702. if (argc == 1)
  703. {
  704. HMODULE hLibrary = LoadLibrary (L"rsaenh.dll");
  705. TempDllRegisterServer = (TempDllRegisterServerT) GetProcAddress (hLibrary, "DllRegisterServer");
  706. TempDllRegisterServer();
  707. }
  708. if (argc == 2)
  709. {
  710. HMODULE hLibrary = LoadLibrary (argv[1]);
  711. TempDllRegisterServer = (TempDllRegisterServerT) GetProcAddress (hLibrary, "DllRegisterServer");
  712. TempDllRegisterServer();
  713. }
  714. if (argc != 3)
  715. {
  716. _tprintf(L"%s\n", L"Syntax: dllanalyze <dll List File> <log file>");
  717. _getch();
  718. return -1;
  719. }
  720. FILE* pDllFile = _tfopen(argv[1], L"rt");
  721. g_OutFile = _tfopen(argv[2], L"wt");
  722. _fputts(L"Hello, I am a log\n", g_OutFile);
  723. RegisterAndLogAllDlls(pDllFile);
  724. /*
  725. RegOpenKey(HKEY_LOCAL_MACHINE, L"Software", &temp);
  726. RegCloseKey(temp);
  727. // CreateFile("Kuku", 0, 0, 0, 0, 0, 0);
  728. BOOL b = InterceptSystemFunctions();
  729. RegOpenKey(HKEY_LOCAL_MACHINE, L"Software", &temp);
  730. RegDeleteValue(temp, TEXT("doo"));
  731. RegCloseKey(temp);
  732. // CreateFile("Kuku1", 0, 0, 0, 0, 0, 0);
  733. // CreateFile("Kuku2", 0, 0, 0, 0, 0, 0);
  734. // CreateFile("Kuku3", 0, 0, 0, 0, 0, 0);
  735. */
  736. fclose(g_OutFile);
  737. _tsystem(L"start c:\\log.txt");
  738. return 0;
  739. }