PowerShell 學習筆記 - 4 Provider, ItemProperty 與 ACL

PowerShell 學習筆記 - 4 Provider, ItemProperty 與 ACL

本章主要探討 PowerShell 中的 Provider 這一基本概念與如何獲取文件等依靠 PSProvider 提供的對象所具備的屬性等。因爲移植進度或底層異構等緣由,PSProvider 現今於 PowerShell Core 下不推薦使用,這裏的闡述以 Windows PowerShell 爲例:windows

PSProvider

PSProvider 即 PowerShell 提供的文件系統抽象功能,即將其餘結構映射至文件系統結構的功能bash

# 獲取如今註冊的 PSProvider 列表
PS C:\Windows\system32> Get-PSProvider

Name                 Capabilities                                                     Drives                                                          
----                 ------------                                                     ------                                                          
Registry             ShouldProcess, Transactions                                      {HKLM, HKCU}                                                    
Alias                ShouldProcess                                                    {Alias}                                                         
Environment          ShouldProcess                                                    {Env}                                                           
FileSystem           Filter, ShouldProcess, Credentials                               {C, E, D}                                                       
Function             ShouldProcess                                                    {Function}                                                      
Variable             ShouldProcess                                                    {Variable}

Registry 即註冊表爲例,其提供了兩個驅動器,即文件系統入口:架構

PS C:\Windows\system32> Get-PSProvider Registry

Name                 Capabilities                                                     Drives                                                          
----                 ------------                                                     ------                                                          
Registry             ShouldProcess, Transactions                                      {HKLM, HKCU}                                                    

# 所以可利用驅動器模式進入,即 Set-Location 至 $Drive + ":" 下,以後便可以用文件系統的 cmdlet 進行操做
PS C:\Windows\system32> cd HKLM:

PS HKLM:\> ls


    Hive: HKEY_LOCAL_MACHINE


Name                           Property                                                                                                               
----                           --------                                                                                                               
BCD00000000                                                                                                                                           
HARDWARE                                                                                                                                              
SAM                                                                                                                                                   
ls : 不容許所請求的註冊表訪問權。
所在位置 行:1 字符: 1
+ ls
+ ~~
    + CategoryInfo          : PermissionDenied: (HKEY_LOCAL_MACHINE\SECURITY:String) [Get-ChildItem], SecurityException
    + FullyQualifiedErrorId : System.Security.SecurityException,Microsoft.PowerShell.Commands.GetChildItemCommand
 
SOFTWARE                                                                                                                                              
SYSTEM                                                                                                                                                



PS HKLM:\> cd .\SYSTEM
PS HKLM:\SYSTEM> ls


    Hive: HKEY_LOCAL_MACHINE\SYSTEM


Name                           Property                                                                                                               
----                           --------                                                                                                               
ActivationBroker                                                                                                                                      
ControlSet001                                                                                                                                         
DriverDatabase                 Version       : 167772160                                                                                              
                               SchemaVersion : 65536                                                                                                  
                               Architecture  : 9                                                                                                      
                               UpdateDate    : {32, 198, 183, 38...}                                                                                  
                               OemInfMap     : {191, 128}                                                                                             
...

另外一個經常使用於 Windows 平臺下的主要功能,即爲發現已經掛載的驅動器ide

# Windows 「多根文件系統架構」,即每一個驅動器都可做爲「根」
PS C:\Users\chuny> (Get-PSProvider FileSystem).Drives

Name           Used (GB)     Free (GB) Provider      Root                                               CurrentLocation
----           ---------     --------- --------      ----                                               ---------------
C                  51.98         65.07 FileSystem    C:\                                                    Users\chuny
E                  28.05         87.19 FileSystem    E:\
D                                      FileSystem    D:\

# UNIX 「單根文件系統架構」,即單個 UNIX 操做系統的管控域下只有一個根,即 root
PS /> (Get-PSProvider FileSystem).Drives

Name           Used (GB)     Free (GB) Provider      Root                                               CurrentLocation
----           ---------     --------- --------      ----                                               ---------------
/                   4.50         54.31 FileSystem    /

