ericpuwang

Iptabes 规则匹配条件

源地址匹配

-s 用于匹配报文的源地址,可以同时指定多个源地址,每个IP之间用逗号隔开,也可以指定为一个网段

# 丢掉来自192.168.1.111和192.168.1.118的报文
iptables -t filter -I INPUT -s 192.168.1.111,192.168.1.118 -j DROP

# 接收来自192.168.1.0/24网段的报文
iptables -t filter -I INPUT -s 192.168.1.0/24 -j ACCEPT

# 接收非192.168.1.0/24网段的报文
iptables -t filter -I INPUT ! -s 192.168.1.0/24 -j ACCEPT

目标地址

-d用于匹配报文的目标地址,可以同时指定多个目标地址,每个IP之间用逗号隔开,也可以指定为一个网段

# 丢掉发往192.168.1.111和192.168.1.118的报文
iptables -t filter -I OUTPUT -d 192.168.1.111,192.168.1.118 -j DROP

# 接收发往192.168.1.0/24网段的报文
iptables -t filter -I INPUT -d 192.168.1.0/24 -j ACCEPT

# 接收发往非192.168.1.0/24网段的报文
iptables -t filter -I INPUT ! -d 192.168.1.0/24 -j ACCEPT

协议类型

-p用于匹配报文的协议类型,可以匹配的协议类型tcp、udp、udplite、icmp、esp、ah、sctp等

# 接收来自192.168.1.146的TCP报文
iptables -t filter -I INPUT -p tcp -s 192.168.1.146 -j ACCEPT

# 接收来自非192.168.1.146的UDP报文
iptables -t filter -I INPUT ! -p udp -s 192.168.1.146 -j ACCEPT

网卡接口

-i用于匹配报文是从哪个网卡接口流入本机的,由于匹配条件只是用于匹配报文流入的网卡,所以在OUTPUT链与POSTROUTING链中不能使用此选项

# 丢掉来自eth4网络接口的icmp协议报文
iptables -t filter -I INPUT -p icmp -i eth4 -j DROP

# 丢掉非eth4网络接口的icmp协议报文
iptables -t filter -I INPUT -p icmp ! -i eth4 -j DROP

-o用于匹配报文将要从哪个网卡接口流出本机,于匹配条件只是用于匹配报文流出的网卡,所以在INPUT链与PREROUTING链中不能使用此选项。

iptables -t filter -I OUTPUT -p icmp -o eth4 -j DROP
iptables -t filter -I OUTPUT -p icmp ! -o eth4 -j DROP

扩展匹配条件

TCP扩展

# 拒绝源端口22且目的地址192.168.1.146的TCP协议报文
iptables -t filter -I OUTPUT -d 192.168.1.146 -p tcp -m tcp --sport 22 -j REJECT

# 拒绝源地址192.168.1.146且目标端口22-25的TCP协议报文
iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m tcp --dport 22:25 -j REJECT

iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m tcp --dport :22 -j REJECT

iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m tcp --dport 80: -j REJECT

# 接受目标地址192.168.1.146且源端口非22的TCP协议报文
iptables -t filter -I OUTPUT -d 192.168.1.146 -p tcp -m tcp ! --sport 22 -j ACCEPT

# 拒绝目标端口22且拥有SYN,ACK,FIN,RST,URG,PSH SYN标志的TCP报文
iptables -t filter -I INPUT -p tcp -m tcp --dport 22 --tcp-flags SYN,ACK,FIN,RST,URG,PSH SYN -j REJECT

iptables -t filter -I INPUT -p tcp -m tcp --dport 22 --tcp-flags ALL SYN -j REJECT

iptables -t filter -I INPUT -p tcp -m tcp --dport 22 --syn -j REJECT

UDP扩展

iptables -t filter -I OUTPUT -d 192.168.1.146 -p udp -m udp --sport 22 -j REJECT

iptables -t filter -I INPUT -s 192.168.1.146 -p udp -m udp --dport 22:25 -j REJECT

