iptables基本匹配

news/2024/10/23 12:38:57/

文章目录

    • 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 --helpUsage:    # 使用案例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中有三条链,分别是INPUTFORWARDOUTPUT。默认规则为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-unreachableChain 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.指定具体的协议      		tcp5.指定具体的端口	   		806.指定匹配后的动作是什么     ACCEPT2.后拒绝: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


http://www.ppmy.cn/news/212921.html

相关文章

Libevent学习

一、Libevent概述 1、简介 Libevent 是一个用C语言编写的、轻量级的开源高性能事件通知库,主要有以下几个亮点:事件驱动( event-driven),高性能;轻量级,专注于网络,不如 ACE 那么臃肿庞大&…

BLIP使用教程

文章目录 准备测试示例一示例二: 结论源代码 原理篇: BLIP2-图像文本预训练论文解读 准备 如果无网络需提前下载相关模型 安装torch、transformers pip install torch trtransformers测试 测试blip基于图片生成文本描述能力(Caption&…

1132 Cut Integer(17行代码)

分数 20 全屏浏览题目 切换布局 作者 CHEN, Yue 单位 浙江大学 Cutting an integer means to cut a K digits lone integer Z into two integers of (K/2) digits long integers A and B. For example, after cutting Z 167334, we have A 167 and B 334. It is intere…

金立S8 线刷

由于金立s8广告太多,想通过线刷刷回以前版本,记录关键内容: 1.进REC方式:按电源键音量上键,选择REC,进入机器人倒地界面,按电源键音量下键五秒后按电源上键,进入REC 2. 线刷方式&a…

金立android在哪里设置密码,金立s5.5手机怎么设置锁屏密码 金立s5.5屏幕密码设置教程...

金立s5.5怎么设置屏幕密码?手机里有隐私,不想让别人看到如何设置屏幕密码呢?下面脚本之家小编为大家介绍下s5.5设置屏幕密码教程!不知道的赶快来看下吧! 1.在手机界面点击“设置”,之后会出现“滑动”、“图…

联发科6758_搭载MTK MT6758!金立M7现身跑分网站

随着流行趋势的变化,当前众手机厂商的目光都盯紧了全面屏领域,已上市的全面屏产品之外,另有多家厂商明确表态下半年会有全面屏新品面世,其中就包括金立手机,金立官方早早便宣布,下半年重点发力全面屏领域&a…

金立(Gionee)金立M7 Power root 大金刚 GN5007 刷机TWRP 面具 XP框架 线刷包

刷机 免解锁BL 刷精简官方系统,如果需要售后刷机软件 请联系获取免费获取 成功安装框架、 成功 root 金立(Gionee)金立M7 Power root 大金刚 GN5007 刷机TWRP 面具 XP框架,手机root刷机包 柒叁伍叁玖肆零零陆 ( 转换成小写数字) 远程ro…

金立android手机怎么截图,金立M6手机怎么截图 金立M6截屏/截图方法(两种)

昨天下午,国产老牌手机厂商金立发布了M6安全长续航手机,这款手机主要有两大亮点,一是超级续航,而是内置安全加密芯片。注重续航和安全的用户可能会更加看中这点准备入手。如今就有网友开始咨询金立M6怎么截图,为此脚本…