.net – 通过SetSecurityDescriptor设置WMI ACL

前端之家收集整理的这篇文章主要介绍了.net – 通过SetSecurityDescriptor设置WMI ACL前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我似乎无法通过Power shell设置WMI ACL.调用
  1. Invoke-WmiMethod -Name "SetSecurityDescriptor" -Path "__systemsecurity=@" -ArgumentList $acl.psobject.immediateBaSEObject

返回此异常:

  1. Invoke-WmiMethod : Invalid method Parameter(s)
  2. At line:1 char:17.
  3. + Invoke-WmiMethod <<<< -Name "SetSecurityDescriptor" -Path "__systemsecurity=@" -ArgumentList $acl.psobject.immediateBaSEObject
  4. + CategoryInfo : InvalidOperation: (:) [Invoke-WmiMethod],ManagementException
  5. + FullyQualifiedErrorId : InvokeWMIManagementException,Microsoft.PowerShell.Commands.InvokeWmiMethod

SetSecurityDescriptor只接受__SecurityDescriptor类型的一个参数,而我在-Arguments中使用的$acl对象本身似乎没问题:

  1. PS C:\Windows\system32> $acl | gm
  2.  
  3.  
  4. TypeName: System.Management.ManagementBaSEObject#\__SecurityDescriptor
  5.  
  6. Name MemberType Definition
  7. ---- ---------- ----------
  8. ControlFlags Property System.UInt32 ControlFlags {get;set;}
  9. DACL Property System.Management.ManagementObject#__ACE[] DACL ...
  10. Group Property System.Management.ManagementObject#__ACE Group {...
  11. Owner Property System.Management.ManagementObject#__ACE Owner {...
  12. SACL Property System.Management.ManagementObject#__ACE[] SACL ...
  13. TIME_CREATED Property System.UInt64 TIME_CREATED {get;set;}
  14. __CLASS Property System.String __CLASS {get;set;}
  15. __DERIVATION Property System.String[] __DERIVATION {get;set;}
  16. __DYNASTY Property System.String __DYNASTY {get;set;}
  17. __GENUS Property System.Int32 __GENUS {get;set;}
  18. __NAMESPACE Property System.String __NAMESPACE {get;set;}
  19. __PATH Property System.String __PATH {get;set;}
  20. __PROPERTY_COUNT Property System.Int32 __PROPERTY_COUNT {get;set;}
  21. __RELPATH Property System.String __RELPATH {get;set;}
  22. __SERVER Property System.String __SERVER {get;set;}
  23. __SUPERCLASS Property System.String __SUPERCLASS {get;set;}

I can get off the docs开始,我调用参数集:路径重载,因此参数集似乎不会缺少必需的参数.

我基本上是在this MSDN blog post on the very same topic删除代码,而使用类似调用的GetSecurityDescriptor会提供所需的结果:

  1. $output = Invoke-WmiMethod -Path "__systemsecurity=@" -Name GetSecurityDescriptor

SetSecurityDescriptor不断向我抛出异常.我如何让它工作?

上下文中的代码,供参考:

  1. # connect to SystemSecurity
  2. $invokeparams = @{Path="__systemsecurity=@"}
  3.  
  4. # get SecurityDescriptor with ACL
  5. $output = Invoke-WmiMethod @invokeparams -Name GetSecurityDescriptor
  6. if ($output.ReturnValue -ne 0) {
  7. throw "GetSecurityDescriptor Failed: $($output.ReturnValue)"
  8. }
  9.  
  10. # ACL object reference is in the .Descriptor property
  11. $acl = $output.Descriptor
  12.  
  13. $ace = (New-Object System.Management.ManagementClass("win32_Ace")).CreateInstance()
  14.  
  15. # AccessMask is WBEM_ENABLE,$WBEM_METHOD_EXECUTE,$WBEM_WRITE_PROVIDER,$WBEM_REMOTE_ACCESS
  16. $ace.AccessMask = 1 + 2 + 0x10 + 0x20
  17. # AceFlags are $OBJECT_INHERIT_ACE_FLAG,$CONTAINER_INHERIT_ACE_FLAG
  18. $ace.AceFlags = 0x01 + 0x2
  19. # AceType is ACCESS_ALLOWED_ACE_TYPE
  20. $ace.AceType = 0x1
  21.  
  22. # get user SID
  23. $getparams = @{Class="Win32_Account";Filter="Domain='MYDOMAIN' and Name='SERVER$'"}
  24. $win32account = Get-WmiObject @getparams
  25. # and build a new Trustee object
  26. $trustee = (New-Object System.Management.ManagementClass("win32_Trustee")).CreateInstance()
  27. $trustee.SidString = $win32account.Sid
  28. $ace.Trustee = $trustee
  29.  
  30. # Add ACE to ACL
  31. $acl.DACL += $ace.psobject.immediateBaSEObject
  32.  
  33. # apply new ACL
  34. $setparams = @{Name="SetSecurityDescriptor";ArgumentList=$acl.psobject.immediateBaSEObject} + $invokeParams
  35. $output = Invoke-WmiMethod @setparams
  36. if ($output.ReturnValue -ne 0) {
  37. throw "SetSecurityDescriptor Failed: $($output.ReturnValue)"
  38. }

我也已经尝试过播放aforementioned blog post by Steve Lee评论中建议的.AceFlags属性 – 无济于事.

在你引用的文章中,调用是不同的,这些差异可能很重要 – 参数是一个单独的哈希表,包含所有参数作为名称/值对:
  1. $invokeparams = @{Namespace=$namespace;Path="__systemsecurity=@"}
  2.  
  3. $setparams = @{Name="SetSecurityDescriptor";ArgumentList=$acl.psobject.immediateBaSEObject} + $invokeParams
  4.  
  5. $output = Invoke-WmiMethod @setparams

猜你在找的Windows相关文章