做word文档什么网站好,wordpress文章内图片幻灯片,做网站资料准备,韩国网站建站利用 fail2ban 保护 SSH 服务器 一、关于 fail2ban1. 基本功能与特性2. 工作原理 二、安装与配置1. Debian/Ubuntu系统#xff1a;2. CentOS/RHEL系统#xff1a; 三、保护 SSH四、启动 fail2ban 服务五、测试和验证六、查看封禁的 IP 地址七、一些配置八、注意事项 作者2. CentOS/RHEL系统 三、保护 SSH四、启动 fail2ban 服务五、测试和验证六、查看封禁的 IP 地址七、一些配置八、注意事项 作者高玉涵 时间2024.6.30 12:56 博客blog.csdn.net/cg_i 环境CentOS8
左右左右下下 按按 滴——抓娃娃机
一、关于 fail2ban
Fail2ban是一款实用的入侵防御软件旨在保护服务器免受暴力攻击特别是通过密码猜测和暴力破解的方式。以下是关于Fail2ban的详细介绍
1. 基本功能与特性
日志监控与封禁Fail2ban能够监视系统日志文件如/var/log/auth.log、/var/log/apache/access.log等匹配日志中的错误信息通过正则表达式匹配并对符合特定规则的IP地址执行封禁操作。支持多种服务和动作Fail2ban支持大量的服务如sshd、apache、qmail、proftpd等并允许执行多种动作如通过iptables或tcp-wrapper更新防火墙规则发送邮件通知等。灵活性高用户可以根据自己的需求自定义监控规则、封禁时间和动作Fail2ban的配置文件提供了丰富的选项来满足这些需求。开箱即用Fail2ban附带了许多服务的默认过滤器用户无需从头开始配置即可快速部署。
2. 工作原理
Fail2ban通过扫描系统日志文件来检测失败的登录尝试或恶意行为。当检测到某个IP地址在指定时间内超过预设的失败尝试次数时Fail2ban会根据配置执行相应的封禁动作如通过iptables更新防火墙规则以拒绝该IP地址的访问。此外Fail2ban还可以配置为在封禁IP地址时发送邮件通知给管理员。
二、安装与配置
Fail2ban的安装和配置相对简单以下以基于Debian和CentOS的系统为例说明安装步骤
1. Debian/Ubuntu系统
更新软件源sudo apt-get update安装Fail2bansudo apt-get install -y fail2ban
2. CentOS/RHEL系统
更新软件源sudo yum update安装EPEL仓库如果尚未安装sudo yum install -y epel-release安装Fail2bansudo yum install -y fail2ban
配置Fail2ban时通常建议编辑/etc/fail2ban/jail.local文件如果不存在则从/etc/fail2ban/jail.conf复制一份并重命名。在该文件中可以启用和配置不同的监控服务设置全局参数如封禁时间、最大尝试次数等以及为特定服务配置日志路径和动作。
三、保护 SSH
[rootcentos8 ~]vi /etc/fail2ban/jail.local添加以下内容并保存
[sshd]
enabled true
filter sshd
port 22
logpath /var/log/secure
maxretry 3
bantime 600下面是对每一行配置的解释
enabled true启用对sshd服务的监控。filter sshd使用sshd过滤器来匹配日志中的错误信息。port 22指定sshd服务监听的端口号默认是22。logpath /var/log/secure指定sshd服务的日志文件路径。在大多数基于Red Hat的系统中sshd的日志记录在/var/log/secure文件中。maxretry 3设置最大尝试次数。如果某个IP地址在指定时间内尝试连接sshd服务并失败超过3次则触发封禁动作。bantime 600设置封禁时间单位为秒。在这个例子中被封禁的IP地址将在600秒内无法访问sshd服务。
四、启动 fail2ban 服务
systemctl enable --now fail2ban因我已启用了 fail2ban 为了让新配置生效重启服务即可。
[rootcentos8 ~]# systemctl restart fail2ban
[rootcentos8 ~]# systemctl status fail2ban
● fail2ban.service - Fail2Ban ServiceLoaded: loaded (/usr/lib/systemd/system/fail2ban.service; enabled; vendor preset: disabled)Active: active (running) since Sun 2024-06-30 13:22:15 CST; 5s agoDocs: man:fail2ban(1)Process: 3847005 ExecStop/usr/bin/fail2ban-client stop (codeexited, status0/SUCCESS)Process: 3847068 ExecStartPre/bin/mkdir -p /run/fail2ban (codeexited, status0/SUCCESS)Main PID: 3847070 (fail2ban-server)Tasks: 15 (limit: 49300)Memory: 22.8MCGroup: /system.slice/fail2ban.service├─3847070 /usr/bin/python3.6 -s /usr/bin/fail2ban-server -xf start├─3847098 /bin/bash /usr/sbin/sendmail -f rootcentos8. rootlocalhost├─3847116 /bin/bash /usr/sbin/sendmail -f rootcentos8. rootlocalhost├─3847134 /bin/bash /usr/sbin/sendmail -f rootcentos8. rootlocalhost├─3847137 /usr/bin/dotlockfile -p -l /root/.esmtp_queue/.deliver_lock├─3847138 /usr/bin/dotlockfile -p -l /root/.esmtp_queue/.deliver_lock└─3847492 /bin/bash /usr/sbin/sendmail -f rootcentos8. rootlocalhostJun 30 13:22:15 centos8 systemd[1]: fail2ban.service: Succeeded.
Jun 30 13:22:15 centos8 systemd[1]: Stopped Fail2Ban Service.
Jun 30 13:22:15 centos8 systemd[1]: Starting Fail2Ban Service...
Jun 30 13:22:15 centos8 systemd[1]: Started Fail2Ban Service.
Jun 30 13:22:16 centos8 fail2ban-server[3847070]: Server ready五、测试和验证
客户端连续输错 3 次密码
ssh root10.10.10.138
root10.10.10.138s password:随便输入
Permission denied,please try again.
root10.10.10.138s password:随便输入
Permission denied,please try again.
root10.10.10.138s password:随便输入
Permission denied(publickey,gssapi-keyex,gssapi-with-mic,password).再次连接到服务器
ssh root10.10.10.138
ssh:connect to host 10.10.10.138 port 22: Connection refused连接断开没有给我们输入密码的机会六、查看封禁的 IP 地址
查看封禁状态
通过防火墙策略查看当前封禁的 IP 地址
[rootcentos8 ~]firewall-cmd --list-all
public (active)target: defaulticmp-block-inversion: nointerfaces: eth0sources: services: cockpit dhcpv6-client sshports: protocols: forward: nomasquerade: noforward-ports: source-ports: icmp-blocks: rich rules: rule familyipv4 source address10.10.10.128 port portssh protocoltcp reject typeicmp-port-unreachable(IP 被禁止访问 SSH 端口。)通过 fail2ban-client status命令检查
fail2ban-client status
Status
|- Number of jail: 1
- Jail list: sshd可以看到此时有一条 Jail 生效为 sshd 的信息。要想进一步了解通过 fail2ban-client status sshd命令获得详细信息。
[rootcentos8 ~]# fail2ban-client status sshd
Status for the jail: sshd
|- Filter
| |- Currently failed: 1
| |- Total failed: 2
| - Journal matches: _SYSTEMD_UNITsshd.service _COMMsshd
- Actions|- Currently banned: 1|- Total banned: 2- Banned IP list: 10.10.10.128我们主要关注Actions下Currently banned当前封禁 IP 数量、Total banned总封禁次数、Banned IP list封禁的 IP 地址。
七、一些配置
[nginx-http-auth]
# HTTP 验证防暴力破解
enabled true
filter nginx-http-auth
port 80
logpath /var/log/nginx/error.log[nginx-badbots]
# 屏蔽爬虫
enabled true
port 80
filter nginx-badbots
logpath /var/log/nginx/access.log
maxretry 2[nginx-nohome]
# 避免恶意请求网站目录结构
enable true
port 80
filter nginx-nohome
logpath /var/log/nginx/access.log
maxretry 2[nginx-noproxy]
# 避免被人利用反向代理
enable true
port 80
filter nginx-noproxy
logpath /var/log/nginx/access.log
maxretry 2八、注意事项
在配置Fail2ban时应谨慎设置封禁时间和最大尝试次数等参数以避免误封合法用户。Fail2ban虽然能够降低恶意登录尝试的发生率但并不能完全消除弱身份验证带来的安全风险。因此建议结合其他安全措施如多因子认证来提高系统的整体安全性。定期检查和更新Fail2ban的配置文件和规则集以确保其能够有效地应对新的安全威胁。