命令记不住

登陆

Iptables 防火墙企业安全策略基础

Iptables防火墙企业安全策略基础

  • 企业中安全服务配置情况:

    • selinux(生产中也是关闭的),ids入侵检测,md5指纹。
    • iptables(生产中看情况,内网关闭,外网打开)
  • 企业中安全优化配置原则:

    • 尽可能不给服务器配置外网IP,可以通过代理转发或者通过防火墙映射。
    • 并发不是特别大情况有外网IP,可以开启防火墙服务。
    • 大并发的情况,不能开iptables,影响性能,利用硬件防火墙提升架构安全。
  • 学好iptables的基础:

    • OSI7层模型以及不同层对应哪些协议?
    • TCP/IP三次握手,四次断开的过程,TCP HEADER,状态转换
    • 常用的服务端口要非常清楚了解。
    • 常用服务协议原理http协议,icmp协议。
    • 参考书籍资料:tcp/ip协议卷1

1.1iptables防火墙简介

Netfilter/Iptables(以下简称Iptables)是unix/linux自带的一款优秀且开放源代码的完全自由的基于包过滤的防火墙工具,它的功能十分强大,使用非常灵活,可以对流入和流出服务器的数据包进行很精细的控制。特别是它可以在一台非常低的硬件配置服务器上跑的非常好(赛扬500HZ cpu 64M内存的情况下部署网关防火墙),提供近400人的上网服务丝毫不逊色企业级专业路由器防火墙。iptables+zebra+squid(企业常用网络开源产品)。
iptables是linux2.4及2.6内核中集成的服务。其功能与安全性比其老一辈ipfwadm,ipchains强大的多,iptables主要工作在OSI七层的二、三、四层,如果重新编译内核,iptables也可以支持7层控制(squid代理+iptables)。

1.OSI参考模型或七层模型

  • 应用层

    • 网络服务与最终用户的一个接口。
    • 协议有:HTTP FTP TFTP SMTP SNMP DNS TELNET HTTPS POP3 DHCP
  • 表示层

    • 数据的表示、安全、压缩。(在五层模型里面已经合并到了应用层)
    • 格式有,JPEG、ASCll、DECOIC、加密格式等
  • 会话层

    • 建立、管理、终止会话。(在五层模型里面已经合并到了应用层)
    • 对应主机进程,指本地主机与远程主机正在进行的会话
  • 传输层

    • 定义传输数据的协议端口号,以及流控和差错校验。
    • 协议有:TCP UDP,数据包一旦离开网卡即进入网络传输层
  • 网络层

    • 进行逻辑地址寻址,实现不同网络之间的路径选择。
    • 协议有:ICMP IGMP IP(IPV4 IPV6) ARP RARP
  • 数据链路层

    • 建立逻辑连接、进行硬件地址寻址、差错校验 [2] 等功能。(由底层网络定义协议)
    • 将比特组合成字节进而组合成帧,用MAC地址访问介质,错误发现但不能纠正。
  • 物理层

    • 建立、维护、断开物理连接。(由底层网络定义协议)
TCP/IP 层级模型结构,应用层之间的协议通过逐级调用传输层(Transport layer)、网络层(Network Layer)和物理数据链路层(Physical Data Link)而可以实现应用层的应用程序通信互联。
应用层需要关心应用程序的逻辑细节,而不是数据在网络中的传输活动。应用层其下三层则处理真正的通信细节。在 Internet 整个发展过程中的所有思想和着重点都以一种称为 RFC(Request For Comments)的文档格式存在。针对每一种特定的 TCP/IP 应用,有相应的 RFC文档。

2.iptables名词和术语

1.2.1什么是容器
谁不知道啊,容器就是装东西的,如(箱、包、坛)。没错,恭喜你答对了。词典里解释说,容器就是用来包装或装载物品的贮存器(如箱、罐、坛)或者成形或柔软不成形的包覆材料。在iptables里的呢,就是用来描述这种包含或者说属于的关系。

