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.

876 lines
25 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. WindowsFileProtection.cpp
  5. Abstract:
  6. This AppVerifier shim hooks the file I/O APIs that could
  7. potentially change a file under Windows File Protection.
  8. When one of the files is accessed or modified, an event
  9. is written to the log.
  10. Notes:
  11. This is a general purpose shim.
  12. History:
  13. 06/25/2001 rparsons Created
  14. 11/26/2001 rparsons Remove unused local variables.
  15. Make SHFileOperation more efficent.
  16. --*/
  17. #include "precomp.h"
  18. #include "rtlutils.h"
  19. #include "sfc.h"
  20. IMPLEMENT_SHIM_BEGIN(WindowsFileProtection)
  21. #include "ShimHookMacro.h"
  22. //
  23. // verifier log entries
  24. //
  25. BEGIN_DEFINE_VERIFIER_LOG(WindowFileProtection)
  26. VERIFIER_LOG_ENTRY(VLOG_WFP_COPYFILE)
  27. VERIFIER_LOG_ENTRY(VLOG_WFP_MOVEFILE)
  28. VERIFIER_LOG_ENTRY(VLOG_WFP_DELETEFILE)
  29. VERIFIER_LOG_ENTRY(VLOG_WFP_REPLACEFILE)
  30. VERIFIER_LOG_ENTRY(VLOG_WFP_WRITEFILE)
  31. VERIFIER_LOG_ENTRY(VLOG_WFP_OPENFILE)
  32. VERIFIER_LOG_ENTRY(VLOG_WFP_SHFILEOP)
  33. END_DEFINE_VERIFIER_LOG(WindowFileProtection)
  34. INIT_VERIFIER_LOG(WindowFileProtection);
  35. APIHOOK_ENUM_BEGIN
  36. APIHOOK_ENUM_ENTRY(CreateFileA)
  37. APIHOOK_ENUM_ENTRY(CreateFileW)
  38. APIHOOK_ENUM_ENTRY(OpenFile)
  39. APIHOOK_ENUM_ENTRY(CopyFileA)
  40. APIHOOK_ENUM_ENTRY(CopyFileW)
  41. APIHOOK_ENUM_ENTRY(CopyFileExA)
  42. APIHOOK_ENUM_ENTRY(CopyFileExW)
  43. APIHOOK_ENUM_ENTRY(DeleteFileA)
  44. APIHOOK_ENUM_ENTRY(DeleteFileW)
  45. APIHOOK_ENUM_ENTRY(MoveFileA)
  46. APIHOOK_ENUM_ENTRY(MoveFileW)
  47. APIHOOK_ENUM_ENTRY(MoveFileExA)
  48. APIHOOK_ENUM_ENTRY(MoveFileExW)
  49. APIHOOK_ENUM_ENTRY(MoveFileWithProgressA)
  50. APIHOOK_ENUM_ENTRY(MoveFileWithProgressW)
  51. APIHOOK_ENUM_ENTRY(ReplaceFileA)
  52. APIHOOK_ENUM_ENTRY(ReplaceFileW)
  53. APIHOOK_ENUM_ENTRY(SHFileOperationA)
  54. APIHOOK_ENUM_ENTRY(SHFileOperationW)
  55. APIHOOK_ENUM_ENTRY(_lcreat)
  56. APIHOOK_ENUM_ENTRY(_lopen)
  57. APIHOOK_ENUM_ENTRY(NtCreateFile)
  58. APIHOOK_ENUM_ENTRY(NtOpenFile)
  59. APIHOOK_ENUM_END
  60. /*++
  61. ANSI wrapper for SfcIsFileProtected.
  62. --*/
  63. BOOL
  64. IsFileProtected(
  65. LPCSTR pszFileName
  66. )
  67. {
  68. LPWSTR pwszWideFileName = NULL;
  69. int nLen;
  70. BOOL bReturn = FALSE;
  71. //
  72. // Convert from ANSI to Unicode.
  73. //
  74. nLen = lstrlenA(pszFileName) + 1;
  75. if (nLen) {
  76. pwszWideFileName = (LPWSTR)RtlAllocateHeap(RtlProcessHeap(),
  77. HEAP_ZERO_MEMORY,
  78. nLen * sizeof(WCHAR));
  79. if (!pwszWideFileName) {
  80. DPFN(eDbgLevelError, "[IsFileProtected] Failed to allocate memory");
  81. return FALSE;
  82. }
  83. if (!MultiByteToWideChar(CP_ACP,
  84. 0,
  85. pszFileName,
  86. -1,
  87. pwszWideFileName,
  88. nLen)) {
  89. DPFN(eDbgLevelError, "[IsFileProtected] ANSI -> Unicode failed");
  90. goto cleanup;
  91. }
  92. bReturn = SfcIsFileProtected(NULL, pwszWideFileName);
  93. }
  94. cleanup:
  95. if (pwszWideFileName) {
  96. RtlFreeHeap(RtlProcessHeap(), 0, pwszWideFileName);
  97. }
  98. return bReturn;
  99. }
  100. /*++
  101. Wraps SfcIsFileProtected for NT path names.
  102. --*/
  103. BOOL
  104. IsNtFileProtected(
  105. IN PUNICODE_STRING pstrNtFileName
  106. )
  107. {
  108. NTSTATUS status;
  109. RTL_UNICODE_STRING_BUFFER DosPath;
  110. BOOL fReturn = FALSE;
  111. UCHAR DosPathBuffer[MAX_PATH * 2];
  112. if (!pstrNtFileName) {
  113. DPFN(eDbgLevelError, "[IsNtFileProtected] Invalid parameter");
  114. return FALSE;
  115. }
  116. //
  117. // Convert from an NT path to a DOS path.
  118. //
  119. RtlInitUnicodeStringBuffer(&DosPath, DosPathBuffer, sizeof(DosPathBuffer));
  120. status = ShimAssignUnicodeStringBuffer(&DosPath, pstrNtFileName);
  121. if (!NT_SUCCESS(status)) {
  122. DPFN(eDbgLevelError,
  123. "[IsNtFileProtected] Failed to initialize DOS path buffer");
  124. return fReturn;
  125. }
  126. status = ShimNtPathNameToDosPathName(0, &DosPath, NULL, NULL);
  127. if (!NT_SUCCESS(status)) {
  128. DPFN(eDbgLevelError,
  129. "[IsNtFileProtected] Failed to convert NT \"%ls\" to DOS path",
  130. pstrNtFileName->Buffer);
  131. goto cleanup;
  132. }
  133. //
  134. // Now check for a protected file.
  135. //
  136. if (SfcIsFileProtected(NULL, DosPath.String.Buffer)) {
  137. fReturn = TRUE;
  138. }
  139. cleanup:
  140. RtlFreeUnicodeStringBuffer(&DosPath);
  141. return fReturn;
  142. }
  143. BOOL
  144. APIHOOK(CopyFileA)(
  145. LPCSTR lpExistingFileName,
  146. LPCSTR lpNewFileName,
  147. BOOL bFailIfExists
  148. )
  149. {
  150. //
  151. // Ensure that the destination is not protected.
  152. //
  153. if (!bFailIfExists && IsFileProtected(lpNewFileName)) {
  154. VLOG(VLOG_LEVEL_ERROR,
  155. VLOG_WFP_COPYFILE,
  156. "API: CopyFileA Filename: %s",
  157. lpNewFileName);
  158. }
  159. return ORIGINAL_API(CopyFileA)(lpExistingFileName,
  160. lpNewFileName,
  161. bFailIfExists);
  162. }
  163. BOOL
  164. APIHOOK(CopyFileW)(
  165. LPCWSTR lpExistingFileName,
  166. LPCWSTR lpNewFileName,
  167. BOOL bFailIfExists
  168. )
  169. {
  170. //
  171. // Ensure that the destination is not protected.
  172. //
  173. if (!bFailIfExists && SfcIsFileProtected(NULL, lpNewFileName)) {
  174. VLOG(VLOG_LEVEL_ERROR,
  175. VLOG_WFP_COPYFILE,
  176. "API: CopyFileW Filename: %ls",
  177. lpNewFileName);
  178. }
  179. return ORIGINAL_API(CopyFileW)(lpExistingFileName,
  180. lpNewFileName,
  181. bFailIfExists);
  182. }
  183. BOOL
  184. APIHOOK(CopyFileExA)(
  185. LPCSTR lpExistingFileName,
  186. LPCSTR lpNewFileName,
  187. LPPROGRESS_ROUTINE lpProgressRoutine,
  188. LPVOID lpData,
  189. LPBOOL pbCancel,
  190. DWORD dwCopyFlags
  191. )
  192. {
  193. //
  194. // Ensure that the destination is not protected.
  195. //
  196. if (!(dwCopyFlags & COPY_FILE_FAIL_IF_EXISTS) &&
  197. IsFileProtected(lpNewFileName)) {
  198. VLOG(VLOG_LEVEL_ERROR,
  199. VLOG_WFP_COPYFILE,
  200. "API: CopyFileExA Filename: %s",
  201. lpNewFileName);
  202. }
  203. return ORIGINAL_API(CopyFileExA)(lpExistingFileName,
  204. lpNewFileName,
  205. lpProgressRoutine,
  206. lpData,
  207. pbCancel,
  208. dwCopyFlags);
  209. }
  210. BOOL
  211. APIHOOK(CopyFileExW)(
  212. LPCWSTR lpExistingFileName,
  213. LPCWSTR lpNewFileName,
  214. LPPROGRESS_ROUTINE lpProgressRoutine,
  215. LPVOID lpData,
  216. LPBOOL pbCancel,
  217. DWORD dwCopyFlags
  218. )
  219. {
  220. //
  221. // Ensure that the destination is not protected.
  222. //
  223. if (!(dwCopyFlags & COPY_FILE_FAIL_IF_EXISTS) &&
  224. SfcIsFileProtected(NULL, lpNewFileName)) {
  225. VLOG(VLOG_LEVEL_ERROR,
  226. VLOG_WFP_COPYFILE,
  227. "API: CopyFileExW Filename: %ls",
  228. lpNewFileName);
  229. }
  230. return ORIGINAL_API(CopyFileExW)(lpExistingFileName,
  231. lpNewFileName,
  232. lpProgressRoutine,
  233. lpData,
  234. pbCancel,
  235. dwCopyFlags);
  236. }
  237. BOOL
  238. APIHOOK(DeleteFileA)(
  239. LPCSTR lpFileName
  240. )
  241. {
  242. if (IsFileProtected(lpFileName)) {
  243. VLOG(VLOG_LEVEL_ERROR,
  244. VLOG_WFP_DELETEFILE,
  245. "API: DeleteFileA Filename: %s",
  246. lpFileName);
  247. }
  248. return ORIGINAL_API(DeleteFileA)(lpFileName);
  249. }
  250. BOOL
  251. APIHOOK(DeleteFileW)(
  252. LPCWSTR lpFileName
  253. )
  254. {
  255. if (SfcIsFileProtected(NULL, lpFileName)) {
  256. VLOG(VLOG_LEVEL_ERROR,
  257. VLOG_WFP_DELETEFILE,
  258. "API: DeleteFileW Filename: %ls",
  259. lpFileName);
  260. }
  261. return ORIGINAL_API(DeleteFileW)(lpFileName);
  262. }
  263. BOOL
  264. APIHOOK(MoveFileA)(
  265. LPCSTR lpExistingFileName,
  266. LPCSTR lpNewFileName
  267. )
  268. {
  269. //
  270. // Ensure that the source or destination is not protected.
  271. //
  272. if (IsFileProtected(lpExistingFileName) ||
  273. IsFileProtected(lpNewFileName)) {
  274. VLOG(VLOG_LEVEL_ERROR,
  275. VLOG_WFP_MOVEFILE,
  276. "API: MoveFileA Filename: %s Filename: %s",
  277. lpExistingFileName,
  278. lpNewFileName);
  279. }
  280. return ORIGINAL_API(MoveFileA)(lpExistingFileName,
  281. lpNewFileName);
  282. }
  283. BOOL
  284. APIHOOK(MoveFileW)(
  285. LPCWSTR lpExistingFileName,
  286. LPCWSTR lpNewFileName
  287. )
  288. {
  289. //
  290. // Ensure that the source or destination is not protected.
  291. //
  292. if (SfcIsFileProtected(NULL, lpExistingFileName) ||
  293. SfcIsFileProtected(NULL, lpNewFileName)) {
  294. VLOG(VLOG_LEVEL_ERROR,
  295. VLOG_WFP_MOVEFILE,
  296. "API: MoveFileW Filename: %ls Filename: %ls",
  297. lpExistingFileName,
  298. lpNewFileName);
  299. }
  300. return ORIGINAL_API(MoveFileW)(lpExistingFileName,
  301. lpNewFileName);
  302. }
  303. BOOL
  304. APIHOOK(MoveFileExA)(
  305. LPCSTR lpExistingFileName,
  306. LPCSTR lpNewFileName,
  307. DWORD dwFlags
  308. )
  309. {
  310. //
  311. // Ensure that the source or destination is not protected.
  312. //
  313. if (IsFileProtected(lpExistingFileName) ||
  314. IsFileProtected(lpNewFileName)) {
  315. VLOG(VLOG_LEVEL_ERROR,
  316. VLOG_WFP_MOVEFILE,
  317. "API: MoveFileExA Filename: %s Filename: %s",
  318. lpExistingFileName,
  319. lpNewFileName);
  320. }
  321. return ORIGINAL_API(MoveFileExA)(lpExistingFileName,
  322. lpNewFileName,
  323. dwFlags);
  324. }
  325. BOOL
  326. APIHOOK(MoveFileExW)(
  327. LPCWSTR lpExistingFileName,
  328. LPCWSTR lpNewFileName,
  329. DWORD dwFlags
  330. )
  331. {
  332. //
  333. // Ensure that the source or destination is not protected.
  334. //
  335. if (SfcIsFileProtected(NULL, lpExistingFileName) ||
  336. SfcIsFileProtected(NULL, lpNewFileName)) {
  337. VLOG(VLOG_LEVEL_ERROR,
  338. VLOG_WFP_MOVEFILE, "API: MoveFileExW Filename: %ls Filename: %ls",
  339. lpExistingFileName,
  340. lpNewFileName);
  341. }
  342. return ORIGINAL_API(MoveFileExW)(lpExistingFileName,
  343. lpNewFileName,
  344. dwFlags);
  345. }
  346. BOOL
  347. APIHOOK(MoveFileWithProgressA)(
  348. LPCSTR lpExistingFileName,
  349. LPCSTR lpNewFileName,
  350. LPPROGRESS_ROUTINE lpProgressRoutine,
  351. LPVOID lpData,
  352. DWORD dwFlags
  353. )
  354. {
  355. //
  356. // Ensure that the source or destination is not protected.
  357. //
  358. if (IsFileProtected(lpExistingFileName) ||
  359. IsFileProtected(lpNewFileName)) {
  360. VLOG(VLOG_LEVEL_ERROR,
  361. VLOG_WFP_MOVEFILE,
  362. "API: MoveFileWithProgressA Filename: %s Filename: %s",
  363. lpExistingFileName,
  364. lpNewFileName);
  365. }
  366. return ORIGINAL_API(MoveFileWithProgressA)(lpExistingFileName,
  367. lpNewFileName,
  368. lpProgressRoutine,
  369. lpData,
  370. dwFlags);
  371. }
  372. BOOL
  373. APIHOOK(MoveFileWithProgressW)(
  374. LPCWSTR lpExistingFileName,
  375. LPCWSTR lpNewFileName,
  376. LPPROGRESS_ROUTINE lpProgressRoutine,
  377. LPVOID lpData,
  378. DWORD dwFlags
  379. )
  380. {
  381. //
  382. // Ensure that the source or destination is not protected.
  383. //
  384. if (SfcIsFileProtected(NULL, lpExistingFileName) ||
  385. SfcIsFileProtected(NULL, lpNewFileName)) {
  386. VLOG(VLOG_LEVEL_ERROR,
  387. VLOG_WFP_MOVEFILE,
  388. "API: MoveFileWithProgressW Filename: %ls Filename: %ls",
  389. lpExistingFileName,
  390. lpNewFileName);
  391. }
  392. return ORIGINAL_API(MoveFileWithProgressW)(lpExistingFileName,
  393. lpNewFileName,
  394. lpProgressRoutine,
  395. lpData,
  396. dwFlags);
  397. }
  398. BOOL
  399. APIHOOK(ReplaceFileA)(
  400. LPCSTR lpReplacedFileName,
  401. LPCSTR lpReplacementFileName,
  402. LPCSTR lpBackupFileName,
  403. DWORD dwReplaceFlags,
  404. LPVOID lpExclude,
  405. LPVOID lpReserved
  406. )
  407. {
  408. //
  409. // Ensure that the destination is not protected.
  410. //
  411. if (IsFileProtected(lpReplacedFileName)) {
  412. VLOG(VLOG_LEVEL_ERROR,
  413. VLOG_WFP_REPLACEFILE,
  414. "API: ReplaceFileA Filename: %s",
  415. lpReplacedFileName);
  416. }
  417. return ORIGINAL_API(ReplaceFileA)(lpReplacedFileName,
  418. lpReplacementFileName,
  419. lpBackupFileName,
  420. dwReplaceFlags,
  421. lpExclude,
  422. lpReserved);
  423. }
  424. BOOL
  425. APIHOOK(ReplaceFileW)(
  426. LPCWSTR lpReplacedFileName,
  427. LPCWSTR lpReplacementFileName,
  428. LPCWSTR lpBackupFileName,
  429. DWORD dwReplaceFlags,
  430. LPVOID lpExclude,
  431. LPVOID lpReserved
  432. )
  433. {
  434. //
  435. // Ensure that the destination is not protected.
  436. //
  437. if (SfcIsFileProtected(NULL, lpReplacedFileName)) {
  438. VLOG(VLOG_LEVEL_ERROR,
  439. VLOG_WFP_REPLACEFILE,
  440. "API: ReplaceFileW Filename: %ls",
  441. lpReplacedFileName);
  442. }
  443. return ORIGINAL_API(ReplaceFileW)(lpReplacedFileName,
  444. lpReplacementFileName,
  445. lpBackupFileName,
  446. dwReplaceFlags,
  447. lpExclude,
  448. lpReserved);
  449. }
  450. void
  451. ReportProtectedFileA(
  452. LPCSTR pszFilePath
  453. )
  454. {
  455. UINT uSize = 0;
  456. if (pszFilePath) {
  457. while (TRUE) {
  458. if (IsFileProtected(pszFilePath)) {
  459. VLOG(VLOG_LEVEL_ERROR,
  460. VLOG_WFP_SHFILEOP,
  461. "API: SHFileOperationA Filename: %s", pszFilePath);
  462. }
  463. uSize = lstrlenA(pszFilePath) + 1;
  464. pszFilePath += uSize;
  465. if (*pszFilePath == '\0') {
  466. break;
  467. }
  468. }
  469. }
  470. }
  471. void
  472. ReportProtectedFileW(
  473. LPCWSTR pwszFilePath
  474. )
  475. {
  476. UINT uSize = 0;
  477. if (pwszFilePath) {
  478. while (TRUE) {
  479. if (SfcIsFileProtected(NULL, pwszFilePath)) {
  480. VLOG(VLOG_LEVEL_ERROR,
  481. VLOG_WFP_SHFILEOP,
  482. "API: SHFileOperationW Filename: %ls", pwszFilePath);
  483. }
  484. uSize = lstrlenW(pwszFilePath) + 1;
  485. pwszFilePath += uSize;
  486. if (*pwszFilePath == '\0') {
  487. break;
  488. }
  489. }
  490. }
  491. }
  492. int
  493. APIHOOK(SHFileOperationA)(
  494. LPSHFILEOPSTRUCTA lpFileOp
  495. )
  496. {
  497. //
  498. // If they're going to rename files on collision, don't bother.
  499. //
  500. if (!(lpFileOp->fFlags & FOF_RENAMEONCOLLISION)) {
  501. ReportProtectedFileA(lpFileOp->pFrom);
  502. ReportProtectedFileA(lpFileOp->pTo);
  503. }
  504. return ORIGINAL_API(SHFileOperationA)(lpFileOp);
  505. }
  506. int
  507. APIHOOK(SHFileOperationW)(
  508. LPSHFILEOPSTRUCTW lpFileOp
  509. )
  510. {
  511. //
  512. // If they're going to rename files on collision, don't bother.
  513. //
  514. if (!(lpFileOp->fFlags & FOF_RENAMEONCOLLISION)) {
  515. ReportProtectedFileW(lpFileOp->pFrom);
  516. ReportProtectedFileW(lpFileOp->pTo);
  517. }
  518. return ORIGINAL_API(SHFileOperationW)(lpFileOp);
  519. }
  520. HANDLE
  521. APIHOOK(CreateFileA)(
  522. LPCSTR lpFileName,
  523. DWORD dwDesiredAccess,
  524. DWORD dwShareMode,
  525. LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  526. DWORD dwCreationDisposition,
  527. DWORD dwFlagsAndAttributes,
  528. HANDLE hTemplateFile
  529. )
  530. {
  531. if (IsFileProtected(lpFileName) &&
  532. (dwDesiredAccess & GENERIC_WRITE ||
  533. dwDesiredAccess & GENERIC_READ)) {
  534. VLOG(VLOG_LEVEL_ERROR,
  535. VLOG_WFP_OPENFILE,
  536. "API: CreateFileA Filename: %s",
  537. lpFileName);
  538. }
  539. return ORIGINAL_API(CreateFileA)(lpFileName,
  540. dwDesiredAccess,
  541. dwShareMode,
  542. lpSecurityAttributes,
  543. dwCreationDisposition,
  544. dwFlagsAndAttributes,
  545. hTemplateFile);
  546. }
  547. HANDLE
  548. APIHOOK(CreateFileW)(
  549. LPCWSTR lpFileName,
  550. DWORD dwDesiredAccess,
  551. DWORD dwShareMode,
  552. LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  553. DWORD dwCreationDisposition,
  554. DWORD dwFlagsAndAttributes,
  555. HANDLE hTemplateFile
  556. )
  557. {
  558. if (SfcIsFileProtected(NULL, lpFileName) &&
  559. (dwDesiredAccess & GENERIC_WRITE ||
  560. dwDesiredAccess & GENERIC_READ)) {
  561. VLOG(VLOG_LEVEL_ERROR,
  562. VLOG_WFP_OPENFILE,
  563. "API: CreateFileW Filename: %ls",
  564. lpFileName);
  565. }
  566. return ORIGINAL_API(CreateFileW)(lpFileName,
  567. dwDesiredAccess,
  568. dwShareMode,
  569. lpSecurityAttributes,
  570. dwCreationDisposition,
  571. dwFlagsAndAttributes,
  572. hTemplateFile);
  573. }
  574. HFILE
  575. APIHOOK(OpenFile)(
  576. LPCSTR lpFileName,
  577. LPOFSTRUCT lpReOpenBuff,
  578. UINT uStyle
  579. )
  580. {
  581. if (IsFileProtected(lpFileName) &&
  582. (uStyle & OF_READWRITE ||
  583. uStyle & OF_CREATE ||
  584. uStyle & OF_DELETE)) {
  585. VLOG(VLOG_LEVEL_ERROR,
  586. VLOG_WFP_OPENFILE,
  587. "API: OpenFile Filename: %s",
  588. lpFileName);
  589. }
  590. return ORIGINAL_API(OpenFile)(lpFileName,
  591. lpReOpenBuff,
  592. uStyle);
  593. }
  594. HFILE
  595. APIHOOK(_lopen)(
  596. LPCSTR lpPathName,
  597. int iReadWrite
  598. )
  599. {
  600. if (IsFileProtected(lpPathName) &&
  601. (iReadWrite & OF_READWRITE ||
  602. iReadWrite & OF_WRITE)) {
  603. VLOG(VLOG_LEVEL_ERROR,
  604. VLOG_WFP_OPENFILE,
  605. "API: _lopen Filename: %s",
  606. lpPathName);
  607. }
  608. return ORIGINAL_API(_lopen)(lpPathName,
  609. iReadWrite);
  610. }
  611. HFILE
  612. APIHOOK(_lcreat)(
  613. LPCSTR lpPathName,
  614. int iAttribute
  615. )
  616. {
  617. if (IsFileProtected(lpPathName) && iAttribute != 1) {
  618. VLOG(VLOG_LEVEL_ERROR,
  619. VLOG_WFP_OPENFILE,
  620. "API: _lcreat Filename: %s",
  621. lpPathName);
  622. }
  623. return ORIGINAL_API(_lcreat)(lpPathName,
  624. iAttribute);
  625. }
  626. NTSTATUS
  627. APIHOOK(NtCreateFile)(
  628. PHANDLE FileHandle,
  629. ACCESS_MASK DesiredAccess,
  630. POBJECT_ATTRIBUTES ObjectAttributes,
  631. PIO_STATUS_BLOCK IoStatusBlock,
  632. PLARGE_INTEGER AllocationSize,
  633. ULONG FileAttributes,
  634. ULONG ShareAccess,
  635. ULONG CreateDisposition,
  636. ULONG CreateOptions,
  637. PVOID EaBuffer,
  638. ULONG EaLength
  639. )
  640. {
  641. if (IsNtFileProtected(ObjectAttributes->ObjectName)) {
  642. VLOG(VLOG_LEVEL_ERROR, VLOG_WFP_OPENFILE,
  643. "API: NtCreateFile Filename: %ls",
  644. ObjectAttributes->ObjectName->Buffer);
  645. }
  646. return ORIGINAL_API(NtCreateFile)(FileHandle,
  647. DesiredAccess,
  648. ObjectAttributes,
  649. IoStatusBlock,
  650. AllocationSize,
  651. FileAttributes,
  652. ShareAccess,
  653. CreateDisposition,
  654. CreateOptions,
  655. EaBuffer,
  656. EaLength);
  657. }
  658. NTSTATUS
  659. APIHOOK(NtOpenFile)(
  660. PHANDLE FileHandle,
  661. ACCESS_MASK DesiredAccess,
  662. POBJECT_ATTRIBUTES ObjectAttributes,
  663. PIO_STATUS_BLOCK IoStatusBlock,
  664. ULONG ShareAccess,
  665. ULONG OpenOptions
  666. )
  667. {
  668. if (IsNtFileProtected(ObjectAttributes->ObjectName)) {
  669. VLOG(VLOG_LEVEL_ERROR, VLOG_WFP_OPENFILE,
  670. "API: NtOpenFile Filename: %ls",
  671. ObjectAttributes->ObjectName->Buffer);
  672. }
  673. return ORIGINAL_API(NtOpenFile)(FileHandle,
  674. DesiredAccess,
  675. ObjectAttributes,
  676. IoStatusBlock,
  677. ShareAccess,
  678. OpenOptions);
  679. }
  680. SHIM_INFO_BEGIN()
  681. SHIM_INFO_DESCRIPTION(AVS_WINFILEPROTECT_DESC)
  682. SHIM_INFO_FRIENDLY_NAME(AVS_WINFILEPROTECT_FRIENDLY)
  683. SHIM_INFO_VERSION(1, 5)
  684. SHIM_INFO_FLAGS(AVRF_FLAG_EXTERNAL_ONLY)
  685. SHIM_INFO_INCLUDE_EXCLUDE("E:rpcrt4.dll kernel32.dll")
  686. SHIM_INFO_END()
  687. /*++
  688. Register hooked functions.
  689. --*/
  690. HOOK_BEGIN
  691. DUMP_VERIFIER_LOG_ENTRY(VLOG_WFP_COPYFILE,
  692. AVS_WFP_COPYFILE,
  693. AVS_WFP_GENERAL_R,
  694. AVS_WFP_GENERAL_URL)
  695. DUMP_VERIFIER_LOG_ENTRY(VLOG_WFP_MOVEFILE,
  696. AVS_WFP_MOVEFILE,
  697. AVS_WFP_GENERAL_R,
  698. AVS_WFP_GENERAL_URL)
  699. DUMP_VERIFIER_LOG_ENTRY(VLOG_WFP_DELETEFILE,
  700. AVS_WFP_DELETEFILE,
  701. AVS_WFP_GENERAL_R,
  702. AVS_WFP_GENERAL_URL)
  703. DUMP_VERIFIER_LOG_ENTRY(VLOG_WFP_REPLACEFILE,
  704. AVS_WFP_REPLACEFILE,
  705. AVS_WFP_GENERAL_R,
  706. AVS_WFP_GENERAL_URL)
  707. DUMP_VERIFIER_LOG_ENTRY(VLOG_WFP_WRITEFILE,
  708. AVS_WFP_WRITEFILE,
  709. AVS_WFP_GENERAL_R,
  710. AVS_WFP_GENERAL_URL)
  711. DUMP_VERIFIER_LOG_ENTRY(VLOG_WFP_OPENFILE,
  712. AVS_WFP_OPENFILE,
  713. AVS_WFP_GENERAL_R,
  714. AVS_WFP_GENERAL_URL)
  715. DUMP_VERIFIER_LOG_ENTRY(VLOG_WFP_SHFILEOP,
  716. AVS_WFP_SHFILEOP,
  717. AVS_WFP_GENERAL_R,
  718. AVS_WFP_GENERAL_URL)
  719. APIHOOK_ENTRY(KERNEL32.DLL, CreateFileA)
  720. APIHOOK_ENTRY(KERNEL32.DLL, CreateFileW)
  721. APIHOOK_ENTRY(KERNEL32.DLL, OpenFile)
  722. APIHOOK_ENTRY(KERNEL32.DLL, CopyFileA)
  723. APIHOOK_ENTRY(KERNEL32.DLL, CopyFileW)
  724. APIHOOK_ENTRY(KERNEL32.DLL, CopyFileExA)
  725. APIHOOK_ENTRY(KERNEL32.DLL, CopyFileExW)
  726. APIHOOK_ENTRY(KERNEL32.DLL, DeleteFileA)
  727. APIHOOK_ENTRY(KERNEL32.DLL, DeleteFileW)
  728. APIHOOK_ENTRY(KERNEL32.DLL, MoveFileA)
  729. APIHOOK_ENTRY(KERNEL32.DLL, MoveFileW)
  730. APIHOOK_ENTRY(KERNEL32.DLL, MoveFileExA)
  731. APIHOOK_ENTRY(KERNEL32.DLL, MoveFileExW)
  732. APIHOOK_ENTRY(KERNEL32.DLL, MoveFileWithProgressA)
  733. APIHOOK_ENTRY(KERNEL32.DLL, MoveFileWithProgressW)
  734. APIHOOK_ENTRY(KERNEL32.DLL, ReplaceFileA)
  735. APIHOOK_ENTRY(KERNEL32.DLL, ReplaceFileW)
  736. APIHOOK_ENTRY(SHELL32.DLL, SHFileOperationA)
  737. APIHOOK_ENTRY(SHELL32.DLL, SHFileOperationW)
  738. // 16-bit compatibility file routines.
  739. APIHOOK_ENTRY(KERNEL32.DLL, _lopen)
  740. APIHOOK_ENTRY(KERNEL32.DLL, _lcreat)
  741. APIHOOK_ENTRY(NTDLL.DLL, NtCreateFile)
  742. APIHOOK_ENTRY(NTDLL.DLL, NtOpenFile)
  743. HOOK_END
  744. IMPLEMENT_SHIM_END