iptable防火墙使用简略参考
iptables命令选项输入顺序:iptables -t 表名 <-A/I/D/R> 规则链名 [规则号] <-i/o 网卡名> -p 协议名 <-s 源IP/源子网> --sport 源端口 <-d 目标IP/目标子网> --dport 目标端口 -j 动作
表名包括:
raw:高级功能,如:网址过滤。
mangle:数据包修改(QOS),用于实现服务质量。
net:地址转换,用于网关路由器。
filter:包过滤,用于防火墙规则。
规则链名包括:
INPUT链:处理输入数据包。
OUTPUT链:处理输出数据包。
PORWARD链:处理转发数据包。
PREROUTING链:用于目标地址转换(DNAT),路由前。
POSTOUTING链:用于源地址转换(SNAT),路由后。
动作包括:
accept:接收数据包。
DROP:丢弃数据包。
REDIRECT:重定向、映射、透明代理。
SNAT:源地址转换。
DNAT:目标地址转换。
MASQUERADE:IP伪装(NAT),用于ADSL。
LOG:日志记录。
三表五链
链就是位置,共有五个:数据包进路由之前(PREROUTING)、目的地址为本机,进系统(INPUT)、转发(FORWARD)、原地址为本机,向外发送,出系统(OUTPUT)、发送到网卡之前,出路由(POSTROUTING);数据包到了该链处,会去对应表中查询设置的规则,然后决定是否放行、丢弃、转发还是修改等等操作。每个链都是一个规则列表,对对应的包进行匹配。
具有相同功能的规则的集合叫做表:
filter表:负责过滤功能,经过内核的;
nat表:网络地址转换,不经过内核的 ,实现数据包转发,修改源地址 端口 目标地址端口,实现地址转换;
mangle表:拆解报文,作出修改,封装报文;
raw表:关闭nat表上启用的链接追踪机制。
表链关系:所有自定义的规则都是这四种分类中的规则,或者说所有的规则都存在于这4张表中。
filter表格:放的是经过内核的ip input output forward
nat表格:放的不是经过内核的服务 input output postrouting prerouting
备用表格mangle:input output forward postrouting prerouting
以下指令将在iptables的顶部插入规则:允许所有流量,除非随后由另一个规则处理。
iptables -I INPUT -j ACCEPT
还可以使用以下命令刷新整个iptables设置:
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
comment模块来支持注释
iptables -A INPUT -i lo -j ACCEPT -m comment --comment "Allow all loopback traffic"
iptables -A INPUT ! -i lo -d 127.0.0.0/8 -j REJECT -m comment --comment "Drop all traffic to 127 that doesn't use lo"
iptables -A OUTPUT -j ACCEPT -m comment --comment "Accept all outgoing"
iptables -A INPUT -j ACCEPT -m comment --comment "Accept all incoming"
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT -m comment --comment "Allow all incoming on established connections"
iptables -A INPUT -j REJECT -m comment --comment "Reject all incoming"
iptables -A FORWARD -j REJECT -m comment --comment "Reject all forwarded"
iptables -I INPUT -p tcp --dport 80 -j ACCEPT -m comment --comment "Allow HTTP"
iptables -I INPUT -p tcp --dport 443 -j ACCEPT -m comment --comment "Allow HTTPS"
iptables -I INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT -m comment --comment "Allow SSH"
iptables -I INPUT -p tcp --dport 8071:8079 -j ACCEPT -m comment --comment "Allow torrents"
保存工作现场到文件中
iptables-save > /etc/network/iptables.rules
to accept all incoming traffic you can use following command , -P is to set default policy as accept
iptables -P INPUT ACCEPT
不作解析:iptables -t
列出指定表中的策略:iptables -L
增加策略:iptables -A
网络协议:iptables -P
端口:iptables –dport
数据来源:iptables -s
iptables -j 动作:ACCEPT允许;REJECT拒绝;DROP丢弃。
增加链:iptables -N
修改链名称:iptables -E
删除链:iptables -X
删除指定策略:iptables -D
插入:iptables -I
修改策略:iptables -R
修改默认策略:iptables -P
清除所有策略:iptables -F
修改链的名称: iptables -E redhat Freeoa
修改链的默认策略:iptables -P INPUT DROP
默认策略只可以修改为DROP和ACCET,策略的动作 REJECT –> DROP 从有拒绝提示到没有提示。
不跟-t,默认为filter表格:iptables -t filter -nL
一、基本格式
1. iptable [-t 表] 命令选项 [连名] 匹配条件 [-j 动作]
2.常用命令选项如下:
-A [append] 在指定的连的结尾添加规则
-D [delete]删除指定连中的规则,可以按规则号或规则内容匹配
-I [insert]插入一条新规则,默认是在最前面
-R [replace] 替换某一条规则
-L [list]列出所有规则
-F [flush]清空所有规则
-N [new]自定义一条规则连
-X [--delete-chain] 删除用户自定义规则连
-P [policy]设置默认策略
-n [numeric]以数字方式显示,如:显示ip,但不显示主机名
-v [verbose]显示详细信息
-V [version]查看iptable的版本信息
--line-number 查看规则连是,显示列表号
-Z 清空计数器值
Iptables parameters and Options Definitions
Here are some explanations for iptables parameters, options, and values used in the examples above, as well as a few of them not mentioned before.
| >Parameter | Description |
| -c | counters – allows setting the packet and byte counters of a specific rule |
| -d | destination – can be an address, name of a host or address, etc. |
| -f | fragment – applies the rule to the second and the fragments that follow it |
| -g | goto chain – states that the action will continue in a user-specified chain |
| -i | in-interface – states the name of the interface from where packets come |
| -j | jump – specifies the action if a packet matches the rule |
| -o | out-interface – the name of the interface of an outgoing package |
| -p | protocol – any available protocol such as SSH, TCP, UDP, FTP. |
| -s | source – can be an address, name of a host or address, etc. |
| Value | Description |
| ACCEPT | Allows the packets to pass through |
| DROP | Blocks the packets |
| RETURN | Tells to skip the current chain and resume at the next rule in the previous (calling) chain |
| Chain | Description |
| INPUT | Controls the incoming packets. |
| FORWARDS | Forwards the packets coming to your server but destined for somewhere else |
| OUTPUT | Filters packets going out of your server |
| Option | Description |
| -A | append – adds one (or more) rules of the selected chain |
| -C | check – checks for a rule that matches the criteria in the selected chain |
| -D | delete – deletes only one rule from the selected chain |
| -F | flush – deletes all defined iptables rules |
| -I | insert – insert a rule into the selected chain |
| -L | list – displays the rules of the selected chain |
| -n | numeric – shows the IP address/hostname and return value in a numeric format |
| -N | new-chain <name> – creates a new user-defined chain |
| -v | verbose – used in the combination with -L to provide additional information |
| -X | delete-chain <name> – deletes the user-defined chain. |
二、举例
iptable -t filter -F[清空filter表中所有规则]
iptable -t filter -Z[清空filter表中的计数器值]
iptable -t filter -X [清除filter表中自定义连]
iptable -t filter -P INPUT DROP [设置INPUT连默认策略为DROP]
iptable -t filter -P OUTPUT DROP
iptable -t filter -P FORWROD DROP
iptable -t filter -A INPUT -p tcp -j ACCEPT [在INPUT连最后添加一条允许tcp协议的数据包进入的规则]
iptable -t filter -R INPUT 1 -p tcp -j DROP [替换INPUT连的第1条规则为拒绝tcp数据包进入]
iptable -t nat -vnL --line-number [以详细的、数字的格式列出nat表中的所有规则]
iptable -t nat -D POSTROUTING 1 [删除nat表POSTROUTING 连中的第1条规则]
三、条件匹配
1. 协议匹配:用于检查数据包使用的协议,符合规则就允许,反之拒绝。允许使用的协议名在/etc/protocols文件中。常用的协议有tcp,udp,icmp,ip 和all。[ -p 协议名 ]
iptable -I INPUT -p icmp -j REJECT [拒绝进入防火墙本身的icmp数据包]
iptable -A FORWARD -p udp -j ACCEPT [允许转发udp的所有数据包]
2. 地址匹配:用于检查数据包的地址是否符合规则,包括源地址和目的地址。[-s 源地址,-d 目的地址]
iptable -A FORWARD -s 10.0.0.0/8 -j DROP [拒绝转发来自10.0.0.0/8 网段的数据包]
iptable -A FORWARD -d 80.0.0.0/8 -j DROP [ 拒绝转发目的是80.0.0.0/8 网段的数据包]
3.端口匹配:用于检查数据包的tcp或udp端口,需要和 “-p 协议类型” 一起使用[-sport 源端口,-dport 目的端口]
iptables -A FORWARD -s 10.0.0.0/8 -p tcp --dport 80 -j ACCEPT [允许转发来自10.0.0.0/8网段,目的端口是80的数据包]
iptables -I FORWARD -s 10.0.0.0/8 -p tcp --sport 21 -j ACCEPT[允许转发来自10.0.0.0/8网段,源端口是21的数据包]
4.接口匹配:用于检查数据包从防火墙那个接口进入或出去,来判断是否允许。
iptables -A FORWARD -i eth0 -s 10.0.0.0/8 -p tcp --dport 80 -j ACCEPT
[允许转发从eth0进入,来自10.0.0.0/8网段,使用tcp 协议,目的端口椒80的数据包]
iptables -A INPUT -i eth0 -s 80.0.0.0/8 -j DORP [拒绝从eth0进入,来自80.0.0.0/8的数据包]
5.SNAT转换:一般linux充当网关服务器时使用
SNAT只能用在nat表的POSTROUTING连,用于对源地址进行转换。要结合 --to 使用。
iptables -t nat -A POSTROUTING -s 10.0.0.0/8 -j SNAT --to 202.106.1.1
[将来自10.0.0.0/8网段的所有数据包的源地址转为202.106.1.1]
iptables -t nat -A POSTROUTING -i eth0 -s 80.0.0.0/8 -p tcp --dport 25 -j SNAT --to 202.106.1.1
6.DNAT转换:只能用在nat表中的PREROUTING连,用于对目的地址或端口进行转换。
iptables -t nat -A PREROUTING -i eth1 -d 202.106.1.1 -p tcp --dport 80 -j DNAT --to 10.0.0.10
[将从eth1 进入,目的地址是202.106.1.1,使用tcp 协议,目的端口是80的数据包的目的地址转为10.0.0.1]
7.MASQUERADE:伪装,是SNAT的特例。
iptables -t nat -A POSTROUTING -s 10.0.0.0/8 -o eth1 -j MASQUERADE
[将来自10.0.0.0/8网段,从eth1出去的数据包的源地址伪装为eth1接口地址]
8.使用iptables的iprange模块限制IP段的访问。
假设有一个情况,我们要将某一个网段内的IP“一段IP”封锁起来,如192.168.20.2-192.168.20.61,这个规则如何设定?因为这个网段并没有符合任何一个CIDR网段,因此我们不能使用如"-s 192.168.20.0/28"的网段来匹配这个范围,难道真的要一行一行写吗?其实不用,早期的做法是将192.168.20.0/24切成多个小网段,再去匹配哪一个小网段与被封锁的区段比较接近,然后再将遗漏的部分补足。
借助于ipt_iprange.ko模块,我们可以将上边的规则改写为:
iptables -A INPUT -m iprange --src-range 192.168.20.2-192.168.20.61 -j DROP
iprange模块提供了两个匹配参数:
--src-range: 匹配来源地址的范围,例如,iptables -A INPUT -m iprange --src-range 192.168.20.2-192.168.20.61 -j DROP
--dst-range: 匹配目的地址的范围,例如,iptables -A OUTPUT -m iprange --dst-range 192.168.20.2-192.168.20.61 -j DROP
9.通过相邻的机器进行端口转发进而访问相应的服务
由于跨网段和做了nat映射的原因,不能直接访问10.206.30.71:80,但10.206.30.72却没有这个问题,因此通过访问72的80端口来访问71的80服务。
在72这台机器上操作:
#!/bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -F
iptables -t nat -F
iptables -X
iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 10.206.30.71:80
iptables -t nat -A POSTROUTING -p tcp -d 10.206.30.71 --dport 80 -j SNAT --to-source 10.206.30.72
四、拓展模块
1.按来源MAC地址匹配
iptables -t filter -A FORWARD -m --mac-source 00:12:b2:03:a5:f6 -j DROP
[拒绝转发来自该MAC地址的数据包]
2.按多端口或连续端口匹配
20: 表示20以后的所有端口
20:100 表示20到100的端口
:20 表示20之前的所有端口
-m multiport [--prots, --sports,--dports]
iptables -A INPUT -p tcp -m multiport --dports 21,20,25,53,80 -j ACCEPT [多端口匹配]
iptables -A INPUT -p tcp -dport 20: -j ACCEPT
iptables -A INPUT -p tcp -sport 20:80 -j ACCEPT
iptables -A INPUT -p tcp -sport :80 -j ACCEPT
3.还可以按数据包速率和状态匹配
-m limit --limit 匹配速率 如: -m limit --limit 50/s -j ACCEPT
-m state --state 状态 如: -m state --state INVALID,RELATED -j ACCEPT
五、为iptables设定日志记录
为iptables日志配置rsyslog服务
# vim /etc/rsyslog.d/10-iptables.conf
:msg,contains,"iptables:" /var/log/iptables.log
添加完成后一定要重启rsyslog服务。
iptables -A OUTPUT -p tcp -d 172.16.18.51 --dport 3306 -j LOG --log-level 6 --log-prefix "iptables:"
上述规则添加完成后,有数据流过时就会有日志生成。
对应的日志片断
May 21 17:08:41 freeoa03 kernel: iptables:IN= OUT=eth0 SRC=172.16.18.210 DST=172.16.18.10 LEN=52 TOS=0x00 PREC=0x00 TTL=64 ID=26793 DF PROTO=TCP SPT=38610 DPT=3306 WINDOW=237 RES=0x00 ACK URGP=0
May 21 17:08:55 freeoa03 kernel: iptables:IN= OUT=eth0 SRC=172.16.18.210 DST=172.16.18.10 LEN=52 TOS=0x00 PREC=0x00 TTL=64 ID=26794 DF PROTO=TCP SPT=38610 DPT=3306 WINDOW=237 RES=0x00 ACK FIN URGP=0
May 21 17:08:55 freeoa03 kernel: iptables:IN= OUT=eth0 SRC=172.16.18.210 DST=172.16.18.10 LEN=52 TOS=0x00 PREC=0x00 TTL=64 ID=26795 DF PROTO=TCP SPT=38610 DPT=3306 WINDOW=237 RES=0x00 ACK URGP=0
/sbin/iptables -I FORWARD 4 -j LOG --log-prefix "RULE4:" --log-level 7
/sbin/iptables -t nat -I POSTROUTING 3 -j LOG --log-prefix "NAT3:" --log-level 7
# Log dropped traffic
/sbin/iptables -A INPUT -j LOG -m limit --limit 10/m --log-level 4 --log-prefix "Dropped Traffic: "
# Log outbound traffic for anything not equal private ip ranges (this is defined in some previous rules)
/sbin/iptables -A OUTPUT -j LOG -m limit --limit 10/m --log-level 4 --log-prefix "Outbound Traffic: "
# Log traffic that doesn't hit a rule above (stuff that may be blocked in the future)
/sbin/iptables -A INPUT -j LOG -m limit --limit 10/m --log-level 4 --log-prefix "Potentially Dropped Traffic: "
记录所有出去的链接
Log all new outbound tcp connections excluding connections happening to localhost and IPs in same network.
iptables -I OUTPUT -m state -p tcp --state NEW ! -d $i/24 -m limit --limit 1/m --limit-burst 1 -j LOG --log-uid --log-prefix "OutboundConnection: "
iptables LOG and DROP in one rule
iptables -A INPUT -j LOG --log-prefix "INPUT:DROP:" --log-level 6
iptables -A INPUT -j DROP
示例:
Feb 29 14:18:06 servername kernel: INPUT:DROP:IN=eth1 OUT= MAC=aa:bb:cc:dd:ee:ff:11:22:33:44:55:66:77:88 SRC=x.x.x.x DST=x.x.x.x LEN=48 TOS=0x00 PREC=0x00 TTL=117 ID=x PROTO=TCP SPT=x DPT=x WINDOW=x RES=0x00 SYN URGP=0
Other LOG options:
Turn on kernel logging of matching packets. When this option is set for a rule, the Linux kernel will print some information on all matching packets (like most IP header fields) via the kernel log (where it can be read with dmesg or syslogd(8)). This is a "non-terminating target", i.e. rule traversal continues at the next rule. So if you want to LOG the packets you refuse, use two separate rules with the same matching criteria, first using target LOG then DROP (or REJECT).
--log-level level
Level of logging (numeric or see syslog.conf(5)).
--log-prefix prefix
Prefix log messages with the specified prefix; up to 29 letters long, and useful for distinguishing messages in the logs.
--log-tcp-sequence
Log TCP sequence numbers. This is a security risk if the log is readable by users.
--log-tcp-options
Log options from the TCP packet header.
--log-ip-options
Log options from the IP packet header.
--log-uid
Log the userid of the process which generated the packet.
Let's create a chain to log and accept:
iptables -N LOG_ACCEPT
And let's populate its rules:
iptables -A LOG_ACCEPT -j LOG --log-prefix "INPUT:ACCEPT:" --log-level 6
iptables -A LOG_ACCEPT -j ACCEPT
Now let's create a chain to log and drop:
iptables -N LOG_DROP
And let's populate its rules:
iptables -A LOG_DROP -j LOG --log-prefix "INPUT:DROP: " --log-level 6
iptables -A LOG_DROP -j DROP
Now you can do all actions in one go by jumping (-j) to you custom chains instead of the default LOG / ACCEPT / REJECT / DROP:
iptables -A <your_chain_here> <your_conditions_here> -j LOG_ACCEPT
iptables -A <your_chain_here> <your_conditions_here> -j LOG_DROP
对丢弃RST状态进行记录
iptables -I INPUT -s 11.22.0.0/16 -p tcp --tcp-flags RST RST -j DROP
sudo iptables -I INPUT -s 11.22.0.0/16 -p tcp --tcp-flags RST RST -j LOG --log-prefix "drop rst"
六、问题集:
iptables error: unknown option --dport
iptables v1.4.21: unknown option "--dport
不认识 --dport 选项,一个很可能的原因是指定了 -p all ,如果是 ICMP 协议,那么不存在端口概念,因此这里就会出错,应该指定为 -p tcp 或 -p udp。