如何使用 Powershell 对 TrustedInstaller 拥有的文件设置审核控制?

Dri*_*ise 4 powershell permissions windows-7 audit

我正在尝试使用以下 Powershell 脚本对ACLsWin.txt位于\%Windows%\System32(例如aaclient.dll)中的多个文件(在 中列出)设置审核控制:

$FileList = Get-Content ".\ACLsWin.txt"
$ACL = New-Object System.Security.AccessControl.FileSecurity

$AccessRule = New-Object System.Security.AccessControl.FileSystemAuditRule("Everyone", "Delete", "Failure")
$ACL.AddAuditRule($AccessRule)
foreach($File in $FileList)
{
    Write-Host "Changing audit on $File"
    $ACL | Set-Acl $File
}
Run Code Online (Sandbox Code Playgroud)

每当我运行脚本时,我都会收到错误PermissionDenied [Set-Acl] UnauthorizedAccessException

这似乎是因为这些文件的所有者是TrustedInstaller. 我以管理员身份运行这些脚本(即使我使用的是内置管理员帐户),但它仍然失败。我可以使用“安全”选项卡手动设置这些审计控制,但至少有 200 个文件手动设置可能会导致人为错误。

如何TrustedInstaller使用 Powershell绕过并设置这些审计控件?

Rya*_*ies 7

修改受 Windows 资源保护(TrustedInstaller 帐户是 WRP 的一部分)保护的文件的唯一支持的方法是使用 Windows 模块安装程序服务,这实际上只是说“您不能在自己支持的方式;只有通过安装补丁和服务包,才能以支持的方式修改这些文件。”

sfc.exe实用程序也是 Windows 资源保护的一部分。使用sfc.exe您可以知道该文件已被修改,它将用 WinSxS 存储中的副本替换它。

必须自己拥有这些文件的所有权,然后才能修改它们。最简单的方法是使用takeown.exe.

但事实是您不应该修改这些文件。

应用程序开发人员可以使用SfcIsFileProtectedSfcIsKeyProtectedAPI 来检查文件是否受 WRP 保护。

我不会为某人编写脚本来执行此操作的原因是不支持修改这些文件,作为专业人士,我无法凭良心帮助某人使他们的系统进入不受支持的状态。;)编辑:该死的,我刚刚...

编辑:如果你仍然坚持黑客攻击它,然后在技术上一种选择是推出Powershell.exe使用安全令牌TrustedInstaller.exe

或者,更简单的做法是使用 Try/Catch 块编写脚本,如果 catch 块由 触发[UnauthorizedAccessException],则获取文件的所有权并重试。

C:\Windows\system32>takeown /A /F C:\Windows\System32\aaclient.dll

SUCCESS: The file (or folder): "C:\Windows\System32\aaclient.dll" now owned by the administrators group.
Run Code Online (Sandbox Code Playgroud)

您还可以使用过滤器驱动程序监视对这些文件的修改。这就是诸如防病毒产品之类的东西完成它的方式。但是呃......这可能比你现在想做的工作更多......

再次编辑!:哦,您想之后将所有者设置回 TrustedInstaller 吗?你需要这个SeRestorePrivilege特权。

复制粘贴到 .\Enable-Privilege.ps1:

Function Enable-Privilege 
{
 param([ValidateSet("SeAssignPrimaryTokenPrivilege", "SeAuditPrivilege", "SeBackupPrivilege",
   "SeChangeNotifyPrivilege", "SeCreateGlobalPrivilege", "SeCreatePagefilePrivilege",
   "SeCreatePermanentPrivilege", "SeCreateSymbolicLinkPrivilege", "SeCreateTokenPrivilege",
   "SeDebugPrivilege", "SeEnableDelegationPrivilege", "SeImpersonatePrivilege", "SeIncreaseBasePriorityPrivilege",
   "SeIncreaseQuotaPrivilege", "SeIncreaseWorkingSetPrivilege", "SeLoadDriverPrivilege",
   "SeLockMemoryPrivilege", "SeMachineAccountPrivilege", "SeManageVolumePrivilege",
   "SeProfileSingleProcessPrivilege", "SeRelabelPrivilege", "SeRemoteShutdownPrivilege",
   "SeRestorePrivilege", "SeSecurityPrivilege", "SeShutdownPrivilege", "SeSyncAgentPrivilege",
   "SeSystemEnvironmentPrivilege", "SeSystemProfilePrivilege", "SeSystemtimePrivilege",
   "SeTakeOwnershipPrivilege", "SeTcbPrivilege", "SeTimeZonePrivilege", "SeTrustedCredManAccessPrivilege",
   "SeUndockPrivilege", "SeUnsolicitedInputPrivilege")]$Privilege,
  $ProcessId = $pid,
  [Switch]$Disable)

   $Definition = @'
 using System;
 using System.Runtime.InteropServices;

 public class AdjPriv
 {
  [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
  internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
   ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);

  [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
  internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
  [DllImport("advapi32.dll", SetLastError = true)]
  internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
  [StructLayout(LayoutKind.Sequential, Pack = 1)]
  internal struct TokPriv1Luid
  {
   public int Count;
   public long Luid;
   public int Attr;
  }

  internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
  internal const int SE_PRIVILEGE_DISABLED = 0x00000000;
  internal const int TOKEN_QUERY = 0x00000008;
  internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
  public static bool EnablePrivilege(long processHandle, string privilege, bool disable)
  {
   bool retVal;
   TokPriv1Luid tp;
   IntPtr hproc = new IntPtr(processHandle);
   IntPtr htok = IntPtr.Zero;
   retVal = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
   tp.Count = 1;
   tp.Luid = 0;
   if(disable)
   {
    tp.Attr = SE_PRIVILEGE_DISABLED;
   }
   else
   {
    tp.Attr = SE_PRIVILEGE_ENABLED;
   }
   retVal = LookupPrivilegeValue(null, privilege, ref tp.Luid);
   retVal = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
   return retVal;
  }
 }
'@


 $ProcessHandle = (Get-Process -id $ProcessId).Handle
 $type = Add-Type $definition -PassThru
 $type[0]::EnablePrivilege($processHandle, $Privilege, $Disable)

}
Run Code Online (Sandbox Code Playgroud)

在 Powershell 中,点源 Enable-Privilege.ps1 像这样:

PS C:\> . .\Enable-Privilege.ps1

PS C:\> [System.Security.Principal.NTAccount]$TrustedInstaller = "NT SERVICE\TrustedInstaller"
Run Code Online (Sandbox Code Playgroud)

保存当前 ACL:

PS C:\> $ACL = Get-Acl C:\Windows\System32\aaclient.dll
Run Code Online (Sandbox Code Playgroud)

将所有者更改为 TrustedInstaller:

PS C:\> $ACL.SetOwner($TrustedInstaller)
Run Code Online (Sandbox Code Playgroud)

启用 SeRestorePrivilege:

PS C:\> Enable-Privilege SeRestorePrivilege
Run Code Online (Sandbox Code Playgroud)

重新应用修改后的 ACL。如果您没有明确设置 SeRestorePrivilege 权限,即使对于本地系统帐户,这也是会失败的部分:

PS C:\> Set-Acl -Path C:\Windows\System32\aaclient.dll -AclObject $ACL
Run Code Online (Sandbox Code Playgroud)

我无耻地从这个 TechNet 论坛帖子中窃取了启用权限功能。