代理服務 SQUID 測試


第一部分:SQUID基礎

clip_image001

clip_image001[7]

clip_image001[9]

clip_image001[11]

Squid代理服務的基本配置:

http_port 3128                    #設置監聽的IP與端口號
cache_mem 64 MB                    #額外提供給squid使用的內存,推薦大小為物理內存的1/3-1/2或更多
maximum_object_size 4 MB            #設置squid磁盤緩存最大文件,超過4M的文件不保存到硬盤
minimum_object_size 0 KB            #設置squid磁盤緩存最小文件
maximum_object_size_in_memory 4096 KB        #設置squid內存緩存最大文件,超過4M的文件不保存到內存
cache_dir ufs /var/spool/squid 100 16 256    #定義squid的cache存放路徑 、cache目錄容量(單位M)、一級緩存目錄數量、二級緩存目錄數量
logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh        #log文件日志格式
access_log /var/log/squid/access.log combined    #log文件存放路徑和日志格式
cache_log /var/log/squid/cache.log        #設置緩存日志
logfile_rotate 60                #log輪循 60天
cache_swap_high 95                #cache目錄使用量大於95%時,開始清理舊的cache
cache_swap_low 90                #cache目錄清理到90%時停止。
acl localnet src 192.168.1.0/24            #定義本地網段
http_access allow localnet            #允許本地網段使用
http_access deny all                #拒絕所有
visible_hostname qin1.qin.com            #主機名
cache_mgr qin@qin.com                #管理員郵箱

clip_image001[15]

常用的ACL列表類型:

src              源地址
dst              目標地址
port             端口
srcdomain        源域
dstdomain        目標域
time             訪問時間
maxconn          最大並發連接
url_regex        目標URL地址
urlpath_regex    整個目標URL路徑
acl LAN1 src 192.168.1.0/24
acl PCRang  src  192.168.1.10-192.168.1.20/24
acl PC1 src 192.168.1.66/32
acl Blk_Domain dstdomain .qq.com .kaixin001.com
acl Work_Hours time MTWHF 08:30-17:30
acl Max20_Conn maxconn 20
acl Blk_URL url_regex -i ^rtsp:// ^mms://
acl ksuurl url_regex ^http://www.ksu.edu.tw/cht/.*
acl Blk_Words urlpath_regex -i sex adult
acl RealFile urlpath_regex -i \.rmvb$ \.rm$
acl OverConnLimit maxconn 6

clip_image001[17]

第二部分:准備測試環境

物理機為Win10系統,網關192.168.1.1,自動獲取IP地址上網。
三台虛擬機設置如下:

image
1、RHEL2 作為外網服務器,橋接到物理機無線網卡上,設置固定IP為 192.168.1.200

1
#hostnamectl set-hostname rhel2.rusky.com
#nmcli connection add type ethernet con-name enp0s3 ifname enp0s3 autoconnect yes
#nmcli connection modify enp0s3 ipv4.addresses 192.168.1.200/24 ipv4.gateway 192.168.1.1 ipv4.dns 211.139.29.170 ipv4.method manual connection.autoconnect yes

2、RHEL1 作為內網的一台SQUID代理服務器,部屬squid服務,同時添加兩塊網卡,一塊插到內網“交換機 VirtualBox Host-Only Network #6”,另外一塊連接到內網(橋接到物理機無線網卡上),
如下圖所示:
虛擬網卡 VirtualBox Host-Only Network #6 作為內網的交換機(IP為192.168.100.100)。

001

002

003

#hostnamectl set-hostname rhel1.rusky.com

# nmcli connection down enp0s3  --設置內網IP
# nmcli connection modify enp0s3 ipv4.addresses 192.168.100.1/24 ipv4.gateway 192.168.100.100 ipv4.dns 192.168.100.1 ipv4.method manual connection.autoconnect yes

# nmcli connection down enp0s8   --設置外網IP
# nmcli connection modify enp0s8 ipv4.addresses 192.168.1.201/24 ipv4.gateway 192.168.1.1 ipv4.dns 211.139.29.170 ipv4.method manual connection.autoconnect yes

3、RHEL3作為內網的另外一台客戶端服務器,插到內網“交換機 VirtualBox Host-Only Network #6”,用於測試。

