The file system audit policy in Windows allows to monitor all access events to specific files and folders on a disk. An administrator can enable the audit policy to identify file and folder creation, read, modification, and deletion events on the NTFS file system. File system auditing is most commonly used to control access and changes to shared network folders on Windows file servers that multiple users can access simultaneously.
File system object access auditing is not enabled by default in Windows. Access auditing can be enabled via Group Policy. To configure the audit policy on a standalone server, use the local Group Policy Editor console ( gpedit.msc ). If you need to enable the audit policy on multiple computers in an AD domain, use the domain GPO management console ( gpmc.msc ).
Or enable the local file system audit policy from the command prompt.
List available audit categories:
AuditPol.exe /list /subcategory:*
Enable auditing of successful file system object access events:
AuditPol.exe /set /subcategory:"File System" /success:enable
Check current audit settings:
AuditPol.exe /get /category:"Object Access"
Even if a policy is enabled to audit access to files and folders, no actual events are sent to the Event Viewer. Audit settings for the files and folders to be monitored must be manually enabled and configured by the administrator.
For example, your task is to track read/change/create events for all files in C:\Docs folder.
See an example of how to use Windows auditing policies to find the user who deleted a file from a shared folder.
When configuring file system access auditing policies, enable auditing only for the folders and files you need. The size of the Event Viewer log file increases significantly if you have access auditing enabled for a large number of items.
To enable auditing for a specific directory, PowerShell can be used:
$Path = "C:\Docs"
$AuditChangesRules = New-Object System.Security.AccessControl.FileSystemAuditRule('BUILTIN\Users', 'Delete,DeleteSubdirectoriesAndFiles', 'none', 'none', 'Success')
$Acl = Get-Acl -Path $Path
$Acl.AddAuditRule($AuditChangesRules)
Set-Acl -Path $Path -AclObject $Acl
List the folder audit settings:
(Get-Acl "C:\Docs\" -Audit).Audit
settings from PowerShell" width="" />
If you want to recursively scan all directories and find the subfolders for which file system auditing is enabled, use this script:
$folders=Get-ChildItem "c:\docs" -Recurse |Where-Object
foreach ($folder in $folders)
$auditacl=(Get-Acl $folder.FullName -Audit).audit
if ($auditacl -ne "")
>
The audit policy will write a log to the Event Viewer if any actions are performed on files in the folder with auditing enabled. To view events:
However, the Event Viewer console’s filtering and search capabilities are quite poor, and using it to search for all actions on a particular file is inconvenient.
It is better to use PowerShell to find and list all access events for a particular file system object. The following PowerShell script finds and lists all access events for a specified file in Event Viewer (the Get-WinEvent cmdlet is used to query the Event Viewer):
$fileName = "C:\\docs\\new_test_file.txt" $results = Get-WinEvent -FilterHashtable @` foreach ($result in $results) < $Account = $result.properties[1].Value $objectName = $result.properties[6].Value $accessMask = $result.properties[8].Value if ( $accessMask -like "*00000000-*") < $accessMask=$result.properties[9].Value>$accessMask2 = $result.properties[9].Value $fileOperation = "" switch -Wildcard ($accessMask) < "*%%1538*" < $fileOperation = "READ_CONTROL" >"*%%4416*" < $fileOperation = "ReadData (or ListDirectory)" >"*%%4417*" < $fileOperation = "WriteData (or AddFile)" >"*%%4418*" < $fileOperation = "AppendData (or AddSubdirectory or CreatePipeInstance)" >"*%%4419*" < $fileOperation = "ReadEA" >"*%%4420*" < $fileOperation = "WriteEA" >"*%%4423*" < $fileOperation = "ReadAttributes" >"*%%4424*" < $fileOperation = "WriteAttributes" >"*%%4426*" < $fileOperation = "Delete" >"*%%4428*" < $fileOperation = "ReadControl" >"*%%4429*" < $fileOperation = "WriteDAC" >"*%%4430*" < $fileOperation = "WriteOwner" >"*%%4432*" < $fileOperation = "Synchronize" >"*%%4433*" < $fileOperation = "AccessSystemSecurity" >"*%%4434*" < $fileOperation = "MaximumAllowed" >"*%%4436*" < $fileOperation = "GenericAll" >"*%%4437*" < $fileOperation = "GenericExecute" >"*%%4438*" < $fileOperation = "GenericWrite" >"*%%4439*" < $fileOperation = "GenericRead" >"*%%1537*" < $fileOperation = "DELETE" >default < $fileOperation = "Unknown" >> Write-Host $result.Id $result.TimeCreated $Account $objectName $fileOperation > Write-Host $result.Id $result.TimeCreated $Account $objectName $fileOperation >
You can send the resulting list of access audit events to your log collector, database, text log file, or send an email notification using Send-MailMessage when a monitored file is accessed/modified.