iptables -t filter -I INPUT -s 192.168.1.146 -p udp -m udp --dport :22 -j REJECT

iptables -t filter -I INPUT -s 192.168.1.146 -p udp -m udp --dport 80: -j REJECT

iptables -t filter -I OUTPUT -d 192.168.1.146 -p udp -m udp ! --sport 22 -j ACCEPT

MultiPort扩展

iptables -t filter -I OUTPUT -d 192.168.1.146 -p udp -m multiport --sports 137,138 -j REJECT

iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m multiport --dports 22,80 -j REJECT

iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m multiport ! --dports 22,80 -j REJECT

iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m multiport --dports 80:88 -j REJECT

iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m multiport --dports 22,80:88 -j REJECT

iprange扩展

IP段的始末IP使用”横杠”连接,–src-range–dst-range和其他匹配条件一样,能够使用”!”取反

iptables -t filter -I INPUT -m iprange --src-range 192.168.1.127-192.168.1.146 -j DROP

iptables -t filter -I OUTPUT -m iprange --dst-range 192.168.1.127-192.168.1.146 -j DROP

iptables -t filter -I INPUT -m iprange ! --src-range 192.168.1.127-192.168.1.146 -j DROP

string扩展

可以指定要匹配的字符串,如果报文中包含对应的字符串,则符合匹配条件

iptables -t filter -I INPUT -p tcp --sport 80 -m string --algo bm --string "OOXX" -j REJECT

iptables -t filter -I INPUT -p tcp --sport 80 -m string --algo bm --string "OOXX" -j REJECT

time扩展

iptables -t filter -I OUTPUT -p tcp --dport 80 -m time --timestart 09:00:00 --timestop 19:00:00 -j REJECT

iptables -t filter -I OUTPUT -p tcp --dport 443 -m time --timestart 09:00:00 --timestop 19:00:00 -j REJECT

iptables -t filter -I OUTPUT -p tcp --dport 80  -m time --weekdays 6,7 -j REJECT

iptables -t filter -I OUTPUT -p tcp --dport 80  -m time --monthdays 22,23 -j REJECT

iptables -t filter -I OUTPUT -p tcp --dport 80  -m time ! --monthdays 22,23 -j REJECT

iptables -t filter -I OUTPUT -p tcp --dport 80  -m time --timestart 09:00:00 --timestop 18:00:00 --weekdays 6,7 -j REJECT

iptables -t filter -I OUTPUT -p tcp --dport 80  -m time --weekdays 5 --monthdays 22,23,24,25,26,27,28 -j REJECT

iptables -t filter -I OUTPUT -p tcp --dport 80  -m time --datestart 2017-12-24 --datestop 2017-12-27 -j REJECT

connlimit扩展

iptables -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 2 -j REJECT

iptables -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 20 --connlimit-mask 24 -j REJECT

iptables -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 10 --connlimit-mask 27 -j REJECT

limit扩展

iptables -t filter -I INPUT -p icmp -m limit --limit-burst 3 --limit 10/minute -j ACCEPT

iptables -t filter -A INPUT -p icmp -j REJECT

icmp扩展

ICMP协议的全称为Internet Control Message Protocol,翻译为互联网控制报文协议,它主要用于探测网络上的主机是否可用,目标是否可达,网络是否通畅,路由是否可用等。

iptables -t filter -I INPUT -p icmp -m icmp --icmp-type 8/0 -j REJECT

iptables -t filter -I INPUT -p icmp --icmp-type 8 -j REJECT

iptables -t filter -I OUTPUT -p icmp -m icmp --icmp-type 0/0 -j REJECT

iptables -t filter -I OUTPUT -p icmp --icmp-type 0 -j REJECT

iptables -t filter -I INPUT -p icmp --icmp-type "echo-request" -j REJECT

state扩展

对于state模块的连接而言,”连接”其中的报文可以分为5种状态,报文状态可以为NEW、ESTABLISHED、RELATED、INVALID、UNTRACKED