配置Fail2Ban防SSH暴力破解和nginx的CC攻击

# 1、什么是 Fail2Ban

Fail2Ban 是一款开源入侵防御工具,它可以实时监控日志文件,一旦检测到暴力破解行为(如多次登录失败),就会自动使用防火墙(如 firewalld)将攻击者 IP 临时或永久封禁。

# 2、安装 Fail2Ban

环境信息:

系统版本:Rocky Linux 8 / 9
SSH 登录端口:默认 22(如你自定义端口,请注意替换)
默认 SSH 日志路径:/var/log/secure


Rocky Linux 默认没有内置 Fail2Ban,需要启用 EPEL 仓库:

启用 EPEL 仓库

dnf install epel-release -y

安装 Fail2Ban,为了方便直接使用fail2ban-all安装

dnf install fail2ban-all -y


说明:使用fail2ban安装时会只会安装fail2ban包;而使用fail2ban-all安装时就会把邮件提醒的工具也安装上。
就是mail-whois.conf相关的一些配置文件。

Installed:
fail2ban-all-1.1.0-6.el9.noarch fail2ban-hostsdeny-1.1.0-6.el9.noarch fail2ban-mail-1.1.0-6.el9.noarch libnsl2-2.0.0-1.el9.0.1.x86_64
python3-inotify-0.9.6-25.el9.noarch tcp_wrappers-7.6-97.el9.x86_64 tcp_wrappers-libs-7.6-97.el9.x86_64


安装完成后,Fail2Ban 的核心文件路径为:

配置目录:/etc/fail2ban
服务名:fail2ban


# 3、配置 Fail2Ban 保护 SSH

Fail2Ban 的默认配置文件为 `/etc/fail2ban/jail.conf`,请不要直接修改,应该创建一个新的配置文件:

vim /etc/fail2ban/jail.local

defalut这里是设定全局设置,如果下面的监控没有设置就以全局设置的值设置。

[DEFAULT]

用于指定哪些地址ip可以忽略 fail2ban 防御,以空格间隔。

ignoreip = 127.0.0.1/8

客户端主机被禁止的时长(默认单位为秒,1h表示1小时)

bantime = 3600

过滤的时长(秒),查找时间窗

findtime = 600

匹配到的阈值(次数),最大失败次数(超出则封禁)

maxretry = 3

[sshd]
enabled = true
mode = normal
port = ssh #若是更改了sshd服务的端口号,需要在这里设置对应的端口号,否则配置不生效
logpath = %(sshd_log)s
backend = %(sshd_backend)s

[dropbear]
enabled = true
port = ssh
logpath = %(dropbear_log)s
backend = %(dropbear_backend)s

[selinux-ssh]
enabled = true
port = ssh
logpath = %(auditd_log)s


参数说明:

bantime:封禁时长,可设置为 -1 实现永久封禁
findtime + maxretry:在 findtime 时间内失败超过 maxretry 次,就封禁


# 4、启动并启用 Fail2Ban

启动:

systemctl start fail2ban

systemctl enable --now fail2ban # 启动并设置开机自启


设置开机自动启动:

systemctl enable fail2ban


修改配置文件中重载

fail2ban-client reload


启用防火墙 firewalld

systemctl enable --now firewalld
systemctl status firewalld


查看运行状态

systemctl status fail2ban


查看 Fail2Ban 状态:

fail2ban-client status

[root@localhost ~]# fail2ban-client status
Status
|- Number of jail: 1
`- Jail list: sshd


查看 sshd Jail 的详细状态:

fail2ban-client status sshd

[root@localhost ~]# fail2ban-client status sshd
Status for the jail: sshd
|- Filter
| |- Currently failed: 0
| |- Total failed: 0
| `- Journal matches: _SYSTEMD_UNIT=sshd.service + _COMM=sshd + _COMM=sshd-session
`- Actions
|- Currently banned: 0
|- Total banned: 0
`- Banned IP list:


刚安装还没有被封禁的IP,接下来手动进行封禁

# 5、手动封禁与解封 IP

封禁某 IP:

fail2ban-client set sshd banip 10.0.0.111


现在可以查看到10.0.0.111被封禁了。

通过10.0.0.111是无法登录10.0.0.110的,因为已经被封禁。

解封某 IP:

fail2ban-client set sshd unbanip 10.0.0.111


解封后顺利登录10.0.0.110

> 【温馨提示】我是设置firewalld进行封禁IP的,必须保证firewalld是运行中的,不然无法进行禁用

# 6、附加建议-ssh防护


## 1、修改 SSH 默认端口

编辑 `/etc/ssh/sshd_config`,例如:

Port 2222


