在指定文件或目录设置审计策略的Powershell脚本

  1. $computer = gc env:computername
  2. $path = "C:\New Folder"
  3. $user = "everyone"
  4. $path = $path.replace("\", "\\")
  5. $SD = ([WMIClass] "Win32_SecurityDescriptor").CreateInstance()
  6. $ace = ([WMIClass] "Win32_ace").CreateInstance()
  7. $Trustee = ([WMIClass] "Win32_Trustee").CreateInstance()
  8. $SID = (new-object security.principal.ntaccount $user).translate([security.principal.securityidentifier])
  9. [byte[]] $SIDArray = ,0 * $SID.BinaryLength
  10. $SID.GetBinaryForm($SIDArray,0)
  11. $Trustee.Name = $user
  12. $Trustee.SID = $SIDArray
  13. $ace.AccessMask = [System.Security.AccessControl.FileSystemRights]"Modify"
  14. $ace.AceFlags = "0x67"
  15. $ace.AceType = 2
  16. $ace.Trustee = $trustee
  17. $SD.SACL = $ace
  18. $SD.ControlFlags="0x10"
  19. $wPrivilege = gwmi Win32_LogicalFileSecuritySetting -computername $computer -filter "path='$path'"
  20. $wPrivilege.psbase.Scope.Options.EnablePrivileges = $true
  21. $wPrivilege.setsecuritydescriptor($SD)

$path 参数指定需要设置审核策略的文件或文件夹

参考:
https://blogs.technet.microsoft.com/bulentozkir/2009/12/26/sample-powershell-code-to-enable-auditing-on-a-folder/
https://docs.microsoft.com/es-es/dotnet/api/system.security.accesscontrol.aceflags?view=netframework-4.7.2
https://docs.microsoft.com/en-us/dotnet/api/system.security.accesscontrol.acetype?view=netframework-4.7.2


打开组策略中的文件访问审计

使用secedit 或 auditpol 命令

1. secedit

导出策略
secedit /export /cfg gp.inf /log 1.log

修改审核对象访问为成功和失败:

  1. echo [version] > 1.inf
  2. echo signature="$CHICAGO$" >> 1.inf
  3. echo [Event Audit] >> 1.inf
  4. echo AuditObjectAccess=3 >> 1.inf
  5. secedit /configure /db 1.sdb /cfg 1.inf /log 1.log /quiet
  6. del 1.*
  7. gpupdate /force

审核对象访问
AuditObjectAccess=3
键值含义:
0:无审核,1:成功,2:失败,3:成功失败

Signature
必须为$Windows NT$ or $Chicago$,表示所有Windows系统

gpupdate /force
强制更新组策略

2. auditpol

中文系统上,获取或设置参数是需使用中文,“chcp 437”转换成英文后仍有部分内容是中文,使用该命令建议测试下兼容性。
使用Powershell脚本开启文件夹的审计策略 - 图1

图形化界面操作

打开审计访问策略

使用Powershell脚本开启文件夹的审计策略 - 图2
使用Powershell脚本开启文件夹的审计策略 - 图3

选择审计的具体文件

1、 文件属性->高级
使用Powershell脚本开启文件夹的审计策略 - 图4

2、 审核标签
使用Powershell脚本开启文件夹的审计策略 - 图5
使用Powershell脚本开启文件夹的审计策略 - 图6

3、 添加审计人员EveryOne
使用Powershell脚本开启文件夹的审计策略 - 图7
使用Powershell脚本开启文件夹的审计策略 - 图8
使用Powershell脚本开启文件夹的审计策略 - 图9
使用Powershell脚本开启文件夹的审计策略 - 图10