iptables基本匹配

您所在的位置:网站首页 tcp丢弃数据包 iptables基本匹配

iptables基本匹配

2023-06-07 13:03| 来源: 网络整理| 查看: 265

文章目录 Iptables命令1.1 列出表中链的规则1.2 添加规则1.3 修改规则1.4 删除规则1.5 清空所有规则 示例1示例2示例3

Iptables命令 iptables -t ${表名} ${Commands} ${链名} ${链中的规则号} ${匹配条件} ${目标动作} * 表名:4张表,filter、nat、mangle、raw * Commands: -t: 指定操作的表 -A, --append 追加一条规则到链中,在对应链的末尾添加规则 -D, --delete 删除链中的规则 -I, --insert 插入一条规则,插入到顶部 -R, --replace 修改 -L, --list 列出当前的规则 -S, --list-rules 列出所有的规则 -F, --flush 清空 -Z, --zero 清空计数器( 包数量 、包大小) -N, --new-chain 创建一个自定义 链 -X, --delete-chain 删除一个自定义链 -P, --policy 指定链的默认策略 * 链名:5条链,PREROUTING、INPUT、FORWOARD、OUTPUT、POSTROUTING * 匹配条件:-p协议、-s 源地址、-d 目标地址、-i 网络接口名 源地址:-s 192.168.1.0/24 目标地址:-d 192.168.1.11 协议:-p tcp|udp|icmp 从哪个网卡进来:-i eth0|lo 从哪个网卡出去:-o eth0|lo 目标端口(必须制定协议):-p tcp|udp --dport 8080 源端口(必须制定协议):-p tcp|udp --sport 8080 * 目标动作: 拒绝访问-j REJECT、 允许通过-j ACCEPT、 丢弃-j DROP、 记录日志 -j LOG、 源地址转换-j snat、 目标地址转换-j dnat、 还有RETURN、QUEUE

可以通过像如下查看使用帮助文档。主要可以分如下三个部分

# iptables --help Usage: # 使用案例 Commands: # 命令选项 Options: # 其他可选项 1.1 列出表中链的规则 # iptables -t filter -L -n -v Chain INPUT (policy ACCEPT 1 packets, 40 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

可以看出表filter中有三条链,分别是INPUT、FORWARD、OUTPUT。默认规则为policy ACCEPT允许通过。

-t filter:指定用filter表 -L 列出当前的规则 -n 地址和端口的数字输出,不解析IP地址 -v 详细模式 -line-numbers:显示规则的序号(简写为--line) 省略 -t 选项时,表示默认操作 filter 表中的规则: 1.2 添加规则

添加禁止ping本机规则

# iptables -t filter -I INPUT -p ICMP -j REJECT # iptables -L -n -v --line-numbers Chain INPUT (policy ACCEPT 156 packets, 10871 bytes) num pkts bytes target prot opt in out source destination 1 3 252 REJECT icmp -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 102 packets, 11225 bytes) num pkts bytes target prot opt in out source destination -I INPUT: 往INPUT链中插入规则 -p ICMP 指定协议条件 -j REJECT 指定动作拒绝 规则号 收到的报数 收到的长度 num pkts bytes target prot opt in out source destination 1 3 252 REJECT icmp -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable 1.3 修改规则 # iptables -t filter -R INPUT 1 -p ICMP -j DROP root@kaka-virtual-machine:/home/kaka# iptables -L -n -v --line-numbers Chain INPUT (policy ACCEPT 7 packets, 732 bytes) num pkts bytes target prot opt in out source destination 1 0 0 DROP icmp -- * * 0.0.0.0/0 0.0.0.0/0 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 6 packets, 576 bytes) num pkts bytes target prot opt in out source destination -R INPUT 1 修改INPUT链中的第一条规则 j DROP 动作丢弃

清空计数器,pkts和bytes

# iptables -t filter -Z # iptables -L -n -v --line-numbers Chain INPUT (policy ACCEPT 8 packets, 488 bytes) num pkts bytes target prot opt in out source destination 1 0 0 DROP icmp -- * * 0.0.0.0/0 0.0.0.0/0 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 5 packets, 556 bytes) num pkts bytes target prot opt in out source destination 1.4 删除规则

指定编号删除规则

# iptables -t filter -D INPUT 1 # iptables -t filter -L -n -v --line-numbers Chain INPUT (policy ACCEPT 56 packets, 3936 bytes) num pkts bytes target prot opt in out source destination Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 39 packets, 3204 bytes) num pkts bytes target prot opt in out source destination 1.5 清空所有规则 # iptables -t filter -F root@kaka-virtual-machine:/home/kaka# iptables -t filter -L -n -v --line-numbers Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) 示例1

仅允许 192.168.213.130 访问 192.168.213.137 的80端口,其他的都拒绝。

1.先允许 1.添加到filter表, INPUT链 2.指定来源的IP地址 192.168.213.130 3.指定目标的IP地址 192.168.213.137 4.指定具体的协议 tcp 5.指定具体的端口 80 6.指定匹配后的动作是什么 ACCEPT 2.后拒绝: 1.添加到filter表,INPUT链、 2.明确指定拒绝所有 3.具体的配置: [root]# iptables -t filter -I INPUT -s 192.168.213.130 -d 192.168.213.137 -p tcp --dport 80 -j ACCEPT [root]# iptables -t filter -A INPUT -p tcp -j DROP #无条件拒绝所有tcp的请求包

查看添加的规则

# iptables -t filter -I INPUT -s 192.168.213.130 -d 192.168.213.137 -p tcp --dport 80 -j ACCEPT # iptables -t filter -A INPUT -p tcp -j DROP # iptables -t filter -L -n -v --line-numbers Chain INPUT (policy ACCEPT 18 packets, 1339 bytes) num pkts bytes target prot opt in out source destination 1 0 0 ACCEPT tcp -- * * 192.168.213.130 192.168.213.137 tcp dpt:80 2 23 2196 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 26 packets, 2714 bytes) num pkts bytes target prot opt in out source destination

当数据包来到的时候先匹配第一条规则,如果符合规则则接受。如果不符合第一条规则,接着匹配第二条规则,如果是tcp包执行DROP动作,丢弃报文。如果不是tcp包执行默认规则ACCEPT。

将规则修改成拒绝所有包

[root]# iptables -t filter -R INPUT 2 -j DROP #无条件拒绝所有tcp的请求包 # iptables -t filter -L -n -v --line-numbers Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 0 0 ACCEPT tcp -- * * 192.168.213.130 192.168.213.137 tcp dpt:80 2 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 示例2

凡是由本机发出的TCP协议报文都允许出去,其他的协议不允许;

先允许: 1.添加到filter表, OUTPUT链 后 拒绝: 1.拒绝所有 3.具体配置: [root@lb01 ~]# iptables -t filter -F #清空全部规则 [root@lb01 ~]# iptables -t filter -I OUTPUT -p tcp -j ACCEPT [root@lb01 ~]# iptables -t filter -A OUTPUT -j DROP

查看添加的规则

# iptables -t filter -L -n -v --line-numbers Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 2 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0

当数据包发出的时候先匹配第一条规则,如果TCP包符合规则则接受发出。如果不符合第一条规则,接着匹配第二条规则,不是tcp包执行DROP动作,丢弃报文。

示例3

示例3:禁止其他主机 从ens38发送来的ping请求

1.添加到filter表,INPUT链 2.指定从哪个网络接口过来的数据包 ens38 -i 3.指定具体的协议 icmp 4.指定具体的动作 DROP [root@lb01 ~]# iptables -t filter -I INPUT -i ens38 -p icmp -j DROP

查看添加的规则

# iptables -t filter -L -n -v --line-numbers Chain INPUT (policy ACCEPT 33 packets, 1944 bytes) num pkts bytes target prot opt in out source destination 1 6 504 DROP icmp -- ens38 * 0.0.0.0/0 0.0.0.0/0 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 30 packets, 5336 bytes) num pkts bytes target prot opt in out source destination

从ens38进来的icmp包全都丢弃,从其他网络接口就来的包不匹配第一条规则,执行默认规则ACCEPT



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3