iptables [-t table] {-A|-D} chain rule-specification
-A:向某个链中追加一个规则。
-D:删除某个链中的规则。
iptables [-t table] -I chain [rulenum] rule-specification
-I:向某个链中插入规则,如果不指定行号则默认插入到顺序第一行。
iptables [-t table] -R chain rulenum rule-specification
-R:修改指定表中的指定链的指定行中的规则。
iptables [-t table] -D chain rulenum
-D:删除符合指定表中指定行号的规则。
iptables [-t table] -S [chain [rulenum]]
-S:指定显示指定表中指定链内指定行的规则,如果没有指定则显示链内所有规则。
iptables [-t table] {-F|-L|-Z} [chain [rulenum]] [options...]
-F:清空链中的规则。
-L:列出指定表中的所有的规则。
-Z:清空计数器。
iptables [-t table] -N chain
-N:创建一条自定义的规则链。
iptables [-t table] -X [chain]
-X:删除一条自定义的规则链。
iptables [-t table] -P chain target
-P:为指定的链设定默认动作。
iptables [-t table] -E old-chain-name new-chain-name
-E:重命名自定义的规则链。
iptables规则中如不指定对哪个表进行操作则默认对filter表进行操作。
###################################################################################################
数据包处理机制
ACCEPT:允许
DROP:丢弃
REJECT :拒绝
SNAT :源地址转换
DNAT :目标地址转换
REDIRECT :重新封包
LOG :记录到日志
iptables -t filter -I INPUT -s 192.168.1.9 -j LOG --log-prefix "INPUT packages"
MARK :标记
2.2.2.2-2.2.2.5(范围)
iptables -t nat -A POSTROUTING -p tcp -o eth1 -j SNAT --to-source 2.2.2.2 (源ip地址转换)
PREROUTING -p tcp -i eth1 -j DNAT --to-destination 192.168.1.1-192.168.1.255:80(目标地址转换)
通策略:默认是堵(拒绝/丢弃),放行匹配到的规则
堵策略:默认是允许,仅丢弃匹配到规则的数据包
基于扩展模块实现访问控制
-m 模块名 --模块专有选项
time:基于时间实现访问控制。
--datestart YYYY[-MM[-DD[hh[:mm[:ss]]]]]
--datestop YYYY[-MM[-DD[hh[:mm[:ss]]]]]
--timestart hh[:mm[:ss]]
--timestop hh[:mm[:ss]]
--monthdays day[,day...]
--weekdays day[,day...]
iptables -t filter -A INPUT -s 192.168.1.0/24 -m time --timestart 12:00:00 --timestop 13:30:00 -j ACCEPT
string : 检查数据包中是否有我们关心的敏感字符。
--alog[bm|kmp]:仅能够对英文字符串实现匹配,必选选项。
--string ‘STRING’:指定要查找的敏感字符。
iptables -t filter -A OUTPUT -s 192.168.1.9 -m sting --algo bm --string "hello" -j DROP
limit:按数据包的数率进行匹配,用于限速。
--limit #[/second|/minute|/hour|/day]:限制速率。
iptables -t filter -I OUTPUT -s 192.168.1.9 -d 192.168.1.0/24 -m limit --limit 2/second -j ACCEPT
connlimit:限制每个IP对指定服务的最大并发连接数。
--connlimit-above #:限制仅能够连接的最大连接数。
iptables -t filter -I INPUT -s 192.168.1.0/24 -d 192.168.1.9 -p tcp --dport 80 -m connlimit --connlimit-above 5 -j DROP
multiport:多端口匹配,该模块需与-p tcp或-p udp联合使用。
--source-ports| --sports:最多允许使用15个端口。
--destination-ports| --dports:目标端口。
--ports:仅匹配端口,源端口或目标端口均可生效。
iptables -t filter -I INPUT -s 192.168.1.0/24 -d 192.168.1.9 -p tcp -m multiport --dports 80,22 -j ACCEPT
iprange:指定地址范围。
--src-rang #[-#]:指定源IP的范围。
--dst-rang #[-#]:指定目标IP的范围。
iptables -t filter -I INPUT -d 192.168.1.9 -p tcp --dport 80 -m iprange --src-rang 192.168.1.100-192.168.1.200 -j ACCEPT