1.2.2什么是Netfilter/iptables?
Netfilter是表(tables)的容器。这样解释大家肯定还是晕。举个例子,如果把Netfilter看成是某个小区的一栋楼。那么表(tables)就是楼里的其中的一套房子。这套房子“表(tables)”属于这栋楼“Netfilter”。

1.2.3什么是表(tables)?
表(tables)是链的容器,即所有的链(chains)都属于其对应的表(tables)。如上,如果把Netfilter看成是某个小区的一栋楼。那么表(tables)就是楼里的其中的一套房子。

1.2.4什么是链(chains)?
链(chains)是规则(Policys)的容器。接上,如果把表(tables)当作有一套房子,那么链(chains)就可以说是房子里的家具(柜子等)。

1.2.5什么是规则(Policy)?
规则(Policy)就比较容易理解了,就是iptables一系列过滤信息的规范和具体方法条款了。可以理解为柜子如何增加并摆放柜子东西等。

Netfilter 表(tables) 链(chains) 规则(Policy)
一栋楼 楼里的房子 房子里的柜子 柜子里衣服

工作流程小结:

  • 1.防火墙是层层过滤的,实际是按照配置规则的顺序从上到下,从前到后进行过滤的。
  • 2.如果匹配上规则,即明确表示是阻止还是通过,数据包就不再向下匹配新的规则。
  • 3.如果规则中没有明确表明是阻止还是通过的,也就是没有匹配规则,向下进行匹配,直到匹配默认规则得到明确的阻止还是通过。
  • 4.防火墙的默认规则是所有规则执行完才执行的。

提示信息:

  • iptables防火墙规则的执行顺序默认从前到后(从上到下)依次执行,遇到匹配的规则就
    不再继续向下检查,只有需要到不匹配的规则才会继续向下进行匹配
  • 重点:匹配上了拒绝规则也是匹配

3.iptables表(tables)和链(chains)

默认情况下,iptables根据功能和表的定义划分包含三个表,filter,nat,mangle,其每个表又包含不同的操作链(chains)。
实际iptables包含4张表和五个链,但主要记住两张表即可filter和nat表即可。

未摘

filter表信息详细介绍

未摘

NAT表信息详细介绍

未摘

1Mangle表信息详细介绍

Mangle表:主要负责修改数据包中特殊的路由标记,如TTL,TOS,MARK等,这个表定义了5个链。

4.iptables命令帮助信息

有问题查帮助,下面是很全的帮助信息(必须拿下它)

[root@x86cloud ~]# iptables -V
iptables v1.4.7
[root@x86cloud ~]# iptables -h		<- 如果要详细的查看帮助,man iptables
iptables v1.4.7

Usage: 
iptables -[ACD] chain rule-specification [options]
iptables -I chain [rulenum] rule-specification [options]
iptables -R chain rulenum rule-specification [options]
iptables -D chain rulenum [options]
iptables -[LS] [chain [rulenum]] [options]
iptables -[FZ] [chain] [options]
iptables -[NX] chain
iptables -E old-chain-name new-chain-name
iptables -P chain target [options]						<- 其中-P等同于--policy规则
iptables -h (print this help information)

Commands:
Either long or short options are allowed.
提示:长格式或短格式命令都可用
--append		-A	chain					Append to chain
												添加链中的规则信息
--check			-C	chain					Check for the existence of a rule
--delete			-D	chain					Delete matching rule from chain
												删除链上匹配的规则
--delete			-D	chain rulenum			Delete rule rulenum (1 = first) from chain
												删除链上指定的规则序号(1=first)
--insert			-I	chain [rulenum]			Insert in chain as rulenum (default 1=first)
												在链中根据序号插入规则(默认插入规则序号为1=first)
--replace		-R	chain rulenum			Replace rule rulenum (1 = first) in chain
--list			-L	[chain [rulenum]]			List the rules in a chain or all chains
												列出一个链或所有链中的规则信息
--list-rules		-S	[chain [rulenum]]			Print the rules in a chain or all chains
--flush			-F	[chain]					Delete all rules in chain or all chains
												删除一个链或所有链中的规则信息(不含默认规则)
--zero			-Z	[chain [rulenum]]			Zero counters in chain or all chains
												一个链或所有链的计数器清零
--new 			-N	chain					Create a new user-defined chain
												创建新的用户定义链
--delete-chain	-X	[chain]					Delete a user-defined chain
												删除用户自定义的链
--policy			-P	chain target				Change policy on chain to target
												改变链上的最终默认规则策略
--rename-chain	-E	old-chain new-chain		Change chain name, (moving any references)

Options:
[!]	--proto			-p	proto					protocol: by number or name, eg. `tcp'
													指定协议信息,通过数字或名称表示,例如 tcp
[!]	--source			-s	address[/mask][...]		source specification
													指定源地址信息
[!]	--destination		-d	address[/mask][...]		destination specification
													指定目标地址信息
[!]	--in-interface		-i	input name[+]			network interface name ([+] for wildcard)
													指定匹配进入指定网络接口数据
	--jump			-j	target					target for rule (may load target extension)
													规则目标(可以加载扩展目标)
	--goto 			-g	chain					jump to chain with no return
	--match			-m	match					extended match (may load extension)
													扩展匹配(可以加载扩展参数)
	--numeric			-n							numeric output of addresses and ports
													地址和端口号的号码信息输出
[!]	--out-interface 	-o	output name[+]			network interface name ([+] for wildcard)
													指定匹配流出指定网络接口数据
		--table			-t 	table					table to manipulate (default: `filter')
													指定主要应用的表(默认应用filter表)
	--verbose			-v							verbose mode
													显示详细信息
		--line-numbers								print line numbers when listing
														当列出规则信息时,打印规则行号
		--exact			-x							expand numbers (display exact values)
														扩展数字(显示精确数值)
[!]	--fragment		-f							match second or further fragments only
	--modprobe=<command>						try to insert modules using this command
	--set-counters PKTS BYTES						set the counter during insert/append
[!]	--version			-V							print package version.

5.实际测试iptables规则

1. 启动和查看iptables状态

/etc/init.d/iptables start
iptables -L -n 或 iptables -nL

实例演示01:

[root@x86cloud ~]# /etc/init.d/iptables start
iptables: Applying firewall rules:                         [  OK  ]
[root@x86cloud ~]# iptables -L -n
Chain INPUT (policy ACCEPT)		<- 表示当所有规则都不匹配时,默认规则是什么
target		prot opt source				destination       
ACCEPT		all		-- 	f0.0.0.0/0			0.0.0.0/0			state RELATED,ESTABLISHED 
ACCEPT		icmp	-- 	0.0.0.0/0			0.0.0.0/0         
ACCEPT 		all		-- 	0.0.0.0/0			0.0.0.0/0         
ACCEPT		tcp		-- 	0.0.0.0/0			0.0.0.0/0			state NEW tcp dpt:22 
REJECT		all		--	0.0.0.0/0			0.0.0.0/0			reject-with icmp-host-prohibited 
说明:以上信息为INPUT链下面的规则信息

Chain FORWARD (policy ACCEPT)
target		prot opt source				destination       
REJECT		all		--	0.0.0.0/0			0.0.0.0/0 		reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT)
target		prot opt source				destination

提示:如果遇到下面的无法启动IPTABLES的情况

[root@x86cloud ~]# /etc/init.d/iptables start
[root@x86cloud ~]# /etc/init.d/iptables start
[root@x86cloud ~]# /etc/init.d/iptables status
Firewall is stopped
解决:setup-->Firewall configuration-->enable

扩展说明:如果无法进入Firewall configuration解决方法
解决一:字符集没有进行设置 
[root@x86cloud ~]# locale
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
# 需要进行设置即可
[root@x86cloud ~]# export LC_CTYPE="en_US.UTF-8"
[root@x86cloud ~]# export LC_ALL="en_US.UTF-8"

解决二:系统总线进行没有开启
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.FileNotFound: 
Failed to connect to socket /var/run/dbus/system_bus_socket: No such file or directory
# 开启系统总线服务即可,并且可以设置为开机自启动
[root@x86cloud ~]# dbus-daemon --system
# dbus是一个消息传递系统,应用程序间可通过它来相互传递消息,dbus-daemon是一个守护进程
# 在rc.sysinit最后添加一行dbus-daemon --system,这样系统启动时,avahi就不会出问题了

