Squid使用SSLBump正向代理
本文最后更新于 2405 天前,其中的信息可能已经有所发展或是发生改变。
背景:最近需要使用nodeJs访问一个历史久远的网站,那个网站的服务器证书是错误的,且使用的协议是TLSv1.0。浏览器上不得不使用IE并关闭各种安全设置和设置为兼容模式才能正常访问。Nodejs的新版本已经不支持这种不安全的协议,所以需要搭建一个代理服务进行访问。刚开始使用的是mitmproxy ,当时的最新版本对于SSL错误会报错,所以将源码中校验部分去掉就可以正常使用了。但部署到生产环境,会出现cpu占用过高的情况。所以后面换了Squid。
部署Squid参考了一篇文章,原文:here,下面是简单翻译。

 

Squid是个很强大的Web代理,它可以应用于无线认证、重定向、用户身份验证、日志记录等。但其在SSL方面一直存在局限性。在3.2版本之前,Squid的策略只是简单地传递SSL加密流量,因为它无法校验受信任的SSL链接。对于每个SSL连接也无法给用户做预警。但3.5版本后允许更多的控制,但要注意仍然无法校验受信任的SSL链接。

这里不会过多地介绍使用SSL Bump配置Squid以外的知识,对于编译和配置squid的细节你最好看下官方文档。 如果通过代理自动监测,通知浏览器连接到代理服务器,这样是挺简单的。但有些用户可能会试图规避这一点,并尝试直接连接。 在这种情况下,我们需要强制这些用户通过截取流量来使用代理。SSL Bumping的前提是中间人攻击,但由于这是在您的用户清楚所有流量都是检查过的环境中完成的,因此这并不是真正的攻击,这只是一种策略。Squid用于执行SSL Bumping有一些新方法,但是我会总结出最具实战性的方法:Squid收到一个https请求,然后为用户建立安全连接。 远程连接建立后,Squid会重新加密所有流量; 这时候的加密使用的是本地私钥,以及本地证书颁发机构即时创建出来的证书。

所以我们首先要确保的是你已经安装了3.5或更高版本的Squid。尽管3.2,3.3以及3.4版本也能实现,但3.5版本后SSL Bump的指令有了些显著变化。另外,编译Squid时需要带上‘–with-openssl’参数。

