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

相关推荐
A小辣椒1 天前
TShark:Wireshark CLI 功能
linux
A小辣椒2 天前
TShark:基础知识
linux
AlfredZhao2 天前
OCI 明明分配了 200G 系统盘,为什么 df 只看到 30G?
linux·oci
AlfredZhao2 天前
vi 删除指定范围的行,不用再反复按 dd
linux·vi
用户9718356334663 天前
银河麒麟 KY10 申威(SW64) 安装 nginx-1.16.1-2.p01.ky10.sw_64.rpm 详细步骤
linux
猪脚踏浪3 天前
linux 拷贝文件或目录到指定的位置
linux
摇滚侠3 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
bush43 天前
嵌入式linux学习记录十四、术语
linux·嵌入式
载数而行5203 天前
Linux 11 动态监控指令top
linux
网络研究院3 天前
2026年网络安全
网络·安全·法律·法规·趋势·发展