iptables就是软件防火墙,它能给予ip数据包过滤,应用层(内容)过滤,传输层(状态、端口)过滤。

iptables 2中表格

nat filter mangle

5种转换链

172628369.png

常用的动作

-j  ACCEPT(允许)

DROP(拒绝)

MASQUERQDE(将地址映射到出口地址)

REDIRECT ip重新定位)

现在的软件有很多端口,不容易过滤。L7,它将常用的软件的所有端口及属性,分析出来,供我们使用,我们只要引用它即可过滤此软件。

而,我们系统自带的iptables不支持l7,所以,我们要重新编译iptables

卸载iptables

[root@localhost ~]# cd /etc/init.d/

[root@localhost init.d]# cp -p iptables /tmp

[root@localhost init.d]# rpm -e iptables --nodeps

172646743.png

一般的内核并没有编译iptables的模块,因此,要重新编译内核,可以在官网下载https://www.kernel.org/(我们用的是linux-2.6.25.19.tar.bz2

make menuconfig

   //配置内核时,在“Networking ---> Networking Options ---> Network Packet filtering framework (Netfilter) ”处主要注意两个地方:

   1) ---> Core Netfilter Configuration

       //将“Netfilter connection tracking suport (NEW)”选择编译为模块(M),需选取此项才能看到layer7支持的配置。

       //layer7stringstatetimeIPseciprangeconnlimit……等编译成模块,根据需要看着办。

   2) ---> IP: Netfilter Configuration

       //将“IPv4 connection tracking support (require for NAT)”编译成模块。

       //将“Full NAT”下的“MASQUERADE target support”和“REDIRECT target support”编译成模块。

合并kernellayer7补丁

[root@localhost ~]#tar -zxvf netfilter-layer7-v2.20.tar.gz -C /usr/src/

[root@localhost ~]# cd /usr/src/linux-2.6.25.19/

[root@localhostlinux-2.6.25.19]#patch -p1 < /usr/src/netfilter-layer7-v2.20/kernel-2.6.25-layer7-2.20.patch

合并iptableslayer7补丁

[root@localhost ~]# tar jxvf iptables-1.4.2.tar.bz2 -C /usr/src/ #就将其解压

[root@localhost ~]# cd /usr/src/netfilter-layer7-v2.20/iptables-1.4.1.1-for-kernel-2.6.20forward/ #进入

[root@localhost iptables-1.4.1.1-for-kernel-2.6.20forward]# cp libxt_layer7.c libxt_layer7.man /usr/src/iptables-1.4.2/extensions/ #layer7的内容拷到iptables

编译安装

[root@localhost iptables-1.4.1.1-for-kernel-2.6.20forward]# cd /usr/src/iptables-1.4.2/

[root@localhost iptables-1.4.2]# ./configure --prefix=/ --with-ksource=/usr/src/linux-2.6.25.19

[root@localhost iptables-1.4.2]# make && make install

172710317.png

安装l7-protocols模式包

[root@localhost ~]# tar -zxvf l7-protocols-2009-05-10.tar.gz -C /etc/# 解压

[root@localhost ~]# mv /etc/l7-protocols-2009-05-10 /etc/17-protocols #重命名

开启路由转发功能

[root@localhost ~]# vim /etc/sysctl.conf

 7 net.ipv4.ip_forward = 1

172723418.png

为了能够ssh,我们做了包过滤

[root@localhost ~]# iptables -t filter -A INPUT -s 192.168.102.198 -p tcp --dport 22 -j ACCEPT

[root@localhost ~]# iptables -t filter -A OUTPUT -d 192.168.102.198 -p tcp --sport 22 -j ACCEPT

为了能够使内网能够上网 ,我做了snat  

[root@localhost ~]# iptables -t nat -A POSTROUTING  -o eth0 -j MASQUERADE

拒绝所有

[root@localhost ~]# iptables -P INPUT DROP

[root@localhost ~]# iptables -P OUTPUT DROP

[root@localhost ~]#

实验拓扑图:

172741783.png

测试

192.168.1.10 访问百度

172757909.png

下面。我们过滤qq

正常情况

172820357.png

设置过滤qq

[root@localhost ~]# iptables -A FORWARD -m layer7 --l7proto qq -j DROP

172929251.png

设置后,就不能登陆了

查看日志

[root@localhost ~]# tail -f /var/log/messages

172945559.png

L7匹配了qq

l7拦截同花顺

正常情况下

173005361.png

设置过滤同花顺

[root@localhost ~]# iptables -A FORWARD -m layer7 --l7proto tonghuashun  -j DROP

173023460.png

不能登陆了

查看服务器日志

173039156.png

匹配了同花顺

限制下载速度

正常情况下100kb/s

173055928.png

设置限制下载速度

[root@localhost ~]# iptables -A FORWARD -d 192.168.1.0/24 -m limit --limit 40/s -j ACCEPT

[root@localhost ~]# iptables -A FORWARD -d 192.168.1.0/24  -j DROP  

可以看到能够限制住

173112508.png