介绍
对于 SSH 服务的常见的攻击就是暴力破解攻击 -- 远程攻击者通过不同的密码来无限次地进行登录尝试. 当然 SSH 可以设置使用非密码验证验证方式来对抗这种攻击, 例如公钥验证或者双重验证. 将不同的验证方法的优劣处先放在一边, 如果我们必须使用密码验证方式怎么办? 你是如何保护你的 SSH 服务器免遭暴力破解攻击的呢?
幸运的是, 有一种工具可以缓解这种攻击这个工具是 fail2ban. 这可以配置为允许使用 SSH 进行合法登录, 但在经过一定次数后无法正确验证后攻击将禁用 IP 地址. 我们将在 Debian 上安装和配置该软件. 没有服务器的同学可以在这里购买 https://cloud.tencent.com/product/cvm , 不过我个人更推荐您使用免费的腾讯云开发者实验室 https://cloud.tencent.com/developer/labs 进行试验, 学会安装后在购买服务器 https://cloud.tencent.com/product/cvm .
第一步 - 安装 fail2ban
Debian 在其默认存储库中包含 fail2ban. 我们可以使用以下命令下载并安装它:
- sudo apt-get update
- sudo apt-get install fail2ban
这不仅会安装 fail2ban, 还会自动设置启动服务.
第二步 - 配置 fail2ban
fail2ban 配置保存在 / etc/fail2ban 目录中. 默认禁止规则的配置文件称为 jail.conf.
由于 fail2ban 在程序有新版本时更新其配置文件的方式, 因此我们不应编辑默认配置文件.
- cd /etc/fail2ban
- sudo cp jail.conf jail.local
- sudo nano jail.local
- actionstart = iptables -N fail2ban-<name>
- iptables -A fail2ban-<name> -j RETURN # questionable usefulness
- iptables -I <chain> -p <protocol> -m multiport --dports <port> -j fail2ban-<name>
- actionstop = iptables -D <chain> -p <protocol> -m multiport --dports <port> -j fail2ban-<name>
- iptables -F fail2ban-<name>
- iptables -X fail2ban-<name>
- sudo iptables -L
- Chain INPUT (policy ACCEPT)
- target prot opt source destination
- fail2ban-ssh tcp -- anywhere anywhere multiport dports ssh
- Chain FORWARD (policy ACCEPT)
- target prot opt source destination
- Chain OUTPUT (policy ACCEPT)
- target prot opt source destination
- Chain fail2ban-ssh (1 references)
- target prot opt source destination
- DROP all -- xxx-xxxxxxxx.dyn.xxxxxxxxx.net anywhere
- RETURN all -- anywhere anywhere
来源: https://www.cnblogs.com/qcloud1001/p/9437449.html