CentOS 7 firewalld+ipset+定时任务防御ssh暴力破解——筑梦之路

对于暴露在公网上的linux服务器,很容易被暴力破解登陆,为了增强服务器的安全性,因此对于ssh安全加固是很有必要的,这里主要介绍centos7 系统如何使用ipset+firewalld+定时任务来对ssh服务进行安全加固。

定义firewalld ipset

bash 复制代码
firewall-cmd --permanent --new-ipset=blacklist --type=hash:ip

定义firewalld规则调用ipset

bash 复制代码
firewall-cmd --permanent --add-rich-rule 'rule family="ipv4" source ipset=blacklist port protocol="tcp" port=22 drop'

将日志中的ip添加到ipset中

bash 复制代码
# 脚本实现自动加入

#!/bin/bash
# Author: merry
# Description: 使用root账号登录错误大于8次的IP被加入到黑名单中

for ip in `grep -i 'Failed password for root' /var/log/secure | awk '{print $11}' | sort -n | uniq -c | sort -k1nr | awk '{if ($1>8) print $2}'`; do
    firewall-cmd --permanent --ipset=blacklist --add-entry="${ip}";
done;
firewall-cmd --reload;

添加定时任务

bash 复制代码
0 * * * * /opt/blockips.sh >/dev/null 2>&1

检查验证

bash 复制代码
# 查询黑名单IP

firewall-cmd --permanent --info-ipset=blacklist
firewall-cmd --ipset=blacklist --get-entries

# 通过文件查看

cat /etc/firewalld/ipsets/blacklist.xml

当然也可以使用fail2ban这款软件来实现,可根据需要进行选择。

相关推荐
多弗朗皮卡丘18 分钟前
网络基础:数据链路层与其他协议或技术
linux·网络·tcp/ip
dingdingfish7 小时前
TLPI 第52章 读书笔记:POSIX Message Queues
linux·mq·ipc·posix·message·tlpi
wacsii7 小时前
【Linux进程(五)】进程控制全解析:从创建、终止、等待到程序替换
linux·服务器
七夜zippoe8 小时前
Nexent Linux 部署:用 deploy.sh
linux·服务器·docker·部署·ai智能体·nexent
去码头整点薯条988 小时前
NFS服务器
linux·运维·服务器
蜗牛一步步往上爬9 小时前
第一章 初识Linux shell
linux·运维·服务器
拂拉氏9 小时前
【知识讲解】 Linux线程中锁的认识
linux·互斥锁
无足鸟ICT9 小时前
【RHCA+】系统调优配置文件
linux·服务器
七维大脑9 小时前
【开源自荐】7Shell:跨平台 SSH 运维客户端,把终端、SFTP、容器和 K8s 放进一个窗口
ssh·运维工具·开源工具·开源ssh