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

相关推荐
上邪o_O3 分钟前
Git 的基本使用指南(1)
linux·git
nuoxin1141 小时前
CY7C68013A-56LTXC -USB2.0控制器芯片-富利威,国产CBM9002A-56ILG可替代
网络·人工智能·单片机·嵌入式硬件·硬件工程
椿融雪2 小时前
高效轻量的C++ HTTP服务:cpp-httplib使用指南
网络·网络协议·http·cpp-httplib
程序员JerrySUN2 小时前
OpenCV 全解读:核心、源码结构与图像/视频渲染能力深度对比
linux·人工智能·驱动开发·opencv·计算机视觉·缓存·音视频
程序员老徐2 小时前
Netty的Http解码器源码分析
网络·网络协议·http
网安Ruler2 小时前
Web开发-PHP应用&原生语法&全局变量&数据接受&身份验证&变量覆盖&任意上传(代码审计案例)
网络·安全·网络安全·渗透·红队
wyjcxyyy2 小时前
打靶日记-RCE-labs(续)
linux·运维·服务器
is08153 小时前
linux 启动流程?
linux
六点半8883 小时前
【Linux】Linux编译器-gcc/g++使用
linux·运维·服务器