然后重启 SSH 服务。

## 2、禁止 root 登录

PermitRootLogin no


## 3、仅允许特定 IP 登录 SSH: 利用 firewalld:

添加放行的ip访问ssh使用的端口号
firewall-cmd --zone=public --list-ports
firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="你的IP" port protocol="tcp" port="22" accept' --permanent
firewall-cmd --reload

移除默认放行的ssh

firewall-cmd --zone=public --list-services
firewall-cmd --zone=public --remove-service=ssh --permanent
firewall-cmd --reload

根据安全要求,Fail2Ban 几乎是必装工具.

# 7、配置 Fail2Ban 保护nginx防止被CC攻击

服务器遭遇CC攻击,大量恶意请求占用连接资源。通过Fail2Ban动态封禁IP,结合Nginx限流策略,10分钟内恢复服务。

前提条件:nginx中限流模块ngx_http_limit_req_module里的limit_req_zone指令和limit_zone限流指令

## 1. 配置Nginx请求频率限制

在nginx.conf中设置全局限流规则:

http {
limit_req_zone $binary_remote_addr zone=cc_protection:10m rate=100r/m;

server {
    location / {
        limit_req zone=cc_protection burst=200 nodelay;
        proxy_pass http://backend;
    }
}

}


## 2. 使用Fail2Ban自动封禁恶意IP

vim /etc/fail2ban/jail.local

[DEFAULT]
bantime = 1h # 封禁时间(1 小时)
findtime = 10m # 查找时间窗(10 分钟)
maxretry = 5 # 最大失败次数(超出则封禁)

[nginx-limit-req]
enabled = true
port = http,https
logpath = %(nginx_error_log)s # nginx默认错误日志路径:/var/log/nginx/error.log

[nginx-http-auth]
mode = normal
enabled = true
port = http,https
logpath = %(nginx_error_log)s

[nginx-botsearch]
enabled = true
port = http,https
logpath = %(nginx_error_log)s

[nginx-bad-request]
enabled = true
port = http,https
logpath = %(nginx_access_log)s

[nginx-forbidden]
enabled = true
port = http,https
logpath = %(nginx_error_log)s


查看具体的监听日志路径的方式

fail2ban-client status nginx-http-auth

Status for the jail: nginx-http-auth
|- Filter
| |- Currently failed: 0
| |- Total failed: 0
| - File list: /var/log/nginx/error.log # 监听日志路径 - Actions
|- Currently banned: 0
|- Total banned: 0
`- Banned IP list:



查看被firewalld封禁的ip访问信息

firewall-cmd --zone=public --list-rich-rules

rule family="ipv4" source address="192.168.2.203" port port="https" protocol="tcp" reject type="icmp-port-unreachable"
rule family="ipv4" source address="192.168.2.203" port port="http" protocol="tcp" reject type="icmp-port-unreachable"


解除被firewalld封禁的ip访问

firewall-cmd --zone=public --remove-rich-rule='rule family="ipv4" source address="192.168.2.203" port port="http" protocol="tcp" reject' --permanent
firewall-cmd --zone=public --remove-rich-rule='rule family="ipv4" source address="192.168.2.203" port port="https" protocol="tcp" reject' --permanent
firewall-cmd --reload


从firewalld中移除后,ip就可以访问了,但是在fail2ban中被封禁的IP信息还存在

在fail2ban中移除被封禁的IP信息

fail2ban-client set nginx-limit-req unbanip 192.168.2.203


# 8、fail2ban邮件预警

用mail进行发邮件,也即是mailx,或者是s-nail,在这里使用s-nail。

在`~/.mailrc`末尾配置发件人的信息:

cat ~/.mailrc

set smtp-auth=login
set smtp-use-starttls
set v15-compat
set mta='smtp://gaojing%40jdd966.com:gj123%2C.%23%40Abc@smtp.qiye.aliyun.com:587'
set from=gaojing@jdd966.com


然后在jail.local(自己的配置文件里),加入:

[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 5

mta = mail
sendername = Fail2Ban Alerts
destemail = gaojing@jdd966.com
sender = gaojing@jdd966.com

[nginx-limit-req]
enabled = true
port = http,https
logpath = %(nginx_error_log)s
action = %(action_mwl)s # 增加这一行


sendername  是发件人名称
destemail   是填入收件人邮箱
sender      是填入发送人邮箱
最后重新加载下配置即可:`fail2ban-client reload`

发送邮件测试

echo "邮件内容".|mail -v -s "邮件标题" gaojing@jdd966.com


然后进行访问测试

posted @ 2025-06-25 15:52  哈喽哈喽111111  阅读(519)  评论(0)    收藏  举报