红队-C2 Server基础构建
字数 1973 2025-08-25 22:58:55
红队-C2 Server基础构建教学文档
0x01 C2 Server基础建设
修改默认配置
-
修改默认端口:
- Cobalt Strike默认使用50050端口,应修改teamserver脚本文件中的端口配置
- 修改位置:teamserver脚本文件最后一行
-
证书替换:
- 不要使用默认的CS证书
- 使用keytool生成新证书:
keytool -genkey -alias [别名] -keyalg RSA -validity [有效期] -keystore [存储文件名] - 示例参数:
- 密钥算法:RSA
- 有效期:36500天
- 存储类型:JKS(建议迁移到PKCS12)
C2 Server隔离策略
-
隔离原则:
- 根据不同功能隔离不同C2通信,互不交叉
- 隔离维度:地址、端口、配置方式、流量特征、操作频率、RAT类型
-
C2分类:
- Long-haul(长期维权):
- 特点:操作频率低,回连次数少
- 配置:高sleep和jitter时间
- 权限维持:WMI和服务拉dll方式
- 要求:C2 Profile唯一,流量特征不交叉
- Staging Servers(建立初始Beacon):
- 特点:用于建立C2和Beacon之间的Stage过程
- 配置:睡眠时间较长
- Post-exploitation Servers(执行载荷):
- 特点:执行payload、开代理、横向扩权
- 配置:回连时间短,可在内存执行
- Long-haul(长期维权):
-
协议选择:
- 不同协议有不同优缺点
- 需为不同协议配置"正常"的出站目标端口
重定向器(Redirectors)配置
-
重定向目的:
- 混淆攻击行为中的域名
- 隐藏实际C2服务器位置
-
实现方式:
- 基础工具:
- iptables
- socat(示例):
socat TCP4-LISTEN:80,fork TCP4:[C2_IP]:80
- 中间件代理:
- Apache的mod_rewrite
- Nginx代理
- 云服务:
- Amazon EC2
- Cloudflare(域前置技术)
- 基础工具:
-
CS配置:
- 生成时可指定多个重定向地址(逗号分隔)
Apache Mod_Rewrite配置
-
优势:
- 隐藏红队攻击行为
- 可设置IP白名单
- 阻止非团队IP访问
-
准备工作:
- 修改
/etc/apache2/apache2.conf:AllowOverride All - 启用模块:
a2enmod rewrite proxy proxy_http
- 修改
-
自动化脚本:
- 使用
apache_redirector_setup.py脚本:python apache_redirector_setup.py --malleable="<C2 Profile路径>" --block_url="https://google.com" --block_mode="redirect" --allow_url="<Team server地址>" --allow_mode="proxy" - IP黑名单:
python apache_redirector_setup.py --ip_blacklist="1.1.1.1;1.1.1.2" --block_url="https://google.com" --block_mode="redirect" --allow_url="<Team server地址>" --allow_mode="proxy" - 复杂配置:
python apache_redirector_setup.py --mobile_url="https://mobile-payload.com" --mobile_mode="proxy" --valid_uris="payload;uploads" --ir --ip_blacklist="1.1.1" --block_url="https://GetBlockedNerd.com" --block_mode="redirect" --allow_url="https://Teamserver.com" --allow_mode="proxy"
- 使用
0x02 C2 Server自定义流量
Malleable C2 Profile基础
-
加载方式:
./teamserver [IP] [password] [/path/to/my.profile] -
作用:
- 定制流量规则
- 修改Beacon和Stager的通信流量特征
- 仿造正常通信软件特征
-
基本结构示例:
set sample_name "Putter Panda"; set sleeptime "500"; http-get { set uri "/MicrosoftUpdate/ShellEx/KB242742/default.aspx"; client { header "User-Agent" "Mozilla/4.0 (Compatible; MSIE 6.0;Windows NT 5.1)"; metadata { netbiosu; parameter "tmp"; } } server { header "Content-Type" "application/octet-stream"; output { print; } } }
配置选项详解
-
全局设置(set):
set sleeptime:心跳包时间set jitter:波动设置set useragent:请求User-Agentset uri:可指定多个URI(空格分隔)
-
事务(Transaction):
http-get和http-post:定义不同请求方式的设置- 分为
client和server块
-
元数据(Metadata)处理:
- Beacon向C2发送的系统信息
- 编码方式:
metadata { netbiosu; parameter "tmp"; } - 内置编码语法:
base64netbiosnetbiosumaskxor
-
终止语句(Termination statements):
- 控制数据存放位置:
header:HTTP头parameter:HTTP参数uri-append:URI追加print:HTTP Body
- 控制数据存放位置:
-
服务器响应配置:
- 添加正常响应头:
server { header "Server" "Apache/2.4.39 (Unix)"; header "Content-Type" "application/javascript; charset=utf-8"; } - 响应内容处理:
output { base64; prepend "/*! * jquery.base64.js..."; append "r64 = [256],r256 = [256],i = 0;..."; print; }
- 添加正常响应头:
特殊转义字符
| 转义序列 | 描述 |
|---|---|
| \n | 新行 |
| \r | 回车 |
| \t | 制表符 |
| \xHH | 十六进制字节值 |
Stage过程配置
-
http-stager:
- 定义Stage通信中C2响应的HTTP头
- 示例:
http-stager { server { header "Cache-Control" "private, max-age=0"; header "Content-Type" "text/html; charset=utf-8"; } }
-
Payload下载URI:
uri_x86:x86 payload下载URIuri_x64:x64 payload下载URI- 示例:
set uri_x86 "/favicon1.ico"; set uri_x64 "/favicon2.ico";
-
全局HTTP配置(http-config):
- 定义头的顺序和重定向:
http-config { set headers "Date, Server, Content-Length, Keep-Alive, Connection, Content-Type"; header "Server" "Apache"; set trust_x_forwarded_for "false"; }
- 定义头的顺序和重定向:
证书配置
-
自签名证书:
https-certificate { set CN "bobsmalware.com"; set O "Bob's Malware"; } -
有效SSL证书:
- 指定Java密钥库文件和密码:
https-certificate { set keystore "domain.store"; set password "mypassword"; } - 密钥库内容:
- 对称加密:密钥实体和密钥
- 非对称加密:私钥和配对公钥
- 指定Java密钥库文件和密码:
-
keytool生成证书:
keytool -genkey -alias [别名] -keyalg RSA -validity [天数] -keystore [文件名]
Profile检查工具
使用c2lint检查profile语法:
./c2lint [path/to/my.profile]