详细介绍 iptables 命令使用方法:
(1) 规则格式:
- iptables [-t table] COMMAND chain [-m matchname [per-match-options]] -j targetname [per-target-options]
- -t table:
raw, mangle, nat, [默认为: filter]
(2)COMMAND:
(a) 链管理:
-N:new, 自定义一条新的规则链;
-X: delete, 删除自定义的规则链;
注意: 仅能删除 用户自定义的 引用计数为 0 的 空的 链;
-P:Policy, 设置默认策略; 对 filter 表中的链而言, 其默认策略有:
ACCEPT: 接受
DROP: 丢弃
REJECT: 拒绝
-E: 重命名自定义链; 引用计数不为 0 的自定义链不能够被重命名, 也不能被删除;
(b) 规则管理:
-A:append, 追加;
-I:insert, 插入, 要指明位置, 省略时表示第一条;
-D:delete, 删除;
(1) 指明规则序号;
(2) 指明规则本身;
-R:replace, 替换指定链上的指定规则;
-F:flush, 清空指定的规则链;
-Z:zero, 置零;
iptables 的每条规则都有两个计数器:
(1) 匹配到的报文的个数;
(2) 匹配到的所有报文的大小之和;
(2) 匹配到的所有报文的大小之和;
(c) 查看:
-L:list, 列出指定鏈上的所有规则;
-n:numberic, 以数字格式显示地址和端口号;
-v:verbose, 详细信息;
-vv, -vvv
-x:exactly, 显示计数器结果的精确值;
--line-numbers: 显示规则的序号;
- (3)chain:
- PREROUTING,INPUT,FORWARD,OUTPUT,POSTROUTING
(4) 匹配条件:
(a) 基本匹配条件: 无需加载任何模块, 由 iptables/netfilter 自行提供;
[!] -s, --source address[/mask][,...]: 检查报文中的源 IP 地址是否符合此处指定的地址或范围;
[!] -d, --destination address[/mask][,...]: 检查报文中的目标 IP 地址是否符合此处指定的地址或范围
所有地址: 0.0.0.0/0
- [!] -p, --protocol protocol
- protocol: tcp, udp, udplite, icmp, icmpv6,esp, ah, sctp, mh or "all",{tcp|udp|icmp}
[!] -i, --in-interface name: 数据报文流入的接口; 只能应用于数据报文流入的环节,
只能应用于 PREROUTING,INPUT 和 FORWARD 链;
[!] -o, --out-interface name: 数据报文流出的接口; 只能应用于数据报文流出的环节,
能应用于 FORWARD,OUTPUT 和 POSTROUTING 链
(b) 扩展匹配条件:
隐式扩展: 在使用 - p 选项指明了特定的协议时, 无需再同时使用 - m 选项指明扩展模块的扩展机制; 不需要手动加载扩展模块; 因为它们是对协议的扩展, 所以, 但凡使用 - p 指明了协议, 就表示已经指明了要扩展的模块;
(1) tcp:
[!] --source-port, --sport port[:port]: 匹配报文的源端口; 可以是端口范围;
[!] --destination-port,--dport port[:port]: 匹配报文的目标端口; 可以是端口范围;
[!] --tcp-flags mask comp
mask is the flags which we should examine, written as a comma-separated list, 例如 SYN,ACK,FIN,RST
comp is a comma-separated list of flags which must be set, 例如 SYN
例如:"--tcp-flags SYN,ACK,FIN,RST SYN" 表示, 要检查的标志位为 SYN,ACK,FIN,RST 四个, 其中 SYN 必须为 1, 余下的必须为 0;
[!] --syn: 用于匹配第一次握手, 相当于 "--tcp-flags SYN,ACK,FIN,RST SYN";
(2)udp:
[!] --source-port, --sport port[:port]: 匹配报文的源端口; 可以是端口范围;
[!] --destination-port,--dport port[:port]: 匹配报文的目标端口; 可以是端口范围;
- (3)icmp:
- [!] --icmp-type {type
- |typename}
- echo-request:8
- echo-reply:0
显式扩展: 必须要手动加载扩展模块, [-m matchname [per-match-options]];
- (1)multiport
- This module matches a set of source or destination ports. Up to 15 ports can be specified. A port range (port:port) counts as two ports. It can only be used in conjunction with one of the following protocols: tcp, udp, udplite, dccp and sctp.
以离散或连续的 方式定义多端口匹配条件, 最多 15 个;
[!] --source-ports,--sports port[,port|,port:port]...: 指定多个源端口;
[!] --destination-ports,--dports port[,port|,port:port]...: 指定多个目标端口;
# iptables -I INPUT -d 172.16.0.7 -p tcp -m multiport --dports 22,80,139,445,3306 -j ACCEPT
(2)iprange, 指明连续的 (但一般不是整个网络)IP 地址范围
以连续地址块的方式来指明多 IP 地址匹配条件;
- [!] --src-range from[-to]
- [!] --dst-range from[-to]
- # iptables -I INPUT -d 172.16.0.7 -p tcp -m multiport --dports 22,80,139,445,3306 -m iprange --src-range 172.16.0.61-172.16.0.70 -j REJECT
(3)Mac 扩展
指明源 Mac 地址
适用于: PREROUTING,FORWARD,INPUT chains
[!]--Mac-source XX:XX:XX:XX:XX:XX
(4)time 扩展
- This matches if the packet arrival time/date is within a given range.
- --timestart hh:mm[:ss]
- --timestop hh:mm[:ss]
- [!] --weekdays day[,day...]
- [!] --monthdays day[,day...]
- --datestart YYYY[-MM[-DD[Thh[:mm[:ss]]]]]
- --datestop YYYY[-MM[-DD[Thh[:mm[:ss]]]]]
--kerneltz: 使用内核配置的时区而非默认的 UTC;
(5)string 扩展
This modules matches a given string by using some pattern matching strategy.
--algo {bm|kmp} 字符匹配算法
[!] --string pattern 要检测的字符串模式
[!] --hex-string pattern 要检测字符串模式, 16 机制格式
--from offset 开始偏移
--to offset 结束偏移
~]# iptables -I OUTPUT -m string --algo bm --string "gay" -j REJECT
(6)connlimit: 根据每客户端 IP 做并发连接数数量匹配
可防止 CC(Challenge Collapsar 挑战黑洞)
Allows you to restrict the number of parallel connections to a server per client IP address (or client address block).
--connlimit-upto n: 连接的数量小于等于 n 时匹配
--connlimit-above n: 连接的数量大于 n 时匹配
~]# iptables -I INPUT -d 172.16.0.7 -p tcp --syn --dport 22 -m connlimit --connlimit-above 2 -j REJECT
(7)limit, 基于收发报文的速率做匹配, 令牌桶过滤器
- This module matches at a limited rate using a token bucket filter.
- --limit rate[/second|/minute|/hour|/day]
- --limit-burst number
- ~]iptables -I INPUT -d 172.16.0.67 -p icmp --icmp-type 8 -m limt --limt-brust 5 --limit 20/minute -j ACCEPT
- ~]# iptables -I OUTPUT -s 172.16.0.7 -p icmp --icmp-type 0 -j ACCEPT
限制本机某 tcp 服务接收新请求的速率:--syn, -m limit
(8)state
根据 "连接追踪机制" 去检查连接的状态, 较耗资源
conntrack 机制: 追踪本机上的请求和响应之间的关系
- [!] --state state
- INVALID, ESTABLISHED, NEW, RELATED or UNTRACKED.
NEW: 新连接请求; 连接追踪信息库中不存在此连接的相关信息条目, 因此, 将其识别为第一次发出的请求
ESTABLISHED: 已建立的连接, NEW 状态之后, 连接追踪信息库中为其建立的条目失效之前期间内所进行的通信状态
INVALID: 无法识别的连接, 如 flag 标记不正确
RELATED: 相关联的连接, 当前连接是一个新请求, 但附属于某个已存在的连接; 新发起的但与已有的连接相关联的连接, 如: ftp 协议中的数据连接与命令连接之间的关系
UNTRACKED: 未追踪的连接; 如 raw 表中关闭追踪
- iptables -A OUTPUT -s 172.20.11.58 -m state --state ESTABLISHED -j ACCEPT
- iptables -I INPUT -d 172.20.11.58 -m state --state ESTABLISHED -j ACCEPT
- iptables -A INPUT -d 172.20.11.58 -p tcp -m multiport --dports 22,80,139,445,3306 -m state --state NEW -j ACCEPT
- iptables -A OUTPUT -s 172.20.11.58 -j REJECT
- iptables -A INPUT -d 172.20.11.58 -j REJECT
- iptables -R OUTPUT 2 -s 172.20.11.58 -p udp -m multiport --dports 123,323 -m state --state NEW -j ACCEPT
state 扩展:
内核模块装载:
- nf_conntrack
- nf_conntrack_ipv4
手动装载:
modprode nf_conntrack_ftp
追踪到的连接, 已经追踪到的并记录下来的连接信息库
/proc.NET/nf_conntrack
调整连接追踪功能所能够容纳的最大连接数量
/proc/sys.NET/nf_conntrack_max
超时时长:
/proc/sys.NET/netfilter/*timeout*
centos7 需要加载模块:
modprode nf_conntrack
iptables 的链接跟踪表最大容量为 / proc/sys.NET/nf_conntrack_max, 各种状态的超时链接会从表中删除, 当模板满载时, 后续连接可能会超时,
解决方法两个:
(1) 加大 nf_conntrack_max 的值
- VIM /etc/sysctl.conf
- net.nf_conntrack_max=393216
- net.netfilter.nf_conntrack_max=393216
(2) 降低 nf_conntrack timeout 时间
- VIM /etc/sysctl.conf
- net.netfilter.nf_conntrack_tcp_timeout_established=300
- net.netfilter.nf_conntrack_tcp_timeout_wait=120
- net.netfilter.nf_conntrack_tcp_close_wait=60
- net.netfilter.nf_conntrack_tcp_timeout_fin_wait=120
(3) 处理动作:
- -j targetname [per-target-options]
- ACCEPT
- DROP
扩展 target:
- REJECT:
- This is used to send back an error packet in response to the matched packet: otherwise it is equivalent to DROP so it is a terminating TARGET, ending rule traversal.
- --reject-with type
- The type given can be icmp.NET-unreachable, icmp-host-unreachable, icmp-port-unreachable, icmp-proto-unreach- able, icmp.NET-prohibited, icmp-host-prohibited, or icmp-admin-prohibited (*), which return the appropriate ICMP error message (icmp-port-unreachable is the default).
- LOG:
- Turn on kernel logging of matching packets.
- --log-level
- --log-prefix
默认日志保存于 / var/log/messages
RETURN:
返回调用者; 一般在自定义上使用
自定义链做为 target:-j 调用
来源: http://www.bubuko.com/infodetail-3281915.html