在CentOS我使用以下参数来编译Squid(foam:我带上–enable-ipf-transparent参数会报错,所以我是去掉再编译的

./configure \
        --prefix=/usr \
        --exec-prefix=/usr \
        --includedir=/usr/include \
        --datadir=/usr/share \
        --libdir=/usr/lib64 \
        --libexecdir=/usr/lib64/squid \
        --localstatedir=/var \
        --sysconfdir=/etc/squid \
        --sharedstatedir=/var/lib \
        --with-logdir=/var/log/squid \
        --with-pidfile=/var/run/squid.pid \
        --with-default-user=squid \
        --enable-silent-rules \
        --enable-dependency-tracking \
        --with-openssl \
        --enable-icmp \
        --enable-delay-pools \
        --enable-useragent-log \
        --enable-esi \
        --enable-follow-x-forwarded-for \
        --enable-ipf-transparent \
        --enable-auth

接下来我们需要生成本地的SSL证书,这里我使用的是简单的命令,没有在意怎么去加密。

# Generate Private Key
openssl genrsa -out example.com.private 2048  

然后是创建Certificate Signing Request(CSR),即证书注册请求。

# Create Certificate Signing Request
openssl req -new -key example.com.private -out example.com.csr  
Country Name (2 letter code) [XX]:US  
State or Province Name (full name) []:Illinois  
Locality Name (eg, city) [Default City]:Chicago  
Organization Name (eg, company) [Default Company Ltd]:Example Company LTD.  
Organizational Unit Name (eg, section) []:Information Technology  
Common Name (eg, your name or your server's hostname) []:Example Company LTD.  
Email Address []:  
Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []:  
An optional company name []:Example Company LTD.  

请注意,我并没有给‘Common Name’ 设置一个域名。因为Squid会在用户请求时,使用当前访问的域名动态生成证书。接下来,我们给CSR生成个签名证书。

# Sign Certificate
openssl x509 -req -days 3652 -in example.com.csr -signkey example.com.private -out example.com.cert  

完成以上操作后,请将私钥和证书复制到Squid能够访问的目录。另外,请确保存放私钥的目录是安全的。Squid代理用户需要拥有访问证书的权限,并作为受信任的根证书安装到每个用户的计算机上。 这是我没有使用*或一个域名作为‘Common Name’的真正原因,因为当作为证书颁发机构加载时,该属性就会被固定了。

下面来看看Squid的配置,以下配置只与主题相关,并不是完整的。ps:请按照下面的顺序配置。

# Proxy Aware (non-intercepted traffic)
http_port 192.168.0.1:3128 ssl-bump cert=/etc/squid/example.com.cert key=/etc/squid/example.com.private generate-host-certificates=on version=1 options=NO_SSLv2,NO_SSLv3,SINGLE_DH_USE  
# Intercepted Traffic
https_port 192.168.0.1:3130 cert=/etc/squid/example.com.cert key=/etc/squid/example.com.private ssl-bump intercept generate-host-certificates=on version=1 options=NO_SSLv2,NO_SSLv3,SINGLE_DH_USE

# SSL Bump Config
ssl_bump stare all  
ssl_bump bump all  

下面作些说明:

http_port: 让Squid在ssl_bump模式下监听192.168.0.1的3130端口。这里我拒绝了SSLv2以及SSLv3的链接,并使用SINGLE_DH_USE。使用http_port时,浏览器是知道自己在与代理连接的。

https_port: 让Squid在ssl_bump拦截模式下监听192.168.0.1的3128端口。这里我同样拒绝了SSLv2以及SSLv3的链接,并使用SINGLE_DH_USE。使用https_port时,浏览器并不知道自己在连接代理,也就是说此时是透明代理。

ssl_bump stare:必需指令,关乎证书的正确生成。下面有更详细的介绍。
ssl_bump bump:执行bump的方法。

Squid的3.5版本有多种bump的方法和层次:

splice:旧的方式,并没有涉及到证书。

bump:自动生成证书并对流量重新加密,这正是我们想要的。

peek:可以参与连接,并允许在后续拼接流量。

stare:可以参与连接,并允许在后续对其bump。

为了能够成功拦截流量并生成证书,我们需要知道用来生成证书的域。 如果浏览器知道它正在使用代理,倒是没问题。 但如果我们仅仅是通过拦截流量来获取信息,就会使用远程服务器的IP地址来生成证书,这样是行不通的。 所以我们必须先’stare’ 连接。这样我们就可以获得域并用其生成正确的证书。

看到这里,你应该知道怎么做了。 但请注意,这本质上是打破了SSL信任机制。 你控制并影响了用户的信任策略(对某些人来说可能是一件好事),并默认信任代理服务器。 此配置的强大功能可以让您充分利用Squid强大的功能,如破解SSL保护的站点来过滤一些内容。

Notes::我在CentOS上编译时遇到一个问题,这通过修改 USE_SOLARIS_IPFILTER__MINOR_T_HACK 的定义来解决。大概在38725行。

#define USE_SOLARIS_IPFILTER_MINOR_T_HACK 0

以上为原文翻译,下面贴出我用于访问那个老旧网站的配置

#
# Recommended minimum configuration:
#

# Example rule allowing access from your local networks.
# Adapt to list your (internal) IP networks from where browsing
# should be allowed
acl localnet src 10.0.0.0/8	# RFC1918 possible internal network
acl localnet src 172.16.0.0/12	# RFC1918 possible internal network
acl localnet src 192.168.0.0/16	# RFC1918 possible internal network
acl localnet src fc00::/7       # RFC 4193 local private network range
acl localnet src fe80::/10      # RFC 4291 link-local (directly plugged) machines

acl SSL_ports port 443
acl Safe_ports port 80		# http
acl Safe_ports port 21		# ftp
acl Safe_ports port 443		# https
acl Safe_ports port 70		# gopher
acl Safe_ports port 210		# wais
acl Safe_ports port 1025-65535	# unregistered ports
acl Safe_ports port 280		# http-mgmt
acl Safe_ports port 488		# gss-http
acl Safe_ports port 591		# filemaker
acl Safe_ports port 777		# multiling http
acl CONNECT method CONNECT

#
# Recommended minimum Access Permission configuration:
#
# Deny requests to certain unsafe ports
#http_access deny !Safe_ports

# Deny CONNECT to other than secure SSL ports
#http_access deny CONNECT !SSL_ports

# Only allow cachemgr access from localhost
http_access allow localhost manager
http_access deny manager

#忽略证书错误
sslproxy_cert_error allow all
sslproxy_flags DONT_VERIFY_PEER

#使用TLSv1.0连接
sslproxy_version 4
sslproxy_options ALL

# We strongly recommend the following be uncommented to protect innocent
# web applications running on the proxy server who think the only
# one who can access services on "localhost" is a local user
#http_access deny to_localhost

#
# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
#

# Example rule allowing access from your local networks.
# Adapt localnet in the ACL section to list your (internal) IP networks
# from where browsing should be allowed
http_access allow localnet
http_access allow localhost

# And finally deny all other access to this proxy
http_access allow all

# Squid normally listens to port 3128
http_port 127.0.0.1:8888 ssl-bump cert=/etc/squid/my.cert key=/etc/squid/my.private generate-host-certificates=on


ssl_bump stare all  
ssl_bump bump all 

# Uncomment and adjust the following to add a disk cache directory.
#cache_dir ufs /var/cache/squid 100 16 256

# Leave coredumps in the first cache dir
coredump_dir /var/cache/squid

#
# Add any of your own refresh_pattern entries above these.
#
refresh_pattern ^ftp:		1440	20%	10080
refresh_pattern ^gopher:	1440	0%	1440
refresh_pattern -i (/cgi-bin/|\?) 0	0%	0
refresh_pattern .		0	20%	4320

 

评论

  1. zlx
    Macintosh Chrome
    4年前
    2020-6-08 14:21:16

    我使用的是squid 3.5.27的release源码包,但包里没有configure,只有configure.ac,是将configure.ac重命名为configure吗?谢谢!

    • foam
      foam 博主
      Macintosh Chrome
      4年前
      2020-6-09 14:55:35

      一般都会有的,你再好好看看。的确没有的话,需要用autoconf生成。

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