cheng029 发表于 2018-12-26 11:55:31

Iptables+L7+Squid

小编:现在的企业网应用中,有些业务和一些重要部门管理人员对于网络的带宽具有较高的要求,最重要的就是速度的稳定性。这就要求我们的网络管理员对于企业网中的带宽进行合理分配,特别是一些基于P2P的软件和应用进行限制,可以很好的解决带宽的压力,满足特殊用户对于网速的需求。今天我们就一起来搭建一种基于Iptables之上,并结合Layer7七层过滤模块和Squid代理来实现对于特殊应用的限制,这就是我们接下来将要要介绍的综合过滤模型:Iptables+L7+Squid.
【基本环境搭建】
一)软件需求及简单说明
linux-2.6.25.19.tar.bz2
说明:由于实验环境使用的内核版本是2.6.18,内核编译过程中并没有加入L7七层过滤模块,所以需要对内核进行重新编译,加入L7七层过滤模块.
iptables-1.4.2.tar.bz2
说明:下载新版iptables源码包,编译安装。
netfilter-layer7-v2.20.tar.gz
说明:下载Layer7补丁源码包,用于给新内核打补丁.
l7-protocols-2009-5-10.tar.gz
说明:下载L7-protocols模式源码包,内嵌的模块用于Iptables进行调用.
二)实现目标
给新内核源码包打上Layer7的补丁,对内核进行重新编译,编译前添加L7的相关模块,最终实现基于L7的七层过滤功能,实现对于应用层的过滤,完成基于L7的过滤的应用.
三)具体步骤
Step1:给新版内核打上L7的补丁.
①解压缩内核源码包和layer7补丁包
tar jxvf linux-2.6.25.19.tar.gz2 -C /usr/src/
tar zxvf netfilter-layer7-v2.20.tar.gz -C /usr/src/
②切换到内核目录下,使用patch工具对内核进行打补丁
cd /usr/src/linux-2.6.25.19/
patch -p1 < /usr/src/netfilter-layer7-v2.20/kernel-2.6.25-layer7-2.20.patch
Step2:配置新内核
①拷贝老的内核的编译配置文件生成新版内核配置文件
cp /boot/config-2.6.18-8.el5 .config
②菜单模式下配置编辑内核编译配置文件,添加L7模块
详细步骤如下:
http://blog.运维网.com/attachment/201305/165047811.png
http://blog.运维网.com/attachment/201305/165059151.png
http://blog.运维网.com/attachment/201305/165111519.png
http://blog.运维网.com/attachment/201305/165116566.png
http://blog.运维网.com/attachment/201305/165122293.png
http://blog.运维网.com/attachment/201305/165125564.png
http://blog.运维网.com/attachment/201305/165129159.png
http://blog.运维网.com/attachment/201305/165131990.png
http://blog.运维网.com/attachment/201305/165135827.png
完成后保存退出,这样就给新内核添加了L7模块了,下面就能进行内核编译了.
Step3:编译和安装新版内核
①编译生成makefile文件.
make
②进行内核的编译及新内核的安装.
make modules_install && make install
Step4:重新编译配置iptables
①卸载已装的iptables,解压缩新版iptables源码包.
rpm -e iptables--nodeps (取消依赖关系)
tar jxvf iptables-1.4.2.tar.bz2 -C /usr/src/
②合并iptables和layer7补丁,使新的iptables能够调用L7模块.
cd /usr/src/netfilter-layer7-v2.20/iptables-1.4.1.1-for-kernel-2.6.20forward/
cp libxt_layer7.c libxt_layer7.man/usr/src/iptables-1.4.2/extensions/
③编译安装iptables
cd /usr/src/iptables-1.4.2/
./configure --prefix=/ --with-ksource=/usr/src/linux-2.6.25.19
make && make install
Step5:安装L7-protocols模式包
tar zxvf l7-protocols-2009-5-10.tar.gz -C /etc/
mv /etc/l7-protocols-2009-5-10/etc/l7-protocols
Step6:rpm安装Squid,并启动服务
mkdir/mnt/cdrom
mount/dev/cdrom/mnt/cdrom
cd/mnt/cdrom/Server
rpm -ivhsquid-2.6.STABLE21-3.e15.i386.rpm
servicesquid start
启动过程如果出现下列错误:
http://blog.运维网.com/attachment/201305/165231861.png
http://blog.运维网.com/attachment/201305/165233697.png
http://blog.运维网.com/attachment/201305/165233405.png
好了,到目前为止,我们的基本环境就算搭建完成了.
下面通过案例来说明这种环境下的具体应用,希望对大家有所启发.
【环境应用举例】
实例一:
①实验拓扑
http://blog.运维网.com/attachment/201305/165513970.png
②实验目的
使用Linux Red Hat做为软件防火墙,模拟LAN内的网络出口防火墙.
添加Iptables规则,实现下列功能
SNAT:内网的用户通过SNAT访问Internet.
DNAT:内网DMZ区域存在服务器,服务内网服务器.
③具体配置
网卡参数配置
http://blog.运维网.com/attachment/201305/165541394.png

为了测试网络的联通性,我们先把iptables的默认规则改为允许,并添加SSH允许的规则.
iptables -t filter -A INPUT-p tcp --dport 22 -j ACCEPT
iptables -t filter -A OUTPUT -p tcp --sport 22 -j ACCEPT
iptables -t filter -P INPUT ACCEPT
iptables -t filter -P OUTPUT ACCEPT
iptables -t filter -P FORWARDACCEPThttp://blog.运维网.com/attachment/201305/165616448.png
修改防火墙的DNS指向
http://blog.运维网.com/attachment/201305/165711128.png

开启防火墙的包过滤功能
http://blog.运维网.com/attachment/201305/165714329.png
http://blog.运维网.com/attachment/201305/165717103.png
http://blog.运维网.com/attachment/201305/165833552.png
再把默认规则改为DROP
iptables -t filter -P INPUT DROP
iptables -t filter -P OUTPUT DROP
iptables -t filter -P FORWARDDROP
http://blog.运维网.com/attachment/201305/165848582.png

【SNAT配置】
配置:
http://blog.运维网.com/attachment/201305/165949849.png
测试:WinXP虚拟机Host-Only虚拟网卡VMnet1
地址参数如下:
http://blog.运维网.com/attachment/201305/170102272.png
访问宽带路由器的LAN口地址测试
http://blog.运维网.com/attachment/201305/170127944.png
分析:这是由于filter链中的FORWORD默认规则为拒绝所有的.
http://blog.运维网.com/attachment/201305/170132912.png
方法:修改规则为ACCEPT,再次测试.
http://blog.运维网.com/attachment/201305/170134552.png
http://blog.运维网.com/attachment/201305/170152980.png
【DNAT配置】
配置:
http://blog.运维网.com/attachment/201305/170918828.png测试:WinXP虚拟机Host-Only虚拟网卡VMnet2,
地址参数如下:
http://blog.运维网.com/attachment/201305/171101720.png
添加远程登录账号,加入到远程登陆组并开启允许远程连接:
http://blog.运维网.com/attachment/201305/171322264.png
http://blog.运维网.com/attachment/201305/171336849.png
http://blog.运维网.com/attachment/201305/171409206.png
http://blog.运维网.com/attachment/201305/171435202.png
http://blog.运维网.com/attachment/201305/171446442.png
http://blog.运维网.com/attachment/201305/171541110.png
http://blog.运维网.com/attachment/201305/171744974.png
http://blog.运维网.com/attachment/201305/172013427.png
宿主机(192.168.102.222)远程登录测试:

http://blog.运维网.com/attachment/201305/172851849.png
http://blog.运维网.com/attachment/201305/172856862.png
http://blog.运维网.com/attachment/201305/172525692.png
实例二:
①实验拓扑
http://blog.运维网.com/attachment/201305/173536240.png
②方案说明
某公司有三个部门
工程部门 地址分配:192.168.2.10-192.168.2.20
软件部门地址分配:192.168.2.21-192.168.2.30
经理办    地址分配:192.168.2.31-192.168.2.40
【限制条件】上班时间(周一至周五 08:00--20:00)
1.工程部门
上班时间
允许ftp【服务器地址为:192.168.102.253】
不允许qq、迅雷、http
下班无限制
2.软件部门
上班时间
允许访问http
不允许非法站点sina
不允许使用迅雷
限制连接数最多3个
不允许使用QQ聊天工具
不允许使用pplive网络电视
不允许看图片
下班后无限制   
3.经理办公室
http 、qq 都可以
下班后无限制
4.dmz区域rdp-server服务器进行发布
③具体配置和相关测试.
1.工程部门
Step1:上班时间允许登录ftp服务器
①iptables -t filter-A FORWARD
-m iprange --src-range 192.168.2.10-192.168.2.20
-mtime --timestart 08:00 --timestop 20:00
--weekdays Mon,Tue,Wed,Thu,Fri
-d192.168.102.253 -p tcp --dport 21 -j ACCEPT
②iptables -t filter -A FORWARD
-m state --stateESTABLISHED,RELATED -j ACCEPT
查看FORWARD表格.
http://blog.运维网.com/attachment/201305/173615391.png工程部门逐级FTP登录测试
网络参数:
http://blog.运维网.com/attachment/201305/173625455.png
登录测试
http://blog.运维网.com/attachment/201305/173631117.png
显示登录成功,但是命令没法使用,缺少下面模块的支持.
http://blog.运维网.com/attachment/201305/173632291.png
再次登录测试,并比较iptables表格中条目在登陆前后的匹配情况.
http://blog.运维网.com/attachment/201305/173639728.png
http://blog.运维网.com/attachment/201305/173641707.png
Step2:上班时间不允许登录Web服务器、QQ聊天工具、迅雷下载
①iptables -t filter -A FORWARD
-m iprange --src-range 192.168.2.10-192.168.10.20
-m time --timestart 08:00 --timestop 20:00
--weekdays Mon,Tue,Wed,Thu,Fri
-m layer7 --l7proto qq -j DROP
②iptables -t filter -A FORWARD
-m iprange --src-range 192.168.2.10-192.168.10.20
-m time --timestart 08:00 --timestop 20:00
--weekdays Mon,Tue,Wed,Thu,Fri
-m layer7 --l7proto xunlei -j DROP
Step3:下班后无限制,周末无限制.
①iptables -t filter -A FORWARD -s 192.168.2.0/24
-m time --timestart 20:01 --timestop 07:59 -o eth1 -j ACCEPT
②iptables -t filter -A FORWARD -s 192.168.2.0/24
-m time --weekdays Sat,Sun-j ACCEPT
【测试】
①修改当前时间为工作日上班时间,测试Web访问
http://blog.运维网.com/attachment/201305/173642110.png
http://blog.运维网.com/attachment/201305/173646878.png
下载安装最新版2013版QQ,进行登录测试

http://blog.运维网.com/attachment/201305/173708582.png

查看系统日志 (tail-f/var/log/messages )
http://blog.运维网.com/attachment/201305/173709996.png

下载新版的迅雷进行下载测试.
http://blog.运维网.com/attachment/201305/173714129.png

查看日志,发现并没有出现matched xunlei 的信息.
(说明:虽然迅雷被一定程度的限制,但是并没有调用L7的模块进行限制,只是Iptables的默认规则,把迅雷给限制了.)
修改当前时间为工作日下班时间,测试Web访问
http://blog.运维网.com/attachment/201305/173714572.png

http://blog.运维网.com/attachment/201305/173722349.png
查看客户端访问前后的数据包匹配状态
http://blog.运维网.com/attachment/201305/173724412.png

http://blog.运维网.com/attachment/201305/173741628.png

下班后,登录QQ测试
http://blog.运维网.com/attachment/201305/173746543.png

修改当前时间为周末,测试Web访问
http://blog.运维网.com/attachment/201305/173747912.png

http://blog.运维网.com/attachment/201305/173754994.png

查看客户端访问前后的数据包匹配状态
http://blog.运维网.com/attachment/201305/173759446.png

http://blog.运维网.com/attachment/201305/173801721.png

2.软件部门
由于软件部门有些限制是基于应用层的内容进行过滤,所以要结合Squid透明代理实现对于内容的过滤.
Step1:Iptables对于软件部门访问Internet进行端口重定向.
①iptables -t nat -A PREROUTING -m iprange
--src-range 192.168.2.21-192.168.2.30 -p tcp --dport 80
-j REDIRECT --to-port 3128
②iptables -t filter -A INPUT -p tcp --dport 3128 -j ACCEPT
③iptables -t filter -A INPUT -m state --state ESTABLISHED -j ACCEPT
④iptables -t filter -A OUTPUT -p tcp --dport 80 -j ACCEPT
⑤iptables -t filter -A OUTPUT -m state --state ESTABLISHED -j ACCEPT
Step2:为了实现用户的DNS的解析,需要对DNS的流量规则设为允许.
①iptables -t filter -A FORWARD -m iprange
--src-range 192.168.2.21-192.168.2.40 -p udp --dport 53 -o eth1 -j ACCEPT
②iptables -t filter -A OUTPUT -p udp --dport 53-j ACCEPT
访问Internet,测试透明代理
网卡参数:
http://blog.运维网.com/attachment/201305/173827984.png
访问Internet测试,会出现下面错误,需要编辑Squid主配置文档.
http://blog.运维网.com/attachment/201305/173837174.png

Step3:编辑Squid主配置文件,添加对非法域名sina、图片和最大连接数的限制.
vim /etc/squid/squid.conf
开启透明代理
http://blog.运维网.com/attachment/201305/173839268.png