ItemProperty

事實上,文件系統即爲提供於 PSProvider 上面的對象,所以每一個文件均存在其對應的根(PSProvider)以及所對應的驅動器:學習

PS C:\Users\chuny> (Get-ItemProperty -LiteralPath .\.bash_history).PSProvider

Name                 Capabilities                                      Drives
----                 ------------                                      ------
FileSystem           Filter, ShouldProcess, Credentials                {C, E, D}


PS C:\Users\chuny> (Get-ItemProperty -LiteralPath .\.bash_history).PSDrive

Name           Used (GB)     Free (GB) Provider      Root                                               CurrentLocation
----           ---------     --------- --------      ----                                               ---------------
C                  51.98         65.07 FileSystem    C:\                                                    Users\chuny

上面所涉及到的 Get-ItemProperty 即爲獲取文件系統路徑對應對象屬性的基本 cmdlet,因爲註冊表也被抽象爲了一個 PSProvider,咱們甚至能夠經過路徑直接獲取註冊表中的信息:操作系統

PS HKLM:\> Get-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Control\


BootDriverFlags           : 28
CurrentUser               : USERNAME
EarlyStartServices        : {RpcSs, Power, BrokerInfrastructure, SystemEventsBroker...}
PreshutdownOrder          : {vmms, UsoSvc, DeviceInstall, gpsvc...}
SvcHostSplitThresholdInKB : 3670016
WaitToKillServiceTimeout  : 5000
SystemStartOptions        :  NOEXECUTE=OPTIN  HYPERVISORLAUNCHTYPE=AUTO
SystemBootDevice          : multi(0)disk(0)rdisk(0)partition(4)
FirmwareBootDevice        : multi(0)disk(0)rdisk(0)partition(2)
LastBootSucceeded         : 1
LastBootShutdown          : 0
DirtyShutdownCount        : 5
PSPath                    : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\
PSParentPath              : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet
PSChildName               : Control
PSDrive                   : HKLM
PSProvider                : Microsoft.PowerShell.Core\Registry

ACL

同時引入的一個重要概念即爲 ACL(Access Control List,訪問控制列表),對於每個路徑用戶亦能夠獲取其訪問權限設置,並檢查自身是否能夠訪問。設計

以以下的 .\.bash_history 爲例,該文件是 DESKTOP-H5Q0G1S 域下的用戶 chuny 建立的,且除自身外還容許 BUILTIN\AdministratorsNT AUTHORITY\SYSTEM 兩個用戶進行徹底控制:code

PS C:\Users\chuny> Get-Acl .\.bash_history


    目錄: C:\Users\chuny


Path          Owner                 Access
----          -----                 ------
.bash_history DESKTOP-H5Q0G1S\chuny NT AUTHORITY\SYSTEM Allow  FullControl...
PS C:\Users\chuny> (Get-Acl .\.bash_history).Access


FileSystemRights  : FullControl
AccessControlType : Allow
IdentityReference : NT AUTHORITY\SYSTEM
IsInherited       : True
InheritanceFlags  : None
PropagationFlags  : None

FileSystemRights  : FullControl
AccessControlType : Allow
IdentityReference : BUILTIN\Administrators
IsInherited       : True
InheritanceFlags  : None
PropagationFlags  : None

FileSystemRights  : FullControl
AccessControlType : Allow
IdentityReference : DESKTOP-H5Q0G1S\chuny
IsInherited       : True
InheritanceFlags  : None
PropagationFlags  : None

但 Windows NT 系列的 ACL 系統設計得至關複雜,而進入 Windows AD 後的 ACL 更加複雜,已經脫離本機的 ACL 範圍,交由 AD 中心控制(這也是用戶名會被標識爲域下的用戶的緣由,如 IdentityReference : DESKTOP-H5Q0G1S\chuny)。這已經超出了本章範疇,詳細能夠參考 Access Control Model對象

相關文章
相關標籤/搜索