iptables — administration tool for IPv4 packet filtering and NAT:管理员工具,用来过滤数据报文和nat转发
四表五链说明:https://blog.csdn.net/longbei9029/article/details/53056744
原理:
-
firewall:工作在主机或者网络边缘,对进出的报文按事先定义的规则进行检查,并且由匹配到的规则进行处理的一组硬件或软件,甚至可以是二者的结合,
-
分类:
-
主机防火墙:工作在内核中的tcp/ip协议栈中,进行规则匹配检查,对单台主机进行防护
-
网络防火墙:工作在网络的边缘。对局域网进行防护,
-
硬件防火墙:在硬件级别实现部分防火功能,软件实现另一部分
-
软件防火墙:在
-
入侵检测系统:IDS,检测网络是否有入侵行为
-
HIDS:OSEC 主机检测
-
NISC:snort 网络检测
-
IPS:IDS+Firewall:入侵检测系统和防御系统,检测到入侵后,通知关闭防火墙,抵御攻击
-
表名:
-
raw:高级功能,如:网址过滤,关闭net表上启用的链接追踪机制
-
mangle:数据包修改,比如:每经过一个路由,TTL值要修改,防火墙标记
-
nat:地址转换,用于网关路由,局域网里所有电脑访问外网,要经过net转换为公网地址访问
-
filter:包过滤,用于防火墙
-
规则链名称:
-
INPUT:处理输入数据包
-
OUTPUT:处理输出数据包
-
FORWARD:处理转发数据包
-
PREROUTING:用于目标地址转换(DNAT)
-
POSTROUTING:用于源地址转换(SNAT)
-
表中包含的链:
filter表:INPUT, FORWARD, OUTPU
[root@localhost ~]#iptables -t filter -nvL Chain INPUT (policy ACCEPT 1876 packets, 264K bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 236 packets, 21566 bytes) pkts bytes target prot opt in out source destination
raw表: PREROUTING, OUTPUT
[root@localhost ~]#iptables -t raw -nvL Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination
nat表:PREROUTING, POSTROUTING, OUTPUT
[root@localhost ~]#iptables -t nat -nvL Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination
mangle表:PREROUTING, INPUT, FORWARD, OUTPUT, POSTROUTING
[root@localhost ~]#iptables -t mangle -nvL Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination
报文流向: 到达本机某进程的报文:PREROUTING --> INPUT 由本机某个进程发出的报文:OUTPUT --> POSTROUTING 由本机转发的报文:PREROUTING --> RORWARD --> POSTROUTING
下面是简化版的,只关心关注的地方
检测优先级次序:(由高到低)
raw –> mangle –> nat –> filter
表名包括:
raw :高级功能,如:网址过滤。
mangle :数据包修改(QOS),用于实现服务质量。
nat :地址转换,用于网关路由器。
filter :包过滤,用于防火墙规则。
动作包括:
ACCEPT :接收数据包。
DROP :丢弃数据包。
REDIRECT :重定向、映射、透明代理。
SNAT :源地址转换。
DNAT :目标地址转换。
MASQUERADE :IP伪装(NAT),用于ADSL。
LOG :日志记录。
rpm包
注意:实验必须在本地进行,不能在本地和云主机间进行,避免设置错误,将自己拒之门外。本地测试成功后才能部署在云主机上,
centos6:默认安装
contos7:firewalld服务,是iptables的二次封装,默认开启的服务,不好用,推荐使用iptables服务,将firewalld服务关闭,并关闭开机启动。
[root@localhost ~]#systemctl status firewalld Active: active (running) [root@localhost ~]#systemctl stop firewalld [root@localhost ~]#systemctl is-enabled firewalld #关闭开机启动 enabled [root@localhost ~]#systemctl disable firewalld Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service. [root@localhost ~]#systemctl is-enabled firewalld disabled
关于iptables的服务在centos7上面有多个
[root@localhost ~]#rpm -qf /usr/sbin/iptables iptables-1.4.21-16.el7.x86_64 [root@localhost ~]#rpm -qf /usr/lib/systemd/system/iptables.service iptables-services-1.4.21-16.el7.x86_64 [root@localhost ~]#rpm -qf /usr/lib/systemd/system/firewalld.service firewalld-0.3.9-14.el7.noarch
可以将centos7中的firewalld服务卸载了。并安装iptables-services,这样就可以用systemctl来开启和关闭服务了。注意不是iptables服务,这样服务没有启动脚本。还要再看看。
[root@localhost ~]#yum remove firewalld [root@localhost ~]#yi iptables-services [root@localhost ~]#rpm -qf /usr/sbin/iptables iptables-1.4.21-16.el7.x86_64
netfilter:位于内核中TCP/IP协议栈中的报文处理模块
iptables:用来设置规则的工具。
链管理命令
iptables [-t table] -P chain target #设置链的默认行为,参数table是可选的, 如果没有设置table默认则是filter。target可以为ACCEPT或者DROP,(白名单或者黑名单)
选项:
-t, --table table:table可以是以下的选项,指明设置的是哪个表, filter:包过滤,用于防火墙 net:地址转换,用于网关路由,局域网里所有电脑访问外网,要经过net转换为公网地址访问 mangle:数据包修改,比如:每经过一个路由,TTL值要修改,防火墙标记 raw:高级功能,如:网址过滤,关闭net表上启用的链接追踪机制
-P, --policy chain target:chain可以是以下的选项,指明是那一条链 INPUT:处理输入数据包 OUTPUT:处理输出数据包 FORWARD:处理转发数据包 PREROUTING:用于目标地址转换(DNAT) POSTROUTING:用于源地址转换(SNAT)
target可以是以下选项,表示设置的默认策略
ACCEPT:接收
DROP:丢弃
示例:
此主机ip:172.18.19.10 [root@localhost ~]#iptables -L #默认input和output都是accept,表示都接收, Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination -------------------------------------------------------------------------------- 此主机ip:172.18.19.30 [root@localhost ~]#ping 172.18.19.10 #可以ping通 PING 172.18.19.10 (172.18.19.10) 56(84) bytes of data. bytes from 172.18.19.10: icmp_seq=1 ttl=64 time=1.11 ms bytes from 172.18.19.10: icmp_seq=2 ttl=64 time=0.637 ms
–
此主机ip:172.18.19.10 [root@localhost ~]#iptables -t filter -P INPUT DROP #将input设置为drop,丢弃, [root@localhost ~]#iptables -L Chain INPUT (policy DROP) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination ----------------------------------------------------------------------------------- 此主机ip:172.18.19.30 [root@localhost ~]#ping 172.18.19.10 #显示一直为等待 PING 172.18.19.10 (172.18.19.10) 56(84) bytes of data. 。。。 解释:10主机接收到ping报文的时候,将其丢弃,30主机以为ping包还没有到达目标,一直显示在等待
链查看命令
iptables [-t table] -L [chain [rulenum]] [options...] #-L列出规则,
option的选项: -n:数字格式,一般要加上-n选项,不然linux会试图去反解ip,响应很慢。 -v -vv -vvv:显示详细精确值 -x:显示计数的精确值 --line-numbers:显示链上的规则编号 常用的组合:[root@localhost ~]#iptables -nvL
[root@localhost ~]#iptables -L INPUT --line-numbers Chain INPUT (policy ACCEPT) num target prot opt source destination ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED ACCEPT icmp -- anywhere anywhere ACCEPT all -- anywhere anywhere ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh REJECT all -- anywhere anywhere reject-with icmp-host-prohibited [root@localhost ~]#iptables -L INPUT -v Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 117K ACCEPT all -- any any anywhere anywhere state RELATED,ESTABLISHED 0 ACCEPT icmp -- any any anywhere anywhere 0 ACCEPT all -- lo any anywhere anywhere 52 ACCEPT tcp -- any any anywhere anywhere state NEW tcp dpt:ssh 142K REJECT all -- any any anywhere anywhere reject-with icmp-host-prohibited [root@localhost ~]#iptables -L INPUT -n Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
链编辑命令
iptables [-t table] -N chain #创建链,一般用于自定义链,只有在默认规则链调用,才会生效,每个链都有引用计数
[root@localhost ~]#iptables -N myfilter [root@localhost ~]#iptables -L Chain myfilter (0 references) target prot opt source destination
iptables [-t table] -X [chain] #删除自定义的空的,引用计数为0的链,取交集成立才会删除,不指定链名,全删,不可逆
[root@localhost ~]#iptables -A myfilter -p tcp -j REJECT [root@localhost ~]#iptables -L Chain myfilter (0 references) target prot opt source destination REJECT tcp -- anywhere anywhere reject-with icmp-port-unreachable [root@localhost ~]#iptables -t filter -X INPUT iptables: Invalid argument. Run `dmesg' for more information. [root@localhost ~]#iptables -t filter -X myfilter iptables: Directory not empty. [root@localhost ~]#iptables -t filter -D myfilter 1 [root@localhost ~]#iptables -t filter -X myfilter 说明:默认的链不能被删除,为了测试,为自定义的链增加一条规则,无法删除。只有自定义的链为空,里面没有规则的时候,才能被删除。
iptables [-t table] -F [chain] #-F删除规则,如果没有指定链,清空指定表的所有链,
[root@localhost ~]#iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED ACCEPT icmp -- anywhere anywhere [root@localhost ~]#iptables -F INPUT [root@localhost ~]#iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination
iptables [-t table] -E old-chain-name new-chain-name #重命名,引用计数为0的链
[root@localhost ~]#iptables -t filter -E OUTPUT hehe iptables: No chain/target/match by that name. [root@localhost ~]#iptables -t filter -E myfilter hehe [root@localhost ~]#iptables -L Chain hehe (0 references) target prot opt source destination REJECT tcp -- anywhere anywhere reject-with icmp-port-unreacha 说明:系统自带的链无法重命名,只能修改自己定义的链的名字
iptables [-t table] -Z [chain [rulenum]] [options...] # 清空数据包计数,每条规则默认有两个计数器,匹配到的包个数(pkgs),以及总字节数(byte)
[root@localhost ~]#iptables -L -v Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 682 58973 ACCEPT all -- any any anywhere anywhere state RELATED,ESTABLISHED [root@localhost ~]#iptables -Z INPUT 1[root@localhost ~]#iptables -L -v Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 9 648 ACCEPT all -- any any anywhere anywhere state RELATED,ESTABLISHED
规则管理类命令
-
iptables规则的组成部分:
-
根据指定的规则匹配每个流经此处的ip报文,一旦匹配成功,则由规则后面指定的动作进行处理
-
匹配条件:
-
网络层首部:Source IP,Destination IP,传输层的协议
-
传输层首部:Source Port,Destination Port
-
扩展检查机制:借助扩展模块进行指定的匹配条件
-
隐式扩展:已经在基本匹配条件中指明的协议相关的扩展
-
显示扩展:隐式之外的扩展
-
处理动作:target
-
基本动作:ACCEPT,DROP,REJECT 见:示例1
-
扩展动作:需要借助扩展模块进行,但无需显示指定,仅需指明动作;
-
添加规则要考虑:
-
报文流向位置:用于判断将规则添加道哪个链
-
实现的功能:判断将规则添加至哪个表
-
报文的流向:判断哪个是源,哪个是目标
-
匹配条件:用于编写能够正常匹配目标报文的规则
规则匹配是自上而下的,如果第一条匹配不到,接着匹配下一条,一直到匹配到符合指定的规则,如果没有,则为链的默认规则(放行或丢弃)
规则管理
iptables [-t table] {-A|--append} chain rule-specification #添加规则,参数table是可选的,如果没有设置table默认则是filter。追加规则到指定的链 iptables [-t table] {-I|--insert} chain [rulenum] rule-specification #插入规则到指定位置(插入设置为第几条),默认插入到链首 iptables [-t table] -R chain rulenum rule-specification #指定规则替换为新的规则
示例1:
此主机ip:172.18.19.10 [root@localhost ~]#iptables -I INPUT -s 172.18.19.30 -j DROP [root@localhost ~]#iptables -L -v -n Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 924 DROP all -- * * 172.18.19.30 0.0.0.0/0 533K ACCEPT all -- * * 172.18.19.0/24 0.0.0.0/0 说明:省略-t选项,默认插入到第一条,省略rulenum,指定30主机drop。 此主机ip:172.18.19.30 [root@localhost ~]#ping 172.18.19.10 PING 172.18.19.10 (172.18.19.10) 56(84) bytes of data. ^C --- 172.18.19.10 ping statistics --- packets transmitted, 0 received, 100% packet loss, time 3000ms 此主机ip:172.18.19.10 [root@localhost ~]#iptables -R INPUT 1 -j REJECT #如果不指定条件,表示默认所有的条件,把自己给拒掉了。 [root@localhost ~]#iptables -vnL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 104K REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable [root@localhost ~]#iptables -R INPUT 1 -s 172.18.19.30 -j REJECT #这里命令替换必须指定rulenum, [root@localhost ~]#iptables -vnL Chain INPUT (policy ACCEPT 2 packets, 143 bytes) pkts bytes target prot opt in out source destination 17304 REJECT all -- * * 172.18.19.30 0.0.0.0/0 reject-with icmp-port-unreachable 750K ACCEPT all -- * * 172.18.19.0/24 0.0.0.0/0 此主机ip:172.18.19.30 [root@localhost ~]#ping 172.18.19.10 PING 172.18.19.10 (172.18.19.10) 56(84) bytes of data. From 172.18.19.10 icmp_seq=1 Destination Port Unreachable From 172.18.19.10 icmp_seq=2 Destination Port Unreachable 说明:30的报文到达10的input链的时候,直接给30返回了主机不可达。
iptables [-t table] {-D|–delete} chain rule-specification #删除规则,好像要全部条件都加上才能够全部删除,很不方便。两种匹配条件,都可以
[root@localhost ~]#iptables -A OUTPUT -s 172.18.19.10 -d 172.18.19.0/24 -p tcp --sport 22 -j ACCEPT [root@localhost ~]#iptables -nvL Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 576 ACCEPT tcp -- * * 172.18.19.10 172.18.19.0/24 tcp spt:22 [root@localhost ~]#iptables -D OUTPUT -s 172.18.19.10 -d 172.18.19.0/24 -p tcp --sport 22 -j ACCEPT [root@localhost ~]#iptables -nvL Chain OUTPUT (policy ACCEPT 8 packets, 1056 bytes) pkts bytes target prot opt in out source destination
iptables [-t table] -D chain rulenum #删除规则,两种匹配条件,都可以
[root@localhost ~]#iptables -L --line-numbers Chain INPUT (policy ACCEPT) num target prot opt source destination ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED ACCEPT icmp -- anywhere anywhere ACCEPT all -- anywhere anywhere ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh REJECT all -- anywhere anywhere reject-with icmp-host-prohibited [root@localhost ~]#iptables -D INPUT 2 [root@localhost ~]#iptables -L --line-numbers Chain INPUT (policy ACCEPT) num target prot opt source destination ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED ACCEPT all -- anywhere anywhere ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh REJECT all -- anywhere anywhere reject-with icmp-host-prohibited 说明:当删除第2条规则的时候,原来的第3条规则会更新为第2条规则
匹配条件:多重条件之间的隐含条件是“与”操作
防火墙规则匹配到第一条就不会再匹配第二条了。不同类别的规则,应该把访问批频繁的访问放在前面,比如web访问大,ssh访问小,应该web放在前面,如果ssh放在前面,每次都会去匹配ssh,浪费资源,速度慢
如果允许全网段的主机访问,但是不允许某一台(172.18.19.30)访问,应该把这个单个主机的规则放在前面,不然不会起作用。
iptables [-t table] {-A|–append} chain [-m matchname [per-match-options] ] [-j targetname [per-target-options]]
基本匹配条件
[!] -s, –source address[/mask][,…]:检查报文中的源IP地址是否符合此处指定的IP,或者IP范围,前面的!表示不匹配这个条件的执行后面的操作。
[root@localhost ~]#iptables -t filter -A INPUT -s 172.18.19.0/24 -j ACCEPT [root@localhost ~]#iptables -L -v -n Chain INPUT (policy ACCEPT 60 packets, 8459 bytes) pkts bytes target prot opt in out source destination 1001 129K ACCEPT all -- * * 172.18.19.0/24 0.0.0.0/0
[!] -i, –in-interface name
[!] -o, –out-interface name:设置 从哪个网卡经过,
[root@localhost ~]#iptables -I INPUT 4 -i lo -j ACCEPT [root@localhost ~]#iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 1363K ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 18483 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 multiport dports 80,6800,9001,9002,9146,9321,35124 state NEW 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 multiport dports 9001,9002,9146,9321 state NEW 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
设置只要是从lo网卡(本地回环地址)进来的报文,不管是什么格式都放行,这样,在命令行里面:ping 127.0.0.1 可以ping通,这里的网卡可以是回环网卡,也可以是实际的网卡,都可以。
–
–
–
评论前必须登录!
注册