Haitham810/mini-c2

GitHub: Haitham810/mini-c2

一个用 Python 编写的教学型极简 C2 框架,用于演示加密 beaconing、异步任务队列和流量混淆等后渗透命令与控制基础设施的核心机制。

Stars: 0 | Forks: 0

# MiniC2 — 教学型命令与控制 (C2) 框架 ## 概述 MiniC2 是一个用 Python 编写的极简、完全自定义的 C2 框架。它的构建旨在演示后渗透命令与控制基础设施的底层机制,其层级低于 Metasploit Meterpreter 等现成工具——从而迫使你深入接触其底层概念:加密 beaconing、异步任务队列以及流量混淆。 **本项目不是什么:** 生产级别的植入物 (implant)。目标是学习,而非实际的作战能力。其设计刻意保持了极高的可读性。 ## 架构 ``` ┌─────────────────────────────────────────────────────────────────┐ │ ATTACKER MACHINE (Kali) │ │ │ │ ┌─────────────────┐ ┌──────────────────────────────┐ │ │ │ operator.py │──────▶ │ server.py │ │ │ │ (CLI console) │ HTTP │ (Flask + TLS + SQLite) │ │ │ └─────────────────┘ └──────────────┬───────────────┘ │ │ │ │ └─────────────────────────────────────────────┼──────────────────┘ │ HTTPS (port 4443) AES-256-GCM encrypted payload │ ┌─────────────────────────────────────────────┼──────────────────┐ │ TARGET MACHINE │ │ │ ▼ │ │ ┌─────────────────────────┐ │ │ │ agent.py │ │ │ │ (beacons every ~10s) │ │ │ └─────────────────────────┘ │ └─────────────────────────────────────────────────────────────────┘ ``` ### 通信流 ``` Agent Server │ │ │──── POST /beacon ────────────────▶│ (encrypted metadata + agent ID) │◀─── {tasks: [...]} ───────────────│ (encrypted task list or empty) │ │ │ [executes command in subprocess] │ │ │ │──── POST /result ────────────────▶│ (encrypted task_id + output) │◀─── {status: "ok"} ───────────────│ │ │ │ [sleep BEACON_INTERVAL ± jitter] │ │ │ └───────────── (repeat) ────────────┘ ``` ### 加密 所有 agent↔server 流量均通过两层进行加密: | 层级 | 机制 | 用途 | |------------------|----------------------|--------------------------------------------| | 传输层 (外层)| TLS 1.2+ (HTTPS) | 加密 HTTP 会话 | | 应用层 (内层)| AES-256-GCM | 加密 HTTP 内部的 JSON payload | 内部 AES 层意味着,即使在 TLS 检查下或 TLS 被代理剥离时(这是一种常见的防御手段),命令和结果依然是不可读的。 AES-GCM 还提供**身份验证加密**:GCM 标签可证明密文在传输过程中未被篡改。 ## 项目结构 ``` mini-c2/ ├── README.md ├── server/ │ ├── server.py # Flask HTTPS C2 server │ ├── operator.py # Interactive operator CLI │ ├── crypto.py # AES-256-GCM encrypt/decrypt │ ├── requirements.txt │ ├── cert.pem # generated by gen_cert.sh (gitignored) │ └── key.pem # generated by gen_cert.sh (gitignored) ├── agent/ │ ├── agent.py # Implant — runs on target machine │ ├── crypto.py # Same module (deployed independently) │ └── requirements.txt ├── scripts/ │ └── gen_cert.sh # Self-signed TLS cert generator └── writeup/ └── traffic-analysis.md # Wireshark comparison: MiniC2 vs Meterpreter ``` ## 设置与使用 ### 前置条件 - Kali Linux(服务器 + 操作端) - 位于同一 NAT 子网的目标虚拟机(Metasploitable2、Ubuntu 或 Windows) - 两台机器均需安装 Python 3.10+ - `openssl`(Kali 上已预装) ### 1 — 生成 TLS 证书 (Kali) ``` cd mini-c2/ chmod +x scripts/gen_cert.sh ./scripts/gen_cert.sh ``` 这将生成 `server/cert.pem` 和 `server/key.pem`。 ### 2 — 安装服务器依赖 (Kali) ``` cd mini-c2/server/ pip install -r requirements.txt ``` ### 3 — 启动 C2 服务器 (Kali) ``` python server.py # [*] MiniC2 服务器正在 https://0.0.0.0:4443 上启动 ``` ### 4 — 配置并部署 Agent (目标虚拟机) 编辑 `agent/agent.py` 并将 `C2_HOST` 设置为你的 Kali IP: ``` C2_HOST = "https://192.168.x.x:4443" # ← your Kali IP here ``` 将 `agent/` 目录复制到目标虚拟机,安装依赖并运行: ``` pip install -r requirements.txt python agent.py ``` ### 5 — 打开操作端控制台 (Kali,新终端) ``` cd mini-c2/server/ python operator.py ``` ``` ███╗ ███╗██╗███╗ ██╗██╗ ██████╗██████╗ ... Educational C2 Framework — Lab Use Only c2> agents AGENT ID HOSTNAME OS IP LAST SEEN ────────────────────────────────────────────────────────────────────────── a1b2c3d4-... metasploit Linux 4.4.0 192.168.x.x 2025-... c2> use a1b2c3d4-... c2(a1b2c3)> id [+] Task queued — ID: f5e4d3c2-... c2(a1b2c3)> results ┌─ Task f5e4d3c2... @ 2025-... │ uid=0(root) gid=0(root) groups=0(root) └────────────────────────────────────────────────────────── ``` ## 操作端控制台命令 | 命令 | 描述 | |--------------------------|----------------------------------------------------| | `agents` | 列出所有已注册的 agent | | `use ` | 为 agent 打开交互式伪 shell | | `task ` | 排队执行单个命令(非交互式) | | `results ` | 查看 agent 的所有任务结果 | | `help` | 显示帮助信息 | | `exit` | 退出 | **在伪 shell 内 (`use `):** | 命令 | 描述 | |------------|----------------------------------------------| | `` | 将其作为 shell 命令发送给 agent 排队执行 | | `results` | 获取该 agent 的最新输出 | | `exit` | 返回主控制台 | ## 自定义 Beacon 行为 在 `agent/agent.py` 中: ``` BEACON_INTERVAL = 10 # seconds between check-ins JITTER = 3 # ± random seconds added (prevents regular timing) TASK_TIMEOUT = 30 # max seconds per executed command ``` 相对于 `BEACON_INTERVAL` 增大 `JITTER`,会使基于流量检测的引擎更难区分 beacon 的定时通信与正常用户流量。 ## 扩展本项目 下一次迭代的想法: - [ ] **ECDH 密钥交换** — 基于每个会话派生 AES 密钥,而不是使用 PSK - [ ] **域名前置 (Domain fronting)** — 通过 CDN 路由 beacon 流量 - [ ] **文件传输命令** — `upload` 和 `download` 处理程序 - [ ] **屏幕截图捕获** — 添加 `screenshot` 任务类型 (Pillow) - [ ] **持久化机制** — cron 任务、systemd unit 或注册表运行键 - [ ] **编译为二进制文件** — 使用 PyInstaller 生成独立的可执行 agent ## 流量分析 请参阅 [`writeup/traffic-analysis.md`](writeup/traffic-analysis.md),了解基于 Wireshark 的 MiniC2 流量模式与 Meterpreter `reverse_tcp` 及 `reverse_https` 的对比分析,内容包括检测指标和规避讨论。 ## 相关资源 - [The Art of Hacking — h4cker](https://github.com/The-Art-of-Hacking/h4cker) — 精选的进攻安全参考资料 - [HackTricks — C2 infrastructure](https://book.hacktricks.xyz) — 真实环境下的 C2 规避技术 - [MITRE ATT&CK — Command and Control (TA0011)](https://attack.mitre.org/tactics/TA0011/) — C2 技术分类 - [Meterpreter internals](https://www.rapid7.com/blog/post/2015/03/25/stageless-meterpreter-payloads/) — Meterpreter 的对比方式 *构建于进攻安全学习系列课程 —— CT080-3-2 道德黑客与事件响应,亚太科技大学。*
标签:C2框架, IP 地址批量处理, Python, 安全, 安全学习资源, 安全测试工具, 数据展示, 无后门, 流量混淆, 红队, 超时处理, 逆向工具