美文网首页
CobaltStrike4.0 payload执行过程

CobaltStrike4.0 payload执行过程

作者: doinb1517 | 来源:发表于2022-04-12 18:04 被阅读0次

前言

使用CobaltStrike 4.0生成多个payload,分析其加载过程和检测方法

生成Payload

设置监听之后生成EXE

加载过程

先说结论:

  • 生成的exe只是为了加载恶意DLL

  • 恶意DLL是使用了XOR加密存储的,原始数据和密钥都存放在exe的.data段

  • 恶意DLL头部被修改过,使用反射注入技术将DLL注入自身

拖进IDA查看,循环结构,休眠十秒继续执行。执行逻辑如下:

main.png

1、在函数sub_401800中创建了管道%c%c%c%c%c%c%c%c%cMSSE-%d-server,可以作为特征提取

2、创建管道结束后在sub_401608中将.data段的数据复制到管道,大小是33000h

main.png

3、在函数sub_4017A2中申请内存,调用sub_401559进行解密数据。

main.png

4、创建新线程去执行恶意DLL

createthread.png

动态分析

在最后CreateThread函数处下断点,查看解密出的DLL

dll1.png dll2.png

这时可以利用脚本dump出文件,便于之后单独分析。

from idc import *


tmp_list = []
for byte in idc.GetManyBytes(0x690000, 0x33000):
    tmp_list.append(byte)

with open("dump.bin", "wb") as f:
    f.write(bytes(tmp_list))
print("success")

关注创建新线程逻辑的sub_401550处代码,是跳到eax处,这时eax保存的是恶意dll的地址。

401550.png

这个时候就很奇怪,存在一个问题:为什么跳到一个PE头部可以正常执行??答案就是这个PE头部是被精心构造的。

pe_head.png

Msf中也有相似的加载手法,可以参考meterpreter_loader的源码:https://github.com/rapid7/metasploit-framework/blob/master/lib/msf/core/payload/windows/meterpreter_loader.rb

对照函数asm_invoke_metsrv处的逻辑可以理解上述汇编的作用。

def asm_invoke_metsrv(opts={})
    asm = %Q^
        ; prologue
          dec ebp               ; 'M'
          pop edx               ; 'Z'
          call $+5              ; call next instruction
          pop ebx               ; get the current location (+7 bytes)
          push edx              ; restore edx
          inc ebp               ; restore ebp
          push ebp              ; save ebp for later
          mov ebp, esp          ; set up a new stack frame
        ; Invoke ReflectiveLoader()
          ; add the offset to ReflectiveLoader() (0x????????)
          add ebx, #{"0x%.8x" % (opts[:rdi_offset] - 7)}
          call ebx              ; invoke ReflectiveLoader()
        ; Invoke DllMain(hInstance, DLL_METASPLOIT_ATTACH, config_ptr)
          ; offset from ReflectiveLoader() to the end of the DLL
          add ebx, #{"0x%.8x" % (opts[:length] - opts[:rdi_offset])}
    ^

    unless opts[:stageless] || opts[:force_write_handle] == true
      asm << %Q^
          mov [ebx], edi        ; write the current socket/handle to the config
      ^
    end

    asm << %Q^
          push ebx              ; push the pointer to the configuration start
          push 4                ; indicate that we have attached
          push eax              ; push some arbitrary value for hInstance
          call eax              ; call DllMain(hInstance, DLL_METASPLOIT_ATTACH, config_ptr)
    ^
  end

这里就是精心构造了PE头部,使用了反射注入技术,将dll注入自身,返回的是dllmain的地址。0x650022call eax实际上就开始执行DLLMain处的逻辑了,可以对比下面两张图。之后就正常分析dump出的DLL即可

dllmain.png dllmain2.png

检测

1、创建管道的部分存在硬编码%c%c%c%c%c%c%c%c%cMSSE-%d-server,可以使用yara规则检测。

2、原始加密数据和解密密钥存放的位置在不同版本的cs中是不同的,可以收集不同版本的CS生成恶意payload进行分析,可以区分不同的CS版本。

相关文章

网友评论

      本文标题:CobaltStrike4.0 payload执行过程

      本文链接:https://www.haomeiwen.com/subject/dfiasrtx.html