iptables简介:

iptables 是与 Linux 内核集成的 IP 信息包过滤系统。如果 Linux 系统连接到因特网或 LAN、服务器或连接 LAN 和因特网的代理服务器, 则该系统有利于在 Linux 系统上更好地控制 IP 信息包过滤和防火墙配置。

netfilter/iptables IP 信息包过滤系统是一种功能强大的工具,可用于添加、编辑和除去规则,这些规则是在做信息包过滤决定时,防火墙所遵循和组成的规则。这些规则存储在专用的信 息包过滤表中,而这些表集成在 Linux 内核中。在信息包过滤表中,规则被分组放在我们所谓的链(chain)中。

虽然 netfilter/iptables IP 信息包过滤系统被称为单个实体,但它实际上由两个组件netfilter 和 iptables 组成。

netfilter 组件也称为内核空间(kernelspace),是内核的一部分,由一些信息包过滤表组成,这些表包含内核用来控制信息包过滤处理的规则集。

iptables 组件是一种工具,也称为用户空间(userspace),它使插入、修改和除去信息包过滤表中的规则变得容易。除非您正在使用 Red Hat Linux 7.1 或更高版本,否则需要下载该工具并安装使用它。

重新编译linux内核

一、合并kernel+layer7补丁

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

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

  2、配置新内核

[root@localhost linux-2.6.25.19]#cp /boot/config-2.6.18-8.el5 .config    //沿用旧的内核配置
[root@localhost linux-2.6.25.19]#make menuconfig

在“Networking --> Networking Options --&gt; Network Packet filtering framework (Netfilter) ”--&gt; Code Netfilter Configuration
将“Netfilter connection tracking suport (NEW)”选择编译为模块(M),需选取此项才能看到layer7支持的配置。

配置如下:     

1 2 3 4 5

将layer7、string、state、time、IPsec、iprange、connlimit,ftp等编译成模块。

6  8 11

9 10  12

2) ---&gt; IP: Netfilter Configuration
将“IPv4 connection tracking support (require for NAT)”编译成模块。

15

13

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

14 
3、编译及安装模块、新内核
[root@localhost linux-2.6.25.19]#make && make modules_install && make install
编译安装成后后,重启选择使用新的内核(2.6.25.19)引导系统

[root@localhost linux-2.6.25.19]#init 6

二、重新编译iptables
    1、卸载现有iptables

[root@localhost ~]#rpm -e iptables iptstat --nodeps
    2、合并iptables+layer7补丁
[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/
3、编译安装

root@localhost ~]#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

安装l7-protocols模式包
[root@localhost ~]#tar zxvf l7-protocols-2008-10-04.tar.gz -C /etc/
[root@localhost ~]#mv /etc/l7-protocols-2008-10-04 /etc/l7-protocols

二,iptables 模块介绍
  1. string(字符串匹配,可以用做内容过滤)

下面是例子:

iptables -I FORWARD -m string --string "腾讯" -j DROP
iptables -I FORWARD -s 192.168.3.159 -m string --string "qq.com" -j DROP
iptables -I FORWARD -d 192.168.3.0/24 -p tcp --sport 80 -m string --string "广告" -j DROP 

2. connlimit(同时连接个数限制匹配)

cat connlimit/info

这个增加一个iptables匹配允许你限制每个客户ip地址的并发tcp连接,即同时连接到一个服务器个数. 
例子:

allow 2 telnet connections per client host (允许每个客户机同时两个telnet连接)
iptables -p tcp --syn --dport 23 -m connlimit --connlimit-above 2 -j REJECT 
iptables -p tcp --syn --dport 23 -m connlimit ! --connlimit-above 2 -j ACCEPT
这下面例子限制80端口最多同时16个连接请求) 
iptables -p tcp --syn --dport 80 -m connlimit --connlimit-above 16 --connlimit-mask 24 -j REJECT
模块 connlimit 作用:连接限制 
--connlimit-above n 限制为多少个
--connlimit-mask n 这组主机的掩码,默认是connlimit-mask 32 ,即每ip.
例如:只允许每个ip同时5个80端口转发,超过的丢弃:
iptables -I FORWARD -p tcp --syn --dport 80 -m connlimit --connlimit-above 5 -j DROP
例如:只允许每组C类ip同时10个80端口转发:
iptables -I FORWARD -p tcp --syn --dport 80 -m connlimit --connlimit-above 10 --connlimit-mask 24 -j DROP
例如:为了防止DOS太多连接进来,那么可以允许最多15个初始连接,超过的丢弃.
/sbin/iptables -A INPUT -s 192.186.1.0/24 -p tcp --syn -m connlimit --connlimit-above 15 -j DROP
/sbin/iptables -A INPUT -s 192.186.1.0/24 -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT 
3. time(时间匹配)

