如何使用denyhosts和or fail2ban阻止所有root登录尝试?


8

我目前使用root阻止所有ssh登录。但是我想加倍努力,阻止试图以root用户身份登录的客户端的IP地址。我目前拥有denyhosts和fail2ban的设置并可以正常工作,我可以使用denyhosts和fail2ban阻止那些尝试以root用户身份登录的IP地址吗?

Answers:


2

根据您的分布,编辑/etc/fail2ban/jail.conf 更新[ssh]部分以显示类似的内容

[ssh]

enabled = true
port    = ssh
filter  = sshd
logpath  = /var/log/auth.log
bantime = 3600
maxretry = 3

根据需要更改参数。它不会专门阻止root,但是每次尝试都会失败。请注意maxretrybantime。如果您使用自己的密码失败,则密码maxtretry设置为低时,您将阻止自己bantime。重新启动fail2ban。

我不会永远阻止IP,因为许多尝试来自动态IP,它们可能会在以后的某个时间阻止合法用户。

(某些发行版提供了jail.options文件供您进行修改。这是放置更改的首选位置,因为更改不会受到覆盖conf的更新的影响。)


4
好信息,但我想他想知道如何使用root用户阻止所有登录...在您的答案中看不到。也许你忘记了。
Mose

17

将此代码复制到新文件/etc/fail2ban/filter.d/sshd-root.conf中:

[INCLUDES]

# Read common prefixes. If any customizations available -- read them from
# common.local
before = common.conf

[Definition]

_daemon = sshd

failregex = ^%(__prefix_line)sFailed (?:password|publickey) for root from <HOST>(?: port \d*)?(?: ssh\d*)?$

ignoreregex = 

请注意,您可能必须编辑failregex才能准确识别失败的root登录尝试-使用:

fail2ban-regex /var/log/auth.log /etc/fail2ban/filter.d/sshd-root.conf

测试它是否标识正确的日志条目。

然后,您需要编辑jail.local以使用新的过滤器-添加类似以下内容:

[ssh]

enabled  = true
port     = 1:65535
filter   = sshd-root
logpath  = /var/log/auth.log
bantime  = 604800
maxretry = 3

显然,您应该根据需要调整这些值。上面的设置将在尝试三次以root用户身份登录后从有问题的IP地址中丢弃所有传入的数据包,并在一周后再次释放IP。


1
这确实是可以接受的答案,因为它实际上可以回答问题。
Peelman 2015年

这绝对是正确的答案。最好在sshd config中禁用root登录,然后在jail.conf中将maxretry设置为1。
anteatersa

1

由于默认值/etc/fail2ban/filter.d/sshd.conf已经具有AllowUsers和DenyUsers的正则表达式...

...
^%(__prefix_line)sUser .+ from <HOST> not allowed because not listed in AllowUsers\s*$
^%(__prefix_line)sUser .+ from <HOST> not allowed because listed in DenyUsers\s*$
...

以下内容将:

  • 允许exampleusername来自外部IP的连接
  • root或本地网络(192.168.0。*)上的任何连接

行“ / etc / ssh / sshd_config”:

AllowUsers exampleusername *@192.168.0.* *@localhost *@127.0.0.1

并在/etc/fail2ban/jail.conf

ignoreip = 127.0.0.1/8 192.168.0.2/255
...
...
[ssh]

enabled  = true
port     = ssh
filter   = sshd
logpath  = /var/log/auth.log
maxretry = 1
findtime = 99999999 
bantime  = 99999999

0

您如何阻止ssh登录?/ bin / false还是sshd_config DenyUsers选项?

我想不出一个答案,但是IIRC denyhosts解析了日志文件,所以只要看看有人尝试禁用它登录到root后,您是否在日志文件中输入了失败的条目


2
我编辑了ssh配置文件,/etc/ssh/sshd_config然后PermitRootLogin从yes 更改为no。我不知道这是否相关,但是我确实安装了rssh,仅允许某些用户使用sftp登录,但不允许ssh。
samwell 2011年

您是否检查了ssh日志文件是否尝试了失败的用户登录?
MitziMeow 2011年

是的,我可以看到许多不同的IP地址进行了许多失败的用户登录尝试,但我不认识。
samwell 2011年

那么denyhosts应该可以工作
MitziMeow 2011年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.