4

主機名:rhel3.rusky.com IP:192.168.100.3

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

第三部分:在外網服務器RHEL2上架設測試網站,在RHEL1上配置SQUID代理,然后從RHEL3通過代理來訪問RHEL2上架設的測試網站。之后完成一些簡單的測試。

1、在RHEL2上架設測試網站

#yum install httpd -y
#firewall-cmd --permanent --add-service=http
#firewall-cmd --reload
#echo lxjtest > /var/www/html/index.html
#systemctl restart httpd

2、在RHEL1上配置squid服務器

#yum install -y squid
#firewall-cmd --permanent --add-port=3128/tcp
#firewall-cmd --reload
#systemctl restart squid

最簡單的代理服務器,安裝完成后,啥也不用修改,默認就可以使用了。內網其它客戶機把RHEL1當做跳板來訪問外網RHEL2上架設的網站。

3、在客戶機RHEL3上訪問測試

直接Ping RHEL2的IP 192.168.1.200 是不通的。
瀏覽器未設置代理服務器之前也無法訪問RHEL2 192.168.1.200 上的網站,設置代理服務器為192.168.100.1:3128之后就可以訪問了。(這就是正向代理)

image

SQUID初始化:

#vi /etc/squid/squid.conf
cache_dir ufs /var/spool/squid 100 16 256    #取消注釋,再執行squid -z才會建立,如果不啟用,則只使用內存,生產環境中一定要取消注釋該行。
#squid -k parse  #檢測配置文件
#systemctl stop squid
#squid -z  #重建緩存,在/var/spool/squid下生成很多緩存目錄。

[root@rhel1 squid]# pwd
/var/spool/squid
[root@rhel1 squid]# ls
00  01  02  03  04  05  06  07  08  09  0A  0B  0C  0D  0E  0F  swap.state
[root@rhel1 squid]# ls 00/
00  0A  14  1E  28  32  3C  46  50  5A  64  6E  78  82  8C  96  A0  AA  B4  BE  C8  D2  DC  E6  F0  FA
01  0B  15  1F  29  33  3D  47  51  5B  65  6F  79  83  8D  97  A1  AB  B5  BF  C9  D3  DD  E7  F1  FB
02  0C  16  20  2A  34  3E  48  52  5C  66  70  7A  84  8E  98  A2  AC  B6  C0  CA  D4  DE  E8  F2  FC
03  0D  17  21  2B  35  3F  49  53  5D  67  71  7B  85  8F  99  A3  AD  B7  C1  CB  D5  DF  E9  F3  FD
04  0E  18  22  2C  36  40  4A  54  5E  68  72  7C  86  90  9A  A4  AE  B8  C2  CC  D6  E0  EA  F4  FE
05  0F  19  23  2D  37  41  4B  55  5F  69  73  7D  87  91  9B  A5  AF  B9  C3  CD  D7  E1  EB  F5  FF
06  10  1A  24  2E  38  42  4C  56  60  6A  74  7E  88  92  9C  A6  B0  BA  C4  CE  D8  E2  EC  F6
07  11  1B  25  2F  39  43  4D  57  61  6B  75  7F  89  93  9D  A7  B1  BB  C5  CF  D9  E3  ED  F7
08  12  1C  26  30  3A  44  4E  58  62  6C  76  80  8A  94  9E  A8  B2  BC  C6  D0  DA  E4  EE  F8
09  13  1D  27  31  3B  45  4F  59  63  6D  77  81  8B  95  9F  A9  B3  BD  C7  D1  DB  E5  EF  F9

#systemctl start squid
#tail -f /var/log/squid/access.log 查看訪問日志
#tail -f /var/log/squid/cache.log  查看cache日志

測試一:通過SQUID服務來阻止某一網段或固定IP的客戶端訪問測試網站。

在RHEL1上修改SQUID配置文件,增加兩行:

# vi /etc/squid/squid.conf

# Example rule allowing access from your local networks.
# Adapt to list your (internal) IP networks from where browsing
# should be allowed
acl lxjtest1 src 192.168.100.3   #也可以設置成一個網段
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 lxjtest1
http_access deny !Safe_ports

# Deny CONNECT to other than secure SSL ports

添加完上面的兩行后,重啟SQUID服務。

