10

通过Mono(跨平台.NET运行环境)执行shellcode

 3 years ago
source link: https://3gstudent.github.io/3gstudent.github.io/%E9%80%9A%E8%BF%87Mono(%E8%B7%A8%E5%B9%B3%E5%8F%B0.NET%E8%BF%90%E8%A1%8C%E7%8E%AF%E5%A2%83)%E6%89%A7%E8%A1%8Cshellcode/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

0x00 前言


在上篇文章《通过Boolang语言执行shellcode的利用分析》介绍了利用Boolang语言执行shellcode的方法,本文将按照同样的思路,使用Mono(跨平台.NET运行环境)实现执行shellcode的功能,分析利用思路,给出防御建议

0x01 简介


  • Mono简介
  • Mono使用示例
  • 通过Mono执行shellcode的方法

0x02 Mono简介


参考资料:

https://www.mono-project.com/

https://github.com/mono/mono

Mono是Microsoft .NET Framework的开源实现

包括C#编译器和通用语言架构

Mono项目不仅可以运行于Windows系统上,还可以运行于Linux,FreeBSD,Unix,OS X和Solaris

0x03 Mono使用示例


开发环境: Win7x64

下载Mono安装包,地址如下:

https://www.mono-project.com/download/stable/#download-win

默认安装路径:C:\Program Files\Mono\

1.编译程序输出hello world

HelloWorld.cs的内容如下:

using System;

public class HelloWorld
{
    public static void Main(string[] args)
    {
        Console.WriteLine ("Hello Mono World");
    }
}

使用mcs.exe进行编译,命令如下:

"C:\Program Files\Mono\bin\mcs" HelloWorld.cs

生成HelloWorld.exe

2.编译程序调用Messagebox

Messagebox.cs的内容如下:

using System;
using System.Windows.Forms;
namespace MessageboxTest
{
    class Program
    {
        static void Main(string[] args)
        {          
            MessageBox.Show("Hello World");
        }
    }
}

使用mcs.exe进行编译,命令如下:

"C:\Program Files\Mono\bin\mcs" Messagebox.cs -r:System.Windows.Forms.dll

注:

需要加-r参数指定引用的dll,否则报错提示如下:

Messagebox.cs(2,22): error CS0234: The type or namespace name `Forms' does not exist in the namespace `System.Windows'. Are you missing `System.Windows.Forms' assembly reference?
Compilation failed: 1 error(s), 0 warnings

3.编译程序执行shellcode

Shellcode.cs已上传至GitHub,地址如下:

https://github.com/3gstudent/Homework-of-C-Sharp/blob/master/Shellcode.cs

使用mcs.exe进行编译,命令如下:

"C:\Program Files\Mono\bin\mcs" Shellcode.cs

生成Shellcode.exe

0x04 通过Mono执行shellcode的方法


在0x03简单介绍了使用mcs.exe编译程序的方法

这一节着重介绍利用Mono的特性绕过静态检测的方法

1.将启动shellcode的代码和payload分离

(1)将payload作base64加密并保存在文件中

部分示例代码如下:

using System.IO;
byte[] shellcode64 = new byte[276] {xxx};
String AsBase64String = Convert.ToBase64String(shellcode64);
StreamWriter sw = new StreamWriter(@"C:\test\ShellcodeBase64.txt");
sw.Write(AsBase64String);
sw.Close()

base64编码后的payload已上传至GitHub,地址如下:

https://github.com/3gstudent/Homework-of-C-Sharp/blob/master/ShellcodeBase64.txt

(2)从指定文件读取payload,作base解密后加载shellcode

实现代码已上传至GitHub,地址如下:

https://github.com/3gstudent/Homework-of-C-Sharp/blob/master/ReadShellcode.cs

2.使用csc将生成的文件编译成只能通过Mono加载的形式

使用mcs编译出来的程序可以直接运行,这有可能被查杀

使用csc进行编译可选的选项更多,我在csc帮助文件中发现可以生成只能通过Mono加载的形式,参数说明如下:

-target:appcontainerexe       Build an Appcontainer executable (Short form: -t:appcontainerexe)

因此完整的编译命令如下:

"C:\Program Files\Mono\bin\csc" ReadShellcode.cs -target:appcontainerexe

生成文件ReadShellcode.exe,无法直接执行,只能通过Mono加载,加载的命令如下:

"C:\Program Files\Mono\bin\mono.exe" ReadShellcode.exe

3.构造精简的Mono运行环境

如果我们要在另一系统执行以上文件,不必安装Mono的安装包

经过测试只需要以下文件:

  • /bin/mono-2.0-sgen.dll
  • /bin/mono.exe
  • /bin/ReadShellcode.exe
  • /bin/ShellcodeBase64.txt
  • /lib/mono/4.5/mscorlib.dll

树形图如下图

Alt text

注:

调用Messagebox引用System.Windows.Forms.dll时所需文件的树形图如下图

Alt text

0x05 利用分析


通过Mono(跨平台.NET运行环境)执行shellcode,有以下两个优点:

1.通过Mono.exe加载启动程序,启动程序不包括恶意的功能,payload可保存在另一个脚本文件中

2.通过Mono能够构造一个.Net环境,这可以解决某些程序对.Net环境的依赖

程序只能在.Net 4下运行,而当前系统不支持.Net 4,借助Mono就可以解决这个问题

0x06 防御检测


通过跨平台.NET运行环境Mono执行shellcode,父进程为Mono.exe,值得注意

这个技术无法绕过对程序行为的检测,所以可以通过检测进程行为的方式进行防御

0x07 小结


本文介绍了Mono(跨平台.NET运行环境)的使用方法,分享了几种绕过静态检测的方法,分析利用思路,给出防御建议


LEAVE A REPLY


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK