Powershell Remoting建立在windows WinRM服務之上,可以一對一或一對多遠程控制,也可以建立HTTP 或 HTTPS的“listeners”,使用WS-MAM協議接收遠程傳遞的命令。
Windows 遠程管理(WinRM)是 WS-Management 協議的 Microsoft 實現,該協議為使用 Web 服務的本地計算機和遠程計算機之間的通信提供了一種安全的方式。 也就是說在WS-MAN協議基礎上,客戶端運行環境可以多樣化。 比如openwsman。
圖片來源:v3 Secrets of PowerShell Remoting
Powershell Remoting在windows server 2008以前默認是不開啟的,需要通過administrator用戶執行Enable-PSRemoting命令開啟。
在windows server 2012中,Powershell Remoting默認開啟。
在windows下,powershell默認使用winrm進行遠程管理,winrm版本不同默認的監聽端口也不同。如下:
The default ports for winrm 1.1 are http port 80 and https port 443
The default ports for winrm 2.x are http port 5985 and https port 5986
可以在參考這里判斷winrm版本。
通過Enable-PSRemoting
命令打開PS遠程,默認是啟動了Kerberos認證。這個方法只適合兩臺電腦在相同域或信任域內的指定電腦(名字可以帶后綴).但它不支持跨域、域外或IP地址。
如果要跨域、或指定IP地址執行時我們可以在客戶端這里執行下面的代碼,需要將所有或單一遠程主機添加在信任表中。
Set-Item WSMan:\localhost\Client\TrustedHosts -Value * -Force
刪除所有遠程信任主機
Clear-Item WSMan:\localhost\Client\TrustedHosts
如果要刪除單一遠程主機,則可以執行:
$newvalue = ((Get-ChildItem WSMan:\localhost\Client\TrustedHosts).Value).Replace("computer01,","")
Set-Item WSMan:\localhost\Client\TrustedHosts $newvalue
更改computer01。
列出所有遠程信任主機
Get-Item WSMan:\localhost\Client\TrustedHosts
在使用遠程執行時如果只提供用戶名,那么則會彈窗輸入密碼。此時我們可以建立PSCredential對象將用戶名和密碼保存在里面。然后再傳遞給-Credential
參數。-ScriptBlock
參數后跟要執行的代碼。
$UserName = "admin3"
$serverpass = "admin123!@"
$Password = ConvertTo-SecureString $serverpass -AsPlainText –Force
$cred = New-Object System.Management.Automation.PSCredential($UserName,$Password)
invoke-command -ComputerName localhost -Credential $cred -ScriptBlock { ipconfig }
使用help * -Parameter computername
命令可以列出所有默認可以遠程使用的命令。并且認證過程都可以像上面的代碼一樣傳遞$cred。
之后寫個for循環就可以一對多的執行了。
如果輸出內容過于冗雜,還可以使用ConvertTo-Csv
或者ConvertTo-Html
將powershell對象的輸出轉換為html或者csv。
如果想一對一獲取交互式powershell,可以像這樣執行Enter-PSSession
:
Enter-PSSession -ComputerName 192.168.200.161 -Credential $cred
在使用invoke-command
的時候,computername
可為多個參數。在執行的時候可以使用-Asjob
參數將執行過程放在后臺。 接收回顯的時候可以使用get-job
查看job id
,然后用receive-job
接收全部回顯結果。 但是如果我只是想查看某個遠程主機的執行結果呢? 那么就可以像下面這樣做:
Get-Job -Id 1 | select -ExpandProperty childjobs
得到child job id
之后,再用 receive-job
接收回顯結果。
基本的信息搜集(日志、進程、服務等)可以靠上面列出的命令來收集,但是遠程執行invoke-command
是需要憑證的,如果是在域內我們是不是可以先用nltest
搜集下信任域?
在windows中有個System.DirectoryServices.ActiveDirectory
命名空間,和windows域有關。 其下有個類Domain,其中GetAllTrustRelationships()
方法可以獲得信任域。
那么在powershell就可以這樣執行:
([System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()).GetAllTrustRelationships()
獲得域之前的信任關系。 如果需要自行開發腳本,也可以參考下面的文檔。
除此之外,還記得之前metasploit筆記中那個local_admin_search
模塊嗎?veil-powerview
中也有通過相同的方式實現了這一過程。
兩種不同的腳本都通過調用OpenSCManagerA API連接遠程主機測試是否成功。
Local_admin_search.rb
Invoke-CheckLocalAdminAccess
附veil-powerview作者博客中的測試截圖:
https://www.blackhat.com/docs/us-14/materials/arsenal/us-14-Schroeder-The-Veil-Framework-Slides.pdf
https://www.blackhat.com/docs/us-14/materials/arsenal/us-14-Schroeder-The-Veil-Framework-Slides.pdf
整理的過程發現了很多牛人的博客和項目,在這里分享一下。
Powershell HID attack toolkit :https://github.com/samratashok/Kautilya
post exploitation :https://github.com/samratashok/nishang
Remote DLL inject :https://github.com/clymb3r
aspx的Powershell webshell :https://github.com/samratashok/nishang/tree/master/Antak- WebShell
Veil Post exploitation :https://github.com/Veil-Framework/Veil-PowerView
A PowerShell Post-Exploitation Framework :https://github.com/mattifestation/PowerSploit
local privilege escalation : https://github.com/HarmJ0y/PowerUp