Linux之Iptables简易应用,2024年最新万字解析

文档形成时期:2009-2024年
和iptables打交道有15年+了,经过无数实践后,形成一个简易应用文档。
文档主题是简易应用,所以其原理不详述了。
因软件世界之复杂和个人能力之限,难免疏漏和错误,欢迎指正。

文章目录

iptables简要说明

rhel7以下系列使用iptables,从rhel7开始使用firewalld,但iptables也存在,正因为共同存在,所以常给初用者造成诸多困扰。
rhel8开始firewalld已经与iptables解绑。

iptables和firewalld的关系

在RHEL7里有几种防火墙共存:firewalld、iptables、ebtables,默认是使用firewalld来管理netfilter子系统,不过底层调用的命令仍然是iptables等。

如何选择防火墙

一般来讲:
rhel7或以下,选择iptables;
rhel8或以上,可以采用firewalld,使用iptables也是可以的,但两者选其一即可。
如果有docker、k8s等紧密依赖iptables的环境,建议采用iptables,禁用firewalld,请把这个配置写入新系统初始配置中。
ubuntu22仍然可保持采用iptables。
iptables

禁用firewalld

#停止firewalld服务
systemctl stop firewalld
#禁用firewalld服务
systemctl mask firewalld

启用iptables

yum install -y iptables
yum install -y iptables-services

systemctl enable iptables.service
systemctl start iptables.service

保存经验

默认保存位置:/etc/sysconfig/iptables

两种常见保存方式:

  • 方式一
    service iptables save #安装了iptables套件后才有效,默认保存位置:/etc/sysconfig/iptables,iptables套件配置文件是/etc/sysconfig/iptables-config
  • 方式二
    iptables-save -c > /etc/sysconfig/iptables #没有安装iptables套件也可执行,参数-c是保存计数

保存为不同版本的示例

iptables-save > ipt.v1.0 #版本保存
iptables-restore < ipt.v1.0 #恢复

datetime=KaTeX parse error: Expected group after '_' at position 65: …config/iptables_̲{datetime} #以时间为版本保存
iptables-restore -c < /etc/sysconfig/iptables_${datetime}

在多人管理的工作环境中,采用同一脚本去管理iptables是非常推荐的做法,但如果确实混乱不堪,维护好/etc/sysconfig/iptables是最好的选择。参考常规简易配置综合脚本范例

在iptables和firewalld共存的系统,比如rhel7系列,但又没有安装iptables套件,或采用了firewalld和iptables共同管理防火墙,造成了iptables策略混乱,这时候可用iptables-save命令保存策略,不过这时可能更应该考虑采用firewalld去管理iptables

2024年实践中发现,k8s的网络插件calico管理的iptables策略可能造成iptables-save命令也无法完整保存策略,这需要根据生产场景来制定应对方案,以避免重启后策略丢失。

通过自定义链并写入开机启动的解决案例

#!/bin/bash
###################################

function iptables防火墙因服务不正常而采用的临时处置

参考创建或使用示例: mkdir -p /root/sh/log; touch /root/sh/iptables_for_tmp.sh; chmod a+x /root/sh/iptables_for_tmp.sh

if (! grep -q iptables_for_tmp /etc/rc.local); then echo ‘/root/sh/iptables_for_tmp.sh >> /root/sh/log/iptables_for_tmp.log 2>&1’ >> /etc/rc.local; else echo “This script already exists in /etc/rc.local”; fi; chmod a+x /etc/rc.d/rc.local

Change History:

date author note

20240105 N k8s的calico导致防火墙iptables服务无法恢复运行,改用脚本

###################################

export LANG=C
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

22端口处理

iptables -F FOR_TMP > /dev/null 2>&1
iptables -D INPUT -j FOR_TMP > /dev/null 2>&1
iptables -X FOR_TMP > /dev/null 2>&1
iptables -N FOR_TMP
iptables -I INPUT -j FOR_TMP

#放通
iptables -I FOR_TMP -s 1.2.3.4 -p tcp --dport 22 -j ACCEPT

#限制
iptables -A FOR_TMP -p tcp --dport 22 -j DROP

#保存
iptables-save -c > /etc/sysconfig/iptables # 执行后保存

if (! grep -q iptables_for_tmp /etc/rc.local); then echo ‘/root/sh/iptables_for_tmp.sh >> /root/sh/log/iptables_for_tmp.log 2>&1’ >> /etc/rc.local; else echo “This script already exists in /etc/rc.local”; fi; chmod a+x /etc/rc.d/rc.local

该脚本可以反复执行,不会产生冗余策略。

加载内核模块

在早期版本的Linux内核,iptables的NAT,一般不能使用FTP的20端口,可以使用下面命令加载模块
insmod /lib/modules/2.6.18-164.el5/kernel/net/ipv4/netfilter/ip_conntrack_ftp.ko
insmod /lib/modules/2.6.18-164.el5/kernel/net/ipv4/netfilter/ip_nat_ftp.ko
或使用modprobe命令加载(推荐modprobe,因为insmod不解决依赖关系)
允许NAT转换FTP的跟踪连接,如下:
modprobe ip_nat_ftp
允许本机FTP的跟踪连接,这样就不必设置允许20和被动端口了
modprobe ip_conntrack_ftp

脚本中常用配置:

