ACL访问控制

aiofo2022-08-15  210

在日常工作中,企业员工一般是通过公司内部的网关服务器来访问互联网,当将Squid服务程序部署为公司网络的网关服务器后,Squid服务程序的访问控制列表(ACL)功能将发挥它的用武之地。它可以根据指定的策略条件来缓存数据或限制用户的访问。比如很多公司会分时段地禁止员工逛淘宝、打网页游戏,这些禁止行为都可以通过Squid服务程序的ACL功能来实现。大家如果日后在人员流动较大的公司中从事运维工作,可以牢记本节内容,在公司网关服务器上部署的Squid服务程序中添加某些策略条件,禁止员工访问某些招聘网站或竞争对手的网站,没准还能有效降低员工的流失率。

Squid服务程序的ACL是由多个策略规则组成的,它可以根据指定的策略规则来允许或限制访问请求,而且策略规则的匹配顺序与防火墙策略规则一样都是由上至下;在一旦形成匹配之后,则立即执行相应操作并结束匹配过程。为了避免ACL将所有流量全部禁止或全部放行,起不到预期的访问控制效果,运维人员通常会在ACL的最下面写上deny all或者allow all语句,以避免安全隐患。

下面的4个实验向大家演示Squid服务程序的ACL功能有多么强大。

实验1:只允许IP地址为192.168.10.20的客户端使用服务器上的Squid服务程序提供的代理服务,禁止其余所有的主机代理请求。

下面的配置文件依然是Squid服务程序的配置文件,但是需要留心配置参数的填写位置。如果写的太靠前,则有些Squid服务程序自身的语句都没有加载完,也会导致策略无效。当然也不用太靠后,大约在26~32行的位置就可以,而且采用分行填写的方式也便于日后的修改。

[root@linuxprobe ~]# vim /etc/squid/squid.conf

1 #

2 # Recommended minimum configuration:

3 #

4

5 # Example rule allowing access from your local networks.

6 # Adapt to list your (internal) IP networks from where browsing

7 # should be allowed

8 acl localnet src 10.0.0.0/8 # RFC1918 possible internal network

9 acl localnet src 172.16.0.0/12 # RFC1918 possible internal network

10 acl localnet src 192.168.0.0/16 # RFC1918 possible internal network

11 acl localnet src fc00::/7 # RFC 4193 local private network range

12 acl localnet src fe80::/10 # RFC 4291 link-local (directly plugged) mac hines

13

14 acl SSL_ports port 443

15 acl Safe_ports port 80 # http

16 acl Safe_ports port 21 # ftp

17 acl Safe_ports port 443 # https

18 acl Safe_ports port 70 # gopher

19 acl Safe_ports port 210 # wais

20 acl Safe_ports port 1025-65535 # unregistered ports

21 acl Safe_ports port 280 # http-mgmt

22 acl Safe_ports port 488 # gss-http

23 acl Safe_ports port 591 # filemaker

24 acl Safe_ports port 777 # multiling http

25 acl CONNECT method CONNECT

26 acl client src 192.168.10.20

27 #

28 # Recommended minimum Access Permission configuration:

29 #

30 # Deny requests to certain unsafe ports

31 http_access allow client

32 http_access deny all

33 http_access deny !Safe_ports

34 [root@linuxprobe ~]# systemctl restart squid

上面的配置参数其实很容易理解。首先定义了一个名为client的别名。这其实类似于13.6节讲解的DNS分离解析技术,当时我们分别定义了两个名为china与american的别名变量,这样当再遇到这个别名时也就意味着与之定义的IP地址了。保存配置文件后重启Squid服务程序,这时由于客户端主机的IP地址不符合我们的允许策略而被禁止使用代理服务,如图16-8所示。

 

图16-8 使用代理服务浏览网页失败

实验2:禁止所有客户端访问网址中包含linux关键词的网站。

Squid服务程序的这种ACL功能模式是比较粗犷暴力的,客户端访问的任何网址中只要包含了某个关键词就会被立即禁止访问,但是这并不影响访问其他网站。

[root@linuxprobe ~]# vim /etc/squid/squid.conf