在RHEL3客戶機上訪問測試不通,提示被拒絕。如果把上面配置文件第二行修改為allow,則訪問正常。這個設置規則可以參照基礎部分的列表類型來進行設置。

image

但是在其它客戶端上通過代理訪問測試網站正常。

測試二:通過一個列表文件來限制訪問

創建一個列表文件:

# vi /etc/squid/ipblock.list  --名稱隨便寫,把需要限制的IP地址加進來
192.168.100.3
192.168.200/24

#vi /etc/squid/squid.conf --修改主配置文件,添加里面兩行內容

# Example rule allowing access from your local networks.
# Adapt to list your (internal) IP networks from where browsing
# should be allowed
acl ipblocktest  src "/etc/squid/ipblock.list"
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 portsa
http_access deny ipblocktest
http_access deny !Safe_ports

重啟squid服務

# systemctl restart squid.service

通過客戶端訪問測試,可以看到我們配置的限制列表文件已生效了。

生產環境中經常通過限制域名的方式來限制用戶對外網的訪問,比如,新建一個/etc/squid/domainblock.list文件,添加如下內容:
.taobao.com
.qq.com
.jd.com
.toutiao.com
等等來限制員工對外網的訪問。主配置文件添加下面的兩行:

acl domainblock dstdomain "/etc/squid/domainblock.list"

http_access deny domainblock

從基礎內容部分的列表類型可以看出,我們還可以限制用戶在指定的時間范圍內對外網的訪問。SQUID確實功能挺強大。

測試三:ACL列表的另外一種寫法

# vi /etc/squid/squid.conf

acl allowclienttest src 192.168.100.3 192.168.100.123   --可以添加多個IP,空格分隔
http_access allow allowclienttest
http_access deny !allowclienttest
     --加!符號表示拒絕非allowclienttest指定的IP客戶端訪問。

測試四:通過時間范圍控制對外網的訪問

只允許指定客戶端在指定時間范圍內訪問,否則全部拒絕。

# vi /etc/squid/squid.conf

acl allowclienttest src 192.168.100.111 192.168.100.10 192.168.100.123
acl allowtimetest time mtwhf 9:00-17:00
  --指定周一至周五(周六a,周天s),9點至17點的時間范圍
http_access allow allowclienttest allowtimetest
http_access deny all
     --拒絕所有

實驗五、SQUID安全機制:通過htpasswd工具生成認證

其實就是通過apache httpd自帶的htpasswd工具創建一個存有用戶名和密碼的認證文件。之后,配置squid啟動密碼認證機制 auth_param basic program,用自己的basic_ncsa_auth命令去調認證文件。即當你在訪問代理服務時,需要輸入管理員給你指定的用戶名和密碼才能訪問。如果不這么做,則任何用戶只要知道squid服務器的IP和端口就可以通過代理服務器上網,這樣也不安全。

在RHEL1上

安裝httpd-tool工具
# yum install httpd-tools.x86_64

查看認證程序命令位置

[root@rhel1 ~]# rpm -ql squid | grep ncsa
/usr/lib64/squid/basic_ncsa_auth
/usr/share/man/man8/basic_ncsa_auth.8.gz

創建隱藏的用戶密碼文件userlist,並添加用戶testusersquid1

[root@rhel1 ~]# htpasswd -c /etc/squid/.userlist testusersquid1  #-c 創建
New password:
Re-type new password:
Adding password for user testusersquid1

添加testusersquid2用戶

[root@rhel1 ~]# htpasswd  /etc/squid/.userlist testusersquid2 #添加用戶testusersquid2
New password:
Re-type new password:
Adding password for user testusersquid2

(不用加-c參數,否則會把前邊加的用戶覆蓋了)。如果加參數m,則使用MD5加密,有時候客戶端不支持,所以一般不加m參數。

查看認證文件:

[root@rhel1 ~]# cat /etc/squid/.userlist
testusersquid1:$apr1$CRSWnTTt$XnSYo.q3JHgdn5WTRUzwZ/
testusersquid2:$apr1$dG69l9Q/$al8O2ziswVuAIM7gw/otO.

修改主配置文件,加入下面的內容:

