红联Linux门户
Linux帮助

iptables--L7-filter实现高级管理

发布时间:2014-09-27 09:37:05来源:linux网站作者:roqi410

l7-filter是一个定义在应用层上的来过滤数据包的。它可以按照不同的应用进行过滤,比如过滤聊天工具QQ、MSN、eDonkey等应用。


接下来讲述l7-filter的编译安装和使用:


1、准备工作:
内核:linux-2.6.28.10.tar.gz
l7协议:l7-protocols-2009-05-28.tar.gz
l7过滤包:netfilter-layer7-v2.22.tar.gz


2、解压内核,为内核打补丁,编译内核
# tar zxvf  linux-2.6.28.10.tar.gz  -C  /usr/src
# tar zxvf  netfilter-layer7-v2.22.tar.gz  -C  /usr/src
# ln –s  /usr/src/linux-2.6.28.10/   /usr/src/linux
# cd /usr/src/linux/
# patch -p1  <  ../netfilter-layer7-v2.22/kernel-2.6.25-2.6.28-layer7-2.22.patch
# cp /boot/config-2.6.18-164.el5  /usr/src/linux/.config
# make  menuconfig

找到这个位置Networking support → Networking Options →
Network packet filtering framework →Code Netfilter Configuration,选中以下几个模块:
<M>  Netfilter connection tracking support
<M>  “layer7” match support
<M>  “string” match support
<M>  “time”  match support
<M>  “iprange”  match support
<M>  “connlimit”  match support
<M>  “state”  match support
<M>  “conntrack”  connection  match support
<M>  “mac”  address  match support
<M>  "multiport" Multiple port match support
然后在Networking support → Networking Options →
Network packet filtering framework → IP: Netfilter Configuration下
选择如下几个模块:
<M>IPv4 connection tracking support (required for NAT)
<M>Full NAT
<M>MASQUERADE target support    
<M>NETMAP target support
<M>REDIRECT target support
当然可以根据自己的需要,进行定制自己的内核。
内核定制完成之后就可以进入编辑的阶段了
# make
# make modules_install
# make install
然后打开/boot/grub/grub.conf 把默认的启动项设置为新内核
重新启动系统


3、卸载以前安装的iptables,编译安装iptables-1.4.6
# cp /etc/init.d/iptables ~/iptables  备份iptables脚本,方便后面应用
# cp /etc/sysconfig/iptables-config .  备份iptables的配置文件
# rpm  -e  iptables-ipv6  iptables  iptstate  --nodeps
# tar jxvf iptables-1.4.6.tar.bz2 –C  /usr/src
# cd /usr/src/iptables-1.4.6
# cp ../netfilter-layer7-v2.22/iptables-1.4.3forward-for-kernel \
-2.6.20forward/libxt_layer7.*   ./extensions/
# ./configure  --prefix=/usr  --with-ksource=/usr/src/linux
# make
# make install
编译完成


4、安装l7所对应的协议
# tar zxvf l7-protocols-2009-05-28.tar.gz
# cd l7-protocols-2009-05-28
# make install
# vim ~/iptables
修改内容
 :%s#/sbin/$IPTABLES#/usr/sbin/$IPTABLES#g 全局替换内容
# mv ~/iptables  /etc/rc.d/init.d/
# service iptables start
这样整个环境的设置就算是完成,接下来就是应用了。


5、使用l7-filter来过滤指定的流量,达到预想的设置效果:
#iptables -A FORWARD -p tcp -s 192.168.1.12 -d 192.168.1.13  -o eth0 -j DROP(禁止IP)
#iptables -A FORWARD -p tcp -s 192.168.1.11/24 -d www.qq.com -o eth0 -j DROP(禁止网段)
#安全规则类似windows防火墙
#iptables -A INPUT -p tcp –-dport 1:1024 -j DROP
#iptables -A INPUT -p udp –-dport 1:1024 -j DROP   这两条可以防止nmap探测
防止攻击扫描
防止同步包洪水(Sync Flood)
# iptables -A FORWARD -p tcp –-syn -m limit –-limit 1/s -j ACCEPT
也可以写作
#iptables -A INPUT -p tcp –-syn -m limit –-limit 1/s -j ACCEPT
–-limit 1/s 限制syn并发数每秒1次,可以根据自己的需要修改
防止各种端口扫描
# iptables -A FORWARD -p tcp –-tcp-flags SYN,ACK,FIN,RST RST -m limit –-limit 1/s -j ACCEPT
Ping洪水攻击(Ping of Death)
# iptables -A FORWARD -p icmp –-icmp-type echo-request -m limit –-limit 1/s -j ACCEPT
# Null Scan(possibly)XiKc.om
iptables -A INPUT -i eth0 -p tcp –-tcp-flags ALL NONE -j DROP
iptables NAT 策略:
#iptables -t nat -A PREROUTING -p tcp --dport 80 -d $INTERNET_ADDR -j DNAT –-to-destination 192.168.100.2
只允许访问指定的网段:
#iptables -A Filter -d www.linux.com -j ACCEPT
#iptables -A Filter -d www.linux.net -j ACCEPT
开放多个端口的例子:
#iptables -A Filter -p tcp -m multiport –-source-port 22,53,80,110 -s 192.168.20.3 -j REJECT
#iptables -A Filter -p tcp –source-port 2:80 -s 192.168.20.3 -j REJECT
指定时间上网
#iptables -A Filter -s 10.10.10.253 -m time –-timestart 6:00 –-timestop 11:00 –-days Mon,Tue,Wed,Thu,Fri,Sat,Sun -j DROP
#iptables -A Filter -m time –-timestart 12:00 –-timestop 13:00 –-days Mon,Tue,Wed,Thu,Fri,Sat,Sun -j ACCEPT
#iptables -A Filter -m time –-timestart 17:30 –-timestop 8:30 –-days Mon,Tue,Wed,Thu,Fri,Sat,Sun -j ACCEPT
基于MAC,只能收发邮件,其它都拒绝
#iptables -I Filter -m mac –-mac-source 00:0A:EB:97:79:A1 -j DROP
#iptables -I Filter -m mac -–mac-source 00:0A:EB:97:79:A1 -p tcp --dport 25 -j ACCEPT
#iptables -I Filter -m mac –-mac-source 00:0A:EB:97:79:A1 -p tcp --dport 110 -j ACCEPT