-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
-p tcp -m tcp –-sport
用于匹配tcp协议报文的源端口,可以使用冒号指定一个连续的端口范围-p tcp -m tcp –-dport
用于匹配tcp协议报文的目标端口,可以使用冒号指定一个连续的端口范围-p tcp -m tcp –tcp-flags
用于匹配tcp协议报文的标志位-p tcp -m tcp –syn
用于匹配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
-p udp -m udp --sport
用于匹配udp协议报文的源端口,可以使用冒号指定一个连续的端口范围-p udp -m udp -–dport
用于匹配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
-p tcp -m multiport –-sports
用于匹配报文的源端口,可以指定离散的多个端口号,端口之间用”逗号”隔开-p udp -m multiport –-dports
用于匹配报文的目标端口,可以指定离散的多个端口号,端口之间用”逗号”隔开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
IP段的始末IP使用”横杠”连接,–src-range
与–dst-range
和其他匹配条件一样,能够使用”!”取反
--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
可以指定要匹配的字符串,如果报文中包含对应的字符串,则符合匹配条件
–algo
: 用于指定匹配算法,可选的算法有bm与kmp–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
–timestart
:用于指定时间范围的开始时间,不可取反–timestop
:用于指定时间范围的结束时间,不可取反–weekdays
:用于指定”星期几”,可取反–monthdays
:用于指定”几号”,可取反–datestart
:用于指定日期范围的开始日期,不可取反–datestop
:用于指定日期范围的结束时间,不可取反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-above
:单独使用此选项时,表示限制每个IP的链接数量。–connlimit-mask
:此选项不能单独使用,在使用–connlimit-above选项时,配合此选项,则可以针对”某类IP段内的一定数量的IP”进行连接数量的限制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-burst
:类比”令牌桶”算法,此选项用于指定令牌桶中令牌的最大数量。–limit
:类比”令牌桶”算法,此选项用于指定令牌桶中生成新令牌的频率,可用时间单位有second、minute 、hour、day。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协议的全称为Internet Control Message Protocol,翻译为互联网控制报文协议,它主要用于探测网络上的主机是否可用,目标是否可达,网络是否通畅,路由是否可用等。
–icmp-type
:匹配icmp报文的具体类型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模块的连接而言,”连接”其中的报文可以分为5种状态,报文状态可以为NEW、ESTABLISHED、RELATED、INVALID、UNTRACKED