iptables使用总结

摘要:
]--protocol-pproto指定匹配协议protocol:bynumberorname,eg.`tcp'[!]--source-saddress[/mask][...]指定源地址;sourcespecification[!]--destination-daddress[/mask][...]指定目标地址;destinationspecification[!

参考:

1、朱双印博客:https://www.zsythink.net/archives/category/%e8%bf%90%e7%bb%b4%e7%9b%b8%e5%85%b3/iptables

2、https://blog.51cto.com/u_10630401/2089708

本篇文章内容基本来自于朱双印博客,这里只是把一些内容归纳在此,方便查找,学习的话,推荐去他博客看,通俗易懂。

基本概念

链 chain

表 table

规则 rule

一、iptables概览

iptables使用总结第1张

echo "1">/proc/sys/net/ipv4/ip_forward

1.1、iptables命令参数

iptables v1.4.21
Usage: iptables -[ACD] chain rule-specification [options]
       iptables -I chain [rulenum] rule-specification [options]
       iptables -R chain rulenum rule-specification [options]
       iptables -D chain rulenum [options]
       iptables -[LS] [chain [rulenum]] [options]
       iptables -[FZ] [chain] [options]
       iptables -[NX] chain
       iptables -E old-chain-name new-chain-name
       iptables -P chain target [options]
       iptables -h (print this help information)

Commands:
Either long or shortoptions are allowed.
  --append  -A chain            在链中插入规则,在链的尾部插入;Append to chain
  --check   -C chain            Check forthe existence of a rule
  --delete  -D chain            删除规则;Delete matching rule from chain
  --delete  -D chain rulenum
                                删除规则,指定规则行号;Delete rule rulenum (1 =first) from chain
  --insert  -I chain [rulenum]
                                在链中插入规则,可指定插入位置,默认在链的首部插入;Insert in chain as rulenum (default 1=first)
  --replace -R chain rulenum
                                修改规则;Replace rule rulenum (1 = first) inchain
  --list    -L [chain [rulenum]]
                                列出某个或所有链路中的规则;List the rules ina chain or all chains
  --list-rules -S [chain [rulenum]]
                                打印某个或所有链路中的规则;Print the rules ina chain or all chains
  --flush   -F [chain]          清空某个链或所有链上的规则;Delete all rules inchain or all chains
  --zero    -Z [chain [rulenum]]
                                Zero counters inchain or all chains
  --new     -N chain            Create a new user-defined chain
  --delete-chain
            -X [chain]          Delete a user-defined chain
  --policy  -P chain target
                                设置默认策略(动作);Change policy on chain to target
  --rename-chain
            -E old-chain new-chain
                                Change chain name, (moving any references)
Options:
    --ipv4      -4              Nothing (line is ignored by ip6tables-restore)
    --ipv6      -6              Error (line is ignored by iptables-restore)
[!] --protocol  -p proto        指定匹配协议protocol: by number or name, eg. `tcp'
[!] --source    -s address[/mask][...]
                                指定源地址;source specification
[!] --destination -d address[/mask][...]
                                指定目标地址;destination specification
[!] --in-interface -i input name[+]
                                network interface name ([+] forwildcard)
 --jump -j target
                                指定动作;target forrule (may load target extension)
  --goto      -g chain
                              jump to chain with no return
  --match       -m match
                                extended match (may load extension)
  --numeric     -n              ip、port以数字方式显示(iptables命令的source、destination显示,提高查询效率);numeric output of addresses and ports
