fail2banの設定を変えてpfでblockする

2 min read

image-7.png

こんにちは、無能です。

今日は無能オブ無能な記事です。

あれ!?なんかfail2ban.logで変なログ出ている!

よーしよし、fail2banでbanされたものを眺めるか。。。と思ってみてたら

2024-07-24 20:52:43,027 fail2ban.utils          [39963]: INFO    HINT on 127: "Command not found".  Make sure that all commands
 in "{ iptables -w -C f2b-sshd -j RETURN >/dev/null 2>&1; } || { iptables -w -N f2b-sshd || true; iptables -w -A f2b-sshd -j RE
TURN; }\nfor proto in $(echo 'tcp' | sed 's/,/ /g'); do\n{ iptables -w -C INPUT -p $proto -m multiport --dports ssh -j f2b-sshd
 >/dev/null 2>&1; } || { iptables -w -I INPUT -p $proto -m multiport --dports ssh -j f2b-sshd; }\ndone" are in the PATH of fail
2ban-server process (grep -a PATH= /proc/pidof -x fail2ban-server/environ). You may want to start "fail2ban-server -f" separa
tely, initiate it with "fail2ban-client reload" in another shell session and observe if additional informative error messages a
ppear in the terminals.

あれ、なんかコマンドないってでている・・・。

とすっかり忘れていた!!!
FreeBSDの場合はpfで制御しているんだった・・・。

GNU/Linuxの場合はiptablesで良いのですがBSD触っているのにすっかり忘れていた・・・。

というわけでこのサイトを参考にしながら再度設定します・・・。

https://dbdemon.com/pf_and_fail2ban

というわけで私は自鯖を$exsrv1としているので

# Fail2Ban
table <f2b> persist
anchor "f2b/*"
block drop in log quick on $exsrv1 from <f2b> to any  

これで設定。というかf2b/*として正規表現可能なのか・・・。

そして、jail.confを設定し直し。おとなしくDefaultセクションにしました。

[DEFAULT]
banaction = pf[actiontype=<allports>]
banaction_allports = pf[actiontype=<allports>]

[sshd]
enabled = true
port    = ssh
logpath = /var/log/auth.log
bantime = 1w
findtime = 1d
maxretry = 5

[postfix]
enabled = true
port    = smtp,ssmtp,submission
logpath = /var/log/maillog
bantime = 1w
findtime = 1d
maxretry = 10

[dovecot]
enabled = true
port    = pop3,pop3s,imap,imaps,submission
logpath = /var/log/maillog
bantime = 1w
findtime = 1d
maxretry = 10

設定ファイル的にはaction.d/pf.confでデフォルトですでに設定されており見てみると

こんな感じでコマンドの呼び出しには特に手を加えなくても良さそう。

とこれでfail2banとpfの再起動をしてfail2banのログを確認してみると、それまでbanしていったIPはそのまま引き継いでくれるみたいです。

# fail2ban-client status sshd
Status for the jail: sshd
|- Filter
|  |- Currently failed: 1
|  |- Total failed:     1
|  `- File list:        /var/log/auth.log
`- Actions
   |- Currently banned: 37
   |- Total banned:     37
   `- Banned IP list:   *

これでまた様子見てみます。
それでは。

またよろしくお願いします。