iptables默认加载的内核模块

[root@x86cloud ~]# lsmod |egrep "nat|filter"
Module				Size		Used	by
iptable_filter			2793	1 
ip_tables				17831	1		iptable_filter

[root@x86cloud ~]# lsmod |egrep "nat|filter|ipt"
Module				Size		Used	by
iptable_filter			2793  	1 
ip_tables				17831  	1		iptable_filter
ipt_REJECT			2351	2
说明:默认防火墙相关加载的内核模块信息较少

可以通过命令加载如下模块到内核中:

modprobe ip_tables
modprobe iptable_filter
modprobe iptable_nat
modprobe ip_conntrack
modprobe ip_conntrack_ftp
modprobe ip_nat_ftp
modprobe ipt_state			<- centos6版本系统中可忽略此模块的加载

# 模块加载完成后,在进行查看
[root@x86cloud ~]# lsmod |egrep "nat|filter"
Module				Size		Used	by
nf_nat_ftp			3443	0 
nf_conntrack_ftp		12049	1		nf_nat_ftp
iptable_nat			5923	0 
nf_nat				22676	2		nf_nat_ftp,iptable_nat
nf_conntrack_ipv4	9186	5		iptable_nat,nf_nat
nf_conntrack			79537	6		nf_nat_ftp,nf_conntrack_ftp,iptable_nat,
										nf_nat,nf_conntrack_ipv4,xt_state
iptable_filter			2793	1 
ip_tables				17831	2		iptable_nat,iptable_filter
说明:确认已经新增加载上了很多的模块

lsmod查看内核模块命令说明
![lsmod][5]
加载的内核模块介绍说明:

模块 模块说明
ip_tables 是防火墙的核心模块,负责维护防火墙的规则表,通过这些规则,实现防火墙的核心功能。归纳起来,主要有三种功能:包过滤(filter)、NAT以及包处理(mangle)
iptable_filter 防火墙数据包过滤模块,实现对数据包的过滤与放行功能
iptable_nat 防火墙数据包转发模块,用于对数据包进行修改,从而实现数据转发的目的
ip_conntrack 主要用于修改应用层协议控制包,无比重要的作用就是实现nat
ip_conntrack_ftp 与ftp服务nat映射相关的模块
ip_nat_ftp 与ftp服务nat映射相关的模块
ipt_state 防火墙状态信息模块,在新版centos6系统中已经忽略此模块的加载

2. 清除默认规则

iptables -F		<- 清除所有规则,不会处理默认的规则
iptables -X		<- 删除用户自定义的链
iptables -Z		<- 链的计数器清零(数据包计数器与数据包字节计数器)

实例演示02:

[root@x86cloud ~]# iptables -F
[root@x86cloud ~]# iptables --flush
提示:以上两条命令等价
[root@x86cloud ~]# iptables -X
[root@x86cloud ~]# iptables --delete-chain
提示:以上两条命令等价
[root@x86cloud ~]# iptables -Z
[root@x86cloud ~]# iptables --zero
提示:以上两条命令等价 
[root@x86cloud ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination       

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination       

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination 
说明:所有规则策略已经清空,全部默认规则为接收允许

3. 配置禁止规则
禁止ssh默认22端口

# 官方语法
iptables -[ACD] chain rule-specification [options]
iptables -[ACD] chain rule-specification -t [table]
# 具体命令
iptables -A INPUT -p tcp --dport 22 -j DROP
iptables -t filter -A INPUT -p tcp --dport 22 -j DROP
说明:
01. iptables默认用的就是filter表,因此,以上两条命令等价;
02. 其中INPUT DROP要大写
03. --jump   -j		target for rule (may load target extension)
	   基本的处理行为:ACCEPT(接受) DROP(丢弃) REJECT(拒绝)
比较:DROP好于REJECT;捉迷藏问题分析,有响应表示拒绝,没响应表示丢弃
04. 命令行执行的规则,仅仅在内存里临时生效

实例演示:

[root@x86cloud ~]# iptables -A INPUT -p tcp --dport 22 -j DROP
[root@x86cloud ~]# iptables -t filter -A INPUT -p tcp --dport 22 -j DROP
说明:执行以上任意命令可以发现已经无法远程连接服务器了

# 配置规则后进行查看
[root@x86cloud ~]# iptables -L -n
Chain INPUT (policy ACCEPT)		<- 表示当所有规则都不匹配时,默认规则是什么
target		prot opt source				destination       
DROP		all	 -- 	0.0.0.0/0				0.0.0.0/0			tcp dpt:22 

Chain FORWARD (policy ACCEPT)
target		prot opt source				destination       

Chain OUTPUT (policy ACCEPT)
target		prot opt source				destination
说明:生成了禁止ssh服务的22端口DROP策略
iptables -I INPUT -p tcp --dport 22 -j DROP
说明:利用-I参数,实现强行阻止访问22端口,将此规则放在第一位

# 删除配置的禁止连接22端口的规则
iptables -t filter -D INPUT -p tcp --dport 22 -j DROP
iptables -F
/etc/init.d/iptables restart

iptables -nL --line-number		<- 查看链的规则序号信息
iptables -D INPUT 1				<- 根据规则序号信息进行删除指定规则

禁止网段连入(禁止10.0.0.0网段)

iptables -A INPUT -i eth0 -s 10.0.0.0/24 -j DROP
iptables -t filter -A INPUT -i eth0 -s 10.0.0.0/24 -j DROP
说明:iptables默认用的就是filter表,因此,以上两条命令等价

实例演示:

[root@x86cloud ~]# iptables -A INPUT -i eth0 -s 10.0.0.0/24 -j DROP
[root@x86cloud ~]# iptables -t filter -A INPUT -i eth0 -s 10.0.0.0/24 -j DROP
说明:执行以上任意命令可以发现10.0.0.0网段不能访问服务器主机了

实例拓展:

# 删除配置的禁止来源地址为10网段的命令
iptables -D INPUT -i eth0 -s 10.0.0.0/24 -j DROP
iptables -F
/etc/init.d/iptables restart
iptables -D INPUT 1

只允许指定网段连入(允许10.0.0.0网段)

iptables -A INPUT -i eth0 ! -s 10.0.0.0/24 -j DROP
iptables -t filter -A INPUT -i eth0 ! -s 10.0.0.0/24 -j DROP
说明:iptables默认用的就是filter表,因此,以上两条命令等价;并且要注意!放置位置

实例演示:

[root@x86cloud ~]# iptables -A INPUT -i eth0 !-s 10.0.0.0/24 -j DROP
[root@x86cloud ~]# iptables -t filter -A INPUT -i eth0 !-s 10.0.0.0/24 -j DROP
说明:执行以上任意命令可以发现只允许10.0.0.0网段访问服务器主机了

实例拓展:

# 删除配置的禁止来源地址为10网段的命令
iptables -D INPUT -i eth0 -s 10.0.0.0/24 -j DROP
iptables -F
/etc/init.d/iptables restart
iptables -D INPUT 1

教学扩展:测试!非用法

  1. 源地址不是10.0.0.101单个IP的禁止连接
iptables -t filter -I INPUT -i eth0 !-s 10.0.0.101 -j DROP
iptables -A INPUT -p all -i eth0 !-s 10.0.0.101 -j DROP		<- -p udp,tcp,icmp,all
iptables -t filter -I INPUT -p icmp --icmp-type 8 -i eth0 ! -s 10.0.0.101 -j DROP
提示:可以把自己的笔记本IP当做10.0.0.101 
  1. 源地址不是10.0.0.0/24的网段禁止连接
iptables -t filter -I INPUT -i eth0 ! -s 10.0.0.0/24 -j DROP
iptables -t filter -I INPUT -i eth0 -s 10.0.0.0/24 -j ACCEPT
说明:以上两个命令作用等价

# 不允许指定网段以外主机访问服务52113端口,服务协议为tcp?
? iptables -A INPUT -p tcp --dport 52113 ! -s 10.0.0.0/24 -j DROP
? iptables -A INPUT -p tcp --dport 52113 ! -s 172.16.1.0/24 -j DROP
说明:可以任意排列组合,组合策略信息条件取交集

禁止用户访问服务端口(禁止访问80或3306端口)

iptables -A INPUT -p tcp -dport 80 -j DROP
iptables -t filter -A INPUT -p tcp -dport 80 -j DROP
说明:iptables默认用的就是filter表,因此,以上两条命令等价

提示说明:
使用-I和-A的顺序,防火墙的过滤根据规则顺序的
-A是添加规则到指定链的结尾,最后一条
-I是添加规则到指定链的开头,第一条;也可指定插入位置
一般封IP地址时,进行策略配置就需要利用-I参数

实例演示:

[root@x86cloud ~]# iptables -A INPUT -p tcp --dport 80 -j DROP
[root@x86cloud ~]# iptables -t filter -A INPUT -p tcp --dport 80 -j DROP
说明:执行以上任意命令可以发现不能访问服务器主机80端口了

# 配置规则后进行查看
[root@x86cloud ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination       
DROP     tcp  --  0.0.0.0/0            	 0.0.0.0/0           tcp dpt:80 

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination       

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination       
说明:生成了禁止访问服务器主机80端口DROP策略

实例拓展:

# 删除配置的禁止策略
iptables -D INPUT -p tcp -dport 80 -j DROP
iptables -F
/etc/init.d/iptables restart
iptables -D INPUT 1

企业应用:-A和-I说明

awk ‘{print $1}’ /app/log/access_log|sort|uniq -c|sort -rn -k1
iptables -A INPUT -p tcp -s 10.0.0.108 --dport 80 -j DROP
iptables -L -n
说明:采用-A封IP地址,会将策略放置在规则列表最后,可能会造成封不住
iptables -I INPUT -p tcp -s 10.0.0.108 --dport 80 -j DROP
iptables -L -n
说明:采用-I封IP地址,会将策略放置在规则列表最前面,会优先执行封IP策略

4. 流量匹配规则
匹配指定的协议

iptables -A INPUT -p tcp
iptables -A INPUT -p udp
说明:如果不指定-p,默认就是-p(all,tcp,udp,icmp)。默认all。
匹配指定协议外的所有协议
iptables -A INPUT ! -p tcp
iptables -I INPUT ! -p tcp -s 10.0.0.123 -j DROP
iptables -t filter -A INPUT ! -p udp -i eth0 ! -s 10.0.0.116 -j DROP

匹配主机源IP,目的IP

iptables -A INPUT -s 10.0.0.14
iptables -A INPUT -s ! 10.0.0.14
iptables -t nat -A PREROUTING -d 10.0.0.14
iptables -t nat -A PREROUTING ! -d 10.0.0.14

匹配网段

iptables -A INPUT -s 10.0.0.0/24
iptables -A INPUT ! -s 10.0.0.0/24

匹配单一端口

iptables -A INPUT -p tcp --sport 53
iptables -A INPUT -p udp --dport 53
匹配指定端口之外的端口
iptables -A INPUT -p tcp ! --dport 22
iptables -I INPUT -p tcp ! --dport 22 -s 10.0.0.123 -j DROP

匹配端口范围

iptables -A INPUT -p tcp --sport 22:80
iptables -I INPUT -p tcp --dport 21,22,23,24 -j ACCEPT	<- 语法错误
iptables -I INPUT -p tcp -m multiport --dport 21,22,23,24 -j ACCEPT
iptables -I INPUT -p tcp --dport 3306:8809 -j ACCEPT
iptables -I INPUT -p tcp --dport 18:80 -j DROP

实例测验1:测试匹配端口范围

[root@x86cloud ~]# iptables -F
[root@x86cloud ~]# iptables -t filter -A INPUT -p tcp --dport 20:1024 -j DROP
[root@x86cloud ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination       
DROP     tcp  --  0.0.0.0/0            	 0.0.0.0/0           tcp dpts:20:1024
# 测试结果:
01. SSH 22端口终端直接断掉
02. telnet连接80不通
[c:\~]$ telnet 10.0.0.8 80
Connecting to 10.0.0.8:80...

实例测验2:测试匹配列举端口范围

[root@x86cloud ~]# iptables -F
[root@x86cloud ~]# iptables -t filter -A INPUT -p tcp -m multiport --dport 80,90,100 -j DROP
[root@x86cloud ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination       
DROP     tcp  --  0.0.0.0/0            	 0.0.0.0/0           tcp dpts:20:1024
DROP     tcp  --  0.0.0.0/0            	 0.0.0.0/0           multiport dports 80,90,100
# 测试结果:
01. SSH 22端口终端直接断掉
02. telnet连接80不通
[c:\~]$ telnet 10.0.0.8 80
Connecting to 10.0.0.8:80...

匹配ICMP类型

iptables -A INPUT -p icmp --icmp-type 8
iptables -A INPUT -p icmp --icmp-type 8 -j DROP
iptables -A INPUT -p icmp -m icmp --icmp-type any -j ACCEPT
iptables -A FORWARD -s 192.168.1.0/24 -p icmp -m icmp --icmp-type any -j ACCEPT
说明:只有类型8是真正会影响ping,或者也可以采用any;了解很多icmp类型iptables -p icmp -h
http://www.cnitblog.com/yang55xiaoguang/articles/59581.html
匹配指定的网络接口
iptables -A INPUT -i eth0
iptables -A FORWARD -o eth0
# 记忆方法:
[!]	--in-interface		-i	input name[+]		network interface name ([+] for wildcard)
													指定匹配进入指定网络接口数据
[!]	--out-interface 	-o	output name[+]			network interface name ([+] for wildcard)
													指定匹配流出指定网络接口数据

匹配网络状态

-m state --state
NEW:已经或将启动新的连接
ESTABLISHED:已建立的连接
RELATED:正在启动的新连接
INVALID:非法或无法识别的
INVALID
meaning that the packet is associated with no known connection
NEW  
meaning that the packet has started a new connection, or otherwise associated with a connection 
which has not seen packets in both directions
ESTABLISHED
meaning that the packet is associated with a connection which has seen packets in both directions,
RELATED
meaning that the packet is starting a new connection, but is associated with an existing connection, 
such as an FTP data transfer, or an ICMP error.
说明:FTP服务是特殊的,需要配状态连接

# 允许关联的状态包通过(web服务不要使用FTP服务)
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
说明:看电影出去WC或者接个电话,回来也要允许进入。

匹配网络限制策略

# 限制指定时间包的允许通过数量及并发数
-m limit --limit n/{second/minute/hour}: 
解释:指定时间内的请求速率”n”为速率,后面为时间分别为:秒 分 时
--limit-burst [n]
解释:在同一时间内允许通过的请求”n”为数字,不指定默认为5

iptables -I INPUT -s 10.0.1.0/24 -p icmp --icmp-type 8 -m limit --limit 6/min --limit-burst 5 -j ACCEPT

limit
This module matches at a limited rate using a token bucket filter.  A rule using this extension 
will match until this limit is reached (unless the ‘!’ flag is used).  It can be used in combination 
with the LOG target to give limited logging, for example.
这个模块使用令牌桶过滤器来匹配有限速率。使用这个扩展规则用来匹配可以到达的极限信息。
可以与日志结合使用,以提供极限的日志记录

--limit rate[/second|/minute|/hour|/day]
Maximum average matching rate: specified as a number, with an optional ‘/second’, ‘/minute’, ‘/hour’, 
or ‘/day’ suffix; the default is 3/hour.
最大平均匹配率,按照一定时间匹配

--limit-burst number
Maximum initial number of packets to match: this number gets recharged by one every time the limit  
specified above is not reached, up to this number; the default is 5.
要匹配的包的最大初始数量。这个数值可以通过积攒方式获得,在每一次指定的数值没有到达时,都会加一
直到获得指定的最大初始值数量。默认为5
推荐阅读