Squid訪問控制方法的實(shí)例
《Squid訪問控制:ACL元素以及訪問列表》這篇文章為大家詳細(xì)講述了ACL元素以及http_access訪問控制列表的語法以及使用過程中需要注意的問題,下面給出使用這些訪問控制方法的實(shí)例:
(1)允許網(wǎng)段61.0.3.188/24以及172.190.96.33/24內(nèi)的所有客戶機(jī)訪問代理服務(wù)器,并且允許在文件/etc/squid/guest列出的客戶機(jī)訪問代理服務(wù)器,除此之外的客戶機(jī)將拒絕訪問本地代理服務(wù)器:
acl clients src 61.0.3.188/24 172.190.96.33/24
acl guests src “/etc/squid/guest”
acl all src 0.0.0.0/0.0.0.0
http_access allow clients
http_access allow guests
http_access deny all
其中,文件“/etc/squid/guest”中的內(nèi)容為:
172.168.10.3/24
210.113.24.8/16
10.0.1.24/25
(2)允許域名為job.net、gdfq.edu.cn的兩個(gè)域訪問本地代理服務(wù)器,其他的域都將拒絕訪問本地代理服務(wù)器:
acl permitted_domain src job.net gdfq.edu.cn
acl all src 0.0.0.0/0.0.0.0
http_access allow permitted_domain
http_access deny all
(3)使用正則表達(dá)式,拒絕客戶機(jī)通過代理服務(wù)器訪問包含有諸如“sexy”等關(guān)鍵字的網(wǎng)站:
acl deny_url url_regex –i sexy
http_access deny deny_url
(4)拒絕客戶機(jī)通過代理服務(wù)器訪問文件中指定IP或者域名的網(wǎng)站,其中文件/etc/squid/ deny_ip中存放有拒絕訪問的IP地址,文件/etc/squid/deny_dns中存放有拒絕訪問的域名:
acl deny_ip dst “etc/squid/deny_ip”
acl deny_dns dst “etc/squid/deny_dns”
http_access deny deny_ip
http_access deny deny_dns
(5)允許和拒絕指定的用戶訪問指定的網(wǎng)站,其中,允許客戶1訪問網(wǎng)站http://www.sina. com.cn,而拒絕客戶2訪問網(wǎng)站http://www.163.com:
acl client1 src 192.168.0.118
acl client1_url url_regex ^http://www.sina.com.cn
acl client2 src 192.168.0.119
acl client2_url url_regex ^http://www.163.com
http_access allow client1 client1_url
http_access deny client2 client2_url
(6)允許所有的用戶在規(guī)定的時(shí)間內(nèi)(周一至周四的8:30到20:30)訪問代理服務(wù)器,只允許特定的用戶(系統(tǒng)管理員,其網(wǎng)段為: 192.168.10.0/24)在周五下午訪問代理服務(wù)器,其他的在周五下午一點(diǎn)至六點(diǎn)一律拒絕訪問代理服務(wù)器:
acl allclient src 0.0.0.0/0.0.0.0
acl administrator 192.168.10.0/24
acl common_time time MTWH 8:30-20:30
acl manage_time time F 13:00-18:00
http_access allow allclient common_time
http_access allow administrator manage_time
http_access deny manage_time
(7)/etc/squid.conf,系統(tǒng)軟件包提供、推薦的最小化配置如下,用戶可以根據(jù)實(shí)際情況來進(jìn)行定制
acl all src 0.0.0.0/0.0.0.0
acl manager proto cache_object
acl localhost src 192.168.10.3/255.255.255.255
acl SSL_ports port 443 563
acl Safe_ports port 80 # http
acl Safe_ports port 21 # ftp
acl Safe_ports port 443 563 # https, snews
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 Safe_ports port 901 # SWAT
acl purge method PURGE
acl CONNECT method CONNECT
(...)
# Only allow cachemgr access from localhost
http_access allow manager localhost
http_access deny manager
# Only allow purge requests from localhost
http_access allow purge localhost
http_access deny purge
# Deny requests to unknown ports
http_access deny !Safe_ports
# Deny CONNECT to other than SSL ports
http_access deny CONNECT !SSL_ports
#
# INSERT YOUR OWN RULE(S)HERE TO ALLOW ACCESS FROM YOUR CLIENTS
#
http_access allow localhost
# And finally deny all other access to this proxy
http_access deny all
#Default:
# icp_access deny all
#
#Allow ICP queries from eveyone
icp_access allow all
配置帶認(rèn)證的代理服務(wù)抑制非法用戶使用代理服務(wù)
默認(rèn)時(shí),Squid本身不帶任何認(rèn)證程序,但是可以通過外部認(rèn)證程序來實(shí)現(xiàn)用戶認(rèn)證。一般有以下的認(rèn)證程序:LDAP認(rèn)證、SMB認(rèn)證、基于mysql的認(rèn)證、基于sock5的密碼認(rèn)證和基于Radius的認(rèn)證。
下面介紹常用的ncsa實(shí)現(xiàn)的認(rèn)證,ncsa是Squid源代碼包自帶的認(rèn)證程序之一,從squid 2.5開始都包含了ncsa的模塊。在Red Hat Enterprise Linux 發(fā)行套件的/usr/lib/squid目錄下可以找到ncsa_auth文件。
要使用該認(rèn)證服務(wù),首先需要?jiǎng)?chuàng)建認(rèn)證用戶和密碼:
#htpasswd -c /usr/local/squid/etc/ps_file guest
如果是以后添加用戶的話就把-c的參數(shù)去掉。
然后,再更改/etc/squid/squid.conf主配置文件,添加如下:
//配置認(rèn)證文件和用戶文件
auth_param basic program /usr/lib/squid/ncsa_auth /usr/local/squid/etc/ ps_file
//指定認(rèn)證程序的進(jìn)程數(shù)
auth_param basic children 5
//代理服務(wù)器的名稱
auth_param basic realm Squid proxy-caching web server
//認(rèn)證有效時(shí)間為2小時(shí)
auth_param basic credentialsttl 2 hours
//只有認(rèn)證用戶才能訪問
acl normal proxy_auth REQUIRED
http_Access allow normal
最后,重啟squid服務(wù)即可。在瀏覽器里配上這個(gè)代理,打開任意網(wǎng)站,如果彈出了輸入用戶名和密碼的對話框,就證明配置成功了。