#vim /etc/squid/squid.conf
auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid/.userlist   #啟動密碼認證機制,用basic_ncsa_auth命令去調用userlist文件。
auth_param basic children 100
auth_param basic realm Welcome to  lxjtest.rusky.com  web server
auth_param basic credentialsttl 2 hours
acl auth_user proxy_auth REQUIRED
http_access allow auth_user

#當用戶在訪問代理訪問時,需要輸入userlist文件中的配置的用戶名和密碼才能訪問。

image

測試六:透明代理

前面使用的代理方式上網都很麻煩,客戶端瀏覽器必須自己配置代理服務器的IP地址和端口號才能上網。而透明代理則通過在服務器上配置策略,強制用戶走代理服務器上網,所以客戶端瀏覽器不需要做任何設置。實現原理:配置防火牆端口轉發策略來限制用戶上網時自動轉發到代理服務器的3128端口。

修改主配置文件:

#vim /etc/squid/squid.conf
#http_port 3128        #這一行是普通的正向代理,把這行注釋掉,如果不注釋會和下面沖突
http_port 192.168.100.1:3128 transparent    #透明代理加上這行。任何用戶訪問192.168.100.1的3128端口,則自動走透明代理服務。

添加防火牆策略:

如果采用iptables做防火牆,需要打開路由轉發

#iptables -t nat -A PREROUTING -i eth0 -s 192.168.100.0/24 -p tcp --dport 80 -j REDIRECT --to-ports 3128
#iptables -t nat -A PREROUTING -i eth0 -s 192.168.100.0/24 -p tcp --dport 443 -j REDIRECT --to-ports 3128

如果采用firewalld做防火牆

#firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.100.0/24" forward-port port="80" protocol="tcp" to-port="3128" to-addr="192.168.100.1""

#firewall-cmd –reload 重新加載防火牆策略
#內網用戶192.168.100.0/24網段的客戶機如果上外網,則自動轉向代理服務器。

建議采用圖形化的方法添加富規則
#firewall-config
#family選擇ipv4
#element選擇forward-port 規則選擇來源tcp/80轉發到另一個ip為192.168.100.1的3128端口
#source選擇192.168.100.0/24

image

image

測試七:反向代理

反射代理原理:假設 www.cnblogs.com(IP為192.168.1.201) 網站把試驗中的RHEL1作為反向代理服務器,兩塊網卡,一塊連內網,另外一塊連外網(供用戶通過外網訪問),上面安裝SQUID服務,未部屬Apache httpd。然后他們公司內網的4台服務器(192.168.100.2~4)共享同一存儲,每台服務器上都部屬Apache httpd 服務。當有用戶訪問www.cnblogs.com(192.168.1.201)網站時,RHEL1上的反向代理服務會從內網的4服務器中的其中一台上把網頁反向發送給用戶,內網的4個網站由於共享存儲,所以訪問到任何一台服務器上時,網頁內容都是一致的。反向代理服務器RHEL1還起到一個“負載均衡”的作用。

image

在RHEL1上安裝squid,並修改主配置文件如下:

#vim /etc/squid/squid.conf
http_access allow all       #取消之前的規則,開啟全部允許規則
#http_port 3128             #注釋掉正向代理,防止沖突
http_port  192.168.1.201:80 vhost    #當有客戶訪問192.168.1.201 http 服務時,系統反向代理解析為下面的計算機

cache_peer 192.168.100.2 parent 80 0 originserver weight=5 max-conn=30
cache_peer 192.168.100.3 parent 80 0 originserver weight=5 max-conn=30
cache_peer 192.168.100.4 parent 80 0 originserver weight=5 max-conn=30
cache_peer 192.168.100.5 parent 80 0 originserver weight=5 max-conn=30

如果有多台服務器,就添加多條記錄。weight 是權重,比如其中一台服務器的性能比較好,可以把這台服務器的 weight 值設置高一些。max-conn是最大連接數,當連接數達到最大值時,自動“負載”到另外一台服務器。如果其中某一台服務器上的httpd服務停止了,反向代理服務器也會自動到訪問其它內網服務器。

# firewall-cmd --add-port=80/tcp –permanent   --還要添加防火牆策略,允許80端口。雖然RHEL1服務器並未部屬httpd服務,但是它要反向代理內網的httpd服務,所以得開放80端口。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM