Iptables + L7 +squid 实现防火墙功能
为iptables增加layer7补丁(Linux2.6.25内核)
################################################################ 系统环境:RHEL5 [ 2.6.18-8.el5xen ]
软件环境: http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.25.19.tar.bz2 http://www.netfilter.org/projects/iptables/files/iptables-1.4.2.tar.bz2
http://ie.archive.ubuntu.com/sourceforge/l/l7/l7-filter/netfilter-layer7-v2.20.tar.gz http://ie.archive.ubuntu.com/sourceforge/l/l7/l7-filter/l7-protocols-2008-10-04.tar.gz
目标功能: 为iptables增加layer7补丁,实现应用层过滤。
################################################################
一、重新编译内核
1、合并kernel+layer7补丁
shell tar jxvf linux-2.6.25.19.tar.gz2 -C /usr/src/ shell tar zxvf netfilter-layer7-v2.20.tar.gz -C /usr/src/
shell cd /usr/src/linux-2.6.25.19/ shell patch -p1 /usr/src/netfilter-layer7-v2.20/kernel-2.6.25-layer7-2.20.patch
2、配置新内核
shell cp /boot/config-2.6.18-8.el5 .config //偷个懒,沿用旧的内核配置 shell make menuconfig
//配置内核时,在“Networking — Networking Options — Network Packet filtering framework (Netfilter) ”处主要注意两个地方: 1) — Code Netfilter Configuration //将“Netfilter connection tracking suport (NEW)”选择编译为模块(M),需选取此项才能看到layer7支持的配置。 //将layer7、string、state、time、IPsec、iprange、connlimit……等编译成模块,根据需要看着办。
2) — IP: Netfilter Configuration //将“IPv4 connection tracking support (require for NAT)”编译成模块。 //将“Full NAT”下的“MASQUERADE target support”和“REDIRECT target support”编译成模块。
3、编译及安装模块、新内核
shell make make modules_install make install
//编译安装成后后,重启选择使用新的内核(2.6.25.19)引导系统
二、重新编译iptables
1、卸载现有iptables
shell rpm -e iptables iptstat –nodeps
2、合并iptables+layer7补丁
shell tar jxvf iptables-1.4.2.tar.bz2 -C /usr/src/ shell cd /usr/src/netfilter-layer7-v2.20/iptables-1.4.1.1-for-kernel-2.6.20forward/ shell cp libxt_layer7.c libxt_layer7.man /usr/src/iptables-1.4.2/extensions/
3、编译安装
shell cd /usr/src/iptables-1.4.2/
shell ./configure –prefix=/ –with-ksource=/usr/src/linux-2.6.25.19 shell make make install
4、安装l7-protocols模式包
shell tar zxvf l7-protocols-2008-10-04.tar.gz -C /etc/ shell mv /etc/l7-protocols-2008-10-04 /etc/l7-protocols
三、layer7规则示例
1、layer7 match
shell iptables -A FORWARD -m layer7 –l7proto qq -j DROP shell iptables -A FORWARD -m layer7 –l7proto msnmessenger -j DROP shell iptables -A FORWARD -m layer7 –l7proto msn-filetransfer -j DROP shell iptables -A FORWARD -m layer7 –l7proto xunlei -j DROP shell iptables -A FORWARD -m layer7 –l7proto edonkey -j DROP shell iptables -A FORWARD -m layer7 –l7proto bittorrent -j DROP
2、string match
shell iptables -A FORWARD -p udp –dport 53 -m string –string tencent –algo bm -j DROP shell iptables -A FORWARD -p udp –dport 53 -m string –string verycd –algo bm -j DROP shell iptables -A FORWARD -p tcp –dport 80 -m string –string sex –algo bm -j DROP
3、state match
shell iptables -A FORWARD -m state –state NEW -p tcp ! –syn -j DROP shell iptables -A FORWARD -m state –state ESTABLISHED,RELATED -j ACCEPT
4、connlimit match
shell iptables -A FORWARD -p tcp –syn -m connlimit –connlimit-above 100 –connlimit-mask 24 -j DROP
5、time match
shell iptables -A FORWARD -p tcp –dport 80 -m time –timestart 8:00 –timestop 17:00 –weekdays Mon,Tue,Wed,Thu,Fri -j ACCEPT
案例
三个部门
工程部门 192.168.145.10-192.168.145.20
软件部门 192.168.145.21-192.168.145.30
经理办公室 192.168.145.31-192.168.145.40
工程部门 上班时间 ftp服务器 不允许聊天 qq 不允许http上网
下班后无限制
软件部门 上班时间 http 新浪 中华网站 无限制
音乐站点 www.552211.com 限制内容 【无声音】
不允许聊天 qq
下班后无限制
经理办公室 上班时间 http qq smtp pop3
下班后无限制
配置
145.0网段都可上网
打开数据包转发 和打开ftp模块
关闭forward
工程部上班时间只能使用ftp
回来的
下班时候无限制
软件部
允许访问用squid
地址范围
Vim /etc/squid/squid.conf
允许访问站点
上班时间
编辑规则
透明代理
Service squid restart
端口重定向
域名解析
经理办公室
重启squid服务器
Smtp pop3
人要有梦想,有了梦想才会努力奋斗,