# 转发重定向

## 介绍

在部署Cobalt Strike服务器时，我们可能需要前置代理服务器来帮助我们隐藏真实服务器，又或者是进行流量分离防止扫描或主动式的恶意软件探测服务器，再或者是根据目标的不同将其转发到不同的服务器上。

![](https://3226329500-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MDZyrMxFR2BnFjV82cS%2F-MFDvf6gALWWQfeWBSoH%2F-MFFUtthwHdgvG7zvkNW%2FCS%E6%9C%8D%E5%8A%A1%E5%99%A8%E9%83%A8%E7%BD%B2.png?alt=media\&token=42211f70-cefd-4ebf-9752-34ad0d2503f7)

首先我会一次介绍几种重定向的方法

## socat

实验环境linux1 C2(192.168.1.215)，linux2 转发(192.168.1.91)，windows目标机

socat转发非常简单只需一条命令

`socat TCP4-LISTEN:80,fork TCP4:C2ip:80`

一些有用选项

```
-lh将主机名添加到日志消息
-v详细数据流量，文本
-x详细数据流量，十六进制
-d增加详细程度（最多使用4次；建议使用2次）
-lf <logfile>记录到文件
```

![](https://3226329500-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MDZyrMxFR2BnFjV82cS%2F-MFH5_gQWrC6f7fvJYGg%2F-MFH6acBUWeRdF0KFk_X%2Fimage.png?alt=media\&token=212b2475-ad7b-4f99-b0ff-19e1da19d347)

HTTP Hosts和HTTP Host(Stager)填写转发服务器ip\
HTTP Port(C2)填写转发服务器要监听的端口\
HTTP Port(Bind)填写C2服务器要监听的端口

转发服务器执行命令\
`socat -d -d -d -d -lh -v -lf /var/log/socat.log TCP4-LISTEN:80,fork TCP4:C2服务器ip:C2服务器监听Port`

![将本机80端口的流量直接转发到C2服务器(192.168.1.215)的80端口](https://3226329500-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MDZyrMxFR2BnFjV82cS%2F-MFH5_gQWrC6f7fvJYGg%2F-MFH5tSNxq-SvLsl7nHE%2Fimage.png?alt=media\&token=b6d7ab7f-9564-4831-a924-eb21fed66041)

![](https://3226329500-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MDZyrMxFR2BnFjV82cS%2F-MFH5_gQWrC6f7fvJYGg%2F-MFH8b3U90rzvO9EmCtP%2Fimage.png?alt=media\&token=2ab809c7-1051-4337-9669-f9ce223b8ae2)

socat转发会存在一个问题，就是外部ip会显示为转发服务器的ip，因为socat是原方不动的转发流量

## iptables

```
iptables -I INPUT -p tcp -m tcp --dport 转发机监听端口 -j ACCEPT
iptables -t nat -A PREROUTING -p tcp --dport 转发机监听端口 -j DNAT --to-destination c2 ip:c2 port
iptables -t nat -A POSTROUTING -j MASQUERADE
iptables -I FORWARD -j ACCEPT
iptables -P FORWARD ACCEPT
sysctl net.ipv4.ip_forward=1
```

效果与socat一样不做演示

命令详细使用 <https://wangchujiang.com/linux-command/c/iptables.html>

## **Apache**

实验环境linux1 C2(192.168.1.215)，linux2 转发(mywbg.com)，windows目标机

```
apt-get install apache2
a2enmod rewrite headers proxy proxy_http ssl cache
a2dismod -f deflate
a2ensite default-ssl
a2dissite 000-default
service apache2 reload
```

配置/etc/apache2/sites-enabled/default-ssl.conf 添加如下内容

```
<Directory "/var/www/html">
  Options +Indexes +FollowSymLinks +ExecCGI
  AllowOverride All
  Order allow,deny
  Allow from all
  Require all granted
</Directory>
LogLevel alert rewrite:trace5

# Enable SSL
SSLEngine On
# Enable SSL Proxy
SSLProxyEngine On
# Trust Self-Signed Certificates generated by CobaltStrike
SSLProxyVerify none
SSLProxyCheckPeerCN off
SSLProxyCheckPeerName off
SSLProxyCheckPeerExpire off
```

修改/etc/apache2/ports.conf，注释80端口监听，然后执行以下命令

```
git clone https://github.com/threatexpress/cs2modrewrite
cd cs2modrewrite
python3 cs2modrewrite.py -i default.profile -c https://192.168.1.215:8080 -r https://www.baidu.com > /var/www/html/.htaccess
-i 表示所使用的c2配置文件
-c 根据监听器配置设置
-r 表示非法访问重定向url
```

Cobalt Strike配置的时候在C2文件里加入如下选项

```
http-config {
    set trust_x_forwarded_for "true";
}
```

trust\_x\_forwarded\_for 选项决定Cobalt Strike是否使用X-Forwarded-For HTTP头来确定外部ip。使用HTTP重定向器开启此选项，可以让CS显示外部ip的时候不再显示为代理服务器的

以上配置完成后重启apache2访问一下看看是否会自动重定向到百度。

然后启动CS新建监听器，配置如下https hosts和https host(stager)都填写代理服务器ip或域名

![](https://3226329500-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MDZyrMxFR2BnFjV82cS%2F-MFJfSoVCmxXzeukn1xC%2F-MFLaEx93LYcKv5AgYUm%2Fimage.png?alt=media\&token=787f000f-c898-4ad7-90bb-90bceda3c89a)

### 其他

如果你需要自定义apache ssl证书则需要修改/etc/apache2/sites-enabled/default-ssl.conf

```
SSLCertificateFile	/etc/ssl/certs/ssl-cert-snakeoil.pem
SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
#请尽量使用真实的ssl证书
```

以上配置中如果你需要使用http服务来转发你可以不关闭80端口

如果需要修改相关规则可以自行修改/var/www/html/.htaccess

## Nginx

实验环境linux1 C2(192.168.1.215)，linux2 转发(mywbg.com)，windows目标机

```
apt-get install nginx nginx-extras
git clone https://github.com/threatexpress/cs2modrewrite
cd cs2modrewrite
python3 ./cs2nginx.py -i default.profile -c https://192.168.1.215:8080 -r https://www.baidu.com -H mywbg.com >/etc/nginx/nginx.conf
-i 表示所使用的c2配置文件
-c 根据监听器配置设置
-r 表示非法访问重定向url
-H 指向代理主机的域名
```

修改/etc/nginx/nginx.conf

```
#将80端口注释关闭http
#listen 80;
#listen [::]:80;

#取消443端口注释开启https
listen 443 ssl;
listen [::]:443 ssl;

#取消ssl证书设置注释配置ssl证书
ssl_certificate /root/server.crt;
ssl_certificate_key /root/server.key;
```

配置完成后重启nginx，开始配置CS

CS配置C2文件的时候在C2文件里加入如下选项

```
http-config {
    set trust_x_forwarded_for "true";
}
```

trust\_x\_forwarded\_for 选项决定Cobalt Strike是否使用X-Forwarded-For HTTP头来确定外部ip。使用HTTP重定向器开启此选项，可以让CS显示外部ip的时候不再显示为代理服务器的

Cobalt Strike监听器配置和上面一样不在做演示了

### 其他

如果你需要使用http服务来转发你可以不关闭80端口，配置证书的时候尽量使用真实的ssl证书

相关规则修改请查看/etc/nginx/nginx.conf

## Nginx和apache2细节处理

上面的配置都是基本配置并未处理其他web服务

要处理其他web服务请先在配置apache或nginx时使用真实的SSL以免出现一些因SSL验证导致的错误

![](https://3226329500-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MDZyrMxFR2BnFjV82cS%2F-MFTt_-jJ5iEscS_52ig%2F-MFTzUpo86j8VfFjjxKa%2Fimage.png?alt=media\&token=a53ba9ac-b713-4fa4-a0ad-e1bc5eaa23c6)

使用Scripted Web Delivery (S)时请注意修改nginx.conf或.htaccess

![nginx配置里添加对所有以a开头url都进行转发](https://3226329500-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MDZyrMxFR2BnFjV82cS%2F-MFTt_-jJ5iEscS_52ig%2F-MFTzsIAWDmZiqZ5tJHZ%2Fimage.png?alt=media\&token=10288ac6-6b2f-41ca-aaa6-dc307679441d)

![.htaccess配置里添加对所有以a开头url都进行转发](https://3226329500-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MDZyrMxFR2BnFjV82cS%2F-MFU2p7TxYTwm4zAvjKc%2F-MFU3td9ooOOGVWvnBoS%2Fimage.png?alt=media\&token=e133764e-548b-4be8-be17-78f97f9fb3f2)

配置Scripted Web Delivery (S)时我为了方便local Port直接使用和HTTPS监听器相同的端口这样比较方便（注：使用相同的端口需要确保服务相同，HTTPS监听器是Https服务，那么配置Scripted Web Delivery时就必须勾选Enable SSL使用Https，开启Enable SSL必须要先在CS配置文件里配置`https-certificate`）

![](https://3226329500-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MDZyrMxFR2BnFjV82cS%2F-MFUBfTHou_2LATyw45W%2F-MFUC8CRCP0TMqeXhKVr%2Fimage.png?alt=media\&token=46468c75-530e-4305-969e-cec4585960f8)

因为配置原因使用时把命令行的中的端口删掉即可

cs其他web服务配置参照上面即可

## 转发器

备忘录：待写

## 其他

具体怎么设计自己看着来吧我也就随便写写


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wbglil.gitbook.io/cobalt-strike/cobalt-strikekuo-zhan/dai-xie.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