设置软件部门的访问权限
http://blog.运维网.com/attachment/201305/173841802.png

Step4:iptables实现对于QQ、pplive、迅雷限制.
①iptables -t filter -A FORWARD -m iprange
--src-range 192.168.2.21-192.168.2.30
-m time --timestart 08:00 --timestop 20:00
--weekdays Mon,Tue,Wed,Thu,Fri
-m layer7 --l7proto qq-j DROP
②iptables -t filter -A FORWARD -m iprange
--src-range 192.168.2.21-192.168.2.30
-m time --timestart 08:00 --timestop 20:00
--weekdays Mon,Tue,Wed,Thu,Fri
-m layer7 --l7proto pplive-j DROP
③iptables -t filter -A FORWARD -m iprange --src-range 192.168.2.21-192.168.2.30
-m time --timestart 08:00 --timestop 20:00
--weekdays Mon,Tue,Wed,Thu,Fri
-m layer7 --l7proto xunlei-j DROP
【测试】
①访问Internet测试(www.baidu.com)
http://blog.运维网.com/attachment/201305/173850103.png
②访问非法站点测试(www.sina.com)
http://blog.运维网.com/attachment/201305/173857400.png

③图片限制测试(www.163.com)
http://blog.运维网.com/attachment/201305/173913362.png

④最大连接数测试(baidu+163+126+sohu).
http://blog.运维网.com/attachment/201305/173938388.png

上班时间QQ登录测试
http://blog.运维网.com/attachment/201305/174053170.png
http://blog.运维网.com/attachment/201305/174057991.png
迅雷下载测试.
http://blog.运维网.com/attachment/201305/174109798.png

查看日志,发现并没有出现matched xunlei 的信息.
(说明:虽然迅雷被一定程度的限制,但是并没有调用L7的模块进行限制,只是Iptables的默认规则,把迅雷给限制了.)
下载安装PPLive,进行测试.
http://blog.运维网.com/attachment/201305/174144285.png
查看日志,发现并没有出现matched pplive 的信息.
(说明:虽然PPlive被一定程度的限制,但是并没有调用L7的模块进行限制,只是Squid的最大连接数和Iptables的规则,把它给限制了,这时我们再次访问网页,发现已经没有连接数可用了)
http://blog.运维网.com/attachment/201305/175439761.png
修改时间为下班测试,进行测试
http://blog.运维网.com/attachment/201305/175439170.png
上网测试
http://blog.运维网.com/attachment/201305/175449506.png
QQ登录测试
http://blog.运维网.com/attachment/201305/175512147.png

3.经理办公室
①iptables -t nat -A PREROUTING -m iprange --src-range 192.168.2.31-192.168.2.40 -p tcp
--dport 80 -j REDIRECT --to-port 3128
②iptables -t filter -IFORWARD 1 -m iprange --src-range 192.168.2.31-192.168.2.40
-m time --timestart 08:00 --timestop 20:00
--weekdays Mon,Tue,Wed,Thu,Fri
-m layer7 --l7proto qq -j ACCEPT
【测试】
网络参数
http://blog.运维网.com/attachment/201305/175549416.png

修改时间为上班时间
http://blog.运维网.com/attachment/201305/175549654.png

上网测试
http://blog.运维网.com/attachment/201305/175557634.png

QQ登录测试
http://blog.运维网.com/attachment/201305/175623477.png

http://blog.运维网.com/attachment/201305/175624569.png

4.dmz区域rdp-server服务器进行发布
①iptables -t nat -A PREROUTING -d 192.168.102.123
-i eth1 -p tcp --dport 3389 -j DNAT --to 192.168.3.100
②iptables -t filter -A FORWARD -d 192.168.3.100
-p tcp --dport 3389-j ACCEPT
【测试】
宿主机远程桌面测试
http://blog.运维网.com/attachment/201305/175625556.png

http://blog.运维网.com/attachment/201305/175627208.png
http://blog.运维网.com/attachment/201305/175644505.png
查看测试前后的策略匹配情况
http://blog.运维网.com/attachment/201305/175652620.png

http://blog.运维网.com/attachment/201305/175657931.png

【总结】
Iptables+L7+Squid在企业中的应用,对于一个网络管路员来说,是至关重要的.但是由于某些条件的限制,本次试验,只是对QQ做了很好的限制,对于一些P2P软件,没有通过L7进行很好的限制,但是还是可以通过其他途径进行限制的。希望广大博友,积极发表自己的观点、讨论,有好的方法的可以留言哦!!!!!!




页: [1]
查看完整版本: Iptables+L7+Squid