该项目将 Cobalt Strike profile 转换为功能可用的 mod_rewrite
.htaccess 或 Nginx 配置文件,以支持向 Cobalt Strike teamserver 进行 HTTP 反向代理重定向。使用反向代理可为后端 C2 服务器提供保护,使其免受探测分析、调查以及一般互联网背景辐射的影响。
注意:在部署之前,您应根据需要测试和调整输出结果,但这些脚本应该能处理大部分繁重工作。
为便于快速测试,项目中已包含 havex.profile 示例。
.htaccess 或 /etc/nginx/nginx.confpython3 cs2modrewrite.py -i havex.profile -c https://TEAMSERVER -r https://GOHERE -o /etc/apache2/redirect.rules
Apache 配置示例
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
RemoteIPHeader X-Forwarded-For
ErrorLog /var/log/apache2/redirector_error.log
CustomLog /var/log/apache2/redirector_access.log combined
ErrorDocument 401 " "
ErrorDocument 403 " "
ErrorDocument 404 " "
ErrorDocument 500 " "
ErrorDocument 503 " "
# Include redirect.rules
Include /etc/apache2/redirect.rules
</VirtualHost>
建议使用类似如下方式更新 Apache 的 Server Header、ServerTokens 及日志记录。
## Update Apached Server Header, ServerTokens, and logging
echo "Update Update Apached Server Header, ServerTokens, and logging"
sed -i -e 's/\(ServerTokens\s\+\)OS/\1Prod/g' /etc/apache2/conf-enabled/security.conf
sed -i -e 's/\(ServerSignature\s\+\)On/\1Off/g' /etc/apache2/conf-enabled/security.conf
echo "SecServerSignature Server" >> /etc/apache2/conf-enabled/security.conf
echo "LogLevel alert rewrite:trace2" >> /etc/apache2/conf-enabled/security.conf
## Update Apached remoteip.conf
echo "Update Apached remoteip.conf"
echo "RemoteIPHeader X-Forwarded-For" >> /etc/apache2/conf-enabled/remoteip.conf
## Restart apache server
echo "Restart apache server"
systemctl restart apache2
python3 cs2modrewrite.py -i havex.profile -c https://TEAMSERVER -r https://GOHERE -o /var/www/html/.htaccess
apt-get install apache2
a2enmod rewrite headers proxy proxy_http ssl cache
a2dismod -f deflate
service apache2 reload
注意: https://bluescreenofjeff.com/2016-06-28-cobalt-strike-http-c2-redirectors-with-apache-mod_rewrite/ "e0x70i 在下方评论中指出,如果您的 Cobalt Strike Malleable C2 profile 包含 gzip 的 Accept-Encoding 头,您的 Apache 安装可能会默认压缩该流量,从而导致您的 Beacon 无响应或功能异常。要解决此问题,请禁用 mod_deflate(通过 a2dismod deflate,并在您的重写规则中添加 No Encode([NE])标志)。(感谢 e0x70i!)"
确保站点配置(即 /etc/apache2/available-sites/*.conf)中包含以下条目
# Enable SSL
SSLEngine On
# Enable SSL Proxy
SSLProxyEngine On
# Trust Self-Signed Certificates generated by CobaltStrike
SSLProxyVerify none
SSLProxyCheckPeerCN off
SSLProxyCheckPeerName off
SSLProxyCheckPeerExpire off
如果您计划在 .htaccess 文件中使用 mod_rewrite(而不是在站点配置文件中使用),您还需要通过将 AllowOverride None 更改为 AllowOverride All 来启用 .htaccess 文件的使用。对于所有网站,请编辑 /etc/apache2/apache.conf
<Directory /var/www/>
Options FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
最后,为确保万无一失,请再次重启 Apache。
service apache2 restart
如果您需要对重定向规则行为进行故障排查,请在站点配置文件中添加以下行,以启用详细的错误跟踪。
LogLevel alert rewrite:trace5
接下来,重新加载 Apache,并监控 /var/log/access.log 和 /var/log/error.log,以查看哪些规则正在匹配。
apt-get install nginx nginx-extras
注意: 自定义服务器头需要 nginx-extras。如果您无法获取此软件包,请注释掉生成的配置文件中的服务器头行。
将 cs2nginx.py 的输出保存到 /etc/nginx/nginx.conf,并根据需要修改(SSL 参数)。
python3 ./cs2nginx.py -i havex.profile -c https://127.0.0.1 -r https://www.google.com -H mydomain.local >/etc/nginx/nginx.conf
最后,在修改服务器配置文件后,重启 nginx。
service nginx restart
配置好重定向并使其正常运行后,请确保您的 C2 服务器仅允许来自重定向服务器以及您可信 IP(VPN、办公网段等)的入站流量。
建议通过 GeoIP 限制(mod_maxmind)以及恶意 User-Agent 和 IP 网段黑名单,为重定向服务器增加额外的防护。感谢 @curi0usJack 提供的思路。