利用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 链接持久的最佳方法是什么

CentOS7中升级Curl

先情提要

在部署 php7.4 的时候,发现 make 错误,翻看日志提示 curl 版本不支持几个函数,原来是机器部署的 curl 版本过低 curl 7.54.1_2017 (x86_64-pc-linux-gnu)

遂开始着手升级至最新 8.0.30

实施过程

先看看目前curl 版本,是否可以使用

yum update curl

因为部分第三方云已经把内核替换成自家(华为云与火山引擎)不行,则需要自己手工更新

1、首先安装 epel-release

yum -y install epel-release

2、下载并安装 curl 的 rpm 包

wget http://mirror.city-fan.org/ftp/contrib/yum-repo/rhel7/x86_64/city-fan.org-release-3-8.rhel7.noarch.rpm

rpm -ivh city-fan.org-release-2-2.rhel7.noarch.rpm

3、将[city-fan.org]的 enable 值修改为 1

vim /etc/yum.repos.d/city-fan.org.repo

1
2
3
4
5
6
7
[city-fan.org]
name=city-fan.org repository for Red Hat Enterprise Linux (and clones) $releasever ($basearch)
#baseurl=http://mirror.city-fan.org/ftp/contrib/yum-repo/rhel$releasever/$basearch
mirrorlist=http://mirror.city-fan.org/ftp/contrib/yum-repo/mirrorlist-rhel$releasever
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-city-fan.org

4、更新 curl

yum update curl --enablerepo=city-fan.org -y

5、安装完成,检查版本,完成升级

curl -V

6、同时移除 city-fan.org.repo 并删除

cp /etc/yum.repos.d/city-fan.org.repo /etc/yum.repos.d/city-fan_bak.org.repo -a

如果此方法不能正常升级,请确认系统是否安装了多个 curl

which curl

whereis curl

如果无法确认,可以选择编译安装,然后重新系统指向

1
2
3
4
5
6
7
wget https://curl.haxx.se/download/curl-7.76.1.tar.gz
tar -xvf curl-7.76.1.tar.gz
cd ./curl-7.76.1
./buildconf
./configure --with-ssl=/opt/openssl --with-nghttp2=/usr/local --disable-file --without-pic --disable-shared
make
sudo make install

重新指向

echo "export PATH=/usr/local/curl/bin:$PATH" >> /etc/profile

刷新系统变量

source /etc/profile

再次查询 curl 版本

curl --version

curl 7.76.1 (x86_64-unknown-linux-gnu) libcurl/7.76.1 OpenSSL/1.1.1w zlib/1.2.7

Release-Date: 2021-04-14

Protocols: dict file ftp ftps gopher gophers http https imap imaps mqtt pop3 pop3s rtsp smb smbs smtp smtps telnet tftp

Features: alt-svc AsynchDNS HTTPS-proxy IPv6 Largefile libz NTLM NTLM_WB SSL TLS-SRP UnixSocket

这里需要注意的是 curl 的版本号是否和 libcurl 一样,不一样则去浏览

vim /etc/ld.so.conf

资源库是否正确软连接

1
2
include /etc/ld.so.conf.d/*.conf
/usr/local/lib # 正确的curl的资源应该在此软连接到安装目录,64位系统为lib64

更改完毕记得刷新配置文件

sudo ldconfig -v

sudo ldconfig

同时注意有无出现 WARNING

记录问题

Q: curl: symbol lookup error: curl: undefined symbol: curl_mime_free

Q: curl: symbol lookup error: curl: undefined symbol: curl_url_cleanup

Q: curl: symbol lookup error: curl: undefined symbol: curl_url

这类问题是 libcurl 软连接缺失,建议先去确认/usr/local/lib 等地方的 libcurl.so 文件是否链接到正确的地方。

1
2
3
4
5
6
sudo ldconfig -v
# ***
#/usr/local/lib:
# libcurl.so.4 -> libcurl.so.4.4.0
# libpcre.so.1 -> libpcre.so.1.2.10
# 打个比方这里的libcurl就是错误链接了,理应是/usr/local/curl/lib/libcurl.so.4.7.0

记CentOS编译nginx事务

环境须知

Nginx 版本:1.22.1

实施过程

开始编译 Nginx 之前,先参考编译语句

./configure arguments: --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/usr/local/nginx/logs/error.log  --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-openssl=../openssl-1.0.2k --add-module=../lua-nginx-module-0.10.24 --add-module=../ngx_devel_kit-0.3.1

lua 是一个小巧的脚本语言,利用 lua-nginx-module 模块可以使用 lua 处理 nginx 请求

预先准备好 lua-nginx-module 和 ngx_devel_kit 在上级目录,注意版本号,且无需安装

1
2
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.24.tar.gz
tar zxvf v0.10.24.tar.gz
1
2
wget https://github.com/simplresty/ngx_devel_kit/archive/v0.3.1.tar.gz
tar zxvf v0.3.1.tar.gz

到这里可以真正开始编译,并键入make

倘若你本来就有 Nginx,现在是更新或覆盖安装,注意千万不要make install,不然就覆盖安装现有 nginx 了

正常情况下,会弹出以下错误

./configure: error: unsupported LuaJIT version; ngx_http_lua_module requires LuaJIT 2.x.

原因是缺少 LuaJIT 环境

1
2
3
wget https://github.com/openresty/luajit2/archive/refs/tags/v2.1-20220915.tar.gz
tar zxvf luajit2-2.1-20220915.tar.gz
sudo make install PREFIX=/usr/local/LuaJITL

然后再修改 lua-nginx-module 的 config 文件

1
2
3
# 直接声明这两个变量
LUAJIT_INC=/usr/local/LuaJIT/include/luajit-2.1
LUAJIT_LIB=/usr/local/LuaJIT/lib

然后继续开始 make 如无意外成功编译

问题

Q:使用 openresty 时 failed to load the resty.core module

问题是找不到 lualib 库和 resty 模块,默认到/usr/local/lib/ 去找 lualib,然而在编译安装 OpenResty 时 lualib 库默认放到/usr/local/openresty/lualib