1 #

2 # Recommended minimum configuration:

3 #

4

5 # Example rule allowing access from your local networks.

6 # Adapt to list your (internal) IP networks from where browsing

7 # should be allowed 8 acl localnet src 10.0.0.0/

8 # RFC1918 possible internal network

9 acl localnet src 172.16.0.0/12 # RFC1918 possible internal network

10 acl localnet src 192.168.0.0/16 # RFC1918 possible internal network

11 acl localnet src fc00::/7 # RFC 4193 local private network range

12 acl localnet src fe80::/10 # RFC 4291 link-local (directly plugged) mac hines

13

14 acl SSL_ports port 443

15 acl Safe_ports port 80 # http

16 acl Safe_ports port 21 # ftp

17 acl Safe_ports port 443 # https

18 acl Safe_ports port 70 # gopher

19 acl Safe_ports port 210 # wais

20 acl Safe_ports port 1025-65535 # unregistered ports

21 acl Safe_ports port 280 # http-mgmt

22 acl Safe_ports port 488 # gss-http

23 acl Safe_ports port 591 # filemaker

24 acl Safe_ports port 777 # multiling http

25 acl CONNECT method CONNECT

26 acl deny_keyword url_regex -i linux

27 #

28 # Recommended minimum Access Permission configuration:

29 #

30 # Deny requests to certain unsafe ports

31 http_access deny deny_keyword

32 http_access deny !Safe_ports

33 [root@linuxprobe ~]# systemctl restart squid

建议大家在进行实验之前,一定要先把前面实验中的代码清理干净,以免不同的实验之间产生冲突。在当前的实验中,我们直接定义了一个名为deny_keyword的别名,然后把所有网址带有linux关键词的网站请求统统拒绝掉。当客户端分别访问带有linux关键词和不带有linux关键词的网站时,其结果如图16-9所示。

图16-9 当客户端分别访问带有linux关键词和不带linux关键词的网站时,所呈现的结果

实验3:禁止所有客户端访问某个特定的网站。

在实验2中,由于我们禁止所有客户端访问网址中包含linux关键词的网站,这将造成一大批网站被误封,从而影响同事们的正常工作。其实通过禁止客户端访问某个特定的网址,也就避免了误封的行为。下面按照如下所示的参数配置Squid服务程序并重启,然后进行测试,其测试结果如图16-10所示。

[root@linuxprobe ~]# vim /etc/squid/squid.conf

24 acl Safe_ports port 777 # multiling http

25 acl CONNECT method CONNECT

26 acl deny_url url_regex http://www.linuxcool.com 

27 #

28 # Recommended minimum Access Permission configuration:

29 #

30 # Deny requests to certain unsafe ports

31 http_access deny deny_url

32 http_access deny !Safe_ports

33 [root@linuxprobe ~]# systemctl restart squid

图16-10 无法使用代理服务访问这个特定的网站

实验4:禁止员工在企业网内部下载带有某些后缀的文件。

在企业网络中,总会有一小部分人利用企业网络的高速带宽私自下载资源(比如游戏安装文件、电影文件等),从而对其他同事的工作效率造成影响。通过禁止所有用户访问.rar或.avi等后缀文件的请求,可以防止他们继续下载资源,让他们知难而退。下面按照如下所示的参数配置Squid服务程序并重启,然后进行测试,其测试结果如图16-11所示。

如果这些员工是使用迅雷等P2P下载软件来下载资源的话,就只能使用专业级的应用防火墙来禁止了。

[root@linuxprobe ~]# vim /etc/squid/squid.conf

24 acl Safe_ports port 777 # multiling http

25 acl CONNECT method CONNECT

26 acl badfile urlpath_regex -i .mp3$ .rar$

27 #

28 # Recommended minimum Access Permission configuration:

29 #

30 # Deny requests to certain unsafe ports

31 http_access deny badfile

32 http_access deny !Safe_ports

33 [root@linuxprobe ~]# systemctl restart squid

图16-11 无法使用代理服务下载具有指定后缀的文件

转载请注明原文地址:https://www.aiofo.com/read-1439.html