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

hac*_*rb9 5 ssh pam logs fail2ban journalctl

问题

\n\n

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

\n\n

背景

\n\n

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

\n\n

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

\n\n

解决方案?

\n\n

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

\n\n

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

\n\n

Journalctl 示例

\n\n

请勿显示以下内容:

\n\n
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\nDec 12 17:19:21 gaia sshd[10148]: Invalid user git from 14.29.201.30\nDec 12 17:19:21 gaia sshd[10148]: input_userauth_request: invalid user git [preauth]\nDec 12 17:19:21 gaia sshd[10148]: pam_unix(sshd:auth): check pass; user unknown\nDec 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\nDec 12 17:19:22 gaia sshd[10150]: Invalid user molisoft from 5.135.152.97\nDec 12 17:19:22 gaia sshd[10150]: input_userauth_request: invalid user molisoft [preauth]\nDec 12 17:19:22 gaia sshd[10150]: pam_unix(sshd:auth): check pass; user unknown\nDec 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\nDec 12 17:19:23 gaia sshd[10146]: Failed password for root from 139.59.90.40 port 37752 ssh2\nDec 12 17:19:23 gaia sshd[10146]: Received disconnect from 139.59.90.40: 11: Bye Bye [preauth]\nDec 12 17:19:23 gaia sshd[10148]: Failed password for invalid user git from 14.29.201.30 port 41178 ssh2\nDec 12 17:19:23 gaia sshd[10148]: Received disconnect from 14.29.201.30: 11: Bye Bye [preauth]\nDec 12 17:19:24 gaia sshd[10150]: Failed password for invalid user molisoft from 5.135.152.97 port 50730 ssh2\nDec 12 17:19:24 gaia sshd[10150]: Received disconnect from 5.135.152.97: 11: Bye Bye [preauth]\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
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\nDec 10 08:56:16 gaia sshd[1414]: pam_unix(sshd:session): session opened for user sophia by (uid=0)\nDec 10 21:06:37 gaia sshd[1414]: pam_unix(sshd:session): session closed for user sophia\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
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\nDec 12 00:46:30 gaia sshd[30924]: Failed password for sophia from 192.168.9.134 port 55888 ssh2\nDec 12 00:46:33 gaia sshd[30924]: Connection closed by authenticating user sophia 192.168.9.134 port 55888 [preauth]\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
Dec 10 08:56:16 gaia systemd[1]: Started User Manager for UID 3237.\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n\n

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

\n

ica*_*rus 2

使用

 journalctl -f | awk -f filter.awk
Run Code Online (Sandbox Code Playgroud)

文件 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):/
Run Code Online (Sandbox Code Playgroud)

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