SSLHArch Linux

0x00 简介

SSLH是一个ssl/ssh 多路复用器,用通俗易懂的话来说就是一个端口可以同时开ssl服务和ssh服务。比如我们可以在443端口做此设置,这样当我们用浏览器访问443端口时,SSLH会将我们的流量转发给目标服务(如Nginx)处理;当我们使用SSH连接服务器时,SSLH会将流量转发给服务器的sshd服务。你看,一个443端口可以同时用作网页和ssh登录服务!想一想,当我们把ssh服务隐藏在443端口下时,多么有趣!

当然SSLH支持的服务还有HTTP, TLS/SSL (包括 SNIand ALPN), SSH, OpenVPN, tinc, XMPP, SOCKS5,并且可以识别任何其他可以使用正则表达式进行测试的协议。

0x01 Docker安装(不支持透明代理)

1.下载

  1. proxychains4 git clone https://github.com/yrutschle/sslh.git

2.修改Dockerfile

  1. # vim Dockerfile
  2. RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
  3. sslh-select改为sslh-fork

3.构建镜像

  1. make docker

4.修改目标服务配置文件

  1. # vim /etc/ssh/sshd_config
  2. ListenAddress 172.17.0.1

5.修改Docker-Compose.yml

  1. # vim docker-compose.yml
  2. version: "3"
  3. services:
  4. sslh:
  5. image: sslh:latest
  6. hostname: sslh
  7. ports:
  8. - 443:443
  9. command: --listen=0.0.0.0:443 --ssh=172.17.0.1:30022 --tls=172.17.0.1:30443
  10. restart: on-failure

0x02 直接安装

1.下载

  1. pacman -S SSLH

2.修改配置文件

  1. # vim /etc/sslh.cfg
  2. timeout: 2;
  3. listen:
  4. (
  5. { host: "0.0.0.0"; port: "443"; }
  6. );
  7. protocols:
  8. (
  9. { name: "ssh"; service: "ssh"; host: "localhost"; port: "30022";},
  10. { name: "openvpn"; host: "localhost"; port: "31194"; },
  11. { name: "xmpp"; host: "localhost"; port: "35222"; },
  12. { name: "http"; host: "localhost"; port: "30080"; },
  13. { name: "tls"; host: "localhost"; port: "30443"; sni_hostnames: [ "glan.site" ]; },
  14. { name: "anyprot"; host: "localhost"; port: "38443"; }
  15. );

3.开机自启

:::warning 此方式不支持透明代理
透明代理方式请看0x03 设置SSLH开机自启与透明代理 :::

  1. # vim /usr/lib/systemd/system/sslh.service
  2. [Install]
  3. WantedBy=multi-user.target
  4. systemctl enable sslh

4.启动服务

  1. systemctl start sslh
  2. systemctl status sslh

0x03 设置SSLH开机自启与透明代理

SSLH透明代理官方文档: https://github.com/yrutschle/sslh/blob/master/doc/tproxy.md

1.sslh.cfg文件

:::warning 不要使用任何本地回环地址(如127.0.0.1localhost),可以使用网卡的ip地址(如192.168.0.1
本配置仅限于服务都在同主机情况,其他情况请参考SSLH透明代理官方文档
更多配置sslh.cfg的方法请看 基础配置文件示例 详细配置文件示例 ::: 以下例子中,我的eth0网卡地址为10.0.20.9。SSLH与目标服务端口监听在10.0.20.9

  1. # vim /etc/sslh.cfg
  2. timeout: 2;
  3. listen:
  4. (
  5. { host: "10.0.20.9"; port: "443"; }
  6. );
  7. protocols:
  8. (
  9. { name: "ssh"; service: "ssh"; host: "10.0.20.9"; port: "30022";},
  10. { name: "openvpn"; host: "10.0.20.9"; port: "31194"; },
  11. { name: "xmpp"; host: "10.0.20.9"; port: "35222"; },
  12. { name: "http"; host: "10.0.20.9"; port: "30080"; },
  13. { name: "tls"; host: "10.0.20.9"; port: "30443"; sni_hostnames: [ "glan.site" ]; },
  14. { name: "anyprot"; host: "10.0.20.9"; port: "38443"; }
  15. );

2.目标服务配置文件

修改目标服务配置文件,将端口监听在10.0.20.9的相应端口

  1. # vim /etc/ssh/sshd_config
  2. Port 30022
  3. ListenAddress 10.0.20.9
  4. # vim /etc/nginx/nginx.conf
  5. server {
  6. listen 10.0.20.9:30443 ssl;
  7. server_name glan.site;
  8. ...
  9. }

2.开机自启服务

开机自启

3.开机自启脚本

  1. # vim /etc/rc.local.d/sslh.sh
  2. sysctl -w net.ipv4.conf.default.route_localnet=1
  3. sysctl -w net.ipv4.conf.all.route_localnet=1
  4. iptables -t raw -A PREROUTING ! -i lo -d 127.0.0.0/8 -j DROP
  5. iptables -t mangle -A POSTROUTING ! -o lo -s 127.0.0.0/8 -j DROP
  6. iptables -t nat -A OUTPUT -m owner --uid-owner root -p tcp --tcp-flags FIN,SYN,RST,ACK SYN -j CONNMARK --set-xmark 0x01/0x0f
  7. iptables -t mangle -A OUTPUT ! -o lo -p tcp -m connmark --mark 0x01/0x0f -j CONNMARK --restore-mark --mask 0x0f
  8. ip rule add fwmark 0x1 lookup 100
  9. ip route add local 0.0.0.0/0 dev lo table 100
  10. sslh-fork -F /etc/sslh.cfg --transparent