iptables匹配条件

admin 2020-08-29 AM 768℃ 0条

基本匹配条件

#匹配ip段
iptables -t filter -I INPUT -s 192.168.3.103/24 -j DROP 
#匹配多个ip
iptables -t filter -I INPUT -s 192.168.2.101,192.168.2.103 -j DROP
#匹配ip取反
iptables -t filter -I INPUT ! -s 192.168.2.2 -j ACCEPT
#-d 匹配目标ip地址
iptables -t filter -I INPUT -s 192.168.2.101 -d 192.168.2.102 -j DROP
#-p 匹配协议类型,centos6 -p选项支持如下协议类型tcp, udp, udplite, icmp, esp, ah, sctp,centos7还支持icmpv6、mh
iptables -t filter -I INPUT -s 192.168.2.101 -p icmp -j DROP
iptables -t filter -I INPUT -s 192.168.2.101 ! -p tcp -j DROP
#-i 匹配报文流入网卡接口,只能适用于PREROUTING、INPUT、FORWARD链,不适用于OUTPUT、POSTROUTING链
iptables -t filter -I INPUT -i eth1 -p icmp -j DROP
iptables -t filter -I INPUT ! -i eth1 -p tcp -j DROP
#-o 匹配报文流出网卡接口,只能适用于OUTPUT、POSTROUTING、FORWARD链,不适用于PREROUTING、INPUT链
iptables -t filter -I OUTPUT -o eth1 -p icmp -j DROP



扩展匹配条件-常用扩展模块

tcp模块

#--dport 匹配目标端口
#-m tcp表示使用tcp扩展模块,-p tcp与 -m tcp并不冲突,-p用于匹配报文的协议,-m用于指定扩展模块的名称,正好,这个扩展模块也叫tcp。
iptables -t filter -I INPUT -s 192.168.2.101 -p tcp -m tcp --dport 22 -j DROP
#当使用-p选项指定报文的协议时,如没有使用-m指定对应的扩展模块名称,使用了扩展匹配条件,iptables默认会调用与-p选项对应的协议名称相同的模块。
iptables -t filter -I INPUT -s 192.168.2.101 -p tcp ! --dport 22 -j DROP
#--sport 匹配源端口
iptables -t filter -I INPUT -s 192.168.2.101 -p tcp --sport 22 -j DROP 
iptables -t filter -I OUTPUT -d 192.168.2.101 -p tcp --sport 22 -j DROP
#匹配连续端口范围
iptables -t filter -I INPUT -s 192.168.2.101 -p tcp --dport 28:35 -j DROP
iptables -t filter -I INPUT -s 192.168.2.101 -p tcp --dport 8080: -j DROP
iptables -t filter -I INPUT -s 192.168.2.101 -p tcp --dport :35 -j DROP
# --tcp-flags 匹配报文的tcp头的标志位 ,--syn 用于匹配tcp新建连接的请求报文,相当于使用"--tcp-flags SYN,RST,ACK,FIN  SYN"
iptables -I INPUT -p tcp -m tcp --dport 22 --tcp-flags SYN,ACK,FIN,RST,URG,PSH SYN -j REJECT
iptables -I OUTPUT -p tcp -m tcp --sport 22 --tcp-flags SYN,ACK,FIN,RST,URG,PSH SYN,ACK -j REJECT
iptables -t filter -I INPUT -p tcp -m tcp --dport 22 --tcp-flags ALL SYN -j REJECT
iptables -t filter -I OUTPUT -p tcp -m tcp --sport 22 --tcp-flags ALL SYN,ACK -j REJECT
iptables -I INPUT -p tcp -m tcp --dport 22 --syn -j REJECT


multiport模块

#--dports、--sports匹配离散的端口,同时可以匹配连续端口
#-m multiport扩展只能用于tcp、udp协议,即配合-p tcp或者-p udp使用
iptables -t filter -I INPUT -s 192.168.2.101 -p tcp -m multiport --dports 22,80,8888 -j DROP
iptables -t filter -I INPUT -s 192.168.2.101 -p tcp -m multiport --dports 22:80,8888 -j DROP


udp模块

# 与tcp模块方法类似
iptables -t filter -I INPUT -p udp -m udp --dport 53 -j ACCEPT
iptables -t filter -I INPUT -p udp -m udp --dport 137:139 -j DROP
#可以结合multiport模块指定多个离散的端口
iptables -t filter -I INPUT -p udp -m udp -m multiport  --dports 53,123 -j ACCEPT


icmp模块

#禁止进出的icmp包
iptables -I INPUT -p icmp -j REJECT
#发出的ping请求属于类型8的icmp报文,而对方主机的ping回应报文则属于类型0的icmp报文
#禁止外部ping,但允许向外ping。
iptables -I INPUT -p icmp -m icmp --icmp-type 8 -j REJECT 
iptables -I OUTPUT -p icmp -m icmp --icmp-type 0 -j REJECT
#禁止发出ping请求icmp报文,达到禁止服务器向外ping
iptables -I OUTPUT -p icmp -m icmp --icmp-type 8 -j REJECT


iprange扩展模块

#--src-range 、--dst-range,可以指定"一段连续的IP地址范围",
iptables -t filter -I INPUT -m iprange --src-range 192.168.7.1-192.168.7.255 -j DROP


string扩展模块

#--algo 指定对应的匹配算法(bm kmp),如无效换一个匹配算法(input用bm,output用kmp??),注意使用INPUT、还是OUTPUT
#--string 指定匹配的字符串
iptables -t filter -I INPUT -m string --algo bm --string "west" -j DROP 
iptables -t filter -I OUTPUT -m string --algo kmp --string "west" -j DROP


time扩展模块

# time模块匹配时间是UTC,时区差8个小时
#--timestart  --timestop  开始结束时刻几点几分几秒
#--weekdays 每周哪天, --monthdays 每月哪天
#--datestart  与-datestop选项,指定具体的日期范围
#--monthdays与--weekdays可以使用"!"取反,其他选项不能取反
iptables -t filter -I OUTPUT -p tcp --sport 80 -m time --timestart 12:14:00 --timestop 12:15:00 -j DROP
iptables -t filter -I OUTPUT -p tcp --sport 80 -m time --weekdays 3,6,7 -j DROP
iptables -t filter -I OUTPUT -p tcp --sport 80 -m time --monthdays 8,10,11 -j DROP
iptables -t filter -I OUTPUT -p tcp --sport 80 -m time --weekdays 5,6 --monthdays 12,13,14 -j DROP
iptables -t filter -I OUTPUT -p tcp --sport 80 -m time --datestart 2020-08-12 --datestop 2020-08-14 -j DRO
iptables -t filter -I OUTPUT -p tcp --sport 80  -m time --timestart 09:00:00 --timestop 18:00:00 --weekdays 6,7 -j REJECT


connlimit扩展模块

#--connlimit-above  默认表示限制"每个IP"的链接数量
#--connlimit-mask 针对"某类IP段内的一定数量的IP"进行连接数量的限制
iptables -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 10 --connlimit-mask 24 -j DROP


limit扩展模块

# 限制单位时间内流入的包的数量, /second、/minute、/hour、/day
#--limit-burst 默认值为5,
#令牌桶:"--limit"选项就是用于指定"多长时间生成一个新令牌的","--limit-burst"选项就是用于指定"木桶中最多存放几个令牌的"
iptables -A INPUT -p icmp -m limit --limit-burst 3 --limit 10/minute -j ACCEPT
iptables -A INPUT -p icmp -j REJECT


标签: iptables

非特殊说明,本博所有文章均为博主原创。