Match only if today is one of the given days. (format: Mon,Tue,Wed,Thu,Fri,Sat,Sun ; default everyday)
(只是匹配已经给出的天,格式Mon,Tue,Wed,Thu,Fri,Sat,Sun ;默认每天)
(开始日期 日期)
Match only if it is after `date' (Inclusive, format: YYYY]]]] h,m,s start from 0 ; default to 1970)
(只是匹配这个开始日期值之后的(包括,格式: YYYY]]]] h,m,s start from 0 ; 默认是1970):
Match only if it is before `date' (Inclusive, format: YYYY]]]] h,m,s start from 0 ; default to 2037)
(只是匹配这个开始日期值之前的(包括,格式: YYYY]]]] h,m,s start from 0 ; 默认是2037):
Example: (例子:)
-A INPUT -m time --timestart 8:00 --timestop 18:00 --days Mon,Tue,Wed,Thu,Fri
will match packets that have an arrival timestamp in the range 8:00-&gt;18:00 from Monday to Friday.
(上面将匹配从到达日期是星期一至星期五时间从8:00至18:00的包)
-A OUTPUT -m time --timestart 8:00 --timestop 18:00 --Days Mon --date-stop 2010
will match the packets (locally generated) that have a departure timestamp in the range 8:00-&gt;18:00 on Monday only, until 2010
(上面将匹配本地产生的时间范围直到2010年为止的每个星期一8:00至18:00的包)
NOTE: the time match does not track changes in daylight savings time

4. iprange (ip范围匹配)  

This patch makes possible to match source/destination IP addresses against inclusive IP address ranges.
翻译: 这个补丁令匹配源/目标 IP地址可以倚着给出的地址范围进行匹配
Examples:(例子)
iptables -A FORWARD -m iprange --src-range 192.168.1.5-192.168.1.124 -j ACCEPT
这个例子是允许源ip地址范围192.168.1.5-192.168.1.124的包通过
iptables -A FORWARD -m iprange --dst-range 10.0.0.0-10.255.255.255 -j ACCEPT
这个例子是允许目标ip地址范围10.0.0.0-10.255.255.255的包通过

5. geoip(根据地理位置匹配)

This patch makes possible to match a packet by its source or destination country.
翻译:这个补丁令一个包能够根据源或目的国家(地区)匹配
GeoIP options: (选项:)
--src-cc, --source-country country
Match packet coming from (one of) the specified country(ies)
根据包的来源(或非来源)地区匹配
--dst-cc, --destination-country country
Match packet going to (one of) the specified country(ies)
根据包的去向(或非去向)地区匹配
NOTE: The country is inputed by its ISO3166 code.
注意:这个国家地区列表放在ISO3166编码里
The only extra files you need is a binary db (geoipdb.bin) & its index file (geoipdb.idx).Both files are generated from a countries & subnets database with the csv2bin tool,available at www.cookinglinux.org/geoip/. Both files MUST also be moved in /var/geoip/ as the shared library is statically looking for that pathname (ex.: /var/geoip/geoipdb.bin).
这个你需要额外的二进位文件geoipdb.bin 和它的索引文件geoipdb.idx.这两个文件是国家地区网络数据库,是用csv2bin 工具生成的,可以在www.cookinglinux.org/geoip/得到.这些文件必须放在/var/geoip/下,作为一个共享库查找路径名字如/var/geoip/geoipdb.bin

6. Nth(第n个包匹配)
# cat nth/info
Title: iptables nth match (标题: iptables第N个匹配)
Author: Fabrice MARIE (作者:名字,email地址)
Status: Works (状况:运作)
Repository: base (贮仓库: 基础的)
# cat nth/help
This option adds an iptables `Nth' match, which allows you to match every Nth packet encountered. By default there are 16 different counters that can be used.
翻译: 这个选项增加一个第N个匹配,允许你匹配每隔N个包,默认有16不同的计算方法可以使用
This match functions in one of two ways
1) Match ever Nth packet, and only the Nth packet.
example:(例子)
iptables -t mangle -A PREROUTING -m nth --every 10 -j DROP
This rule will drop every 10th packet.
这个规则将丢弃每隔10个包
2) Unique rule for every packet. This is an easy and quick method to produce load-balancing for both inbound and outbound.
为每一个包应用一个独特的规则,这样是一个容易和快速的负载均衡方法
example: (例如)
iptables -t nat -A POSTROUTING -o eth0 -m nth --counter 7 --every 3 --packet 0 -j SNAT --to-source 10.0.0.5
iptables -t nat -A POSTROUTING -o eth0 -m nth --counter 7 --every 3 --packet 1 -j SNAT --to-source 10.0.0.6
iptables -t nat -A POSTROUTING -o eth0 -m nth --counter 7 --every 3 --packet 2 -j SNAT --to-source 10.0.0.7
This example evenly splits connections between the three SNAT addresses.
上面例子由三个源ip地址平滑分割连接
By using the mangle table and iproute2, you can setup complex load-balanced routing. There's lot of other uses. Be creative!
配合iptables 的mangle表和高级路由iproute2,你能设置一个复合的负载平衡路由.还有其他的用途,具有创造性的设置
Suppported options are: (支持选项有:)
--every Nth Match every Nth packet (匹配每N 个包)
num Use counter 0-15 (default:0) (用计算器(默认是0))
num Initialize the counter at the number 'num' instead of 0. Must be between 0 and Nth-1
(初始化一个计算器,用这个num值而不是0. 必需是在0和N减1之间的数
num Match on 'num' packet. Must be between 0 and Nth-1. If --packet is used for a counter than
there must be Nth number of --packet rules, covering all values between 0 and Nth-1 inclusively.
匹配'num' 包,必需是在0和N减1之间的数,如果该包被用一个计算器,必需是第Nth数字包规则,并覆盖0至Nth-1的所有值(这个翻译得不好,请指正)

7. ipp2p(点对点匹配)
# cat ipp2p/info
Title: Detects some P2P packets (标题: 侦查P2P包)
Author: Eicke Friedrich (作者:名字,email地址)
Status: Stable (状况:稳定的)
Repository: extra (贮仓库: 额外的)
Recompile: netfilter, iptables (重新编译:netfilter|iptables)
# cat ipp2p/help
This option makes possible to match some P2P packets therefore helps controlling such traffic.
Dropping all matches prohibits P2P networks.
Combined with conntrack,CONNMARK and a packet scheduler it can be used for accounting or shaping of P2P traffic.
这个选项能够匹配某些P2P包,帮助控制流量.丢弃所有匹配的P2P.结合连接跟踪,和一个包的调度器,它能被用作计算和×××一个P2流量
Examples: (例如:)
iptables -A FORWARD -m ipp2p --edk --kazaa --bit -j DROP
iptables -A FORWARD -p tcp -m ipp2p --ares -j DROP
iptables -A FORWARD -p udp -m ipp2p --kazaa -j DROP
上例可以封杀很多bt等的P2P软件.
更多参数更详细的参考还有ipp2p/iptables/extensions/libipt_ipp2p.man或http://www.ipp2p.org
8. quota(配额匹配)
# cat quota/info Title: iptables quota match (标题: iptables配额匹配)
Author: Sam Johnston (作者:名字,email地址)
Status: worksforme (状况:运作的)
Repository: base (贮仓库: 基础的)
# cat quota/help
This option adds CONFIG_IP_NF_MATCH_QUOTA, which implements network quotas by decrementing a byte counter with each packet.
这个选项增加一个模块匹配包实现网络包通过的配额
Supported options are: (支持选项有:)
--quota
The quota in bytes. (用bytes为单位)
iptables -I FORWARD -s 192.168.3.159 -p tcp --dport 80 -m quota --quota 500 -j DROP
上例是这样的,在ip192.168.3.159的目标端口为80匹配的500个字节内,将被丢弃.直到匹配完这500字节后,才不会丢弃,就是说,才可以打开网页.
KNOWN BUGS: this does not work on SMP systems.
知道的bug:这个不能工作在SMP系统上,(SMP, symmetric multiprocessing 多处理技术)

三,案例配置

所用的拓扑图

 image

1)软件防火墙的网卡配置

1 2

外网卡的地址为192.168.101.31

2)打开软件防火墙路由转发功能

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

image

[root@localhost ~]# sysctl –p

3)做snat允许192.168.2.0访问外网

[root@localhost ~]# iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -o eth2 -j MASQUERADE

访问外网测试

8 

4)把filter表的input,output,forward链的默认规则设置为拒绝允许192.168.1.2通过ssh连接到软件防火墙

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

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

[root@localhost ~]# iptables -A OUTPUT -s 192.168.1.10 -p tcp --sport 22 -j ACCEPT
[root@localhost ~]# iptables -P OUTPUT DROP

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

查看input,output,forward链的默认规则

5

5)允许工程部在上班时间访问ftp其它都拒绝

加载ftp模块

[root@localhost ~]# modprobe ip_nat_ftp

允许工程部在上班时间访问ftp,21端口

[root@localhost ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.2.1-192.168.2.20 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p tcp --dport 21 -j ACCEPT

允许返回的流量

[root@localhost ~]# iptables -t filter -A FORWARD -p tcp --sport 21 -j ACCEPT

允许ftp 20端口的流量

[root@localhost ~]# iptables -t filter -A FORWARD -p tcp --dport 20 -j ACCEPT
[root@localhost ~]# iptables -t filter -A FORWARD -p tcp --sport 20 -j ACCEPT

6)测试上班时间不能上网

9

上班时间允许ftp访问

12

7)设置下班无访问限制

[root@localhost ~]# iptables -t filter -A FORWARD -s 192.168.2.0/24 -o eth2 -m time --timestart 20:01 --timestop 07:59 -j ACCEPT
[root@localhost ~]# iptables -t filter -A FORWARD -d 192.168.2.0/24 -i eth2 -m time --timestart 20:01 --timestop 07:59 -j ACCEPT

设置时间为下班时间

10

测试上网访问

15

 
8)用另外一种方式实现工程部的要求

清空filter表,默认规则不会被清除
[root@localhost ~]# iptables -t  filter -F  FORWARD

上班时间允许ftp
[root@localhost ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.2.1-192.168.2.20 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p tcp --dport 21 -j ACCEPT

对于已连接的流量允许其流量的返回
[root@localhost ~]# iptables -t filter -A FORWARD -m state --state  ESTABLISHED,RELATED -j ACCEPT

下班时间允许所有流量
[root@localhost ~]# iptables -t filter -A FORWARD -s 192.168.2.0/24 -o eth2 -m time --timestart 20:01 --timestop 07:59 -j ACCEPT

设置时间为上班时间测试ftp客户机ip设为192.168.2.11
[root@localhost ~]# date 091815302012

12 

上网访问

9

设置为下班时间测试 上网
[root@localhost ~]# date 091821172012

15

9)设置软件部上班时间能够访问网络但是不可以访问带有“sina”的网站

不允许访问带有“sina”的网站
[root@localhost ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.2.21-192.168.2.40 -m time --timestart 08:00 --timestop 20:00 --weekdays  Mon,Tue,Wed,Thu,Fri -m string --string "sina" --algo bm -j DROP

允许dns流量通过
[root@localhost ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.2.21-192.168.2.40 -m time --timestart 08:00 --timestop 20:00 --weekdays  Mon,Tue,Wed,Thu,Fri -p udp --dport 53  -j ACCEPT

允许软件部访问网络 
[root@localhost ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.2.21-192.168.2.40 -m time --timestart 08:00 --timestop 20:00 --weekdays  Mon,Tue,Wed,Thu,Fri -p tcp --dport 80  -j ACCEPT

不允许软件部登录qq
[root@localhost ~]# iptables –I FORWARD 2 -m iprange --src-range 192.168.2.21-192.168.2.40  -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -m layer7 --l7proto qq -j DROP

不允许使用迅雷
[root@localhost ~]# iptables –I FORWARD 2 -m iprange --src-range 192.168.2.21-192.168.2.40  -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -m layer7 --l7proto xunlei -j DROP

限制每个连接的最大连接数位3 
[root@localhost ~]# iptables –I FORWARD 2 -m iprange --src-range 192.168.2.21-192.168.2.40  -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p tcp --syn --dport 80 -m connlimit --connlimit-above 3 -j DROP

10)上班时间访问网络客户机地址为192.168.2.22

17

访问带有“sina”的网站

18

11)经理部配置允许上网登录qq,访问网络

允许dns流量

[root@localhost ~]# iptables -A FORWARD  -m iprange --src-range 192.168.2.41-192.168.2.60  -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p udp  --dport 53  -j ACCEPT
[root@localhost ~]# iptables -A FORWARD  -m iprange --src-range 192.168.2.41-192.168.2.60  -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p udp  --dport 80  -j ACCEPT

允许qq登录
[root@localhost ~]# iptables -A FORWARD  -m iprange --src-range 192.168.2.41-192.168.2.60  -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -m layer7 --l7proto qq  -j ACCEPT

测试访问网络

 19

 

12)发布内网的www服务器
[root@localhost ~]# iptables -t nat -A PREROUTING -d 192.168.101.31 -p tcp --dport 80 -i eth2 -j DNAT --to 192.168.1.21
[root@localhost ~]# iptables -t filter -A FORWARD -d 192.168.1.21 -p tcp --dport 80 -j ACCEPT

13)外网的一台主机192.168.101.33访问内网服务器

20

配置结束。