YanisLounadi/red-team-offensive-lab

GitHub: YanisLounadi/red-team-offensive-lab

一个教育用途的红队 C2 框架,包含 Windows implant 与 Python 命令服务器,部署于隔离 Azure 实验环境用于网络安全攻防学习。

Stars: 0 | Forks: 0

# s0P0wn3d — 红队 C2 框架 红队 C2 框架:包含一个 Windows implant(C 语言编写,MinGW 交叉编译)和一个 Python 命令服务器,专为部署在 Azure 实验环境而设计。 ## 架构 ``` graph LR subgraph Azure T["Cible Windows"] S["Stager"] A["Implant"] B["Redirector
(Caddy, Linux)"] end C["C2 Server
(operateur)"] T -- "GET /<slug>
HTTPS (public)" --> B B -- "Proxy" --> C C -- "stager.exe" --> B B -- "stager.exe" --> T T -. "Execute" .-> S S -- "GET /stage
HTTPS + HMAC-PSK" --> B B -- "Proxy" --> C C -- "implant.exe" --> B B -- "implant.exe" --> S S -. "CreateProcessW" .-> A A -- "GET /beacon
HTTPS + HMAC-PSK" --> B B -- "HTTP reverse proxy" --> C C -- "Commandes" --> B B -- "Commandes" --> A ``` 该部署使用一个 **stager**(下载并执行):一个小型二进制文件(约 19 KB,3 个 DLL)通过 `GET /stage` 从 C2 下载 implant,将其写入 `%TEMP%\msedgeupdate.exe`,执行它,然后进行自我删除。在静态分析中,单独的 stager 没有任何可见的恶意功能。 C2 暴露了一个 **交付 endpoint**(`GET /`),使用启动时生成的随机 slug,直接提供 stager 服务。操作员将 URL 发送给目标(钓鱼、链接等)。只有此 endpoint 是公开的;其他所有请求都需要 HMAC-PSK 身份验证。 implant 通过 HTTPS 连接至 Linux 重定向器(Caddy),该重定向器将流量反向代理到 C2 服务器。 重定向器保护 C2 的真实 IP:implant 只知道重定向器的 IP。 ## implant 运行机制 ``` flowchart TD START([Lancement]) --> HIDE["FreeConsole()"] HIDE --> ANTIDEBUG{"Anti-debug
(7 techniques)"} ANTIDEBUG -- "Debugger detecte" --> EXIT([return 0 silencieux]) ANTIDEBUG -- "OK" --> ETW["Patch ETW
(EtwEventWrite → xor rax,rax; ret)"] ETW --> PERSIST["Persistance registre
(HKCU Run, XOR 0xAA)"] PERSIST --> KEYLOG["Thread keylogger
(GetAsyncKeyState)"] KEYLOG --> UUID["UUID v4 persistant
(%APPDATA%/.cache_id)"] UUID --> BEACON["Boucle beacon
(GET /beacon, jitter ±50%)"] BEACON --> CMD{"Commande
recue ?"} CMD -- "NOOP" --> SLEEP["Sleep + jitter"] CMD -- "Commande" --> EXEC["Execution"] EXEC --> RESP["POST /response
(resultat + HMAC)"] RESP --> SLEEP SLEEP --> DBGCHECK{"Anti-debug
periodique (30s)"} DBGCHECK -- "Debugger" --> EXIT DBGCHECK -- "OK" --> BEACON ``` ## 权限提升链 ``` stateDiagram-v2 [*] --> USER : Execution initiale USER --> ADMIN : Commande "admin" ADMIN --> SYSTEM : Commande "system" state USER { [*] --> Implant_User note right of Implant_User : Privileges standards } state ADMIN { [*] --> UAC_Bypass UAC_Bypass --> fodhelper : Methode 1 UAC_Bypass --> ComputerDefaults : Methode 2 (fallback) UAC_Bypass --> runas : Methode 3 (popup UAC) note right of UAC_Bypass : Delai 65s pour evader Defender } state SYSTEM { [*] --> Token_Theft Token_Theft --> SeDebugPrivilege : Activation SeDebugPrivilege --> Find_winlogon : Enumeration processus Find_winlogon --> DuplicateTokenEx : Vol de token DuplicateTokenEx --> CreateProcessWithTokenW : Spawn SYSTEM } ``` ## 通信协议 ``` sequenceDiagram participant T as Cible participant S as Stager participant I as Implant participant R as Redirector (Caddy) participant C as C2 Server Note over T,C: Delivery (endpoint public a slug aleatoire) T->>R: GET /(slug) (pas d auth) R->>C: Proxy HTTP C-->>R: stager.exe R-->>T: stager.exe Note over T: Execute stager.exe Note over S,C: Staging (download et execute) S->>R: GET /stage + X-Auth HMAC-SHA256 R->>C: Proxy HTTP C-->>R: payload.exe (implant) R-->>S: payload.exe Note over S: Write %TEMP%\msedgeupdate.exe S->>I: CreateProcessW Note over S: Self-delete (cmd.exe /c timeout et del) Note over I,C: Beacon (polling) I->>R: GET /beacon + X-Implant-Id + X-Auth HMAC R->>C: Proxy HTTP C-->>R: credentials ou NOOP R-->>I: Reponse Note over I,C: Exfiltration I->>R: POST /response + X-Task-Type lznt1_upload R->>C: Proxy HTTP C-->>R: HTTP 200 R-->>I: OK Note over C: Stockage chiffre AES-256-GCM (PBKDF2 600k iter) Note over I,C: HMAC invalide = HTTP 200 vide (rejet silencieux) ``` ## 前置条件 | 工具 | 安装方式 | | --------- | --------------------------------------------------------------------------------------------- | | MinGW-w64 | `brew install mingw-w64` (macOS) / `apt install mingw-w64` (Linux) | | Python 3 | `brew install python3` / `apt install python3` | | Terraform | [terraform.io](https://developer.hashicorp.com/terraform/install) | | Azure CLI | `brew install azure-cli` / [文档](https://learn.microsoft.com/cli/azure/install-azure-cli) | | OpenSSL | 大多数系统上已预安装 | ## 快速开始 ### 1. 配置基础设施 ``` cd azure/terraform cp terraform.tfvars.example terraform.tfvars # 编辑 terraform.tfvars:admin_password 和 ssh_public_key cd .. && make infra # Cree VM + redirecteur (~5 min) make setup # Defender OFF, OpenSSH ON make ssh-setup # Push de la cle SSH make status # Verification connectivite VM ``` ### 2. 配置重定向器 ``` make setup-redir # Upload Caddyfile + cert TLS + demarre Caddy make redir-status # Verification connectivite redirecteur ``` ### 3. 部署并运行 **真实工作流** — 目标通过 URL 下载 stager(无需 SSH 到虚拟机): ``` # Terminal 1 cd azure && make start # build + deploy sur redirecteur # Terminal 2 cd azure && make c2-remote # demarre le C2, affiche l'URL du stager ``` 在目标机器上,在浏览器中打开链接 `https://:443/`。文件 `msedgeupdate.exe` 将自动下载 — 运行它即可。 **开发/迭代工作流** — 直接 SSH 部署,全部在一条命令中完成: ``` # Terminal 1 cd azure && make c2-remote # demarre le C2 # Terminal 2 cd azure && make dev # soften → build + SCP → deploy-c2 → harden → run ``` 如需迭代,请重新运行 `make dev`(如果 C2 代码发生更改,请重启 `c2-remote`)。 ### 5. 获取战利品 ``` make loot-pull # Rsync server_loot/ depuis le redirecteur ``` ### 6. 清理(停止计费) ``` cd azure && make infra-destroy ``` ## 命令 ### 本地命令(操作员) | 命令 | 描述 | | -------------------------------- | ----------------------------------------------------------------- | | `help` | 显示可用命令 | | `sessions` | 列出已连接的 implant(UUID、IP、权限、beacon 间隔) | | `use ` | 通过索引或 UUID 前缀选择 implant | | `background` | 取消选择当前活动会话 | | `decrypt [-p pw] [--session ts]` | 解密凭证(Chrome v10/v20、Edge、Firefox) | | `export [csv\|json] [-p pw]` | 将凭证导出为结构化文件 | | `loot [timestamp]` | 列出战利品会话或某次会话中的文件 | | `clear` | 清空终端 | | `exit` | 退出 C2 | ### implant 命令(在目标上执行) | 命令 | 描述 | | ----------------- | ---------------------------------------------- | | `ping` | 连通性测试(回复 `pong`) | | `check` | 当前权限级别(USER/ADMIN/SYSTEM) | | `whoami` | 当前用户 | | `pwd` | 当前目录 | | `cd ` | 切换目录 | | `sleep ` | 更改 beacon 间隔(500-300000 毫秒) | | `credentials` | 窃取浏览器凭证文件 | | `download ` | 外带文件 | | `loot [path]` | 递归外带感兴趣的文件 | | `dumpsam` | 转储 SAM/SYSTEM/SECURITY(需要管理员权限) | | `admin` | 从 USER 提升至 ADMIN(UAC bypass) | | `system` | 从 ADMIN 提升至 SYSTEM(窃取 winlogon token) | | `exit` | 终止 implant | | `` | 执行任何 shell 命令 | ## 技术模块 ### 隐蔽性与规避 | 机制 | 实现 | 细节 | | ------------------------ | ------------------------------------------- | ------------------------------------------------------ | | **Stager** | 下载并执行(19 KB,3 个 DLL) | 两阶段部署,最小化静态特征 | | **Delivery endpoint** | 随机 Slug URL (`secrets.token_urlsafe`) | 避免被网络扫描器进行静态指纹识别 | | **Patch ETW** | `EtwEventWrite` → `xor rax,rax; ret` | 致盲 Sysmon Event 13、WinHTTP 跟踪、ProcessAccess | | **XOR 0xAA 混淆** | 所有关键字符串 | 注册表路径、exe 名称、权限、PSK | | **HMAC-PSK** | 对每个请求进行 BCrypt SHA256 | 无效时静默拒绝(返回空 HTTP 200) | | **Beacon 抖动** | ±50% + 指数退避 | 规避周期性模式检测 | | **FreeConsole()** | 窗口隐藏 | 无可见界面 | | **LZNT1 压缩** | 从 ntdll 调用 `RtlCompressBuffer` | 减少网络痕迹 | | **UAC 延迟 65 秒** | 13 × `Sleep(5000)` | 规避 Defender 检测(60 秒窗口) | | **注册表清理** | 通过 `--sp0` 标记自动删除 | 提权后无持久化痕迹 | ### 反调试(7 种技术) | # | 技术 | API | 检测方式 | | --- | -------------------------- | ------------------------- | ----------------------------- | | 1 | IsDebuggerPresent | `kernel32` | PEB 的 BeingDebugged 标志 | | 2 | CheckRemoteDebuggerPresent | `kernel32` | 远程/内核调试器 | | 3 | NtQueryInformationProcess | `ntdll` (class 7) | 非零 Debug Port | | 4 | NtQueryInformationProcess | `ntdll` (class 0x1F) | Flags = 0 → 正在调试 | | 5 | NtQueryInformationProcess | `ntdll` (class 0x1E) | Debug object handle | | 6 | 时间检查 | `QueryPerformanceCounter` | 循环 > 100ms → 单步执行 | | 7 | 硬件断点 | `GetThreadContext` | DR0-DR3 非零 | - 在启动时进行完整检查(在任何活动之前) - 在 beacon 循环中每 30 秒进行一次定期检查 - 如果被检测到则静默退出(`return 0`) - fail-open 设计:如果未加载 ntdll,则不会出现误报 ### 凭证 ``` graph TD subgraph "Mode USER/ADMIN" A[Chrome/Edge v10] --> B["DPAPI user-level
(CryptUnprotectData)"] C[Firefox] --> D["key4.db + logins.json
(firepwd-ng)"] end subgraph "Mode SYSTEM (impersonation)" E["Enumeration explorer.exe
(dedup par SID)"] --> F["ImpersonateLoggedOnUser"] F --> G["v10 : DPAPI user-level"] F --> H["RevertToSelf"] H --> I["v20 : Double DPAPI
system-level + user-level"] I --> J["Parse wrapper → AES-256 key"] end subgraph "SAM Dump" K["reg save HKLM\\SAM"] --> L["Exfiltration LZNT1"] M["reg save HKLM\\SYSTEM"] --> L N["reg save HKLM\\SECURITY"] --> L end ``` - **Chrome/Edge v10**:从 `Local State` 提取 `encrypted_key`,AES-256-GCM - **Chrome/Edge v20**(App-Bound Encryption,127+ 版本):双重 DPAPI(系统 + 用户),需要 SYSTEM 权限 - **Firefox**:提取 `key4.db` + `logins.json`,在 C2 端通过 firepwd-ng 解密 - **SAM/SYSTEM/SECURITY**:通过 `reg save` 转储注册表配置单元(需要管理员权限) - **Keylogger**:专用线程(`GetAsyncKeyState`),每 60 秒自动外带一次 ### 持久化 使用 XOR 0xAA 混淆名称的注册表项 `HKCU\Software\Microsoft\Windows\CurrentVersion\Run`。 每次用户打开会话时都会重新启动 implant。 ### 战利品安全 - **传输中**:通过 WinHTTP 使用 HTTPS/TLS - **静态存储**:使用 PBKDF2-HMAC-SHA256(600,000 次迭代)派生密钥的 AES-256-GCM - **存储位置**:`server_loot///`,文件后缀为 `.enc` - **Passphrase**:在启动 C2 时要求输入(或通过 `C2_LOOT_KEY`) ## implant 的 ETW 痕迹 | 操作 | ETW Provider | 事件 | 是否被 Patch 阻断 | | ------------------ | ----------------- | -------------------------- | ------------------ | | 注册表持久化 | Sysmon | Event 13 — RegistryEvent | 是 | | UAC bypass 注册表 | Sysmon | Event 13 — RegistryEvent | 是 | | Spawn fodhelper | Sysmon | Event 1 — Process Create | 是 | | Spawn fodhelper | Security-Auditing | Event 4688 — Process Create| 否 (内核层) | | 转储 SAM | Security-Auditing | Event 4656/4663 | 否 (内核层) | | DPAPI | Crypto-DPAPI | DPAPI ETW 事件 | 是 | | Beacon HTTPS | WinHTTP/WebIO | 网络事件 | 是 | | Token 窃取 | Sysmon | Event 10 — ProcessAccess | 是 | | Token 窃取 | Security-Auditing | Event 4656 | 否 (内核层) | ## 项目结构 ``` s0P0wn3d/ ├── Makefile # Cross-compilation MinGW + generation PSK ├── c2_server.py # Point d'entree C2 ├── requirements.txt # Dependances Python ├── gen_psk.py # Generateur PSK XOR'd ├── gen_xor.py # Utilitaire obfuscation XOR ├── src/ │ ├── main.c # Code complet de l'implant (~1870 lignes) │ └── stager.c # Stager Download & Execute (~230 lignes) ├── bin/ │ ├── s0P0wn3d.exe # Implant compile (PE32+ x86-64, ~47 KB) │ └── stager.exe # Stager compile (PE32+ x86-64, ~19 KB) ├── server/ │ ├── config.py # Configuration (HOST, PORT, TLS, PSK, STAGE_PAYLOAD_PATH, STAGER_URI) │ ├── handler.py # Handler HTTP (GET /, GET /stage, GET /beacon, POST /response) │ ├── sessions.py # Gestion multi-sessions (UUID) │ ├── commands.py # Routeur commandes locales/remote │ ├── tui.py # Interface terminal (rich) │ ├── crypto.py # Chiffrement at-rest (AES-256-GCM) │ └── decryptors/ │ ├── chromium.py # Decrypteur Chrome/Edge (v10 + v20) │ └── firefox.py # Decrypteur Firefox (firepwd-ng) ├── firepwd_tool/ # Outil tiers Firefox (firepwd-ng) ├── documentations/ │ ├── Implant.md # Documentation technique implant + stager │ ├── C2.md # Guide d'utilisation C2 │ └── OSINT.md # Analyse VirusTotal (8/76, 10.5%) ├── loot/ # Fichiers temporaires sur la cible └── server_loot/ # Loot recu cote serveur (chiffre) azure/ ├── Makefile # Orchestration Azure (infra, deploy, harden, test) ├── config.mk # Variables de configuration ├── scripts/ │ ├── setup.ps1 # Provisioning VM (Defender OFF, SSH ON) │ ├── harden.ps1 # Re-activation defenses │ ├── soften.ps1 # Desactivation defenses │ ├── test_etw.ps1 # Test evasion ETW │ ├── test_dbg.c # Harness test anti-debug │ └── Caddyfile # Config reverse proxy └── terraform/ ├── main.tf # VM Windows 10 + reseau + NSG ├── redirector.tf # VM Linux redirecteur (optionnel) ├── variables.tf # Variables Terraform └── terraform.tfvars.example # Template de config COMMAND/ ├── all-command.md # Reference commandes + scenarios └── SCENARIO_ATTACK_GUIDE.md # Guide de test en 5 phases ``` ## 第三方工具和库 | 工具 | 语言 | 用途 | | ------------------------------------------------------------------------- | ---- | -------------------------------------------- | | [MinGW-w64](https://www.mingw-w64.org/) | C | 在 macOS/Linux 上交叉编译 Windows 程序 | | [Terraform](https://www.terraform.io/) | HCL | 基础设施即代码 | | [Caddy](https://caddyserver.com/) | Go | 反向代理 HTTPS → HTTP(重定向器) | | [firepwd-ng](https://github.com/AdrienMusic/firepwd-ng) | Python | 解密 Firefox 凭证 | | [rich](https://github.com/Textualize/rich) | Python | 带有颜色和表格的终端界面 | | [prompt-toolkit](https://github.com/prompt-toolkit/python-prompt-toolkit) | Python | 带有自动补全的交互式 REPL | | [pycryptodomex](https://github.com/Legrandin/pycryptodome) | Python | AES-256-GCM, PBKDF2 | | [lznt1](https://pypi.org/project/lznt1/) | Python | LZNT1 解压(Windows 原生格式) | | [Impacket secretsdump.py](https://github.com/fortra/impacket) | Python | 解析转储的 SAM 配置单元 | ## 测试虚拟机 - **镜像**:Windows 10 22H2 Pro (x86_64) - **规格**:Standard_B2s(2 vCPU,4 GB RAM,约 $0.05/小时) - **区域**:`francecentral` - **可用测试**:`make test-etw`、`make test-antidebug` ## 资源与参考 - [MITRE ATT&CK T1105](https://attack.mitre.org/techniques/T1105/) — Ingress Tool Transfer (stager) - [MITRE ATT&CK T1548.002](https://attack.mitre.org/techniques/T1548/002/) — UAC Bypass via fodhelper - [MITRE ATT&CK T1134.001](https://attack.mitre.org/techniques/T1134/001/) — Token Impersonation/Theft - [MITRE ATT&CK T1003.002](https://attack.mitre.org/techniques/T1003/002/) — SAM Registry Dump - [MITRE ATT&CK T1562.001](https://attack.mitre.org/techniques/T1562/001/) — Disable/Modify ETW - [MITRE ATT&CK T1056.001](https://attack.mitre.org/techniques/T1056/001/) — Keylogging - [MITRE ATT&CK T1547.001](https://attack.mitre.org/techniques/T1547/001/) — Registry Run Keys Persistence - [MITRE ATT&CK T5.003](https://attack.mitre.org/techniques/T1555/003/) — Credentials from Web Browsers - [Chrome App-Bound Encryption (v20)](https://security.googleblog.com/2024/07/improving-security-of-chrome-cookies-on.html) — Google 安全博客 - [EtwEventWrite patching](https://www.ired.team/offensive-security/defense-evasion/evading-windows-defender-with-1-line-of-code) — 用户态 Patch 技术
标签:C2框架, DNS 反向解析, IP 地址批量处理, Python, Windows植入体, 安全学习资源, 安全测试工具, 无后门, 网络信息收集, 网络安全, 逆向工具, 隐私保护