metasploit payload运行原理浅析

时间:2022-08-15 15:33:13

背景

最近在做一些msf相关的事情,今天听到免杀相关的,去查询了下相关资料。

第一个不能错过的就是cobalt strike作者早年写的metasploit-loader项目了,我看了项目源码,找了一些相关资料

Meterpreter载荷执行原理分析 文章发现了一些细节性的东西,也感谢该文作者的抛砖引玉,不过文中有一些错误以及未说明白的地方,我会一一道来。

注意:本文只是对我自己的分析结果进行一次复盘,如果有什么错误之处欢迎大家斧正

metasploit loader

metasploit的shellcode到底做了什么

首先我们需要探讨的第一个问题是metasploit的shellcode到底做了什么?

在msf的官方wiki中,官方有对这个问题做一些简单的解释

从上面的文章我们大致能知道其实我们使用msf生成的shellcode只是一个加载器(Stagers),然后加载器通过我们生成shellcode时指定的ip和端口回连过来取到真正执行的恶意载荷(Stages)

加载器(Stagers)回连的具体流程

那么提出第二个问题,这个加载器(Stagers)回连的具体代码流程是怎样的?

我们通过文档只能知道Stagers通过网络加载Stages,那么Stages是什么?shellcode?可执行文件?反射dll?这些我们还都不清楚。

然后通过网上一些零星的资料,找到了msf邮件组曾经的两封邮件(源地址已无法访问,所幸WebArchive有留存)

里面提到流程以及关键点

流程

No tutorials that I know of, but here are the basic steps:

  • connect to the handler
  • read a 4-byte length
  • allocate a length-byte buffer
  • mark it as writable and executable (on Windows you'll need

    VirtualProtect for this)
  • read length bytes into that buffer
  • jump to the buffer. easiest way to do this in C is cast it to a

    function pointer and call it.

关键点

Assuming this is for X86 arch, you have to make sure that the EDI

register contains your socket descriptor (the value of the ConnectSocket

variable). You can do this via inline asm, but it might be easier to

just prepend the 5 bytes for setting it to your shellcode:

BF 78 56 34 12 mov edi, 0x12345678

For 64 bit, you have to use the RDI register (and need 10 bytes):

48 BF 78 56 34 12 00 00 00 00 mov rdi, 0x12345678

Hope this helps,

Michael

PS: This is the reason why the calling convention within Metasploit is

called "sockedi"