iptables模块

拓展iptables的功能的。

-m : 指定模块

1、连续匹配多个端口(multiport)

	--dports  : 指定多个端口(不同端口之间以逗号分割,连续的端口使用冒号分割)。
	
2、指定一段连续的ip地址范围(iprange)
    --src-range from[-to]:	源地址范围
    --dst-range from[-to]	目标地址范围

3、匹配指定字符串(string)
    --string pattern	# 指定要匹配的字符串
    --algo {bm|kmp}		# 匹配的查询算法
    
4、根据时间段匹配报文(time)
    --timestart hh:mm[:ss]		# 开始时间
    --timestop hh:mm[:ss]		# 结束时间
    --monthdays day[,day...]	# 指定一个月的某一天
    --weekdays day[,day...]		# 指定周 还是  周天 

5、禁ping, 默认本机无法ping别人 、别人无法ping自己
	--icmp-type {type[/code]|typename}
		echo-request  (8) 请求 
		echo-reply    (0) 回应

6、限制链接数,并发连接数(connlimit)
    --connlimit-upto n		#  如果现有连接数小于或等于  n  则 匹配
    --connlimit-above n		#  如果现有连接数大于n 则匹配

7、针对 报文速率 进行限制。 秒、分钟、小时、天。

	--limit rate[/second|/minute|/hour|/day] # 报文数量 
     --limit-burst number  # 报文数量(默认:5)

案例1: 要求将22,80,443以及30000-50000之间所有的端口向外暴露,其他端口拒绝

# -t filter(指定表) -A INPUT(添加规则到链) -p TCP(指定协议)
# -m multiport(指定模块使用模块方法(multiport(连续匹配多个端口)))
# --dports(指定端口,多个) 
# -j ACCEPT(将数据包放行,进行完此处理动作后,将不再比对其它规则,直接跳往下一个规则链)
iptables -t filter -A INPUT -p TCP -m multiport --dports 22,80,443,30000:50000 -j ACCEPT
# ...-j DROP (丢弃包不予处理,进行完此处理动作后,将不再比对其它规则,直接中断过滤程序。)
iptables -f filter -A INPUT -p TCP -j DROP

案例2、要求访问数据包中包含HelloWorld的数据不允许通过。

# ...-m string(指定模块(匹配指定字符串))
# --string "HelloWorld"(触发模块指定要匹配的字符串)
# --algo kmp
iptables -t filter -A INPUT -p TCP -m string --string "HelloWorld" --algo kmp -j DROP

3、要求192.168.15.1 - 192.168.15.10之间的所有IP能够连接192.168.15.81,其他拒绝

# -m iprange(调用指定一段连续的ip地址范围方法)
 # -src-range 192.168.15.1-192.168.15.10(指定源地址范围)
  iptables -t filter -A INPUT -p TCP -m iprange --src-range 192.168.15.1-192.168.15.10 -j ACCEPT 
  iptables -f filter -A INPUT -p TCP -j DROP

4、要求每天的12到13之间,不允许访问

# -m time(指定time模块) 
# --timestart 4:00(开始时间) --timestop 5:00(结束时间)
	iptables -t filter -A INPUT -p TCP -m time  --timestart 4:00   --timestop 5:00 -j DROP
	
	"必须使用UTC时间"

5、要求别人不能ping本机,但是本机可以ping别人

# -m icmp(禁止ping,默认本机无法ping别人,别人无法ping自己(icmp))
# --icmp-type "echo-request"(调用)
# -j DROP(丢弃包不予处理,进行完此处理动作后,将不再比对其它规则,直接中断过滤程序。)
iptables -t filter -A INPUT -p TCP -m icmp --icmp-type "echo-request" -j DROP

6、要求主机连接最多有2个

# ...--dport 22(目标端口linux)
# -m connlimit(限制链接数,并发连接数)
# --connlimit-above 2(如果现有连接数大于2)
# -j DROP(则不匹配)
iptables -t filter -A INPUT -p TCP --dport 22 -m connlimit --connlimit-above 2 -j DROP

7、针对 报文速率 进行限制。 秒、分钟、小时、天。

--limit rate[/second|/minute|/hour|/day] # 报文数量 
     --limit-burst number  # 报文数量(默认:5)
     
	iptables -t filter -A INPUT -p TCP -m limit 333/s -j ACCEPT
	iptables -t filter -A INPUT -p TCP -j DROP