centos6.9安装fail2ban防爆破软件实操
看下防火墙服务是否开启启动

chkconfig --list|grep iptables

查看当前运行的端口

netstat -tnlp

查看当前防火墙状态

service iptables status
iptables -L -n

添加配置防火墙端口,把需要开放的端口都添加进去

iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 5060 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
iptables -A INPUT -p tcp --dport 6379 -j ACCEPT
iptables -A INPUT -p tcp --dport 7088 -j ACCEPT
iptables -A INPUT -p tcp --dport 10050 -j ACCEPT
iptables -A INPUT -p tcp --dport 10051 -j ACCEPT

#保存配置,不然重启后就没有了

service iptables save 

#安装fail2ban

wget https://codeload.github.com/fail2ban/fail2ban/zip/refs/tags/0.9.7 -O fail2ban.0.9.7.zip
unzip fail2ban.0.9.7.zip
cd fail2ban-0.9.7
python setup.py install

#添加启动脚本

cp files/redhat-initd /etc/init.d/fail2ban

#配置fail2ban

cd /etc/fail2ban/
cp jail.conf jail.d/jail.conf.local
cd jail.d
vim jail.conf.local

找到[sshd]配置段,将这行以前的都删除,以及后面的 [sshd-ddos] 之后的配置段内容都删除(也就是只需要下面的配置就可以了)
更新为以下配置(port端口自己改成服务器的ssh端口号)

[sshd]
enabled = true
# To use more aggressive sshd filter (inclusive sshd-ddos failregex):
#filter = sshd-aggressive
filter = sshd
port = ssh
#port    = 5060
action = iptables[name=SSH,port=ssh,protocol=tcp]
#action = iptables[name=SSH,port=5060,protocol=tcp]
sendmail-whois[name=SSH,dest=root,sender=joyber@qq.com]
logpath = /var/log/secure
maxretry = 3
findtime = 300
bantime = 86400

[sshd-ddos]
# This jail corresponds to the standard configuration in Fail2ban.
# The mail-whois action send a notification e-mail with a whois request
# in the body.
port    = ssh
#port    = 5060
logpath = %(sshd_log)s
backend = %(sshd_backend)s

解决fail2ban在执行 iptables 命令报错 -w 需要参数的问题

cd /etc/fail2ban/action.d
vim iptables-common.conf
#把 lockingopt = -w 这一行注掉,改成这样的空配置
#lockingopt = -w
lockingopt = 

重启防火墙和fail2ban

service iptables restart
service fail2ban start

有些系统可能安装后fail2ban-client等执行文件并没有在/usr/bin/fail2ban-client这个路径
需要修改一下启动脚本

vim /etc/init.d/fail2ban

#FAIL2BAN="/usr/bin/fail2ban-client"
FAIL2BAN="/usr/local/bin/fail2ban-client"

查看日志文件,是否有报错

tail -f /var/log/fail2ban.log

最后就是自己找一个测试机测试一下,一直输入错误的密码是否会被禁止访问了

ssh 47.100.31.xxx
#被禁止访问了
ssh: connect to host 47.100.31.xxx port 22: Connection refused

#查看fail2ban运行状态和禁止的IP列表等信息
fail2ban-client status
fail2ban-client status sshd

测试成功后如果 iptables 没有自启动的话,可以这样开启

chkconfig iptables on

标签: none

添加新评论