Linux iptables防火墙操作

资料:

网络运维相关 - iptables 【Main】
https://www.zsythink.net/archives/tag/iptables/

netfilter 在 Linux 内核 TCP/IP协议栈中的位置 【框架】【Aulaxiry】
https://zhuanlan.zhihu.com/p/93630586

1 概念详解

● 防火墙概念

○ 主机防火墙 + 网络防火墙

○ 硬件防火墙 + 软件防火墙

○ iptables 客户端代理 + netfilter 安全框架(真正的防火墙,位于内核空间)

○ 作用:封包过滤、数据内容修改、网络地址转换(NAT)

● iptables基础

○ 按规则执行指定动作

■ 源地址、目的地址、传输协议、服务类型;

■ 放行、拒绝、丢弃

● 链的概念

○ prerouting + input + output + forward + postrouting

● 表的概念

○ filter表:负责过滤功能,防火墙;内核模块:iptables_filter

○ nat表:network address translation,网络地址转换功能;内核模块:iptable_nat

○ mangle表:拆解报文,做出修改,并重新封装 的功能;iptable_mangle

○ raw表:关闭nat表上启用的连接追踪机制;iptable_raw

● 表链关系

○ 不同链上表的类型不同 , 表是功能,链是钩子

■ raw : PREROUTING,OUTPUT

■ mangle :PREROUTING,INPUT,FORWARD,OUTPUT,POSTROUTING

■ nat :PREROUTING,OUTPUT,POSTROUTING(centos7中还有INPUT,centos6中没有)

■ filter:INPUT,FORWARD,OUTPUT

○ 每个链上规则的匹配次序:raw --> mangle --> nat --> filter

○ 自定义链: 针对特定的应用程序,可以当做动作链入到默认的链

● 数据包经过防火墙的流程:

● 规则的概念

○ 匹配条件 + 处理动作组成

○ 匹配条件: 基本匹配条件 (src ip , dst ip 等)+ 扩展匹配条件 ( src port ,dst port 等)

○ 处理动作: ACCEPT DROP REJECT SNAT MASQUERADE DNAT REDIRECT LOG

2 iptables操作-规则查询

● 表查询操作

○ iptables -t raw -L

○ iptables -t mangle -L

○ iptables -t nat -L

○ iptables -t filter -L 查看 filter表中的内容:

复制代码
■ 不同应用场景操作filter中的链不一样,禁止IP的访问需要在Iput链中设置。
■ 不指定 -t 默认 filter , iptables -L INPUT ; 
■ iptables -vL INPUT 可以查看更多信息 -v

■ iptables -vnL  不对名称反解 -n

○ iptables常用命令:

■ iptables -t 表名 -L

■ iptables -t 表名 -L 链名

■ iptables -t 表名 -v -L

■ iptables -t 表名 -n -L

■ -x 选项表示显示计数器的精确 ; --line 显示规则的序号

3 iptables操作-规则管理

对iptables进行"增、删、改"操作

演示:

● 清空某个链的规则:iptables -F INPUT , 默认全部放行

● 增加规则

○ iptables -t fileter -I INPUT -s 192.168.1.146 -j DROP ( -I 首部插入 -j匹配 )

○ iptables -t fileter -A INPUT -s 192.168.1.146 -j ACEEPT ( -A 末尾追加 )

○ 规则增加有顺序之分:

■ iptables -t fileter -I INPUT 2 -s 192.168.1.146 -j ACEEPT (-I INPUT 2:编号2)

● 删除规则

○ 根据规则的编号去删除规则

■ iptables -t filter -D INPUT 3

○ 根据具体的匹配条件与动作删除规则

■ iptables -D INPUT -s 192.168.1.146 -j ACEEPT

○ 清空链上所有规则/清空

■ iptables -t tablename -F / chainName

● 修改规则

○ 可以先删除某条规则,再进行增加这么操作,直接修改可能会有操作上的风险,不建议。

○ 主要使用-R即可

■ iptables -t filter -R INPUT 1 -s 192.168.1.146 -j REJECT (-s 必须带,无论指定1)

● 保存规则

○ centos6中 :

■ service iptables save 保存在 /etc/sysconfig/iptables

■ iptables-save > /etc/sysconfig/iptables

■ iptables-restore < /etc/sysconfig/iptables (载入规则)

○ ubuntu中:

■ sudo netfilter-persistent save /save6 (需要安装iptables-persistant)

■ /etc/iptables/rules.v4 # IPv4规则 / v6

4 iptables匹配条件

基本匹配条件:

● 源IP

○ -s ip ; -s ip,ip; -s ip/16 设置单IP 多个IP 网段

○ ! -s ip ; 相当于取反

● 目标IP

● 协议类型

● 网卡接口

○ -i选项用于匹配报文从哪个网卡流入

○ -o选项用于匹配报文将从哪个网卡流出

扩展匹配条件:

● tcp扩展模块

iptables -t filter -I OUTPUT -d 192.168.1.146 -p tcp -m tcp --sport 22 -j REJECT

iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m tcp --dport 22:25 -j REJECT

iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m tcp --dport :22 -j REJECT

iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m tcp --dport 80: -j REJECT

iptables -t filter -I OUTPUT -d 192.168.1.146 -p tcp -m tcp ! --sport 22 -j ACCEPT

● multiport扩展模块

iptables -t filter -I OUTPUT -d 192.168.1.146 -p udp -m multiport --sports 137,138 -j REJECT

iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m multiport --dports 22,80 -j REJECT

iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m multiport ! --dports 22,80 -j REJECT

iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m multiport --dports 80:88 -j REJECT

iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m multiport --dports 22,80:88 -j REJECT

5 iptables黑白名单

当链的默认策略为ACCEPT时,链中的规则对应的动作应该为DROP或者REJECT,表示只有匹配到规则的报文才会被拒绝,没有被规则匹配到的报文都会被默认接受,这就是"黑名单"机制。

当链的默认策略为DROP时,链中的规则对应的动作应该为ACCEPT,表示只有匹配到规则的报文才会被放行,没有被规则匹配到的报文都会被默认拒绝,这就是"白名单"机制。

白名单机制似乎更加安全一些,黑名单机制似乎更加灵活一些。

6 iptables防火墙

iptables往往作为主机防火墙,此处讲解为 如何作为网络防火墙

7 iptables动作总结2

相关推荐
2401_892070981 天前
【Linux C++ 日志系统实战】LogFile 日志文件管理核心:滚动策略、线程安全与方法全解析
linux·c++·日志系统·日志滚动
lwx9148521 天前
Linux-Shell算术运算
linux·运维·服务器
somi71 天前
ARM-驱动-02-Linux 内核开发环境搭建与编译
linux·运维·arm开发
双份浓缩馥芮白1 天前
【Docker】Linux 迁移 docker 目录(软链接)
linux·docker
为何创造硅基生物1 天前
ESP32S3的RGB屏幕漂移问题
网络
好运的阿财1 天前
process 工具与子agent管理机制详解
网络·人工智能·python·程序人生·ai编程
黄昏晓x1 天前
Linux ---- UDP和TCP
linux·tcp/ip·udp
路溪非溪1 天前
Linux驱动开发中的常用接口总结(一)
linux·运维·驱动开发
此刻觐神1 天前
IMX6ULL开发板学习-01(Linux文件目录和目录相关命令)
linux·服务器·学习
2401_892070981 天前
【Linux C++ 日志系统实战】高性能文件写入 AppendFile 核心方法解析
linux·c++·日志系统·文件写对象