== 语法:
例子:iptables -t filter -A INPUT -s 10.1.1.0/24 -j DROP
语法:iptables [-t table] command 检查点 [match] -j [target/jump]
1 2 3 4
== -t table 指定我们操作的表,filter nat 默认不写 -t table(不指定表)
默认操作的是filter表
== command 操作规则链,插入规则,删除规则,清空规则,设置默认策略,在链的末尾/头部插入规则等。。。。
== [match] 详细的描述包的特点,根据包的特点来进行过滤(匹配规则)
== [target/jump] 符合match的数据包,最终的处理方法。
典型配置的例子:iptables -t filter -A INPUT -s 10.1.1.0/24 -j DROP
-t table command [match] [target/jump]
第一部分: table ===========================================
不指定表名,默认操作 filter 表 iptables -L iptables -F
-t nat 操作nat表
-t raw
-t mangle
第一部分:command 大写选项 ====================================
-A –append 在所选规则链,的末尾,添加规则。
iptables -A INPUT -d 10.1.1.1 -p tcp --dport 22 -j DROP
在10.1.1.1上增加:禁止别人ssh到我本机。
-L –list 显示所选规则链中的所有规则
iptables -t filter -L INPUT 显示filter 表中INPUT链的所有规则
iptables -L 显示filter表中所有规则链的规则
-D –delete 从所选规则链中删除规则。
1:iptables -A INPUT -d 10.1.1.1 -p tcp --dport 22 -j DROP
2:iptables -D INPUT 1 (iptables -L 编号由上至下)
@@@@ 查看规则的序号:iptales -L -n --line @@@@
3:save vim /etc/sysconfig/iptables
-I –insert 插入,向规则链中的某个序号插入规则。
iptables -I INPUT 1 -s 10.1.1.0/24 -j ACCEPT
在1号规则的上方,插入一条规则。
-F –flush 清除所选链中的所有规则。 清空当前配置的所有规则,不会清空 规则文件中的内容。 除非我们将空规则,save
iptables -F INPUT 清空filter 表中INPUT链的所有规则
iptables -F 清空filter表中所有规则链的规则
-Z –zero 指定链的计数器,清零 ???
-N –new 自定义规则链(检查点)
iptables -N syn-flood
-E –rename 给自定义的规则链重命名 INPUT -s 192.168.0.0/24 -j mychk mychk ==== 1 2 3 4 5
iptables -E syn-flood synflood
旧链名 新链名
-X –delete-chain 删除用户自定义的规则链
iptables -X syn-flood
-P –policy 策略 ###############
iptables -P INPUT DROP 总条件,默认全部拒绝,然后分别放行小条件
Chain INPUT (policy DROP)
不允许大家 ping 我
match: -p icmp -j DROP
关闭所有访问,只允许 10.1.1.X的 ssh 进来
match: -p tcp -s 10.1.1.X --dport 22 -j ACCEPT
@@@@@@ 推荐使用 “脚本管理” 防火墙@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
建议多规则书写方式,通过建立脚本,执行脚本,将规
则导入,在编辑和修改规则的时候,都比较方便。
防火墙管理脚本的基本结构。
#!/bin/bash
#===初始化 清规则,清计数器,清自定义规则链
iptables -F 事前清空 filter表
iptables -t nat -F 事前清空 nat 表
iptables -t nat -X
iptables -X
.....
规则语句1
规则语句2 ##
规则语句3
规则语句4 ##
.....
service iptables save 保存规则
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
解释说明自定义规则链 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
prerouting ----- 检测车辆 本地牌- 往下走 外地牌 进入 -j mychk
|
|----------|
| mychk======》 车上的人
1
2
3==========》处理
4
|
================
|
|
|
input 查后备箱
|
入境
iptables -t filter -P INPUT DROP
iptables -t filter -A INPUT -s 192.168.100.0/24 -p tcp --dport 80 -j ACCEPT
iptables -t filter -A INPUT -s 192.168.100.2/32 -p tcp --dport 22 -j ACCEPT
1、 禁止 INPUT 的全局策略 iptables -t filter -P INPUT DROP
2、 放行 局域网网段的 IP 能够 访问自己的 web 服务。
3、 仅允许 某个 IP 地址能够 连接 自己的 ssh 22号端口。
第三部分:match 匹配条件 =====================================
匹配的条件越多,匹配的精度越高,匹配到的包越少。
-p 装载协议
-i 指定网卡
-o 指定出的网卡
-s 指定原IP地址
-d 指定目标IP地址
-m 模块名称 模块选项 ### 显式匹配 隐含匹配 -p tcp --dport --sport
-p --protocol 指定协议 --------------------------------------------
1\ 直接写协议名:协议名必须是在 /etc/protpcols文件中定义的
2\ all none -p all TCP,UDP,ICMP
3\ 协议列表 -p TCP,UDP 逗号分割
4\ 取反 ! -p TCP =UDP,ICMP
5\ 不写 -p 默认也是匹配所有的协议:
-s --source 指定源IP地址 -------------------------------------------- S: 192.168.2.0/24 -p =====》 D:192.168.2.20
1\ 单个IP地址 -s 10.1.1.3/32 -s 10.1.1.3
iptabels -A INPUT -s 10.1.1.3/32 10.1.1.3/255.255.255.255
2\ ip段 -s 10.1.1.0/24 10.1.1.0/255.255.255.0
3\ 取反 ! -s 10.1.1.0/24 除了10.1.1.0/24 以外的地址
-d --dst 匹配目标地址 。 --------------------------------------------
iptables -A INPUT -d 10.1.1.1
参见-s
练习:我是192.168.100.13 , 在局域网中隐藏自己 。可是 希望 192.168.100.2 能ping通。
iptables -t filter -P INPUT DROP/ACCEPT
-j DROP
-i --input-interface 。 --------------------------------------------
1、 -i eth0 从eth0 进来的包 lo ppp0
2、 通配符 + -i eth+ 从eth类型网卡过来的,不论哪个接口。
3、 ! -i eth0 除了eth0 之外的接口
-o --out-interface 。 --------------------------------------------
-o 离开本地使用的网络接口
A ------------> eth0---防火墙---eth1 <--------------------------------公网
10.1.1.2 10.1.1.1 200.196.72.33
iptables -t filter -A FORWARD -i eth1 -s 10.0.0.0/8 -j DROP
iptables -t filter- A FORWARD -i eth1 -s 172.16.0.0/16 -j DROP
iptables -t filter -A FORWARD -i eth1 -s 192.168.0.0/24 -j DROP
广东铁塔 https://www.jfdaily.com/news/detail?id=100991
-p 装载 协议 tcp udp icmp ================================
@@@ 必须有 -p tcp 作为前提条件 @@@@@@@@
TCP 匹配 : ************************************
--sport source 源端口 。 --------------------------------------------
@@@@ /etc/services 文件中所指定的端口 @@@@
1、 -p tcp --sport 22
2、 -p tcp --sport 80:22 源端口为:22~80号端口
:22 0~22
22: 22~65535
3、
4、 -p tcp --sport 80 除了80端口以外
--dport dst 目标端口 。 -------------------------------------------------
参照以上sport的写法。
允许所有能够 访问 192.168.2.10 的 http 服务
但是不允许 局域网 ssh 连接 192.168.2.10 , 但 192.168.2.207 除外。
网关 单机
INPUT DROP 单机防御 INPUT DROP 网关过滤 FORWARD DROP ACCEPT
iptables -t filter -A INPUT -p tcp --dport 80 -s 192.168.2.0/24 -j ACCEPT
iptables -t filter -A INPUT -p tcp --dport 22 -s 192.168.2.207/32 -j ACCEPT
--tcp-flags 用来指定匹配的TCP标记 。 --------------------------------------------
SYN ACK FIN RET URG PSH
1、 iptables -p tcp --tcp-flags SYN,FIN,ACK SYN
表示TCP包头中,SYN被标记,而FIN和ACK位没有被标记的数据包。
--syn 匹配新建立连接的包 。 --------------------------------------------
-p tcp --syn 等价于 -p tcp --tcp-flags SYN,RST,FIN,ACK SYN
UDP macth: ************************************
-p udp --sport
-p udp --dport
参见tcp的写法
ICMP match: ************************************
-p icmp --icmp-type 8 -p icmp -j DORP
附件 ICMP 的种类
- 列表内容