解决你的服务器总是被暴力破解的办法 - fail2ban https://bytelang.com/article/content/sLL_Cto3k4E=

https://bytelang.com/article/content/sLL_Cto3k4E=

 

前言

对于SSH服务的常见的攻击就是暴力破解攻击——远程攻击者通过不同的密码来无限次地进行登录尝试。

当然SSH可以设置使用非密码验证验证方式来对抗这种攻击,例如公钥验证或者双重验证。

将不同的验证方法的优劣处先放在一边,如果我们必须使用密码验证方式怎么办?你是如何保护你的 SSH 服务器免遭暴力破解攻击的呢?

 

fail2ban 是 Linux 上的一个著名的入侵保护的开源框架,它会监控多个系统的日志文件(例如:/var/log/auth.log 或者 /var/log/secure)并根据检测到的任何可疑的行为自动触发不同的防御动作。事实上,fail2ban 在防御对SSH服务器的暴力密码破解上非常有用。

 

安装

为了在CentOS 或 RHEL上安装fail2ban, 运行以下命令:

 yum install fail2ban

在ubuntu,Debian 或 Linux Mint上安装fail2ban:

apt-get install fail2ban

 

在安装完成之后,配置文件位于 /etc/fail2ban/jail.conf。其中默认配置了很多常用软件的日志扫描参数,如(节选):

[sshd]

port    = ssh
logpath = %(sshd_log)s
backend = %(sshd_backend)s


[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
logpath = %(sshd_log)s
backend = %(sshd_backend)s


[dropbear]

port     = ssh
logpath  = %(dropbear_log)s
backend  = %(dropbear_backend)s


[selinux-ssh]

port     = ssh
logpath  = %(auditd_log)s


#
# HTTP servers
#

[apache-auth]

port     = http,https
logpath  = %(apache_error_log)

 

其中第一条配置为[DEFAULT]这个可以设置下面规则的未定义缺省参数

# "ignoreip" can be an IP address, a CIDR mask or a DNS host. Fail2ban will not
# ban a host which matches an address in this list. Several addresses can be
# defined using space (and/or comma) separator.
ignoreip = 127.0.0.1/8

# External command that will take an tagged arguments to ignore, e.g. <ip>,
# and return true if the IP is to be ignored. False otherwise.
#
# ignorecommand = /path/to/command <ip>
ignorecommand =

# "bantime" is the number of seconds that a host is banned.
bantime  = 600

# A host is banned if it has generated "maxretry" during the last "findtime"
# seconds.
findtime  = 600

# "maxretry" is the number of failures before a host get banned.
maxretry = 5

 

参数解释:尝试5次失败之后,禁止访问600秒。

 

随后,开启服务

service fail2ban restart

 

平常的拦截都会出现在日志中:

/var/log/fail2ban.log
posted @ 2020-04-07 19:47  大浪淘沙、  阅读(500)  评论(0)    收藏  举报