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.

1781 lines
61 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. SecurityChecks.cpp
  5. Abstract:
  6. This AppVerifier shim hooks CreateProcess, CreateProcessAsUser,
  7. and WinExec and checks to see if some conditions exist that
  8. might allow trojan horse behavior to occur.
  9. Notes:
  10. This is a general purpose shim.
  11. History:
  12. 12/13/2001 rparsons Created
  13. --*/
  14. #include "precomp.h"
  15. IMPLEMENT_SHIM_BEGIN(SecurityChecks)
  16. #include "ShimHookMacro.h"
  17. //
  18. // verifier log entries
  19. //
  20. BEGIN_DEFINE_VERIFIER_LOG(SecurityChecks)
  21. VERIFIER_LOG_ENTRY(VLOG_SECURITYCHECKS_BADARGUMENTS)
  22. VERIFIER_LOG_ENTRY(VLOG_SECURITYCHECKS_WINEXEC)
  23. VERIFIER_LOG_ENTRY(VLOG_SECURITYCHECKS_NULL_DACL)
  24. VERIFIER_LOG_ENTRY(VLOG_SECURITYCHECKS_WORLDWRITE_DACL)
  25. END_DEFINE_VERIFIER_LOG(SecurityChecks)
  26. INIT_VERIFIER_LOG(SecurityChecks);
  27. APIHOOK_ENUM_BEGIN
  28. APIHOOK_ENUM_ENTRY(CreateProcessA)
  29. APIHOOK_ENUM_ENTRY(CreateProcessW)
  30. APIHOOK_ENUM_ENTRY(CreateProcessAsUserA)
  31. APIHOOK_ENUM_ENTRY(CreateProcessAsUserW)
  32. APIHOOK_ENUM_ENTRY(WinExec)
  33. APIHOOK_ENUM_ENTRY(CreateFileA)
  34. APIHOOK_ENUM_ENTRY(CreateFileW)
  35. APIHOOK_ENUM_ENTRY(CreateDesktopA)
  36. APIHOOK_ENUM_ENTRY(CreateDesktopW)
  37. APIHOOK_ENUM_ENTRY(CreateWindowStationA)
  38. APIHOOK_ENUM_ENTRY(CreateWindowStationW)
  39. APIHOOK_ENUM_ENTRY(RegCreateKeyExA)
  40. APIHOOK_ENUM_ENTRY(RegCreateKeyExW)
  41. APIHOOK_ENUM_ENTRY(RegSaveKeyA)
  42. APIHOOK_ENUM_ENTRY(RegSaveKeyW)
  43. APIHOOK_ENUM_ENTRY(RegSaveKeyExA)
  44. APIHOOK_ENUM_ENTRY(RegSaveKeyExW)
  45. APIHOOK_ENUM_ENTRY(CreateFileMappingA)
  46. APIHOOK_ENUM_ENTRY(CreateFileMappingW)
  47. APIHOOK_ENUM_ENTRY(CreateJobObjectA)
  48. APIHOOK_ENUM_ENTRY(CreateJobObjectW)
  49. APIHOOK_ENUM_ENTRY(CreateThread)
  50. APIHOOK_ENUM_ENTRY(CreateRemoteThread)
  51. APIHOOK_ENUM_ENTRY(CreateDirectoryA)
  52. APIHOOK_ENUM_ENTRY(CreateDirectoryW)
  53. APIHOOK_ENUM_ENTRY(CreateDirectoryExA)
  54. APIHOOK_ENUM_ENTRY(CreateDirectoryExW)
  55. APIHOOK_ENUM_ENTRY(CreateHardLinkA)
  56. APIHOOK_ENUM_ENTRY(CreateHardLinkW)
  57. APIHOOK_ENUM_ENTRY(CreateMailslotA)
  58. APIHOOK_ENUM_ENTRY(CreateMailslotW)
  59. APIHOOK_ENUM_ENTRY(CreateNamedPipeA)
  60. APIHOOK_ENUM_ENTRY(CreateNamedPipeW)
  61. APIHOOK_ENUM_ENTRY(CreatePipe)
  62. APIHOOK_ENUM_ENTRY(CreateMutexA)
  63. APIHOOK_ENUM_ENTRY(CreateMutexW)
  64. APIHOOK_ENUM_ENTRY(CreateSemaphoreA)
  65. APIHOOK_ENUM_ENTRY(CreateSemaphoreW)
  66. APIHOOK_ENUM_ENTRY(CreateWaitableTimerA)
  67. APIHOOK_ENUM_ENTRY(CreateWaitableTimerW)
  68. APIHOOK_ENUM_ENTRY(CreateEventA)
  69. APIHOOK_ENUM_ENTRY(CreateEventW)
  70. APIHOOK_ENUM_ENTRY(SetFileSecurityA)
  71. APIHOOK_ENUM_ENTRY(SetFileSecurityW)
  72. APIHOOK_ENUM_ENTRY(SetKernelObjectSecurity)
  73. APIHOOK_ENUM_ENTRY(SetNamedSecurityInfoA)
  74. APIHOOK_ENUM_ENTRY(SetNamedSecurityInfoW)
  75. APIHOOK_ENUM_ENTRY(SetSecurityInfo)
  76. APIHOOK_ENUM_ENTRY(RegSetKeySecurity)
  77. APIHOOK_ENUM_ENTRY(SetUserObjectSecurity)
  78. APIHOOK_ENUM_ENTRY(SetServiceObjectSecurity)
  79. APIHOOK_ENUM_ENTRY(SetNtmsObjectSecurity)
  80. APIHOOK_ENUM_ENTRY(ClusterRegCreateKey)
  81. APIHOOK_ENUM_ENTRY(ClusterRegSetKeySecurity)
  82. APIHOOK_ENUM_ENTRY(CreateNtmsMediaPoolA)
  83. APIHOOK_ENUM_ENTRY(CreateNtmsMediaPoolW)
  84. APIHOOK_ENUM_END
  85. BYTE g_ajSidBuffer[SECURITY_MAX_SID_SIZE];
  86. PSID g_pWorldSid = NULL;
  87. WCHAR g_wszWinDir[MAX_PATH];
  88. DWORD g_dwWinDirLen = 0;
  89. void
  90. InitWorldSid(
  91. void
  92. )
  93. {
  94. DWORD dwSidSize = sizeof(g_ajSidBuffer);
  95. if (CreateWellKnownSid(WinWorldSid, NULL, g_ajSidBuffer, &dwSidSize)) {
  96. g_pWorldSid = g_ajSidBuffer;
  97. } else {
  98. g_pWorldSid = NULL;
  99. }
  100. }
  101. void
  102. CheckDacl(
  103. PACL pDacl,
  104. LPCWSTR szCaller,
  105. LPCWSTR szParam,
  106. LPCWSTR szName
  107. )
  108. {
  109. if (!pDacl) {
  110. //
  111. // we have a NULL dacl -- log a problem
  112. //
  113. VLOG(VLOG_LEVEL_ERROR,
  114. VLOG_SECURITYCHECKS_NULL_DACL,
  115. "Called %ls, and specified a NULL DACL in %ls for object '%ls.'",
  116. szCaller,
  117. szParam,
  118. szName);
  119. return;
  120. }
  121. if (!g_pWorldSid) {
  122. //
  123. // we never were able to get the world Sid
  124. //
  125. return;
  126. }
  127. for (DWORD i = 0; i < pDacl->AceCount; ++i) {
  128. PACE_HEADER pAceHeader = NULL;
  129. PSID pSID;
  130. ACCESS_MASK dwAccessMask;
  131. if (!GetAce(pDacl, i, (LPVOID*)&pAceHeader)) {
  132. continue;
  133. }
  134. //
  135. // if it's not some form of ACCESS_ALLOWED ACE, we aren't interested
  136. //
  137. if (pAceHeader->AceType == ACCESS_ALLOWED_ACE_TYPE) {
  138. pSID = &(((PACCESS_ALLOWED_ACE)pAceHeader)->SidStart);
  139. dwAccessMask = ((PACCESS_ALLOWED_ACE)pAceHeader)->Mask;
  140. } else if (pAceHeader->AceType == ACCESS_ALLOWED_OBJECT_ACE_TYPE) {
  141. PACCESS_ALLOWED_OBJECT_ACE pAAOAce = (PACCESS_ALLOWED_OBJECT_ACE)pAceHeader;
  142. //
  143. // who the heck came up with this system? The Sid starts at a different place
  144. // depending on the flags. Anyone ever heard of multiple structs? Sigh.
  145. //
  146. if ((pAAOAce->Flags & ACE_OBJECT_TYPE_PRESENT) && (pAAOAce->Flags & ACE_INHERITED_OBJECT_TYPE_PRESENT)) {
  147. pSID = &(pAAOAce->SidStart);
  148. } else if ((pAAOAce->Flags & ACE_OBJECT_TYPE_PRESENT) || (pAAOAce->Flags & ACE_INHERITED_OBJECT_TYPE_PRESENT)){
  149. pSID = (PSID)&(pAAOAce->InheritedObjectType);
  150. } else {
  151. pSID = (PSID)&(pAAOAce->ObjectType);
  152. }
  153. dwAccessMask = ((PACCESS_ALLOWED_OBJECT_ACE)pAceHeader)->Mask;
  154. } else {
  155. continue;
  156. }
  157. //
  158. // check the validity of the SID, just to be safe
  159. //
  160. if (!IsValidSid(pSID)) {
  161. continue;
  162. }
  163. //
  164. // if the SID is the world, and the access mask allows WRITE_DAC and WRITE_OWNER, we have a problem
  165. //
  166. if ((dwAccessMask & (WRITE_DAC | WRITE_OWNER)) && EqualSid(pSID, g_pWorldSid)) {
  167. VLOG(VLOG_LEVEL_ERROR,
  168. VLOG_SECURITYCHECKS_WORLDWRITE_DACL,
  169. "Called %ls, and specified a DACL with WRITE_DAC and/or WRITE_OWNER for WORLD in %ls for object '%ls.'",
  170. szCaller,
  171. szParam,
  172. szName);
  173. return;
  174. }
  175. }
  176. }
  177. void
  178. CheckSecurityDescriptor(
  179. PSECURITY_DESCRIPTOR pSecurityDescriptor,
  180. LPCWSTR szCaller,
  181. LPCWSTR szParam,
  182. LPCWSTR szName
  183. )
  184. {
  185. BOOL bDaclPresent = FALSE;
  186. BOOL bDaclDefaulted = FALSE;
  187. PACL pDacl = NULL;
  188. if (!pSecurityDescriptor || !szName || !szName[0]) {
  189. //
  190. // there are no attributes, so they get the default, which is fine,
  191. // or the object doesn't have a name, so it can't be highjacked
  192. //
  193. return;
  194. }
  195. if (GetSecurityDescriptorDacl(pSecurityDescriptor, &bDaclPresent, &pDacl, &bDaclDefaulted)) {
  196. if (bDaclPresent) {
  197. CheckDacl(pDacl, szCaller, szParam, szName);
  198. }
  199. }
  200. }
  201. void
  202. CheckSecurityAttributes(
  203. LPSECURITY_ATTRIBUTES pSecurityAttrib,
  204. LPCWSTR szCaller,
  205. LPCWSTR szParam,
  206. LPCWSTR szName
  207. )
  208. {
  209. PSECURITY_DESCRIPTOR pSecurityDescriptor = NULL;
  210. if (!pSecurityAttrib) {
  211. //
  212. // there's no attributes, so they get the default, which is fine
  213. //
  214. return;
  215. }
  216. pSecurityDescriptor = (PSECURITY_DESCRIPTOR)pSecurityAttrib->lpSecurityDescriptor;
  217. CheckSecurityDescriptor(pSecurityDescriptor, szCaller, szParam, szName);
  218. }
  219. void
  220. CheckCreateProcess(
  221. LPCWSTR pwszApplicationName,
  222. LPCWSTR pwszCommandLine,
  223. LPCWSTR pwszCaller
  224. )
  225. {
  226. //
  227. // if applicationname is non-null, there's no problem
  228. //
  229. if (pwszApplicationName) {
  230. return;
  231. }
  232. //
  233. // if there's no command line, there's a problem, but not one we want to solve
  234. //
  235. if (!pwszCommandLine) {
  236. return;
  237. }
  238. //
  239. // if there are no spaces, no problem
  240. //
  241. LPWSTR pSpaceLoc = wcschr(pwszCommandLine, L' ');
  242. if (!pSpaceLoc) {
  243. return;
  244. }
  245. //
  246. // if the beginning of the command line is quoted, no problem
  247. //
  248. if (pwszCommandLine[0] == L'\"') {
  249. return;
  250. }
  251. //
  252. // if the phrase '.exe ' appears before the first space, we'll call that good
  253. //
  254. LPWSTR pExeLoc = wcsistr(pwszCommandLine, L".exe ");
  255. if (pExeLoc && pExeLoc < pSpaceLoc) {
  256. return;
  257. }
  258. //
  259. // if the first part of the command line is windir, we'll call that good
  260. //
  261. if (g_dwWinDirLen && _wcsnicmp(pwszCommandLine, g_wszWinDir, g_dwWinDirLen) == 0) {
  262. return;
  263. }
  264. if (_wcsicmp(pwszCaller, L"winexec") == 0) {
  265. VLOG(VLOG_LEVEL_ERROR,
  266. VLOG_SECURITYCHECKS_BADARGUMENTS,
  267. "Called %ls with command line '%ls'. The command line has spaces, and the exe name is not in quotes.",
  268. pwszCaller,
  269. pwszCommandLine);
  270. } else {
  271. VLOG(VLOG_LEVEL_ERROR,
  272. VLOG_SECURITYCHECKS_BADARGUMENTS,
  273. "Called %ls with command line '%ls'. The lpApplicationName argument is NULL, lpCommandLine has spaces, and the exe name is not in quotes.",
  274. pwszCaller,
  275. pwszCommandLine);
  276. }
  277. }
  278. void
  279. CheckForNoPathInFileName(
  280. LPCWSTR pwszFilePath,
  281. LPCWSTR pwszCaller
  282. )
  283. {
  284. if (!pwszFilePath || !pwszCaller) {
  285. return;
  286. }
  287. //
  288. // skip quotes and space if necessary
  289. //
  290. DWORD dwBegin = 0;
  291. while (pwszFilePath[dwBegin] == L'\"' || pwszFilePath[dwBegin] == L' ') {
  292. dwBegin++;
  293. }
  294. //
  295. // if there's nothing left of the string, get out
  296. //
  297. if (!pwszFilePath[dwBegin] || !pwszFilePath[dwBegin + 1]) {
  298. return;
  299. }
  300. //
  301. // check for DOS (x:...) and UNC (\\...) full paths
  302. //
  303. if (pwszFilePath[dwBegin + 1] == L':' || (pwszFilePath[dwBegin] == L'\\' && pwszFilePath[dwBegin + 1] == L'\\')) {
  304. //
  305. // full path
  306. //
  307. return;
  308. }
  309. VLOG(VLOG_LEVEL_ERROR,
  310. VLOG_SECURITYCHECKS_BADARGUMENTS,
  311. "Called '%ls' with '%ls' specified. Use a full path to the file to ensure that you get the executable you want, and not a malicious exe with the same name.",
  312. pwszCaller,
  313. pwszFilePath);
  314. }
  315. BOOL
  316. APIHOOK(CreateProcessA)(
  317. LPCSTR lpApplicationName,
  318. LPSTR lpCommandLine,
  319. LPSECURITY_ATTRIBUTES lpProcessAttributes,
  320. LPSECURITY_ATTRIBUTES lpThreadAttributes,
  321. BOOL bInheritHandles,
  322. DWORD dwCreationFlags,
  323. LPVOID lpEnvironment,
  324. LPCSTR lpCurrentDirectory,
  325. LPSTARTUPINFOA lpStartupInfo,
  326. LPPROCESS_INFORMATION lpProcessInformation
  327. )
  328. {
  329. LPWSTR pwszApplicationName = ToUnicode(lpApplicationName);
  330. LPWSTR pwszCommandLine = ToUnicode(lpCommandLine);
  331. CheckCreateProcess(pwszApplicationName, pwszCommandLine, L"CreateProcess");
  332. if (pwszApplicationName) {
  333. CheckForNoPathInFileName(pwszApplicationName, L"CreateProcess");
  334. CheckSecurityAttributes(lpProcessAttributes, L"CreateProcess", L"lpProcessAttributes", pwszApplicationName);
  335. CheckSecurityAttributes(lpThreadAttributes, L"CreateProcess", L"lpThreadAttributes", pwszApplicationName);
  336. } else {
  337. CheckForNoPathInFileName(pwszCommandLine, L"CreateProcess");
  338. CheckSecurityAttributes(lpProcessAttributes, L"CreateProcess", L"lpProcessAttributes", pwszCommandLine);
  339. CheckSecurityAttributes(lpThreadAttributes, L"CreateProcess", L"lpThreadAttributes", pwszCommandLine);
  340. }
  341. if (pwszApplicationName) {
  342. free(pwszApplicationName);
  343. pwszApplicationName = NULL;
  344. }
  345. if (pwszCommandLine) {
  346. free(pwszCommandLine);
  347. pwszCommandLine = NULL;
  348. }
  349. return ORIGINAL_API(CreateProcessA)(lpApplicationName,
  350. lpCommandLine,
  351. lpProcessAttributes,
  352. lpThreadAttributes,
  353. bInheritHandles,
  354. dwCreationFlags,
  355. lpEnvironment,
  356. lpCurrentDirectory,
  357. lpStartupInfo,
  358. lpProcessInformation);
  359. }
  360. BOOL
  361. APIHOOK(CreateProcessW)(
  362. LPCWSTR lpApplicationName,
  363. LPWSTR lpCommandLine,
  364. LPSECURITY_ATTRIBUTES lpProcessAttributes,
  365. LPSECURITY_ATTRIBUTES lpThreadAttributes,
  366. BOOL bInheritHandles,
  367. DWORD dwCreationFlags,
  368. LPVOID lpEnvironment,
  369. LPWSTR lpCurrentDirectory,
  370. LPSTARTUPINFOW lpStartupInfo,
  371. LPPROCESS_INFORMATION lpProcessInformation
  372. )
  373. {
  374. CheckCreateProcess(lpApplicationName, lpCommandLine, L"CreateProcess");
  375. if (lpApplicationName) {
  376. CheckForNoPathInFileName(lpApplicationName, L"CreateProcess");
  377. CheckSecurityAttributes(lpProcessAttributes, L"CreateProcess", L"lpProcessAttributes", lpApplicationName);
  378. CheckSecurityAttributes(lpThreadAttributes, L"CreateProcess", L"lpThreadAttributes", lpApplicationName);
  379. } else {
  380. CheckForNoPathInFileName(lpCommandLine, L"CreateProcess");
  381. CheckSecurityAttributes(lpProcessAttributes, L"CreateProcess", L"lpProcessAttributes", lpCommandLine);
  382. CheckSecurityAttributes(lpThreadAttributes, L"CreateProcess", L"lpThreadAttributes", lpCommandLine);
  383. }
  384. return ORIGINAL_API(CreateProcessW)(lpApplicationName,
  385. lpCommandLine,
  386. lpProcessAttributes,
  387. lpThreadAttributes,
  388. bInheritHandles,
  389. dwCreationFlags,
  390. lpEnvironment,
  391. lpCurrentDirectory,
  392. lpStartupInfo,
  393. lpProcessInformation);
  394. }
  395. BOOL
  396. APIHOOK(CreateProcessAsUserA)(
  397. HANDLE hToken,
  398. LPCSTR lpApplicationName,
  399. LPSTR lpCommandLine,
  400. LPSECURITY_ATTRIBUTES lpProcessAttributes,
  401. LPSECURITY_ATTRIBUTES lpThreadAttributes,
  402. BOOL bInheritHandles,
  403. DWORD dwCreationFlags,
  404. LPVOID lpEnvironment,
  405. LPCSTR lpCurrentDirectory,
  406. LPSTARTUPINFOA lpStartupInfo,
  407. LPPROCESS_INFORMATION lpProcessInformation
  408. )
  409. {
  410. LPWSTR pwszApplicationName = ToUnicode(lpApplicationName);
  411. LPWSTR pwszCommandLine = ToUnicode(lpCommandLine);
  412. CheckCreateProcess(pwszApplicationName, pwszCommandLine, L"CreateProcessAsUser");
  413. if (pwszApplicationName) {
  414. CheckForNoPathInFileName(pwszApplicationName, L"CreateProcessAsUser");
  415. CheckSecurityAttributes(lpProcessAttributes, L"CreateProcessAsUser", L"lpProcessAttributes", pwszApplicationName);
  416. CheckSecurityAttributes(lpThreadAttributes, L"CreateProcessAsUser", L"lpThreadAttributes", pwszApplicationName);
  417. } else {
  418. CheckForNoPathInFileName(pwszCommandLine, L"CreateProcessAsUser");
  419. CheckSecurityAttributes(lpProcessAttributes, L"CreateProcessAsUser", L"lpProcessAttributes", pwszCommandLine);
  420. CheckSecurityAttributes(lpThreadAttributes, L"CreateProcessAsUser", L"lpThreadAttributes", pwszCommandLine);
  421. }
  422. if (pwszApplicationName) {
  423. free(pwszApplicationName);
  424. pwszApplicationName = NULL;
  425. }
  426. if (pwszCommandLine) {
  427. free(pwszCommandLine);
  428. pwszCommandLine = NULL;
  429. }
  430. return ORIGINAL_API(CreateProcessAsUserA)(hToken,
  431. lpApplicationName,
  432. lpCommandLine,
  433. lpProcessAttributes,
  434. lpThreadAttributes,
  435. bInheritHandles,
  436. dwCreationFlags,
  437. lpEnvironment,
  438. lpCurrentDirectory,
  439. lpStartupInfo,
  440. lpProcessInformation);
  441. }
  442. BOOL
  443. APIHOOK(CreateProcessAsUserW)(
  444. HANDLE hToken,
  445. LPCWSTR lpApplicationName,
  446. LPWSTR lpCommandLine,
  447. LPSECURITY_ATTRIBUTES lpProcessAttributes,
  448. LPSECURITY_ATTRIBUTES lpThreadAttributes,
  449. BOOL bInheritHandles,
  450. DWORD dwCreationFlags,
  451. LPVOID lpEnvironment,
  452. LPWSTR lpCurrentDirectory,
  453. LPSTARTUPINFOW lpStartupInfo,
  454. LPPROCESS_INFORMATION lpProcessInformation
  455. )
  456. {
  457. CheckCreateProcess(lpApplicationName, lpCommandLine, L"CreateProcessAsUser");
  458. if (lpApplicationName) {
  459. CheckForNoPathInFileName(lpApplicationName, L"CreateProcessAsUser");
  460. CheckSecurityAttributes(lpProcessAttributes, L"CreateProcessAsUser", L"lpProcessAttributes", lpApplicationName);
  461. CheckSecurityAttributes(lpThreadAttributes, L"CreateProcessAsUser", L"lpThreadAttributes", lpApplicationName);
  462. } else {
  463. CheckForNoPathInFileName(lpCommandLine, L"CreateProcessAsUser");
  464. CheckSecurityAttributes(lpProcessAttributes, L"CreateProcessAsUser", L"lpProcessAttributes", lpCommandLine);
  465. CheckSecurityAttributes(lpThreadAttributes, L"CreateProcessAsUser", L"lpThreadAttributes", lpCommandLine);
  466. }
  467. return ORIGINAL_API(CreateProcessAsUserW)(hToken,
  468. lpApplicationName,
  469. lpCommandLine,
  470. lpProcessAttributes,
  471. lpThreadAttributes,
  472. bInheritHandles,
  473. dwCreationFlags,
  474. lpEnvironment,
  475. lpCurrentDirectory,
  476. lpStartupInfo,
  477. lpProcessInformation);
  478. }
  479. UINT
  480. APIHOOK(WinExec)(
  481. LPCSTR lpCmdLine,
  482. UINT uCmdShow
  483. )
  484. {
  485. LPWSTR pwszCmdLine = ToUnicode(lpCmdLine);
  486. VLOG(VLOG_LEVEL_ERROR, VLOG_SECURITYCHECKS_WINEXEC, "Called WinExec.");
  487. CheckForNoPathInFileName(pwszCmdLine, L"WinExec");
  488. CheckCreateProcess(NULL, pwszCmdLine, L"WinExec");
  489. if (pwszCmdLine) {
  490. free(pwszCmdLine);
  491. pwszCmdLine = NULL;
  492. }
  493. return ORIGINAL_API(WinExec)(lpCmdLine, uCmdShow);
  494. }
  495. HANDLE
  496. APIHOOK(CreateFileA)(
  497. LPCSTR lpFileName, // file name
  498. DWORD dwDesiredAccess, // access mode
  499. DWORD dwShareMode, // share mode
  500. LPSECURITY_ATTRIBUTES lpSecurityAttributes, // SD
  501. DWORD dwCreationDisposition, // how to create
  502. DWORD dwFlagsAndAttributes, // file attributes
  503. HANDLE hTemplateFile // handle to template file
  504. )
  505. {
  506. LPWSTR pwszName = ToUnicode(lpFileName);
  507. CheckSecurityAttributes(lpSecurityAttributes, L"CreateFile", L"lpSecurityAttributes", pwszName);
  508. if (pwszName) {
  509. free(pwszName);
  510. pwszName = NULL;
  511. }
  512. return ORIGINAL_API(CreateFileA)(lpFileName,
  513. dwDesiredAccess,
  514. dwShareMode,
  515. lpSecurityAttributes,
  516. dwCreationDisposition,
  517. dwFlagsAndAttributes,
  518. hTemplateFile);
  519. }
  520. HANDLE
  521. APIHOOK(CreateFileW)(
  522. LPCWSTR lpFileName, // file name
  523. DWORD dwDesiredAccess, // access mode
  524. DWORD dwShareMode, // share mode
  525. LPSECURITY_ATTRIBUTES lpSecurityAttributes, // SD
  526. DWORD dwCreationDisposition, // how to create
  527. DWORD dwFlagsAndAttributes, // file attributes
  528. HANDLE hTemplateFile // handle to template file
  529. )
  530. {
  531. CheckSecurityAttributes(lpSecurityAttributes, L"CreateFile", L"lpSecurityAttributes", lpFileName);
  532. return ORIGINAL_API(CreateFileW)(lpFileName,
  533. dwDesiredAccess,
  534. dwShareMode,
  535. lpSecurityAttributes,
  536. dwCreationDisposition,
  537. dwFlagsAndAttributes,
  538. hTemplateFile);
  539. }
  540. HDESK
  541. APIHOOK(CreateDesktopA)(
  542. LPCSTR lpszDesktop, // name of new desktop
  543. LPCSTR lpszDevice, // reserved; must be NULL
  544. LPDEVMODEA pDevmode, // reserved; must be NULL
  545. DWORD dwFlags, // desktop interaction
  546. ACCESS_MASK dwDesiredAccess, // access of returned handle
  547. LPSECURITY_ATTRIBUTES lpsa // security attributes
  548. )
  549. {
  550. LPWSTR pwszName = ToUnicode(lpszDesktop);
  551. CheckSecurityAttributes(lpsa, L"CreateDesktop", L"lpsa", pwszName);
  552. if (pwszName) {
  553. free(pwszName);
  554. pwszName = NULL;
  555. }
  556. return ORIGINAL_API(CreateDesktopA)(lpszDesktop,
  557. lpszDevice,
  558. pDevmode,
  559. dwFlags,
  560. dwDesiredAccess,
  561. lpsa);
  562. }
  563. HDESK
  564. APIHOOK(CreateDesktopW)(
  565. LPCWSTR lpszDesktop, // name of new desktop
  566. LPCWSTR lpszDevice, // reserved; must be NULL
  567. LPDEVMODEW pDevmode, // reserved; must be NULL
  568. DWORD dwFlags, // desktop interaction
  569. ACCESS_MASK dwDesiredAccess, // access of returned handle
  570. LPSECURITY_ATTRIBUTES lpsa // security attributes
  571. )
  572. {
  573. CheckSecurityAttributes(lpsa, L"CreateDesktop", L"lpsa", lpszDesktop);
  574. return ORIGINAL_API(CreateDesktopW)(lpszDesktop,
  575. lpszDevice,
  576. pDevmode,
  577. dwFlags,
  578. dwDesiredAccess,
  579. lpsa);
  580. }
  581. HWINSTA
  582. APIHOOK(CreateWindowStationA)(
  583. LPSTR lpwinsta, // new window station name
  584. DWORD dwReserved, // reserved; must be zero
  585. ACCESS_MASK dwDesiredAccess, // requested access
  586. LPSECURITY_ATTRIBUTES lpsa // security attributes
  587. )
  588. {
  589. LPWSTR pwszName = ToUnicode(lpwinsta);
  590. CheckSecurityAttributes(lpsa, L"CreateWindowStation", L"lpsa", pwszName);
  591. if (pwszName) {
  592. free(pwszName);
  593. pwszName = NULL;
  594. }
  595. return ORIGINAL_API(CreateWindowStationA)(lpwinsta,
  596. dwReserved,
  597. dwDesiredAccess,
  598. lpsa);
  599. }
  600. HWINSTA
  601. APIHOOK(CreateWindowStationW)(
  602. LPWSTR lpwinsta, // new window station name
  603. DWORD dwReserved, // reserved; must be zero
  604. ACCESS_MASK dwDesiredAccess, // requested access
  605. LPSECURITY_ATTRIBUTES lpsa // security attributes
  606. )
  607. {
  608. CheckSecurityAttributes(lpsa, L"CreateWindowStation", L"lpsa", lpwinsta);
  609. return ORIGINAL_API(CreateWindowStationW)(lpwinsta,
  610. dwReserved,
  611. dwDesiredAccess,
  612. lpsa);
  613. }
  614. LONG
  615. APIHOOK(RegCreateKeyExA)(
  616. HKEY hKey,
  617. LPCSTR lpSubKey,
  618. DWORD Reserved,
  619. LPSTR lpClass,
  620. DWORD dwOptions,
  621. REGSAM samDesired,
  622. LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  623. PHKEY phkResult,
  624. LPDWORD lpdwDisposition
  625. )
  626. {
  627. LPWSTR pwszName = ToUnicode(lpSubKey);
  628. CheckSecurityAttributes(lpSecurityAttributes, L"RegCreateKeyEx", L"lpSecurityAttributes", pwszName);
  629. if (pwszName) {
  630. free(pwszName);
  631. pwszName = NULL;
  632. }
  633. return ORIGINAL_API(RegCreateKeyExA)(hKey,
  634. lpSubKey,
  635. Reserved,
  636. lpClass,
  637. dwOptions,
  638. samDesired,
  639. lpSecurityAttributes,
  640. phkResult,
  641. lpdwDisposition);
  642. }
  643. LONG
  644. APIHOOK(RegCreateKeyExW)(
  645. HKEY hKey,
  646. LPCWSTR lpSubKey,
  647. DWORD Reserved,
  648. LPWSTR lpClass,
  649. DWORD dwOptions,
  650. REGSAM samDesired,
  651. LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  652. PHKEY phkResult,
  653. LPDWORD lpdwDisposition
  654. )
  655. {
  656. CheckSecurityAttributes(lpSecurityAttributes, L"RegCreateKeyEx", L"lpSecurityAttributes", lpSubKey);
  657. return ORIGINAL_API(RegCreateKeyExW)(hKey,
  658. lpSubKey,
  659. Reserved,
  660. lpClass,
  661. dwOptions,
  662. samDesired,
  663. lpSecurityAttributes,
  664. phkResult,
  665. lpdwDisposition);
  666. }
  667. LONG
  668. APIHOOK(RegSaveKeyA)(
  669. HKEY hKey, // handle to key
  670. LPCSTR lpFile, // data file
  671. LPSECURITY_ATTRIBUTES lpSecurityAttributes // SD
  672. )
  673. {
  674. LPWSTR pwszName = ToUnicode(lpFile);
  675. CheckSecurityAttributes(lpSecurityAttributes, L"RegSaveKey", L"lpSecurityAttributes", pwszName);
  676. if (pwszName) {
  677. free(pwszName);
  678. pwszName = NULL;
  679. }
  680. return ORIGINAL_API(RegSaveKeyA)(hKey,
  681. lpFile,
  682. lpSecurityAttributes);
  683. }
  684. LONG
  685. APIHOOK(RegSaveKeyW)(
  686. HKEY hKey, // handle to key
  687. LPCWSTR lpFile, // data file
  688. LPSECURITY_ATTRIBUTES lpSecurityAttributes // SD
  689. )
  690. {
  691. CheckSecurityAttributes(lpSecurityAttributes, L"RegSaveKey", L"lpSecurityAttributes", lpFile);
  692. return ORIGINAL_API(RegSaveKeyW)(hKey,
  693. lpFile,
  694. lpSecurityAttributes);
  695. }
  696. LONG
  697. APIHOOK(RegSaveKeyExA)(
  698. HKEY hKey, // handle to key
  699. LPCSTR lpFile, // data file
  700. LPSECURITY_ATTRIBUTES lpSecurityAttributes, // SD
  701. DWORD Flags
  702. )
  703. {
  704. LPWSTR pwszName = ToUnicode(lpFile);
  705. CheckSecurityAttributes(lpSecurityAttributes, L"RegSaveKeyEx", L"lpSecurityAttributes", pwszName);
  706. if (pwszName) {
  707. free(pwszName);
  708. pwszName = NULL;
  709. }
  710. return ORIGINAL_API(RegSaveKeyExA)(hKey,
  711. lpFile,
  712. lpSecurityAttributes,
  713. Flags);
  714. }
  715. LONG
  716. APIHOOK(RegSaveKeyExW)(
  717. HKEY hKey, // handle to key
  718. LPCWSTR lpFile, // data file
  719. LPSECURITY_ATTRIBUTES lpSecurityAttributes, // SD
  720. DWORD Flags
  721. )
  722. {
  723. CheckSecurityAttributes(lpSecurityAttributes, L"RegSaveKeyEx", L"lpSecurityAttributes", lpFile);
  724. return ORIGINAL_API(RegSaveKeyExW)(hKey,
  725. lpFile,
  726. lpSecurityAttributes,
  727. Flags);
  728. }
  729. HANDLE
  730. APIHOOK(CreateFileMappingA)(
  731. HANDLE hFile,
  732. LPSECURITY_ATTRIBUTES lpAttributes,
  733. DWORD flProtect,
  734. DWORD dwMaximumSizeHigh,
  735. DWORD dwMaximumSizeLow,
  736. LPCSTR lpName
  737. )
  738. {
  739. LPWSTR pwszName = ToUnicode(lpName);
  740. CheckSecurityAttributes(lpAttributes, L"CreateFileMapping", L"lpAttributes", pwszName);
  741. if (pwszName) {
  742. free(pwszName);
  743. pwszName = NULL;
  744. }
  745. return ORIGINAL_API(CreateFileMappingA)(hFile,
  746. lpAttributes,
  747. flProtect,
  748. dwMaximumSizeHigh,
  749. dwMaximumSizeLow,
  750. lpName);
  751. }
  752. HANDLE
  753. APIHOOK(CreateFileMappingW)(
  754. HANDLE hFile,
  755. LPSECURITY_ATTRIBUTES lpAttributes,
  756. DWORD flProtect,
  757. DWORD dwMaximumSizeHigh,
  758. DWORD dwMaximumSizeLow,
  759. LPCWSTR lpName
  760. )
  761. {
  762. CheckSecurityAttributes(lpAttributes, L"CreateFileMapping", L"lpAttributes", lpName);
  763. return ORIGINAL_API(CreateFileMappingW)(hFile,
  764. lpAttributes,
  765. flProtect,
  766. dwMaximumSizeHigh,
  767. dwMaximumSizeLow,
  768. lpName);
  769. }
  770. HANDLE
  771. APIHOOK(CreateJobObjectA)(
  772. LPSECURITY_ATTRIBUTES lpJobAttributes, // SD
  773. LPCSTR lpName // job name
  774. )
  775. {
  776. LPWSTR pwszName = ToUnicode(lpName);
  777. CheckSecurityAttributes(lpJobAttributes, L"CreateJobObject", L"lpJobAttributes", pwszName);
  778. if (pwszName) {
  779. free(pwszName);
  780. pwszName = NULL;
  781. }
  782. return ORIGINAL_API(CreateJobObjectA)(lpJobAttributes,
  783. lpName);
  784. }
  785. HANDLE
  786. APIHOOK(CreateJobObjectW)(
  787. LPSECURITY_ATTRIBUTES lpJobAttributes, // SD
  788. LPCWSTR lpName // job name
  789. )
  790. {
  791. CheckSecurityAttributes(lpJobAttributes, L"CreateJobObject", L"lpJobAttributes", lpName);
  792. return ORIGINAL_API(CreateJobObjectW)(lpJobAttributes,
  793. lpName);
  794. }
  795. HANDLE
  796. APIHOOK(CreateThread)(
  797. LPSECURITY_ATTRIBUTES lpThreadAttributes, // SD
  798. SIZE_T dwStackSize, // initial stack size
  799. LPTHREAD_START_ROUTINE lpStartAddress, // thread function
  800. LPVOID lpParameter, // thread argument
  801. DWORD dwCreationFlags, // creation option
  802. LPDWORD lpThreadId // thread identifier
  803. )
  804. {
  805. CheckSecurityAttributes(lpThreadAttributes, L"CreateThread", L"lpThreadAttributes", L"Unnamed thread");
  806. return ORIGINAL_API(CreateThread)(lpThreadAttributes,
  807. (DWORD)dwStackSize,
  808. lpStartAddress,
  809. lpParameter,
  810. dwCreationFlags,
  811. lpThreadId);
  812. }
  813. HANDLE
  814. APIHOOK(CreateRemoteThread)(
  815. HANDLE hProcess, // handle to process
  816. LPSECURITY_ATTRIBUTES lpThreadAttributes, // SD
  817. SIZE_T dwStackSize, // initial stack size
  818. LPTHREAD_START_ROUTINE lpStartAddress, // thread function
  819. LPVOID lpParameter, // thread argument
  820. DWORD dwCreationFlags, // creation option
  821. LPDWORD lpThreadId // thread identifier
  822. )
  823. {
  824. CheckSecurityAttributes(lpThreadAttributes, L"CreateRemoteThread", L"lpThreadAttributes", L"Unnamed thread");
  825. return ORIGINAL_API(CreateRemoteThread)(hProcess,
  826. lpThreadAttributes,
  827. dwStackSize,
  828. lpStartAddress,
  829. lpParameter,
  830. dwCreationFlags,
  831. lpThreadId);
  832. }
  833. BOOL
  834. APIHOOK(CreateDirectoryA)(
  835. LPCSTR lpPathName, // directory name
  836. LPSECURITY_ATTRIBUTES lpSecurityAttributes // SD
  837. )
  838. {
  839. LPWSTR pwszName = ToUnicode(lpPathName);
  840. CheckSecurityAttributes(lpSecurityAttributes, L"CreateDirectory", L"lpSecurityAttributes", pwszName);
  841. if (pwszName) {
  842. free(pwszName);
  843. pwszName = NULL;
  844. }
  845. return ORIGINAL_API(CreateDirectoryA)(lpPathName,
  846. lpSecurityAttributes);
  847. }
  848. BOOL
  849. APIHOOK(CreateDirectoryW)(
  850. LPCWSTR lpPathName, // directory name
  851. LPSECURITY_ATTRIBUTES lpSecurityAttributes // SD
  852. )
  853. {
  854. CheckSecurityAttributes(lpSecurityAttributes, L"CreateDirectory", L"lpSecurityAttributes", lpPathName);
  855. return ORIGINAL_API(CreateDirectoryW)(lpPathName,
  856. lpSecurityAttributes);
  857. }
  858. BOOL
  859. APIHOOK(CreateDirectoryExA)(
  860. LPCSTR lpTemplateDirectory, // template directory
  861. LPCSTR lpNewDirectory, // directory name
  862. LPSECURITY_ATTRIBUTES lpSecurityAttributes // SD
  863. )
  864. {
  865. LPWSTR pwszName = ToUnicode(lpNewDirectory);
  866. CheckSecurityAttributes(lpSecurityAttributes, L"CreateDirectoryEx", L"lpSecurityAttributes", pwszName);
  867. if (pwszName) {
  868. free(pwszName);
  869. pwszName = NULL;
  870. }
  871. return ORIGINAL_API(CreateDirectoryExA)(lpTemplateDirectory,
  872. lpNewDirectory,
  873. lpSecurityAttributes);
  874. }
  875. BOOL
  876. APIHOOK(CreateDirectoryExW)(
  877. LPCWSTR lpTemplateDirectory, // template directory
  878. LPCWSTR lpNewDirectory, // directory name
  879. LPSECURITY_ATTRIBUTES lpSecurityAttributes // SD
  880. )
  881. {
  882. CheckSecurityAttributes(lpSecurityAttributes, L"CreateDirectoryEx", L"lpSecurityAttributes", lpNewDirectory);
  883. return ORIGINAL_API(CreateDirectoryExW)(lpTemplateDirectory,
  884. lpNewDirectory,
  885. lpSecurityAttributes);
  886. }
  887. BOOL
  888. APIHOOK(CreateHardLinkA)(
  889. LPCSTR lpFileName, // link name name
  890. LPCSTR lpExistingFileName, // target file name
  891. LPSECURITY_ATTRIBUTES lpSecurityAttributes
  892. )
  893. {
  894. LPWSTR pwszName = ToUnicode(lpFileName);
  895. CheckSecurityAttributes(lpSecurityAttributes, L"CreateHardLink", L"lpSecurityAttributes", pwszName);
  896. if (pwszName) {
  897. free(pwszName);
  898. pwszName = NULL;
  899. }
  900. return ORIGINAL_API(CreateHardLinkA)(lpFileName,
  901. lpExistingFileName,
  902. lpSecurityAttributes);
  903. }
  904. BOOL
  905. APIHOOK(CreateHardLinkW)(
  906. LPCWSTR lpFileName, // link name name
  907. LPCWSTR lpExistingFileName, // target file name
  908. LPSECURITY_ATTRIBUTES lpSecurityAttributes
  909. )
  910. {
  911. CheckSecurityAttributes(lpSecurityAttributes, L"CreateHardLink", L"lpSecurityAttributes", lpFileName);
  912. return ORIGINAL_API(CreateHardLinkW)(lpFileName,
  913. lpExistingFileName,
  914. lpSecurityAttributes);
  915. }
  916. HANDLE
  917. APIHOOK(CreateMailslotA)(
  918. LPCSTR lpName, // mailslot name
  919. DWORD nMaxMessageSize, // maximum message size
  920. DWORD lReadTimeout, // read time-out interval
  921. LPSECURITY_ATTRIBUTES lpSecurityAttributes // inheritance option
  922. )
  923. {
  924. LPWSTR pwszName = ToUnicode(lpName);
  925. CheckSecurityAttributes(lpSecurityAttributes, L"CreateMailslot", L"lpSecurityAttributes", pwszName);
  926. if (pwszName) {
  927. free(pwszName);
  928. pwszName = NULL;
  929. }
  930. return ORIGINAL_API(CreateMailslotA)(lpName,
  931. nMaxMessageSize,
  932. lReadTimeout,
  933. lpSecurityAttributes);
  934. }
  935. HANDLE
  936. APIHOOK(CreateMailslotW)(
  937. LPCWSTR lpName, // mailslot name
  938. DWORD nMaxMessageSize, // maximum message size
  939. DWORD lReadTimeout, // read time-out interval
  940. LPSECURITY_ATTRIBUTES lpSecurityAttributes // inheritance option
  941. )
  942. {
  943. CheckSecurityAttributes(lpSecurityAttributes, L"CreateMailslot", L"lpSecurityAttributes", lpName);
  944. return ORIGINAL_API(CreateMailslotW)(lpName,
  945. nMaxMessageSize,
  946. lReadTimeout,
  947. lpSecurityAttributes);
  948. }
  949. HANDLE
  950. APIHOOK(CreateNamedPipeA)(
  951. LPCSTR lpName, // pipe name
  952. DWORD dwOpenMode, // pipe open mode
  953. DWORD dwPipeMode, // pipe-specific modes
  954. DWORD nMaxInstances, // maximum number of instances
  955. DWORD nOutBufferSize, // output buffer size
  956. DWORD nInBufferSize, // input buffer size
  957. DWORD nDefaultTimeOut, // time-out interval
  958. LPSECURITY_ATTRIBUTES lpSecurityAttributes // SD
  959. )
  960. {
  961. LPWSTR pwszName = ToUnicode(lpName);
  962. CheckSecurityAttributes(lpSecurityAttributes, L"CreateNamedPipe", L"lpSecurityAttributes", pwszName);
  963. if (pwszName) {
  964. free(pwszName);
  965. pwszName = NULL;
  966. }
  967. return ORIGINAL_API(CreateNamedPipeA)(lpName,
  968. dwOpenMode,
  969. dwPipeMode,
  970. nMaxInstances,
  971. nOutBufferSize,
  972. nInBufferSize,
  973. nDefaultTimeOut,
  974. lpSecurityAttributes);
  975. }
  976. HANDLE
  977. APIHOOK(CreateNamedPipeW)(
  978. LPCWSTR lpName, // pipe name
  979. DWORD dwOpenMode, // pipe open mode
  980. DWORD dwPipeMode, // pipe-specific modes
  981. DWORD nMaxInstances, // maximum number of instances
  982. DWORD nOutBufferSize, // output buffer size
  983. DWORD nInBufferSize, // input buffer size
  984. DWORD nDefaultTimeOut, // time-out interval
  985. LPSECURITY_ATTRIBUTES lpSecurityAttributes // SD
  986. )
  987. {
  988. CheckSecurityAttributes(lpSecurityAttributes, L"CreateNamedPipe", L"lpSecurityAttributes", lpName);
  989. return ORIGINAL_API(CreateNamedPipeW)(lpName,
  990. dwOpenMode,
  991. dwPipeMode,
  992. nMaxInstances,
  993. nOutBufferSize,
  994. nInBufferSize,
  995. nDefaultTimeOut,
  996. lpSecurityAttributes);
  997. }
  998. BOOL
  999. APIHOOK(CreatePipe)(
  1000. PHANDLE hReadPipe, // read handle
  1001. PHANDLE hWritePipe, // write handle
  1002. LPSECURITY_ATTRIBUTES lpPipeAttributes, // security attributes
  1003. DWORD nSize // pipe size
  1004. )
  1005. {
  1006. CheckSecurityAttributes(lpPipeAttributes, L"CreatePipe", L"lpPipeAttributes", L"Unnamed pipe");
  1007. return ORIGINAL_API(CreatePipe)(hReadPipe,
  1008. hWritePipe,
  1009. lpPipeAttributes,
  1010. nSize);
  1011. }
  1012. HANDLE
  1013. APIHOOK(CreateMutexA)(
  1014. LPSECURITY_ATTRIBUTES lpMutexAttributes, // SD
  1015. BOOL bInitialOwner, // initial owner
  1016. LPCSTR lpName // object name
  1017. )
  1018. {
  1019. LPWSTR pwszName = ToUnicode(lpName);
  1020. CheckSecurityAttributes(lpMutexAttributes, L"CreateMutex", L"lpMutexAttributes", pwszName);
  1021. if (pwszName) {
  1022. free(pwszName);
  1023. pwszName = NULL;
  1024. }
  1025. return ORIGINAL_API(CreateMutexA)(lpMutexAttributes,
  1026. bInitialOwner,
  1027. lpName);
  1028. }
  1029. HANDLE
  1030. APIHOOK(CreateMutexW)(
  1031. LPSECURITY_ATTRIBUTES lpMutexAttributes, // SD
  1032. BOOL bInitialOwner, // initial owner
  1033. LPCWSTR lpName // object name
  1034. )
  1035. {
  1036. CheckSecurityAttributes(lpMutexAttributes, L"CreateMutex", L"lpMutexAttributes", lpName);
  1037. return ORIGINAL_API(CreateMutexW)(lpMutexAttributes,
  1038. bInitialOwner,
  1039. lpName);
  1040. }
  1041. HANDLE
  1042. APIHOOK(CreateSemaphoreA)(
  1043. LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, // SD
  1044. LONG lInitialCount, // initial count
  1045. LONG lMaximumCount, // maximum count
  1046. LPCSTR lpName // object name
  1047. )
  1048. {
  1049. LPWSTR pwszName = ToUnicode(lpName);
  1050. CheckSecurityAttributes(lpSemaphoreAttributes, L"CreateSemaphore", L"lpSemaphoreAttributes", pwszName);
  1051. if (pwszName) {
  1052. free(pwszName);
  1053. pwszName = NULL;
  1054. }
  1055. return ORIGINAL_API(CreateSemaphoreA)(lpSemaphoreAttributes,
  1056. lInitialCount,
  1057. lMaximumCount,
  1058. lpName);
  1059. }
  1060. HANDLE
  1061. APIHOOK(CreateSemaphoreW)(
  1062. LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, // SD
  1063. LONG lInitialCount, // initial count
  1064. LONG lMaximumCount, // maximum count
  1065. LPCWSTR lpName // object name
  1066. )
  1067. {
  1068. CheckSecurityAttributes(lpSemaphoreAttributes, L"CreateSemaphore", L"lpSemaphoreAttributes", lpName);
  1069. return ORIGINAL_API(CreateSemaphoreW)(lpSemaphoreAttributes,
  1070. lInitialCount,
  1071. lMaximumCount,
  1072. lpName);
  1073. }
  1074. HANDLE
  1075. APIHOOK(CreateWaitableTimerA)(
  1076. IN LPSECURITY_ATTRIBUTES lpTimerAttributes,
  1077. IN BOOL bManualReset,
  1078. IN LPCSTR lpTimerName
  1079. )
  1080. {
  1081. LPWSTR pwszName = ToUnicode(lpTimerName);
  1082. CheckSecurityAttributes(lpTimerAttributes, L"CreateWaitableTimer", L"lpTimerAttributes", pwszName);
  1083. if (pwszName) {
  1084. free(pwszName);
  1085. pwszName = NULL;
  1086. }
  1087. return ORIGINAL_API(CreateWaitableTimerA)(lpTimerAttributes,
  1088. bManualReset,
  1089. lpTimerName);
  1090. }
  1091. HANDLE
  1092. APIHOOK(CreateWaitableTimerW)(
  1093. IN LPSECURITY_ATTRIBUTES lpTimerAttributes,
  1094. IN BOOL bManualReset,
  1095. IN LPCWSTR lpTimerName
  1096. )
  1097. {
  1098. CheckSecurityAttributes(lpTimerAttributes, L"CreateWaitableTimer", L"lpTimerAttributes", lpTimerName);
  1099. return ORIGINAL_API(CreateWaitableTimerW)(lpTimerAttributes,
  1100. bManualReset,
  1101. lpTimerName);
  1102. }
  1103. HANDLE
  1104. APIHOOK(CreateEventA)(
  1105. IN LPSECURITY_ATTRIBUTES lpEventAttributes,
  1106. IN BOOL bManualReset,
  1107. IN BOOL bInitialState,
  1108. IN LPCSTR lpName
  1109. )
  1110. {
  1111. LPWSTR pwszName = ToUnicode(lpName);
  1112. CheckSecurityAttributes(lpEventAttributes, L"CreateEvent", L"lpEventAttributes", pwszName);
  1113. if (pwszName) {
  1114. free(pwszName);
  1115. pwszName = NULL;
  1116. }
  1117. return ORIGINAL_API(CreateEventA)(lpEventAttributes,
  1118. bManualReset,
  1119. bInitialState,
  1120. lpName);
  1121. }
  1122. HANDLE
  1123. APIHOOK(CreateEventW)(
  1124. IN LPSECURITY_ATTRIBUTES lpEventAttributes,
  1125. IN BOOL bManualReset,
  1126. IN BOOL bInitialState,
  1127. IN LPCWSTR lpName
  1128. )
  1129. {
  1130. CheckSecurityAttributes(lpEventAttributes, L"CreateEvent", L"lpEventAttributes", lpName);
  1131. return ORIGINAL_API(CreateEventW)(lpEventAttributes,
  1132. bManualReset,
  1133. bInitialState,
  1134. lpName);
  1135. }
  1136. BOOL
  1137. APIHOOK(SetFileSecurityA) (
  1138. IN LPCSTR lpFileName,
  1139. IN SECURITY_INFORMATION SecurityInformation,
  1140. IN PSECURITY_DESCRIPTOR pSecurityDescriptor
  1141. )
  1142. {
  1143. if (SecurityInformation & DACL_SECURITY_INFORMATION) {
  1144. LPWSTR pwszName = ToUnicode(lpFileName);
  1145. CheckSecurityDescriptor(pSecurityDescriptor, L"SetFileSecurity", L"pSecurityDescriptor", pwszName);
  1146. if (pwszName) {
  1147. free(pwszName);
  1148. pwszName = NULL;
  1149. }
  1150. }
  1151. return ORIGINAL_API(SetFileSecurityA)(lpFileName,
  1152. SecurityInformation,
  1153. pSecurityDescriptor);
  1154. }
  1155. BOOL
  1156. APIHOOK(SetFileSecurityW) (
  1157. IN LPCWSTR lpFileName,
  1158. IN SECURITY_INFORMATION SecurityInformation,
  1159. IN PSECURITY_DESCRIPTOR pSecurityDescriptor
  1160. )
  1161. {
  1162. if (SecurityInformation & DACL_SECURITY_INFORMATION) {
  1163. CheckSecurityDescriptor(pSecurityDescriptor, L"SetFileSecurity", L"pSecurityDescriptor", lpFileName);
  1164. }
  1165. return ORIGINAL_API(SetFileSecurityW)(lpFileName,
  1166. SecurityInformation,
  1167. pSecurityDescriptor);
  1168. }
  1169. BOOL
  1170. APIHOOK(SetKernelObjectSecurity) (
  1171. IN HANDLE Handle,
  1172. IN SECURITY_INFORMATION SecurityInformation,
  1173. IN PSECURITY_DESCRIPTOR pSecurityDescriptor
  1174. )
  1175. {
  1176. if (SecurityInformation & DACL_SECURITY_INFORMATION) {
  1177. CheckSecurityDescriptor(pSecurityDescriptor, L"SetKernelObjectSecurity", L"pSecurityDescriptor", L"Unknown Kernel Object");
  1178. }
  1179. return ORIGINAL_API(SetKernelObjectSecurity)(Handle,
  1180. SecurityInformation,
  1181. pSecurityDescriptor);
  1182. }
  1183. DWORD
  1184. APIHOOK(SetNamedSecurityInfoA)(
  1185. IN LPSTR pObjectName,
  1186. IN SE_OBJECT_TYPE ObjectType,
  1187. IN SECURITY_INFORMATION SecurityInfo,
  1188. IN PSID psidOwner,
  1189. IN PSID psidGroup,
  1190. IN PACL pDacl,
  1191. IN PACL pSacl
  1192. )
  1193. {
  1194. if (SecurityInfo & DACL_SECURITY_INFORMATION) {
  1195. LPWSTR pwszName = ToUnicode(pObjectName);
  1196. CheckDacl(pDacl, L"SetNamedSecurityInfo", L"pDacl", pwszName);
  1197. if (pwszName) {
  1198. free(pwszName);
  1199. pwszName = NULL;
  1200. }
  1201. }
  1202. return ORIGINAL_API(SetNamedSecurityInfoA)(pObjectName,
  1203. ObjectType,
  1204. SecurityInfo,
  1205. psidOwner,
  1206. psidGroup,
  1207. pDacl,
  1208. pSacl);
  1209. }
  1210. DWORD
  1211. APIHOOK(SetNamedSecurityInfoW)(
  1212. IN LPWSTR pObjectName,
  1213. IN SE_OBJECT_TYPE ObjectType,
  1214. IN SECURITY_INFORMATION SecurityInfo,
  1215. IN PSID psidOwner,
  1216. IN PSID psidGroup,
  1217. IN PACL pDacl,
  1218. IN PACL pSacl
  1219. )
  1220. {
  1221. if (SecurityInfo & DACL_SECURITY_INFORMATION) {
  1222. CheckDacl(pDacl, L"SetNamedSecurityInfo", L"pDacl", pObjectName);
  1223. }
  1224. return ORIGINAL_API(SetNamedSecurityInfoW)(pObjectName,
  1225. ObjectType,
  1226. SecurityInfo,
  1227. psidOwner,
  1228. psidGroup,
  1229. pDacl,
  1230. pSacl);
  1231. }
  1232. DWORD
  1233. APIHOOK(SetSecurityInfo)(
  1234. IN HANDLE handle,
  1235. IN SE_OBJECT_TYPE ObjectType,
  1236. IN SECURITY_INFORMATION SecurityInfo,
  1237. IN PSID psidOwner,
  1238. IN PSID psidGroup,
  1239. IN PACL pDacl,
  1240. IN PACL pSacl
  1241. )
  1242. {
  1243. if (SecurityInfo & DACL_SECURITY_INFORMATION) {
  1244. CheckDacl(pDacl, L"SetSecurityInfo", L"pDacl", L"Unknown Object");
  1245. }
  1246. return ORIGINAL_API(SetSecurityInfo)(handle,
  1247. ObjectType,
  1248. SecurityInfo,
  1249. psidOwner,
  1250. psidGroup,
  1251. pDacl,
  1252. pSacl);
  1253. }
  1254. LONG
  1255. APIHOOK(RegSetKeySecurity) (
  1256. IN HKEY hKey,
  1257. IN SECURITY_INFORMATION SecurityInformation,
  1258. IN PSECURITY_DESCRIPTOR pSecurityDescriptor
  1259. )
  1260. {
  1261. if (SecurityInformation & DACL_SECURITY_INFORMATION) {
  1262. CheckSecurityDescriptor(pSecurityDescriptor, L"RegSetKeySecurity", L"pSecurityDescriptor", L"Unknown Key");
  1263. }
  1264. return ORIGINAL_API(RegSetKeySecurity)(hKey,
  1265. SecurityInformation,
  1266. pSecurityDescriptor);
  1267. }
  1268. BOOL
  1269. APIHOOK(SetUserObjectSecurity)(
  1270. IN HANDLE hObj,
  1271. IN PSECURITY_INFORMATION pSIRequested,
  1272. IN PSECURITY_DESCRIPTOR pSID)
  1273. {
  1274. if (*pSIRequested & DACL_SECURITY_INFORMATION) {
  1275. CheckSecurityDescriptor(pSID, L"SetUserObjectSecurity", L"pSID", L"Unknown Object");
  1276. }
  1277. return ORIGINAL_API(SetUserObjectSecurity)(hObj,
  1278. pSIRequested,
  1279. pSID);
  1280. }
  1281. BOOL
  1282. APIHOOK(SetServiceObjectSecurity)(
  1283. SC_HANDLE hService,
  1284. SECURITY_INFORMATION dwSecurityInformation,
  1285. PSECURITY_DESCRIPTOR lpSecurityDescriptor
  1286. )
  1287. {
  1288. if (dwSecurityInformation & DACL_SECURITY_INFORMATION) {
  1289. CheckSecurityDescriptor(lpSecurityDescriptor, L"SetServiceObjectSecurity", L"lpSecurityDescriptor", L"Unknown Service");
  1290. }
  1291. return ORIGINAL_API(SetServiceObjectSecurity)(hService,
  1292. dwSecurityInformation,
  1293. lpSecurityDescriptor);
  1294. }
  1295. DWORD
  1296. APIHOOK(SetNtmsObjectSecurity)(
  1297. HANDLE hSession,
  1298. LPNTMS_GUID lpObjectId,
  1299. DWORD dwType,
  1300. SECURITY_INFORMATION lpSecurityInformation,
  1301. PSECURITY_DESCRIPTOR lpSecurityDescriptor
  1302. )
  1303. {
  1304. if (lpSecurityInformation & DACL_SECURITY_INFORMATION) {
  1305. CheckSecurityDescriptor(lpSecurityDescriptor, L"SetNtmsObjectSecurity", L"lpSecurityDescriptor", L"Unknown Object");
  1306. }
  1307. return ORIGINAL_API(SetNtmsObjectSecurity)(hSession,
  1308. lpObjectId,
  1309. dwType,
  1310. lpSecurityInformation,
  1311. lpSecurityDescriptor);
  1312. }
  1313. LONG
  1314. APIHOOK(ClusterRegCreateKey)(
  1315. HKEY hKey,
  1316. LPCWSTR lpszSubKey,
  1317. DWORD dwOptions,
  1318. REGSAM samDesired,
  1319. LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  1320. PHKEY phkResult,
  1321. LPDWORD lpdwDisposition
  1322. )
  1323. {
  1324. CheckSecurityAttributes(lpSecurityAttributes, L"ClusterRegCreateKey", L"lpSecurityAttributes", lpszSubKey);
  1325. return ORIGINAL_API(ClusterRegCreateKey)(hKey,
  1326. lpszSubKey,
  1327. dwOptions,
  1328. samDesired,
  1329. lpSecurityAttributes,
  1330. phkResult,
  1331. lpdwDisposition);
  1332. }
  1333. LONG
  1334. APIHOOK(ClusterRegSetKeySecurity)(
  1335. HKEY hKey,
  1336. SECURITY_INFORMATION SecurityInformation,
  1337. PSECURITY_DESCRIPTOR pSecurityDescriptor
  1338. )
  1339. {
  1340. if (SecurityInformation & DACL_SECURITY_INFORMATION) {
  1341. CheckSecurityDescriptor(pSecurityDescriptor, L"ClusterRegSetKeySecurity", L"pSecurityDescriptor", L"Unknown Key");
  1342. }
  1343. return ORIGINAL_API(ClusterRegSetKeySecurity)(hKey,
  1344. SecurityInformation,
  1345. pSecurityDescriptor);
  1346. }
  1347. DWORD
  1348. APIHOOK(CreateNtmsMediaPoolA)(
  1349. HANDLE hSession,
  1350. LPCSTR lpPoolName,
  1351. LPNTMS_GUID lpMediaType,
  1352. DWORD dwAction,
  1353. LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  1354. LPNTMS_GUID lpPoolId
  1355. )
  1356. {
  1357. LPWSTR pwszName = ToUnicode(lpPoolName);
  1358. CheckSecurityAttributes(lpSecurityAttributes, L"CreateNtmsMediaPool", L"lpSecurityAttributes", pwszName);
  1359. if (pwszName) {
  1360. free(pwszName);
  1361. pwszName = NULL;
  1362. }
  1363. return ORIGINAL_API(CreateNtmsMediaPoolA)(hSession,
  1364. lpPoolName,
  1365. lpMediaType,
  1366. dwAction,
  1367. lpSecurityAttributes,
  1368. lpPoolId);
  1369. }
  1370. DWORD
  1371. APIHOOK(CreateNtmsMediaPoolW)(
  1372. HANDLE hSession,
  1373. LPCWSTR lpPoolName,
  1374. LPNTMS_GUID lpMediaType,
  1375. DWORD dwAction,
  1376. LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  1377. LPNTMS_GUID lpPoolId // OUT
  1378. )
  1379. {
  1380. CheckSecurityAttributes(lpSecurityAttributes, L"CreateNtmsMediaPool", L"lpSecurityAttributes", lpPoolName);
  1381. return ORIGINAL_API(CreateNtmsMediaPoolW)(hSession,
  1382. lpPoolName,
  1383. lpMediaType,
  1384. dwAction,
  1385. lpSecurityAttributes,
  1386. lpPoolId);
  1387. }
  1388. SHIM_INFO_BEGIN()
  1389. SHIM_INFO_DESCRIPTION(AVS_SECURITYCHECKS_DESC)
  1390. SHIM_INFO_FRIENDLY_NAME(AVS_SECURITYCHECKS_FRIENDLY)
  1391. SHIM_INFO_FLAGS(0)
  1392. SHIM_INFO_GROUPS(0)
  1393. SHIM_INFO_VERSION(2, 4)
  1394. SHIM_INFO_INCLUDE_EXCLUDE("I:*")
  1395. SHIM_INFO_END()
  1396. /*++
  1397. Register hooked functions.
  1398. --*/
  1399. HOOK_BEGIN
  1400. if (fdwReason == DLL_PROCESS_ATTACH) {
  1401. DWORD dwSize;
  1402. InitWorldSid();
  1403. dwSize = GetSystemWindowsDirectoryW(g_wszWinDir, ARRAYSIZE(g_wszWinDir));
  1404. if (dwSize == 0 || dwSize > ARRAYSIZE(g_wszWinDir)) {
  1405. g_wszWinDir[0] = 0;
  1406. }
  1407. g_dwWinDirLen = wcslen(g_wszWinDir);
  1408. }
  1409. DUMP_VERIFIER_LOG_ENTRY(VLOG_SECURITYCHECKS_BADARGUMENTS,
  1410. AVS_SECURITYCHECKS_BADARGUMENTS,
  1411. AVS_SECURITYCHECKS_BADARGUMENTS_R,
  1412. AVS_SECURITYCHECKS_BADARGUMENTS_URL)
  1413. DUMP_VERIFIER_LOG_ENTRY(VLOG_SECURITYCHECKS_WINEXEC,
  1414. AVS_SECURITYCHECKS_WINEXEC,
  1415. AVS_SECURITYCHECKS_WINEXEC_R,
  1416. AVS_SECURITYCHECKS_WINEXEC_URL)
  1417. DUMP_VERIFIER_LOG_ENTRY(VLOG_SECURITYCHECKS_NULL_DACL,
  1418. AVS_SECURITYCHECKS_NULL_DACL,
  1419. AVS_SECURITYCHECKS_NULL_DACL_R,
  1420. AVS_SECURITYCHECKS_NULL_DACL_URL)
  1421. DUMP_VERIFIER_LOG_ENTRY(VLOG_SECURITYCHECKS_WORLDWRITE_DACL,
  1422. AVS_SECURITYCHECKS_WORLDWRITE_DACL,
  1423. AVS_SECURITYCHECKS_WORLDWRITE_DACL_R,
  1424. AVS_SECURITYCHECKS_WORLDWRITE_DACL_URL)
  1425. APIHOOK_ENTRY(KERNEL32.DLL, CreateProcessA)
  1426. APIHOOK_ENTRY(KERNEL32.DLL, CreateProcessW)
  1427. APIHOOK_ENTRY(ADVAPI32.DLL, CreateProcessAsUserA)
  1428. APIHOOK_ENTRY(ADVAPI32.DLL, CreateProcessAsUserW)
  1429. APIHOOK_ENTRY(KERNEL32.DLL, WinExec)
  1430. APIHOOK_ENTRY(KERNEL32.DLL, CreateFileA)
  1431. APIHOOK_ENTRY(KERNEL32.DLL, CreateFileW)
  1432. APIHOOK_ENTRY(USER32.DLL, CreateDesktopA)
  1433. APIHOOK_ENTRY(USER32.DLL, CreateDesktopW)
  1434. APIHOOK_ENTRY(USER32.DLL, CreateWindowStationA)
  1435. APIHOOK_ENTRY(USER32.DLL, CreateWindowStationW)
  1436. APIHOOK_ENTRY(ADVAPI32.DLL, RegCreateKeyExA)
  1437. APIHOOK_ENTRY(ADVAPI32.DLL, RegCreateKeyExW)
  1438. APIHOOK_ENTRY(ADVAPI32.DLL, RegSaveKeyA)
  1439. APIHOOK_ENTRY(ADVAPI32.DLL, RegSaveKeyW)
  1440. APIHOOK_ENTRY(ADVAPI32.DLL, RegSaveKeyExA)
  1441. APIHOOK_ENTRY(ADVAPI32.DLL, RegSaveKeyExW)
  1442. APIHOOK_ENTRY(KERNEL32.DLL, CreateFileMappingA)
  1443. APIHOOK_ENTRY(KERNEL32.DLL, CreateFileMappingW)
  1444. APIHOOK_ENTRY(KERNEL32.DLL, CreateJobObjectA)
  1445. APIHOOK_ENTRY(KERNEL32.DLL, CreateJobObjectW)
  1446. APIHOOK_ENTRY(KERNEL32.DLL, CreateThread)
  1447. APIHOOK_ENTRY(KERNEL32.DLL, CreateRemoteThread)
  1448. APIHOOK_ENTRY(KERNEL32.DLL, CreateDirectoryA)
  1449. APIHOOK_ENTRY(KERNEL32.DLL, CreateDirectoryW)
  1450. APIHOOK_ENTRY(KERNEL32.DLL, CreateDirectoryExA)
  1451. APIHOOK_ENTRY(KERNEL32.DLL, CreateDirectoryExW)
  1452. APIHOOK_ENTRY(KERNEL32.DLL, CreateHardLinkA)
  1453. APIHOOK_ENTRY(KERNEL32.DLL, CreateHardLinkW)
  1454. APIHOOK_ENTRY(KERNEL32.DLL, CreateMailslotA)
  1455. APIHOOK_ENTRY(KERNEL32.DLL, CreateMailslotW)
  1456. APIHOOK_ENTRY(KERNEL32.DLL, CreateNamedPipeA)
  1457. APIHOOK_ENTRY(KERNEL32.DLL, CreateNamedPipeW)
  1458. APIHOOK_ENTRY(KERNEL32.DLL, CreatePipe)
  1459. APIHOOK_ENTRY(KERNEL32.DLL, CreateMutexA)
  1460. APIHOOK_ENTRY(KERNEL32.DLL, CreateMutexW)
  1461. APIHOOK_ENTRY(KERNEL32.DLL, CreateSemaphoreA)
  1462. APIHOOK_ENTRY(KERNEL32.DLL, CreateSemaphoreW)
  1463. APIHOOK_ENTRY(KERNEL32.DLL, CreateWaitableTimerA)
  1464. APIHOOK_ENTRY(KERNEL32.DLL, CreateWaitableTimerW)
  1465. APIHOOK_ENTRY(KERNEL32.DLL, CreateEventA)
  1466. APIHOOK_ENTRY(KERNEL32.DLL, CreateEventW)
  1467. APIHOOK_ENTRY(ADVAPI32.DLL, SetFileSecurityA)
  1468. APIHOOK_ENTRY(ADVAPI32.DLL, SetFileSecurityW)
  1469. APIHOOK_ENTRY(ADVAPI32.DLL, SetKernelObjectSecurity)
  1470. APIHOOK_ENTRY(ADVAPI32.DLL, SetNamedSecurityInfoA)
  1471. APIHOOK_ENTRY(ADVAPI32.DLL, SetNamedSecurityInfoW)
  1472. APIHOOK_ENTRY(ADVAPI32.DLL, SetSecurityInfo)
  1473. APIHOOK_ENTRY(ADVAPI32.DLL, RegSetKeySecurity)
  1474. APIHOOK_ENTRY(USER32.DLL, SetUserObjectSecurity)
  1475. APIHOOK_ENTRY(ADVAPI32.DLL, SetServiceObjectSecurity)
  1476. APIHOOK_ENTRY(NTMSAPI.DLL, SetNtmsObjectSecurity)
  1477. APIHOOK_ENTRY(CLUSAPI.DLL, ClusterRegCreateKey)
  1478. APIHOOK_ENTRY(CLUSAPI.DLL, ClusterRegSetKeySecurity)
  1479. APIHOOK_ENTRY(NTMSAPI.DLL, CreateNtmsMediaPoolA)
  1480. APIHOOK_ENTRY(NTMSAPI.DLL, CreateNtmsMediaPoolW)
  1481. HOOK_END
  1482. IMPLEMENT_SHIM_END