Havoc C2 Agent开发记录

倾旋
倾旋
|2023-10-12|最后更新: 2023-10-13|
type
status
date
slug
summary
tags
category
icon
password
URL

Havoc Framework

💡
Havoc是一个现代的、可塑性的开发后命令和控制框架,适用于渗透测试人员、红队和蓝队。它是Github上的免费开源软件,由Paul Ungur(C5pider)编写和维护。开源地址:HavocFramework/Havoc: The Havoc Framework. (github.com)
notion image
Havoc Framework分为两部分,TeamServer用于设置监听器、处理Agent请求、处理命令执行、文件下载等功能,Client负责连接TeamServer,通过Websocket与TeamServer管理端口进行认证。
Havoc Framework的仓库中维护了一份默认的C语言版本Demon Agent,这个Agent的功能比较齐全,但由于是开源的,默认情况下生成的Agent样本会被直接查杀,特征较为明显,而在样本的对抗角度作者也提供了一些可以给使用者发挥的空间:
About Evasion
You might ask if the Demon agent bypasses anti-virus (AV) products or even endpoint detection and response (EDR) products, most likely not. The Demon agent wasn't designed to be evasive nor was it within the scope. It was designed to be as malleable and modular as possible to give the operator as much power over it to adapt it for the red team operation without overloading it with evasion techniques and features that are going to be most likely burned and going to be an IOC by itself. And the devs of the agent don't wanna play the cat and mouse game with AV & EDR vendors. That said, the Demon agent is designed to be interoperable with common techniques for bypassing anti-virus software such as loaders, packers, crypters, and stagers.
大致意思就是Demon不是为了绕过 anti-virus而开发,只是提供了源代码,这套源代码类似CobaltStrike的Beacon,这些绕过的活还是得使用者各凭本事。Havoc Framework的架构和CobaltStrike比较相似,只不过TeamServer还负责了Agent的生成、编译动作。

Custom Agent(自定义)

在Havoc Framework的Github主页上,提供了4个Agent的样例:
notion image
观察了一下源代码以后,发现这些Agent全部都不兼容Linux、MacOS,本文介绍一下如何开发跨平台、带一定样本对抗能力的Agent。
 
要学习Havoc Agent的开发,可以先参考:https://codex-7.gitbook.io/codexs-terminal-window/red-team/red-team-dev/extending-havoc-c2/third-party-agents 当然,这个作者写的
PyHmmm
CodeXTF2Updated Oct 30, 2023
是为了教学,所以还是有一些缺陷,不能直接投入使用。
 
第三方Agent注册以后,发送的数据都是固定的结构,每次数据发送到C2监听端口,会检查4个字节的(Magic Value)魔数:
notion image
notion image
(CALLBACK DATA)回调数据会被TeamServer发送到Python处理脚本上,然后Python处理脚本使用Websocket与TeamServer通信。

实现过程

这里主要简单介绍一下实现要点,首先编写Agent类,用于给Havoc注册Agent以及Agent请求的处理逻辑:

注册功能

处理逻辑

agent_json会接受到不通的数据,通过其中的字段区分是那种类型的请求:
  • register Agent上线
  • base64 接收到Base64的数据,解码输出到控制台上
  • get_task 从TeamServer获取任务,发送给Agent
  • post_task 将接收到的内容发送给Client控制台
  • download_file 接收文件保存tmp目录,其实就是文件下载

Agent生成逻辑

💡
GoReleaser 是一个用于简化 Go 项目发布过程的开源工具。它可以自动化构建、打包和发布 Go 项目,并支持将项目发布到各种不同的发布渠道,如二进制文件、Docker 镜像、Homebrew、Snapcraft 等。 🛠️ Garble 是一个通过包装Go工具链来混淆Go代码的一个工具,它基本上兼容了Go的编译命令,在此基础上增加了一些混淆模式的选项,通过设置选项可以构建不同混淆程度的Go二进制程序。
garble
burrowersUpdated Oct 31, 2023
这部分我采用了goreleaser+garble ,能过做一些静态层面的混淆:

命令执行的优化

为了支持执行跨平台的命令,减少命令行的特征,我会考虑将CMD、Powershell、Bash这样的解释器进程创建起来,然后向STDIN写入命令来读取STDOUT获取结果。
通过这个功能,可以在C2中执行:
如此一来,进程命令行就不会产生cmd.exe /c XXX 这样有特征的内容。
notion image
notion image
 
©2021-2024 倾旋. All rights reserved.