[分享]用stunnel把socks5代理伪装成正常 Web 服务器的方法

Web、Mail、Ftp、DNS、Proxy、VPN、Samba、LDAP 等基础网络服务
回复
zhangjint5
帖子: 304
注册时间: 2011-01-02 12:31

[分享]用stunnel把socks5代理伪装成正常 Web 服务器的方法

#1

帖子 zhangjint5 » 2019-02-22 15:45

众所周知 stunnel 可以将任意 tcp 链接封装在 ssl 里面传输

虽然对通信数据抓包,会因为存在加密无法解读数据,但是如果直接用浏览器访问服务器,很容易被发现其不是一个正常的web服务器,从而暴露ssl链接里面是其它业务。

最近阅读 stunnel 文档是发现有个 redirect 参数,刚好可以解决问题,下面就以我的实例来说明。

请看服务器端配置

代码: 全选

debug = 5
output = /var/log/stunnel4/stunnel.log

[socks_server]
client = no
connect = 1080
accept = 443
cert = /etc/stunnel/server.pem
key = /etc/stunnel/server.key
CAfile = /etc/stunnel/cacert.pem
verify = 2
redirect = 80
如上面的配置!
被保护的业务是运行在1080端口上的socks服务器
封装后放在443号端口上服务,用443端口是为了使运营商看起来更像https服务器
还需要在80端口运行http服务(注意,是不加密的http服务),提供一个迷惑别人的网页。

接下来看下官方说明文档里面 verify 参数的说明
verify = LEVEL
verify the peer certificate

This option is obsolete and should be replaced with the verifyChain and verifyPeer options.

level 0
Request and ignore the peer certificate.

level 1
Verify the peer certificate if present.

level 2
Verify the peer certificate.

level 3
Verify the peer against a locally installed certificate.

level 4
Ignore the chain and only verify the peer certificate.

default
No verify.
我这里设置 verify = 2 就是要求所有客户端链接的时候,必须要验证客户端证书。如果无效则拒绝链接。

最后看下 redirect 参数的说明文档
redirect = [HOST:]PORT
redirect TLS client connections on certificate-based authentication failures

This option only works in server mode. Some protocol negotiations are also incompatible with the redirect option.
所以这里设置 redirect = 80 ,也就是上面提到的客户端证书,如果验证失败,就将其中封装的业务重定向到80端口,也就是之前准备的迷惑人的网页

===========================================================

客户端配置

代码: 全选

debug = 5
output = /var/log/stunnel4/stunnel.log

[socks_client]
client = yes
connect = [你的服务器IP]:443
accept = 127.0.0.1:1080
cert = /etc/stunnel/client.pem
key = /etc/stunnel/client.key
CAfile = /etc/stunnel/cacert.pem
verify = 2
由于服务端设置了 verify = 2 ,所以说,客户端必须提供合法的证书才能链接,就必须配置 cert 和 key 参数了。
这里客户端也设置了 verify = 2 ,是为了校验服务器证书合法性,所以还要用一个配套参数 CAfile。
这样做的目的是为了防止中间人、DNS劫持之类的攻击。(就像手机和伪基站一样,不应该无条件的信任何服务基站的信号)

到此为止工作已经完成!

最后实现的效果是!

当陌生人访问服务器的的 443 号端口的时候,由于他没有客户端访问证书,其看起来就是一个非常正常的 Web 服务器。(应该放些网页内容,如共享软件下载什么的,以免被运营商发现网页无内容,但流量也很大,就比较容易引起怀疑)

效果如下图
Untitled.png


而真正的使用者提供了客户端证书,才可以链接到内部封装的1080端口上的socks5业务。
Untitled.png


这个东西在某些需要伪装的领域可能非常有用,希望能帮助到有需要的人。
有什么更好的建议和改进,欢迎大家跟帖。(仅交流纯技术方面细节)
回复