如何隐藏不存在的用户失败的 ssh 尝试?

如何隐藏不存在的用户失败的 ssh 尝试?

问题

如何排除无关紧要的 SSH 登录失败?

背景

为了环境意识,我喜欢在我管理的每个 GNU/Linux 机器上有一些小背景窗口来显示系统错误日志。我以前使用 xconsole 执行此操作,但现在使用 xterm running journalctl -f

不幸的是,在需要服务的机器上ssh,journalctl 日志是来自破解者尝试使用常用名称/密码列表进行 ssh 的持续混乱流。²我看到每秒滚动的故障甚至不存在的帐户。这使得我很难在控制台日志中看到任何其他内容。

解决方案?

我对 systemd 的了解还不及我应有的一半,所以可能有一个巧妙、简单的答案,但我还没有找到。³ 欢迎任何解决方案。我怀疑这会涉及到 pam、journalctl 和/或grep -v.

我正在寻找一种解决方案,该解决方案仍然向我显示针对现有帐户的尝试。但是,如果这太困难,我将接受隐藏所有失败登录尝试的答案。

Journalctl 示例

请勿显示以下内容:

Dec 12 17:19:21 gaia sshd[10146]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=139.59.90.40  user=root
Dec 12 17:19:21 gaia sshd[10148]: Invalid user git from 14.29.201.30
Dec 12 17:19:21 gaia sshd[10148]: input_userauth_request: invalid user git [preauth]
Dec 12 17:19:21 gaia sshd[10148]: pam_unix(sshd:auth): check pass; user unknown
Dec 12 17:19:21 gaia sshd[10148]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=14.29.201.30
Dec 12 17:19:22 gaia sshd[10150]: Invalid user molisoft from 5.135.152.97
Dec 12 17:19:22 gaia sshd[10150]: input_userauth_request: invalid user molisoft [preauth]
Dec 12 17:19:22 gaia sshd[10150]: pam_unix(sshd:auth): check pass; user unknown
Dec 12 17:19:22 gaia sshd[10150]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=5.135.152.97
Dec 12 17:19:23 gaia sshd[10146]: Failed password for root from 139.59.90.40 port 37752 ssh2
Dec 12 17:19:23 gaia sshd[10146]: Received disconnect from 139.59.90.40: 11: Bye Bye [preauth]
Dec 12 17:19:23 gaia sshd[10148]: Failed password for invalid user git from 14.29.201.30 port 41178 ssh2
Dec 12 17:19:23 gaia sshd[10148]: Received disconnect from 14.29.201.30: 11: Bye Bye [preauth]
Dec 12 17:19:24 gaia sshd[10150]: Failed password for invalid user molisoft from 5.135.152.97 port 50730 ssh2
Dec 12 17:19:24 gaia sshd[10150]: Received disconnect from 5.135.152.97: 11: Bye Bye [preauth]

但一定要显示有效的登录信息:

Dec 10 08:56:16 gaia sshd[1414]: Accepted publickey for sophia from 24.22.130.192 port 41610 ssh2: RSA 6b:5f:aa:9c:d8:33:65:2c:c4:0c:88:12:ec:9b:ff:51
Dec 10 08:56:16 gaia sshd[1414]: pam_unix(sshd:session): session opened for user sophia by (uid=0)
Dec 10 21:06:37 gaia sshd[1414]: pam_unix(sshd:session): session closed for user sophia

还显示对有效帐户(root 除外)的失败尝试:

Dec 12 00:46:28 gaia sshd[30924]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.9.134  user=sophia
Dec 12 00:46:30 gaia sshd[30924]: Failed password for sophia from 192.168.9.134 port 55888 ssh2
Dec 12 00:46:33 gaia sshd[30924]: Connection closed by authenticating user sophia 192.168.9.134 port 55888 [preauth]

当然,任何服务不是应该显示 sshd。

Dec 10 08:56:16 gaia systemd[1]: Started User Manager for UID 3237.

1 定义为“用户名不存在”或“用户名是 root”。我的 boxen 上禁用了 root ssh。
² 我确实用过fail2ban。它有帮助,但尝试来自太多 IP 地址。
³ 例如,journalctl 允许grep但不grep -v排除某些条件。

答案1

使用

 journalctl -f | awk -f filter.awk

文件 filter.awk 包含

# get the non sshd stuff out the way
$5 !~ /^sshd\[[0-9]*\]:$/ { print ; next }
# show authentication errors for valid users which are not root
/authentication failure/ && $15 != "" && $15 != "user=root"
/Failed password for/ && !/invalid user/ && !/for root/
# Show valid logins
/Connection closed by authenticating user/
/Accepted publickey/
/pam_unix\(sshd:session):/

没什么很聪明的。如果它不是 sshd 行,则打印它并转到下一行。否则打印与表达式匹配的行。我使用 GNU awk 版本 5.0.1

答案2

您可以通过以下方式显着减少日志垃圾邮件/etc/ssh/sshd_config

  • 禁用密码身份验证并仅允许公钥:

    PasswordAuthentication no
    ChallengeResponseAuthentication no
    PermitRootLogin prohibit-password
    
  • 使用 或更少的级别INFOINFO是 sshd 上游默认值):

    LogLevel INFO
    

唯一还没有消除的问题是当登录垃圾邮件机器人不使用您的密钥交换方法时;然后你得到例如:

Unable to negotiate with 203.0.113.1 port 40442: no matching key exchange method found. Their offer: diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1 [preauth]

我还没有找到摆脱那一点的方法。

相关内容