当前位置: 首页 > news >正文

Linux配置白名单限制访问_ipset+iptables

一、IP清单文件 whitelist.txt

[root@centos610:/opt]# cat whitelist.txt 
00:50:56:ad:72:2e0
00:50:56:b6:c7:07
127.0.0.3
192.168.249.2
127.0.0.300

二、ipset

ipset 是一个在 Linux 系统中用于创建和管理 IP 地址集合的工具,它可以用来快速匹配 IP 地址,并且可以与防火墙规则(如 iptables 或 nftables)结合使用,以实现高效的网络流量过滤。

使用 ipset 创建一个新的集合,可以指定集合的类型。例如,使用 hash:ip或hash:net 类型来存储 IP 地址。

特点hash:iphash:ip,porthash:nethash:net,porthash:machash:iface
存储内容单个 IP 地址IP 网络地址(CIDR)
使用场景精确匹配单个 IP匹配整个 IP 网络段
效率对于单个 IP 匹配效率更高对于网络段匹配效率更高
灵活度相对较低相对较高

创建hash:ip的ip集合

# 创建一个存储单个 IP 地址的集合
ipset create ip_whitelist hash:ip# 添加一个ip地址到 whitelist ip地址集合 
ipset add ip_whitelist <IP地址># 创建hash:ip,port的ip端口集合
ipset create aa hash:ip,port# 添加一个ip:port到 地址集合
ipset add aa 1.1.1.1,22# 创建一个存储 IP 网络地址的集合
ipset create ip_whitelist hash:net# 查看已创建的ipset
ipset list# 删除ipset集合
ipset destroy blacklist

ip规则管理

# 添加一个 IP 网络地址
ipset add ip_whitelist 192.168.11.2# 添加一个ip范围
ipset add ip_whitelist 192.168.10.21-192.168.10.31# 添加一个网段--必须是hash:net
ipset add ip_whitelist 192.168.1.0/24#从ipset集合中删除ip
ipset del blacklist 10.60.10.xx#从ipset集合中清空ip
ipset flush blacklist# 保存ipset规则
ipset save a -f /etc/sysconfig/ipset

二、iptable

查看所有规则使用命令:iptables -L,如下图,重点看chain input

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination   
#添加一个IP段
iptables -A INPUT -s 192.168.1.0/24 -j ACCEPT#添加一个固定IP
iptables -I INPUT -s 192.168.1.1 -j ACCEPT#添加一个IP并指定端口
iptables -I INPUT -s 192.168.1.1 -ptcp --dport 9200 -j ACCEPT#拒绝所有IP的端口
iptables -A INPUT -p tcp -m tcp --dport 9200 -j DROP#创建防火墙规则,设置状态,只有已连接的才可以通过,如果是NEW状态的,就直接拦截
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT#创建防火墙规则,现在规则外的其他都直接Drop
iptables -A INPUT -j DROP#保存规则
#centos5、6
service iptables save       #默认 /etc/sysconfig/iptables
#centos7,8,9  Anolis  Kylin
iptables-save > /etc/sysconfig/iptables

三、iptable_set.sh

iptable_set.sh 读取 whitelist.txt 文件,并配置白名单。

[root@centos79:/opt]# cat whitelist.txt 
00:50:56:ad:72:2e0
00:50:56:b6:c7:07
127.0.0.3
192.168.249.2
127.0.0.300[root@centos79:/opt]# ./iptable_set.sh 
IP/MAC: 00:50:56:ad:72:2e0 地址错误! 
IP/MAC: 127.0.0.300 地址错误! [root@centos79:/opt]# ipset list
Name: whitelist
Type: hash:ip
Revision: 4
Header: family inet hashsize 1024 maxelem 1000000
Size in memory: 216
References: 2
Number of entries: 2
Members:
192.168.249.2
127.0.0.3[root@centos79:/opt]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  gateway              anywhere            
ACCEPT     all  --  127.0.0.3            anywhere            
ACCEPT     all  --  anywhere             anywhere             MAC 00:50:56:B6:C7:07
ACCEPT     all  --  anywhere             anywhere             match-set whitelist src
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
DROP       all  --  anywhere             anywhere            Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination    [root@centos79:/opt]# 

四、完整脚本

#!/bin/bash
#=====================================================#
# File    :  iptable_set.sh(ipset+iptables)
# Ctime   :  2022/08/04
# Mtime   :  2025/09/22
# Version :  4.0.0
# author  :  楚枫默寒
# Copyright (C) 2022-2099
# explain :  ipset配置IP清单,iptables配置IP或MAC地址
#=====================================================## 检查是否以root身份运行
if [[ $EUID -ne 0 ]]; thenecho "此脚本必须以root权限运行!"exit 1
fifunction color_setting(){RC='\033[31;1m'        #红色 errorGC='\033[32;1m'        #绿色 successYC='\033[33;1m'        #黄色 warningBC='\033[34;1m'        #蓝色 outputDC='\033[35;1m'        #粉色 detailAC='\033[36;1m'        #天蓝 infoEC='\033[0m'           #黑白 end
} function get_os_info(){if [[ -e /etc/os-release ]];thenVerfile='/etc/os-release'elif [[ -e /etc/system-release ]];thenVerfile='/etc/system-release'elif [[ -e /etc/redhat-release ]];thenVerfile='/etc/redhat-release'fi os_version=$(cat ${Verfile}|egrep -o '[0-9]{1,3}'|head -n 1)
}function soft_conf(){current=$(date +%Y%m%d%H%M%S)soft_path="/opt"soft_file="${soft_path}/whitelist.txt"soft_errlog="${soft_path}/ipset_err_${current}.log"#安装iptables,系统默认已安装if [[ ! $(which iptables 2>/dev/null) ]];thenyum install iptables -yfi#IP4判断表达式ip_check="\b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])\b"#MAC判断表达式mac_check="^([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$"
}function whitelist_set(){#清单去空行、注释、重复LINELIST=$(sed -e "/#.*.$/d" ${soft_file}|sort -u)#设置默认规则iptables -P INPUT ACCEPT#清空现有所有规则iptables -F iptables -t raw -Fif [[ $(which ipset 2>/dev/null) ]];then #ipset 删除现有白名单ipset destroy whitelist> /etc/sysconfig/ipset#ipset 创建白名单ipset create whitelist hash:ip maxelem 1000000else echo -e "${RC}未安装 ipset ${EC}" >> ${soft_errlog}fi#循环提示IP清单for target_ip in ${LINELIST};do#IP4合法性检查if [[ ${target_ip} =~ ${ip_check} ]];then#ipset配置if [[ $(which ipset 2>/dev/null) ]];then ipset add whitelist ${target_ip} -existfi#iptables配置iptables -A INPUT -s ${target_ip} -j ACCEPTelif [[ ${target_ip} =~ ${mac_check} ]]; theniptables -A INPUT -m mac --mac-source ${target_ip} -j ACCEPTelseecho -e "${AC}IP/MAC:${RC} ${target_ip} 地址错误! ${EC}" >> ${soft_errlog}fi doneif [[ $(which ipset 2>/dev/null) ]];then #创建防火墙白名单规则,01、白名单内的直接通过iptables -A INPUT -m set --match-set whitelist src -p all -j ACCEPT#配置防火墙白名单规则,02、白名单里面不做状态追踪iptables -t raw -A PREROUTING -m set --match-set whitelist src -p all -j NOTRACKfi#创建防火墙规则,03、设置状态只有已连接的才可以通过,如果是NEW状态的,就直接拦截iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT#创建防火墙规则,04其他的都直接Dropiptables -A INPUT -j DROP#保存配置case ${os_version} in5|6)chkconfig ipset onservice ipset save >/dev/null                                               #/etc/sysconfig/ipsetchkconfig iptables onchkconfig --list|egrep "iptables|ipset"cp /etc/sysconfig/iptables /etc/sysconfig/iptables_${current}service iptables save >/dev/null                                            #/etc/sysconfig/iptablesservice iptables restart >/dev/null;;7|8|9|10)ipset save > /etc/sysconfig/ipset                                           #/etc/sysconfig/ipsetcp /etc/sysconfig/iptables /etc/sysconfig/iptables_${current}iptables-save > /etc/sysconfig/iptables;;                                   #/etc/sysconfig/iptablesesac
}function auto_start(){#添加开机启动if [[ ! $(grep -R "iptable_set.sh" /etc/rc.d/rc.local|grep -v "#") ]];thenecho "sh +x ${soft_path}/iptable_set.sh 2>/dev/null" >>/etc/rc.d/rc.localfi 
}
function main(){color_settingget_os_infosoft_confif [ ! -f ${soft_file} ];thenecho -e ${RC}"没有检测到白名单文件"${EC}exitelsewhitelist_setfiauto_startrm -f ${soft_path}/{dba.txt}find ${soft_path} -type f -name 'whitelist_20*' -mtime +30 |xargs rm -f;if [[ -s ${soft_errlog} ]];thencat ${soft_errlog}fi
}main "$@" 

http://www.dtcms.com/a/395247.html

相关文章:

  • 多元化通证经济模型:DAO的神经和血液
  • 高系分十六:web应用
  • 【LeetCode热题100(27/100)】合并两个有序链表
  • 嵌入式(SOC+FreeRTOS)汽车仪表盘接口参数安全:规范遵循与防护实践
  • Maven 完整教程
  • 数据驱动下的用户画像系统:从0到1的技术实战与避坑指南
  • 同一个灰色,POI取出来却是白色:一次Excel颜色解析的踩坑记录
  • Excel——常用函数一
  • 立项依据不足会给项目带来哪些风险
  • 从 0 到 1 精通 SkyWalking:分布式系统的 “透视镜“ 技术全解析
  • SkyWalking 核心概念与智能探针工作原理深度揭秘(下)
  • Dockerfile入门指南
  • iOS 原生开发全流程解析,iOS 应用开发步骤、Xcode 开发环境配置、ipa 文件打包上传与 App Store 上架实战经验
  • 数据分析报告的写作流程
  • 当你的断点在说谎:深入解析RTOS中的“幽灵”Bug
  • [BUG]MarkupSafe==3.0.2
  • 机器学习笔试选择题:题组1
  • 79-数据可视化-地图可视化
  • python全栈-数据可视化
  • 【国产桌面操作系统】安装mysql客户端及C/C++开发
  • IntelliJ:找不到相关的 gradle 配置,请重新导入 Gradle 项目,然后重试。
  • 云计算微服务架构与容器化技术:服务网格与边缘计算融合实践
  • 飞牛NAS上搭建OpenWrt旁路由教程(适用于x86的Docker部署)
  • python14——函数
  • 14.Linux 硬盘分区管理及RAID存储技术
  • ★ Linux ★ 信号
  • macOS在IDEA里滚动行为混乱问题
  • ✨Vue 静态路由详解:构建应用的导航骨架(4)
  • 08-2Dcss动画
  • 使用IOT-Tree消息流Modbus Slave节点,实现Modbus设备的模拟