大网数据广告活动
服务器运维问答

How to send a systemd ssh error so that it's picked by fail2ban

· 52CE 我爱测

I have a setup which uses ansible and vagrant to test a custom role which installs and configures fail2ban. I have written myself a simple test playbook which uses the fail2ban role and applies the necessary configuration as expe…

问题原文

原帖链接 · tftd · CC BY-SA 4.0

I have a setup which uses ansible and vagrant to test a custom role which installs and configures fail2ban.

I have written myself a simple test playbook which uses the fail2ban role and applies the necessary configuration as expected. My intention, however, was to also write a few lines which do an integration test such as simulate a few "failed login" attempts.

From what I saw in How can I send a message to the systemd journal from the command line?, one should be able to do something like this:

echo "Failed password for root from 1.2.3.4 port 35720 ssh2" | systemd-cat -t sshd -p err

This indeed shows up in the journalctl -f logs, but it somehow doesn't end up in fail2ban at all:

fail2ban-client status sshd
Status for the jail: sshd
|- Filter
|  |- Currently failed: 1
|  |- Total failed: 7
|  `- Journal matches:  _SYSTEMD_UNIT=sshd.service + _COMM=sshd
`- Actions
   |- Currently banned: 0
   |- Total banned: 1
   `- Banned IP list:   

The stats from above are from my own login failures.

I also tried with different log levels (i.e. crit, err, info, warning, etc), but without any luck. Is there anything I'm doing wrong here?

已采纳解答

原帖链接 · tftd · CC BY-SA 4.0

I have managed to figure this out after countless hours of reading and reading through a bunch of man documentation:

  1. logger
  2. systemd.journal-fields

Once you go through a bunch of pages in the docs you would find out that you can do something like the following:

journalctl --facility=auth --since -5m --output=export

This shows the raw message with all of it's metadata. The properties which are starting with _ are considered "trusted" and cannot be manipulated (they are appointed by syslog or journald).

Here's an example message:

_SYSTEMD_SLICE=system.slice
_CAP_EFFECTIVE=3fffffffff
SYSLOG_FACILITY=4
_TRANSPORT=syslog
SYSLOG_IDENTIFIER=sshd
_COMM=sshd
_EXE=/usr/sbin/sshd
_SYSTEMD_CGROUP=/system.slice/ssh.service
_SYSTEMD_UNIT=ssh.service
_SYSTEMD_INVOCATION_ID=f29bc3ffcdce4a2dbeb18fe53ac7fea9
_CMDLINE=sshd: root [priv]
SYSLOG_PID=7021
SYSLOG_TIMESTAMP=Aug  6 11:18:42 
_PID=7021
MESSAGE=Connection closed by authenticating user root 192.168.121.1 port 54666 [preauth]
_SOURCE_REALTIME_TIMESTAMP=1659784722477758

In fail2ban the journal match filter is _SYSTEMD_UNIT=sshd.service + _COMM=sshd. So, it is not possible for me to simulate login failures. Therefore I had to change the filter to be journalmatch = SYSLOG_IDENTIFIER=sshd + SYSLOG_FACILITY=4 (located in filers/sshd.conf).

Once I did that, I could then easily simulate a login failure message with the following command:

logger --journald <<END
SYSLOG_FACILITY=4
SYSLOG_IDENTIFIER=sshd
MESSAGE=Failed password for root from 172.31.255.254 port 54666 ssh2
PRIORITY=6
END

And voilà - integration test completed.

Every 2.0s: fail2ban-client status sshd                                                                  localhost: Sat Aug  6 12:00:22 2022

Status for the jail: sshd
|- Filter
|  |- Currently failed: 0
|  |- Total failed:     1110
|  `- Journal matches:  SYSLOG_IDENTIFIER=sshd + SYSLOG_FACILITY=4
`- Actions
   |- Currently banned: 1
   |- Total banned:     8
   `- Banned IP list:   172.31.255.254

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2022-08-05)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。