利用Socat实现单端口转发中继

前言:

oracle cloud 提供的云 mysql 通过外网链接是需要填写指定 CIDR 范围的白名单 IP,然而这个白名单 IP 不支持 0.0.0.0/24 全域,因此提供几个中继思路进行解决。

解决

Socat

假设需求是:PC1(192.168.1.215) 需要使用 PC1 3306 端口直接跳到 PC2 的 MySQL(192.168.1.91)

开始安装

Centos 系统:yum install -y socat

Debian/Ubuntu 系统:apt-get update apt-get install -y socat

成功后即可可以使用

socat -d -d -lh -v -lf /home/mysql.log tcp-l:3306,fork,reuseaddr tcp:192.168.1.91:3306

参数解析

-lh 将主机名添加到日志消息

-v 详细数据流量,文本

-x 详细数据流量,十六进制

-d 增加详细程度(最多使用 4 次;建议使用 2 次)

-lf 记录到文件

但是该命令键入后有个问题,那便是不能持久化运行,初步考虑在命令前添加 nohup 或注册 systemctl

以 debian11 为例,在目录/etc/systemd/system/下创建一个新的 service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[Unit]
Description=socat mysqli to tcp
Documentation=https://blog.catooilg.com
After=network-online.target
Wants=network-online.target systemd-networkd-wait-online.service

[Service]
Restart=always

; User and group the process will run as.
User=root
Group=root

ExecStart=/usr/bin/socat -d -d -lh -v -lf /home/mysql.log tcp-l:3306,fork,reuseaddr tcp:192.168.1.91:3306

; Limit the number of file descriptors; see `man systemd.exec` for more limit settings.
LimitNOFILE=1048576
; Unmodified caddy is not expected to use more than that.
LimitNPROC=512

[Install]
WantedBy=multi-user.

重载服务systemctl daemon-reload

启动服务 systemctl start nginx

即可完成注册

iptables

1
2
3
4
5
6
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

Apache

假设需求是:PC1(192.168.1.215) 需要使用 PC1 IP 直接跳到 mywbg.com

1
2
3
4
5
6
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 添加如下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<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 端口监听,然后执行以下命令

1
2
3
4
5
6
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

同时在对机添加以下参数

1
2
3
http-config {
set trust_x_forwarded_for "true";
}

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

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

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

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

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

Nginx

假设需求是:PC1(192.168.1.215) 需要使用 PC1 IP 直接跳到 mywbg.com

安装

apt-get install nginx nginx-extras

添加新模块

git clone https://github.com/threatexpress/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

1
2
3
4
5
6
7
8
9
10
11
#将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 文件里加入如下选项

1
2
3
http-config {
set trust_x_forwarded_for "true";
}

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

引用

利用 Socat 实现单端口 中继(中转/端口转发)加速

socat 链接持久的最佳方法是什么

作者

Catooilg

发布于

2023-06-26

更新于

2025-02-10

许可协议

评论