[!] --out-interface -o output name[+]
                                network interface name ([+] forwildcard)
  --table       -t table        table to manipulate (default: `filter')
  --verbose     -v              显示详细信息;verbose mode
  --wait        -w [seconds]    maximum waitto acquire xtables lock before give up
  --wait-interval -W [usecs]    wait timeto try to acquire xtables lock
                      iptables v1.4.21
Usage: iptables -[ACD] chain rule-specification [options]
       iptables -I chain [rulenum] rule-specification [options]
       iptables -R chain rulenum rule-specification [options]
       iptables -D chain rulenum [options]
       iptables -[LS] [chain [rulenum]] [options]
       iptables -[FZ] [chain] [options]
       iptables -[NX] chain
       iptables -E old-chain-name new-chain-name
       iptables -P chain target [options]
       iptables -h (print this help information)

Commands:
Either long or shortoptions are allowed.
  --append  -A chain            在链中插入规则,在链的尾部插入;Append to chain
  --check   -C chain            Check forthe existence of a rule
  --delete  -D chain            删除规则;Delete matching rule from chain
  --delete  -D chain rulenum
                                删除规则,指定规则行号;Delete rule rulenum (1 =first) from chain
  --insert  -I chain [rulenum]
                                在链中插入规则,可指定插入位置,默认在链的首部插入;Insert in chain as rulenum (default 1=first)
  --replace -R chain rulenum
                                修改规则;Replace rule rulenum (1 = first) inchain
  --list    -L [chain [rulenum]]
                                列出某个或所有链路中的规则;List the rules ina chain or all chains
  --list-rules -S [chain [rulenum]]
                                打印某个或所有链路中的规则;Print the rules ina chain or all chains
  --flush   -F [chain]          清空某个链或所有链上的规则;Delete all rules inchain or all chains
  --zero    -Z [chain [rulenum]]
                                Zero counters inchain or all chains
  --new     -N chain            Create a new user-defined chain
  --delete-chain
            -X [chain]          Delete a user-defined chain
  --policy  -P chain target
                                设置默认策略(动作);Change policy on chain to target
  --rename-chain
            -E old-chain new-chain
                                Change chain name, (moving any references)
Options:
    --ipv4      -4              Nothing (line is ignored by ip6tables-restore)
    --ipv6      -6              Error (line is ignored by iptables-restore)
[!] --protocol  -p proto        指定匹配协议protocol: by number or name, eg. `tcp'
[!] --source    -s address[/mask][...]
                                指定源地址;source specification
[!] --destination -d address[/mask][...]
                                指定目标地址;destination specification
[!] --in-interface -i input name[+]
                                network interface name ([+] forwildcard)
 --jump -j target
                                指定动作;target forrule (may load target extension)
  --goto      -g chain
                              jump to chain with no return
  --match       -m match
                                extended match (may load extension)
  --numeric     -n              ip、port以数字方式显示(iptables命令的source、destination显示,提高查询效率);numeric output of addresses and ports
[!] --out-interface -o output name[+]
                                network interface name ([+] forwildcard)
  --table       -t table        table to manipulate (default: `filter')
  --verbose     -v              显示详细信息;verbose mode
  --wait        -w [seconds]    maximum waitto acquire xtables lock before give up
  --wait-interval -W [usecs]    wait timeto try to acquire xtables lock
                                default is 1second
  --line-numbers                查询规则结果以有序列表方式打印(列第一行会显示序列号);print line numbers when listing
  --exact       -x              显示精确值(-v会显示可读性较好的值(如:K),但不够精确);expand numbers (display exact values)
[!] --fragment  -f              match second or further fragments only
  --modprobe=<command>try to insert modules using this command
  --set-counters PKTS BYTES     set the counter during insert/append
[!] --version   -V              print package version.          default is 1second
  --line-numbers                查询规则结果以有序列表方式打印(列第一行会显示序列号);print line numbers when listing
  --exact       -x              显示精确值(-v会显示可读性较好的值(如:K),但不够精确);expand numbers (display exact values)
[!] --fragment  -f              match second or further fragments only
  --modprobe=<command>try to insert modules using this command
  --set-counters PKTS BYTES     set the counter during insert/append
[!] --version   -V              print package version.
View Code

1.2、iptables命令结果字段说明

pkts:对应规则匹配到的报文的个数。

bytes:对应匹配到的报文包的大小总和。

target:规则对应的target,往往表示规则对应的”动作”,即规则匹配成功后需要采取的措施。

prot:表示规则对应的协议,是否只针对某些协议应用此规则。

opt:表示规则对应的选项。

in:表示数据包由哪个接口(网卡)流入,我们可以设置通过哪块网卡流入的报文需要匹配当前规则。

out:表示数据包由哪个接口(网卡)流出,我们可以设置通过哪块网卡流出的报文需要匹配当前规则。

source:表示规则对应的源头地址,可以是一个IP,也可以是一个网段。

destination:表示规则对应的目标地址。可以是一个IP,也可以是一个网段。

1.3、iptables常用动作

ACCEPT:允许数据包通过。

DROP:直接丢弃数据包,不给任何回应信息,这时候客户端会感觉自己的请求泥牛入海了,过了超时时间才会有反应。

REJECT:拒绝数据包通过,必要时会给数据发送端一个响应的信息,客户端刚请求就会收到拒绝的信息。

SNAT:源地址转换,解决内网用户用同一个公网地址上网的问题。

MASQUERADE:是SNAT的一种特殊形式,适用于动态的、临时会变的ip上。

DNAT:目标地址转换。

REDIRECT:在本机做端口映射。

LOG:在/var/log/messages文件中记录日志信息,然后将数据包传递给下一条规则,也就是说除了记录以外不对数据包做任何其他操作,仍然让下一条规则去匹配。

1.4、其他

表优先级:raw > mangle > nat > filter

默认表: filter

规格从上往下匹配,匹配到了,则执行响应动作,后续的,不再匹配。

二、命令详解

2.1、查

iptables -t 表名 -L
iptables -t 表名 -L 链名
iptables -t 表名 -v -L
iptables -t 表名 -vL
iptables -t 表名 -n -L
iptables -t 表名 -nL
iptables --line-numbers -t 表名 -L
iptables --line -t 表名 -L
iptables -t 表名 -v -x -L
iptables -t 表名 -vxL
iptables --line -t filter -nvxL INPUT

2.2、删

# 清空规格 -F
命令语法:iptables -t 表名 -F 链名

# 清空表中所有链的规则
命令语法:iptables -t 表名 -F

方法一:根据规则的编号去删除规则

方法二:根据具体的匹配条件与动作删除规则

# 删除规则 -D
命令语法:iptables -t 表名 -D 链名 规则序号
命令语法:iptables -t 表名 -D 链名 匹配条件 -j 动作
iptables -t filter -D INPUT 3 # 根据规则的编号去删除规则,删除指定行号的规则。行号从--line-numbers那获取
iptables -t filter -D INPUT -s 192.168.0.1 -j DROP # 根据匹配规则条件去删除规则

2.3、增

# 添加规则 -I -A
命令语法:iptables -t 表名 -I 链名 匹配条件 -j 动作 # -I首部插入
命令语法:iptables -t 表名 -I 链名 规则序号 匹配条件 -j 动作
命令语法:iptables -t 表名 -A 链名 匹配条件 -j 动作 # -A尾部追加
示例:
iptables -t 表名 -I 链名 -s 192.168.0.1 -j DROP
iptables -t 表名 -A 链名 -s 192.168.0.1 -j DROP
iptables -t 表名 -I 链名 2 -s 192.168.0.1 -j DROP # 在编号行为2那,插入行。行号从--line-numbers那获取

2.4、改

# 修改规则 -R
命令语法:iptables -t 表名 -R 链名 规则序号 规则原本的匹配条件 -j 动作
#!!! 建议先删除规则,再新建规则。而不是直接修改规则
iptables -t 表名 -R INPUT 2 -s 192.168.0.1 -j DROP # 根据规则编号去修改规则内容,原本匹配规则必须写上,否则没写的内容会变成默认内容

#注意:上例中, -s选项以及对应的源地址不可省略,即使我们已经指定了规则对应的编号,但是在使用-R选项修改某个规则时,必须指定规则对应的原本的匹配条件(如果有多个匹配条件,都需要指定),否则,会回到默认的一些变量。

2.5、设置默认动作

# 修改默认策略 -P
iptables -t 表名 -P 链名 DROP/ACCEPT

命令语法:iptables -t 表名 -P 链名 动作
示例:iptables -t filter -P FORWARD ACCEPT

当前链的默认策略:

packets bytes只有使用-v参数时,才会显示。

iptables使用总结第2张

2.6、保存规则

# 保存规则
centos6:
service iptables save
规则默认保存在/etc/sysconfig/iptables文件中
centos7:
使用firewall替代了原来的iptables service,需要先安装iptables-services,然后可以和centos6一样使用iptables

#停止firewalld
# systemctl stop firewalld
#禁止firewalld自动启动
# systemctl disable firewalld
#配置好yum源以后安装iptables-service
# yum install -y iptables-services
#启动iptables
# systemctl start iptables
#将iptables设置为开机自动启动,以后即可通过iptables-service控制iptables服务
# systemctl enable iptables

iptables-save命令,将当前的iptables规则以”保存后的格式”输出到屏幕上。
iptables-save > /etc/sysconfig/iptables 重定向保存文件
iptables-restore < /etc/sysconfig/iptables 从指定文件中载入规则,重载规则时,现有规则将会被覆盖

三、高级匹配条件

3.1、匹配源地址 -s

Tip:不指定,默认表示0.0.0./0;取反操作与同时指定多个IP的操作不能同时使用。

# 匹配一个IP
iptables -t <表> -I <链> -s 192.168.0.1 -j <动作>

# 匹配多个IP,用逗号隔开,逗号两侧不能有空格

iptables -t <表> -I <链> -s 192.168.0.1,192.168.0.2 -j <动作>
# 匹配网段IP

iptables -t <表> -I <链>-s 192.168.0.0/16 -j <动作>
# IP取反,表示非此ip

iptables -t <表> -I <链>! -s 192.168.1.146 -j <动作>

3.2、匹配目的地址 -d

Tip:不指定,默认表示0.0.0./0;取反操作与同时指定多个IP的操作不能同时使用

语法:iptables -t <表> -I <链> -s <源ip> -d <目标ip> -j <动作>

使用方式同 `3.1、匹配源地址 -s` 一样

3.3、匹配协议 -p

语法:iptables -t <表> -I <链> -s <源ip> -d <目标ip> -p <协议> -j <动作>

centos6中,-p选项支持如下协议类型

tcp, udp, udplite, icmp, esp, ah, sctp

centos7中,-p选项支持如下协议类型

tcp, udp, udplite, icmp, icmpv6,esp, ah, sctp, mh

当不使用-p指定协议类型时,默认表示所有类型的协议都会被匹配到,与使用-p all的效果相同。

3.4、匹配网卡 -i -o

-i 表示流量流入; -o 表示流量流出。

语法:

iptables -t <表> -I <链> -s <源ip> -i <网卡名> -j <动作>

iptables -t <表> -I <链> -s <源ip> -o <网卡名> -j <动作>

tip:-i选项只能用于PREROUTING链、INPUT链、FORWARD链,那么-o选项只能用于FORWARD链、OUTPUT链、POSTROUTING链。

四、扩展模块

如果想要使用扩展匹配条件,则需要依赖一些扩展模块,或者说,在使用扩展匹配条件之前,需要指定相应的扩展模块才行。

扩展匹配条件使用 -- 来表示字段
-m 扩展模块名

iptables -t <表> -I <链> -s <源ip> -m <模块名> --xxx-key-扩展匹配条件 <xxx-val-扩展匹配值> -j <动作>

Tip:当使用-p选项指定了报文的协议时,如果在没有使用-m指定对应的扩展模块名称的情况下,使用了扩展匹配条件, iptables默认会调用与-p选项对应的协议名称相同的模块。所以,如果这个扩展匹配条件所依赖的扩展模块名正好与-p对应的协议名称相同,那么则可省略-m选项,否则则不能省略-m选项,必须使用-m选项指定对应的扩展模块名称。

4.1、tcp

4.1.1、tcp扩展模块 匹配目标端口 --dport

Tip:使用–dport选项时,必须事先指定了使用哪种协议,即必须先使用-p选项。

iptables -t <表> -I <链> -s <源ip> -d <目标ip> -p <协议> -m tcp --dport 22-j <动作>

# 取反

iptables -t <表> -I <链> -s <源ip> -d<目标ip> -p <协议> -m tcp ! --dport 22-j <动作>

# 端口范围,表示22 23 24 25端口; 特殊: 0到22 :22 88到65535 88: 但无法指定不连续的端口

iptables -t <表> -I <链> -s <源ip> -d<目标ip> -p <协议> -m tcp ! --dport 22:25 -j <动作>

示例:

iptables -t filter -I INPUT -s 192.168.0.1 -p tcp -m tcp --dport 22 -j REJECT # -m tcp可省略
iptables -t filter -I INPUT -s 192.168.0.1 -p tcp -m tcp ! --dport 22 -j REJECT # 取反,表示非22端口
iptables -t filter -I INPUT -s 192.168.0.1 -p tcp -m tcp --dport 22:25 -j REJECT # 指定端口范围,22 23 24 25
iptables -t filter -I INPUT -s 192.168.0.1 -p tcp -m tcp --dport :22 -j REJECT # 指定端口范围,0-22
iptables -t filter -I INPUT -s 192.168.0.1 -p tcp -m tcp --dport 80: -j REJECT # 指定端口范围,80号端口以及其以后的所有端口(直到65535)

4.1.2、tcp扩展模块匹配源端口 --sport

同 `4.1、匹配目标端口 --dport` 一样

示例:

iptables -t filter -I INPUT -s 192.168.0.1 -p tcp -m tcp --sport 22 -j REJECT # -m tcp可省略
iptables -t filter -I INPUT -s 192.168.0.1 -p tcp -m tcp ! --sport 22 -j REJECT # 取反,表示非22端口
iptables -t filter -I INPUT -s 192.168.0.1 -p tcp -m tcp --sport 22:25 -j REJECT # 指定端口范围,22 23 24 25
iptables -t filter -I INPUT -s 192.168.0.1 -p tcp -m tcp --sport :22 -j REJECT # 指定端口范围,0-22
iptables -t filter -I INPUT -s 192.168.0.1 -p tcp -m tcp --sport 80: -j REJECT # 指定端口范围,80号端口以及其以后的所有端口(直到65535)

4.1.3、-–tcp-flags

# ”–tcp-flags”指的就是tcp头中的标志位,匹配报文的tcp头的标志位

4.2、multiport

# 只能用于tcp协议与udp协议,即配合-p tcp或者-p udp使用
-m multiport –dports 22,36,80
-m multiport –dports 22,36:80

-m multiport –sports 22,36,80
-m multiport –sports 22,36:80

4.3、iprange

# 指定一段连续的ip地址范围,用于匹配报文的源地址或者目标地址
# iprange可用的扩展匹配条件:--src-range --dst-range,也能够用取反操作
iptables -t filter -I INPUT -m iprange --src-range 192.168.1.127-192.168.1.200 -j REJECT
iptables -t filter -I INPUT -m iprange ! --src-range 192.168.1.127-192.168.1.200 -j REJECT

4.4、string

# 指定要匹配的字符串,如果报文中包含对应的字符串,则符合匹配条件
# string模块可用的扩展匹配条件: --algo 指定匹配算法,可选有bm kmp,必须指定一个,选哪个随便。 --string 指定需要匹配的字符串
iptables -t filter -I INPUT -m string --algo bm --string "OOXX" -j REJECT # 收到报文含有OOXX的字符,就拒绝进入本机

4.5、time扩展模块

# 指定时间段匹配报文
-–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

4.6、connlimit

# 对连接数量进行限制,限制每个IP地址同时链接到server端的链接数量
–-connlimit-above:单独使用此选项时,表示限制每个IP的链接数量。可取反,表示不超过(因为不能等于此值,所以永远都不会超过)
–-connlimit-mask:此选项不能单独使用,在使用–connlimit-above选项时,配合此选项,则可以针对”某类IP段内的一定数量的IP”进行连接数量的限制,如果不明白可以参考上文的详细解释。
–connlimit-upto:centos7中新加的匹配条件,与connlimit-above取反,效果一致。
iptables -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 2 -j REJECT # 表示限制每个客户端IP的ssh并发链接数量不能高于2
iptables -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 20 --connlimit-mask 24 -j REJECT # –connlimit-mask 24″表示某个C类网段,没错,mask为掩码之意,所以将24转换成点分十进制就表示255.255.255.0,所以,上图示例的规则表示,一个最多包含254个IP的C类网络中,同时最多只能有20个ssh客户端连接到当前服务器
iptables -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 10 --connlimit-mask 27 -j REJECT # ”–connlimit-mask 27″表示某个C类网段,通过计算后可以得知,这个网段中最多只能有30台机器(30个IP),这30个IP地址最多只能有10个ssh连接同时连接到服务器端,是不是比刚才的设置大方多了,当然,这样并不能避免某个IP占用所有连接的情况发生,假设,报文来自192.168.1.40这个IP,按照掩码为27进行计算,这个IP属于192.168.1.32/27网段,如果192.168.1.40同时占用了10个ssh连接,那么当192.168.1.51这个IP向服务端发起ssh连接请求时,同样会被拒绝,因为192.168.1.51这个IP按照掩码为27进行计算,也是属于192.168.1.32/27网段,所以他们共享这10个连接名额。

4.7、limit

# 对”报文到达速率”进行限制的
–-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


4.8、hashlimit扩展模块

# 对端口限速用,网上看到的模块
iptables -A INPUT -p tcp --dport 22 -m hashlimit --hashlimit-name conn_limitA --hashlimit-htable-expire 30000 --hashlimit-upto 100kb/s --hashlimit-mode srcip --hashlimit-burst 200kb -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j DROP
----------------------------上传----------------------------------
iptables -A OUTPUT -p tcp --sport 22 -m hashlimit --hashlimit-name conn_limitB --hashlimit-htable-expire 30000 --hashlimit-upto 100kb/s --hashlimit-mode dstip --hashlimit-burst 200kb -j ACCEPT
iptables -A OUTPUT -p tcp --sport 22 -j DROP
-----------------------------下载---------------------------------

朱双印博客,扩展5 6 7 8 9 10 11没仔细看~

五、自定义链

自定义链
# 当规则很多时,方便管理
使用自定义链步骤:
1、创建自定义链
2、基于自定义链配置规则
3、在默认链中引用自定义链
示例:
iptables -t filter -N IN_WEB # 在filter表中创建IN_WEB自定义链
iptables -t filter -I IN_WEB -s 192.168.0.1 -j REJECT # 给IN_WEB链添加规则
iptables -t filter -I INPUT -p tcp --dport 80 -j IN_WEB # 示例:在INPUT链中引用刚才创建的自定义链

重命名自定义链
#示例:将IN_WEB自定义链重命名为WEB
iptables -E IN_WEB WEB

删除自定义链
删除自定义链需要满足两个条件
1、自定义链中没有任何规则,可用-F清空规则
2、自定义链没有被引用,可在引用链上删除引用,-D方式

#示例:删除引用计数为0并且不包含任何规则的WEB链
iptables -X WEB

六、动作详解

6.1、基础动作和扩展动作

基础动作:ACCEPT DROP

扩展动作:REJECT

Tip:使用扩展动作也需要借助扩展模块,但是,扩展动作可以直接使用,不用像使用”扩展匹配条件”那样指定特定的模块。

6.2、REJECT动作常用选项

reject动作常用选项为–-reject-with,使用–reject-with选项,可以设置提示信息,当对方被拒绝时,会提示对方为什么被拒绝。

icmp-port-unreachable (默认)
icmp-net-unreachable
icmp-host-unreachable
icmp-proto-unreachable
icmp-net-prohibited
icmp-host-pro-hibited
icmp-admin-prohibited

使用示例:

-j REJECT –reject-with icmp-host-unreachable

6.3、LOG动作

LOG动作只负责记录匹配到的报文的相关信息,不负责对报文的其他处理,如果想要对报文进行进一步的处理,可以在之后设置具体规则,进行进一步的处理。

使用的匹配条件过于宽泛,所以匹配到的报文数量将会非常之多,记录到的信息也不利于分析,所以在使用LOG动作时,匹配条件应该尽量写的精确一些,匹配到的报文数量也会大幅度的减少,这样冗余的日志信息就会变少,同时日后分析日志时,日志中的信息可用程度更高。

6.3.1、LOG动作存储路径

LOG动作会将报文的相关信息记录在/var/log/message文件中,也可指定输出日志路径,方式如下:

修改/etc/rsyslog.conf文件(或者/etc/syslog.conf),在rsyslog配置文件中添加如下配置即可 kern.warning /var/log/iptables.log,然后重启syslog,service rsyslog restart

6.3.2、LOG动作常用选项

–log-level选项可以指定记录日志的日志级别,可用级别有emerg,alert,crit,error,warning,notice,info,debug。
–log-prefix选项可以给记录到的相关信息添加”标签”之类的信息,以便区分各种记录到的报文信息,方便在分析时进行过滤。在日志中通过搜索便签信息,便可过滤出所有的此规则日志
tip:–log-prefix对应的值不能超过29个字符。
示例:
iptables -I INPUT -p tcp --dport 22 -j LOG --log-level error --log-prefix "the-prefix-info"

6.4、★★★SNAT DNAT

推荐阅读:https://www.zsythink.net/archives/1764

NAT是Network Address Translation的缩写,译为”网络地址转换”,NAT说白了就是修改报文的IP地址,NAT功能通常会被集成到路由器、防火墙、或独立的NAT设备中。

内部网络的报文发送出去时,报文的源IP会被修改,也就是源地址转换。外部网络的报文响应时,响应报文的目标IP会再次被修改,也就是目标地址转换。整个过程被称为SNAT还是DNAT,取决于整个过程的前半段使用了SNAT还是DNAT。

这两句话,有利于理解snat dnat为什么匹配的链是那几条

(1)当一个数据包进入网卡时,数据包首先进入PREROUTING链,内核根据数据包目的IP判断是否需要转送出去
(2)如果数据包就是进入本机的,数据包就会到达INPUT链。经INPUT链检查后,数据包被发往本地进程。本地进程进行相应处理后发送响应数据包,数据包经过OUTPUT链,然后到达POSTROUTING链输出;如果数据包是要转发出去的,且内核允许转发,数据包就会向右移动,经过FORWARD链,然后到达POSTROUTING链输出。

6.4.1、SNAT动作

SNAT规则同时配置后,即可正常DNAT;即只是用于配置SNAT的话,我们并不用手动的进行DNAT设置,iptables会自动维护NAT表,并将响应报文的目标地址转换回来。

在centos7中,SNAT规则只能存在于POSTROUTING链与INPUT链中;
在centos6中,SNAT规则只能存在于POSTROUTING链中;

示例:
iptables -t nat -A POSTROUTING -s 10.1.0.0/16 -j SNAT --to-source 192.168.1.146

6.4.2、DNAT动作

DNAT规则同时配置后,不可正常SNAT,还需要加上一条SNAT规则(参考 https://blog.csdn.net/CHEndorid/article/details/107139022);

DNAT规则只配置在PREROUTING链与OUTPUT链中;

示例:
iptables -t nat -A PREROUTING -d 192.168.1.146 -p tcp --dport 3389 -j DNAT --to-destination 10.1.0.6:3389
iptables -t nat -A POSTROUTING -s 10.1.0.0/16 -j SNAT --to-source 192.168.1.146

6.5、MASQUERADE动作

作用:MASQUERADE会动态的将源地址转换为可用的IP地址,其实与SNAT实现的功能完全一致,都是修改源地址,只不过SNAT需要指明将报文的源地址改为哪个IP,而MASQUERADE则不用指定明确的IP,会动态的将报文的源地址修改为指定网卡上可用的IP地址。

作用场景:当我们拨号网上时,每次分配的IP地址往往不同,不会长期分给我们一个固定的IP地址,如果这时,我们想要让内网主机共享公网IP上网,就会很麻烦,因为每次IP地址发生变化以后,我们都要重新配置SNAT规则,这样显示不是很人性化,我们通过MASQUERADE即可解决这个问题。

MASQUERADE理解为动态的、自动化的SNAT,如果没有动态SNAT的需求,没有必要使用MASQUERADE,因为SNAT更加高效。

示例:

iptables -t nat -I POSTROUTING -s 10.1.0.0/16 -o <网卡名> -j MASQUERADE

6.6、REDIRECT动作

作用:可以在本机上进行端口映射。

REDIRECT规则只能定义在PREROUTING链或者OUTPUT链中。

示例:当别的机器访问本机的80端口时,报文会被重定向到本机的8080端口上。

iptables -t nat -A PREROUTING -p tcp –dport 80 -j REDIRECT –to-ports 8080

七、ipset

https://blog.csdn.net/gymaisyl/article/details/101695697

在iptables中使用ipset,只要加上-m set --match-set即可。

目的ip使用ipset(ipset集合为bbb)
iptables -I INPUT -s 192.168.100.36 -m set --match-set bbb dst -j DROP

源ip使用ipset(ipset集合为aaa)
iptables -I INPUT -m set --match-set aaa src -d 192.168.100.36 -j DROP

源和目的都使用ipset(源ip集合为aaa,目的ip集合为bbb)
iptables -I INPUT -m set --match-set aaa src -m set --match-set bbb dst -j DROP

白名单机制:
# 默认策略为ACCEPT,防止误操作-F,则拒绝所有报文,正确的白名单机制如下
iptables -I INPUT -p tcp --dport 22 -j ACCEPT
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -j REJECT

iptables做网络防火墙

https://www.zsythink.net/archives/1663

.

免责声明:文章转载自《iptables使用总结》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇深入研究自监督单目深度估计:Monodepth2golang-mongodb范例下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

makefile从无到有

Makefile这玩意在上学时就应该学,可是一直沉浸于IDE的诱惑,所谓“死于安乐”,直到现在一把年纪才开始接触这种基础东西。 创建C程序 先写个c程序,保存在main.c里: view plain ////////////////// //file:main.c ////////////////// #include<stdio.h>...

Struts2(二)— Result结果配置、Servlet的API的访问、模型驱动、属性驱动

一.Result结果配置 1.全局和局部结果 ​   平常我们设置跳转页面,是在action标签里面加上 result标签来控制,这种设置的页面跳转,称之为局部结果页面但是我们有时候在很多个action里面,针对不同的结果进行跳转时,也有可能跳转同一个页面,那么这个时候就可以配置全局结果页面。 1.1局部结果 在action里面写上的result跳转的页...

TCP/IP协议(一)网络基础知识 网络七层协议

参考书籍为《图解tcp/ip》-第五版。这篇随笔,主要内容还是TCP/IP所必备的基础知识,包括计算机与网络发展的历史及标准化过程(简述)、OSI参考模型、网络概念的本质、网络构建的设备等     下面是协议层从底层至顶层的一个模型图:   一、计算机网络的背景 1.1 计算机的发展 有人说:“20世纪最伟大的发明就是计算机”,自诞生伊始,计算机经历了...

linux的ftp安装及配置

一、安装ftp服务 1.检查是否已经安装 rpm -qa| grep vsftpd 2.没安装则安装 centos用: yum -y install vsftpd ubuntu用: apt-get install vsftpd 二、启动ftp服务 1.启动ftp服务: service vsftpd start 重启ftp service vsf...

namp命令详解

我将用两个不同的部分来涵盖大部分NMAP的使用方法,这是nmap关键的第一部分。在下面的设置中,我使用两台已关闭防火墙的服务器来测试Nmap命令的工作情况。 192.168.0.100–server1.tecmint.com 192.168.0.101–server2.tecmint.com NMAP命令用法 #nmap[ScanType(s)][Op...

domino控制台命令

Broadcast message 向该服务器的用户广播消息 Broadcast message usernames 向该服务器的某一用户广播消息 Dbcache Flush 关闭当前在数据库高速缓存中打开的所有数据库。使用此命令可以在高速缓存中维护数据库,例如:备份或恢复数据库。 Drop username 关闭一个或多个服务器会话。如果...