Posts fail2ban 防ssh暴力破解
Post
Cancel

fail2ban 防ssh暴力破解

fail2ban 防ssh暴力破解

fail2ban 出现主要针对 暴力访问服务器而设计

原理是通过linux 服务器上的访问日志,通过正则表达式,获取访问信息

如ssh登录防暴力破解,通过正则ssh访问日志记录,获取暴利访问ip,家ip 通过防火墙隔离。’’

安装

官网 fail2ban

1
2
3
4
5
6
7
8
# centos7
wget "https://github.com/fail2ban/fail2ban/archive/0.9.4.tar.gz"
ll
tar -zxvf 0.9.4.tar.gz
cd fail2ban-0.9.4
python setup.py install #注意,这里需要python 2.6以上
cp files/redhat-initd /etc/init.d/fail2ban # 启动文件添加到init.d目录
chkconfig --add fail2ban #添加启动软链接

配置

ssh 防止暴力登录

1
2
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local #添加本地配置文件
vim /etc/fail2ban/jail.local  # 自动以配置

配置内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 禁止ip方式
banaction = firewallcmd-ipset  
banaction_allports = firewallcmd-allports
#centos7防火墙默认是 firewalld 如果是iptables,就改为iptables  目录:action.d/

# sshd 配置
[sshd]
enabled = true  #启动
filter = sshd #过滤规则  目录:filter.d/
port    = 22 #拦截端口
#logpath = %(sshd_log)s 
logpath = /var/log/secure  #监控日志
backend = %(sshd_backend)s 
action = %(action_)s #封禁ip的动作
bantime  = 172800 #ip封禁时间
maxretry = 3  # 阈值  --同一个ip登录失败多少次后封禁ip

启动

1
2
fail2ban-client -d #检查 jail.local 配置是否正确
systemctl start fail2ban #启动

查看

1
2
3
4
systemctl status fail2ban # 查看启动状态 running 为成功
fail2ban-client status # 查看状态
fail2ban-client status sshd #查看sshd 状态
tail -f /var/log/fail2ban.log #查看fail2ban监控日志

This post is licensed under CC BY 4.0 by the author.