modules=“ip_conntrack_ftp ip_conntrack_irc ip_nat_pptp ip_gre ip_conntrack_pptp ip_conntrack_proto_gre”
for mod in m o d u l e s d o t e s t m o d = ‘ l s m o d ∣ g r e p " modules do testmod=`lsmod | grep " modulesdotestmod=lsmodgrep"{mod}"`
if [ “$testmod” == “” ]; then
modprobe $mod
fi
done

基于近些年Linux内核版本的系统,比如rhel6+,还未仔细分析过是否默认支持上述模块,没有的话,可以根据业务场景选择是否加载。

基本操作

iptables规则执行顺序是从上到下,前面的匹配后就不再看后面的。
基本语法:
iptables [-t table] -[AD] chain rule-specification [options]
iptables [-t table] -I chain [rulenum] rule-specification [options]
iptables [-t table] -R chain rulenum rule-specification [options]
iptables [-t table] -D chain rulenum [options]
iptables [-t table] -[LFZ] [chain] [options]
iptables [-t table] -N chain
iptables [-t table] -X [chain]
iptables [-t table] -P chain target [options]
iptables [-t table] -E old-chain-name new-chain-name

iptables -A 添加一条规则到末尾
iptables -D 删除
iptables -I 插入一条规则到最前面
iptables -R 替换
iptables -vnL 列表
iptables -vnL --line-numbers 列表时同时显示策略号,方便更新局部策略,而不需要重新配置所有策略。

常用模块和参数

iprange

–src-range ip-ip Match source IP in the specified range.
–dst-range ip-ip
例:
-m iprange --src-range 192.168.10.2-192.168.10.6
iptables -I INPUT -m iprange --src-range 192.168.10.2-192.168.10.6 -p tcp --dport 22 -j ACCEPT

icmp

-p icmp --icmp-type 类型 ping: type 8 pong: type 0

sport和dport

-p tcp
-p udp
–sport 和–dport 必须配合-p 参数使用

multiport

This module matches a set of source or destination ports. Up to 15 ports can be specified. A port range (port:port) counts as two
ports. It can only be used in conjunction with -p tcp or -p udp.

–source-ports [!] port[,port[,port:port…]]
Match if the source port is one of the given ports. The flag --sports is a convenient alias for this option.

–destination-ports [!] port[,port[,port:port…]]
Match if the destination port is one of the given ports. The flag --dports is a convenient alias for this option.

–ports [!] port[,port[,port:port…]]
Match if either the source or destination ports are equal to one of the given ports.

例:
-p tcp -m multiport --dports 20,21,22

state

-m state --state 状态 状态:NEW、RELATED、ESTABLISHED、INVALID
NEW:有别于tcp 的syn
ESTABLISHED:连接态
RELATED:衍生态,与conntrack 关联(比如FTP常用)
INVALID:不能被识别属于哪个连接或没有任何状态

mac

mac --mac-source [!] address
Match source MAC address. It must be of the form XX:XX:XX:XX:XX:XX. Note that this only makes sense for packets coming from
an Ethernet device and entering the PREROUTING, FORWARD or INPUT chains.

line-numbers

–line-numbers
When listing rules, add line numbers to the beginning of each rule, corresponding to that rule’s position in the chain.

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数网络安全工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年网络安全全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上网络安全知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注网络安全获取)
img

本人从事网路安全工作12年,曾在2个大厂工作过,安全服务、售后服务、售前、攻防比赛、安全讲师、销售经理等职位都做过,对这个行业了解比较全面。

最近遍览了各种网络安全类的文章,内容参差不齐,其中不伐有大佬倾力教学,也有各种不良机构浑水摸鱼,在收到几条私信,发现大家对一套完整的系统的网络安全从学习路线到学习资料,甚至是工具有着不小的需求。

最后,我将这部分内容融会贯通成了一套282G的网络安全资料包,所有类目条理清晰,知识点层层递进,需要的小伙伴可以点击下方小卡片领取哦!下面就开始进入正题,如何从一个萌新一步一步进入网络安全行业。

学习路线图

其中最为瞩目也是最为基础的就是网络安全学习路线图,这里我给大家分享一份打磨了3个月,已经更新到4.0版本的网络安全学习路线图。

相比起繁琐的文字,还是生动的视频教程更加适合零基础的同学们学习,这里也是整理了一份与上述学习路线一一对应的网络安全视频教程。

网络安全工具箱

当然,当你入门之后,仅仅是视频教程已经不能满足你的需求了,你肯定需要学习各种工具的使用以及大量的实战项目,这里也分享一份我自己整理的网络安全入门工具以及使用教程和实战。

项目实战

最后就是项目实战,这里带来的是SRC资料&HW资料,毕竟实战是检验真理的唯一标准嘛~

面试题

归根结底,我们的最终目的都是为了就业,所以这份结合了多位朋友的亲身经验打磨的面试题合集你绝对不能错过!

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
img

959.jpeg)

项目实战

最后就是项目实战,这里带来的是SRC资料&HW资料,毕竟实战是检验真理的唯一标准嘛~

面试题

归根结底,我们的最终目的都是为了就业,所以这份结合了多位朋友的亲身经验打磨的面试题合集你绝对不能错过!

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
[外链图片转存中…(img-aJu7touC-1712676343616)]

  • 10
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值