FreeBSD blacklistd ipfw

blacklistd(8)の保持しているルールはblacklistclt(8)コマンドで次のように確認できます。システムを再起動した段階では次のようになんのルールも入っているはずです。

図 まだ何も検出していない状態のblacklistd(8)

# blacklistctl dump -a
        address/ma:port id      nfail   last access
#

blacklistd(8)の設定は/etc/blacklistd.confです。このファイルはデフォルトでは次のようになっています。この場合、sshにおける認証が3回失敗すると以後24時間はアクセスできない状態になります。

リスト blacklistd(8)の設定ファイルである/etc/blacklistd.conf

# $FreeBSD: releng/11.1/etc/blacklistd.conf 301226 2016-06-02 19:06:04Z lidl $
#
# Blacklist rule
# adr/mask:porttype	proto	owner		name	nfail	disable
[local]
ssh		stream	*	*		*	3	24h
ftp		stream	*	*		*	3	24h
smtp		stream	*	*		*	3	24h
submission	stream	*	*		*	3	24h
#6161		stream	tcp6	christos	*	2	10m
*		*	*	*		*	3	60

# adr/mask:port	type	proto	owner		name	nfail	disable
[remote]
#129.168.0.0/16	*	*	*		=	*	*
#6161		=	=	=		=/24	=	=
#*		stream	tcp	*		=	=	=

第113回 blacklistd(8)を使ってsshd DoS攻撃を防止する方法 | gihyo.jp

デフォルト 3回失敗→disable無効期間 60(s)

「ポート」はポート番号ないしはポート名。ポート名は /etc/services をもってポート番号を解釈する。
blacklistd 始めました - Qiita 2020

sshのポートを22222に変更した場合

22222		stream	*	*		*	3	24h

を追加すると。3回失敗で24h無効

sudo ipfw table port22 list

Create the sentinal file detected by the blacklistd-helper daemon to enable ipfw packet filtering commands. Use a non-default offset value, as the default rules for the "workstation" configuration end above rule 2000.

echo "ifpw_offset=4000" > /etc/ipfw-blacklist.rc

FreeBSD 11.0 Blacklistd Notes

see /usr/libexec/blacklistd-helper
デフォルトのipfw_offset=2000の場合ssh 22だと2000+22で2022

# Allow TCP through if setup succeeded
ipfw -q add pass tcp from any to any established
のルールを入れる場合は2022以降になるようにする
ipfw -q add 10000 pass tcp from any to any established

ブロックリストに登録されたIPを接続可にする

確認
sudo blacklistctl dump -a

blacklistd.confのホワイトリストに追加

sudo vi etc/blacklistd.conf

# adr/mask:port type    proto   owner           name    nfail   disable
[remote]
xxx.xxx.xxx.xxx       *       *       *               =       *       *

sudo /etc/rc.d/blacklistd restart

sudo ipfw table port22 delete xxx.xxx.xxx.xxx/32

ただ単にホワイトリストをつくって /etc/blacklistd.conf に入れておけば終わりです。
FreeBSD/blacklistd 運用について - Qiita

Q.やっべ、自分の接続元IPアドレスがブロックリストに登録されてしまった!
A.大丈夫だ。問題無い。現在 blocklistctl に解除する機能が無いので ipfw table port22 delete XXX.XXX.XXX.XXX/32 とコマンドを打って消そう!
なお拒否期限が残ってる間は blacklistd を再起動する度に登録されるのでよろしく!

blacklistd 始めました - Qiita

robots.txt

Note that Crawl-delay is not part of the original robots.txt specification. But it’s no problem to include them for those parsers that understand it, as the spec defines: Unrecognised headers are ignored.
So older robots.txt parsers will simply ignore your Crawl-delay lines.

web crawler - Robots.txt - What is the proper format for a Crawl Delay for multiple user agents? - Stack Overflow

One or more user-agent lines that is followed by one or more rules. The group is terminated by a user-agent line or end of file. The last group may have no rules, which means it implicitly allows everything.

https://developers.google.com/search/reference/robots_txt

例えば

User-agent: a
Crawl-delay: 5

User-agent: b
Disallow:/

User-agent: a が Crawl-delay を解釈しない場合

User-agent: a

User-agent: b
Disallow:/

になる。このため Allow か Disallow は明示的に書く

User-agent: a
Disallow:
Crawl-delay: 5

User-agent: b
Disallow:/

参考
https://www.robotstxt.org/robotstxt.html

Robots.txtの標準化仕様が(今さら)提出される - Qiita

check_logfiles postgres ログ監視

log_directory = '/var/log/pgsql' 
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' 
log_file_mode = 0644
log_rotation_age = 1d 
log_rotation_size = 0 

/var/log/pgsql/postgresql-2019-07-19_000000.log 
/usr/local/etc/check_logfiles_postgres.cfg

@searches = {
  tag => 'postgres',
  logfile => '/var/log/pgsql/dummy',
  archivedir => '/var/log/pgsql',
  rotation => 'postgresql-\d{4}-\d{2}-\d{2}_\d{6}.log',
  type => 'rotating::uniform',
  criticalpatterns => [
      '\] (ERROR|FATAL|PANIC):'
  ],
};

/usr/local/etc/nrpe.d/check_postgres.cfg 

command[check_logfiles_postgres]=/usr/local/libexec/nagios/check_logfiles -f /usr/local/etc/check_logfiles_postgres.cfg

  • /var/log/pgsql/dummy は存在しなくてOK

rotating::uniform

--tag failorder --type rotating::uniform --logfile /test/dummy \
--rotation "james-${date +"%F"}_\d+-${HOSTNAMEIP}-appserver0.log"

If you add a "-v" you can see what happens inside. Type rotating::uniform tells check_logfiles that the rotation scheme makes no difference between current log and rotated archives regarding the filename. (You frequently find something like xyz..log). What check_logfile does is to look into the directory where the logfiles are supposed to be. From /test/dummy it only uses the directory part. Then it takes all the files inside /test and compares the filenames with the --rotation argument. Those files which match are sorted by modification time. So check_logfiles knows which of the files in question was updated recently and the newest is considered to be the current logfile. And inside this file check_logfiles searches the criticalpattern. Gerhard
https://stackoverflow.com/questions/53369095/how-do-i-use-nagios-to-monitor-a-log-file-that-generates-a-random-id

参考

https://github.com/lausser/check_logfiles/issues/31#issuecomment-331728873
https://www.ashisuto.co.jp/db_blog/article/20151221_pg_monitoring.html
https://labs.consol.de/nagios/check_logfiles/examples/

groonga-normalizer-mysql FreeBSD

FreeBSD 11 ソースコンパイル

autoconf automake libtool が必要

curl -o groonga-normalizer-mysql-1.1.4.tar.gz https://github.com/groonga/groonga-normalizer-mysql/archive/v1.1.4.tar.gz
tar xvzf groonga-normalizer-mysql-1.1.4.tar.gz
cd groonga-normalizer-mysql-1.1.4
setenv PKG_CONFIG_PATH /usr/local/lib/pkgconfig
./autogen.sh
./configure
gmake
gmake install

freebsd shell 日本語メール

サブジェクトの先頭に日付(2019/07/09)をいれる

#! /bin/sh
FROM=from@example.com
TO=to@example.com
DATE=`date +%Y/%m/%d`
SUBJECT=`echo -n "$DATE " | cat - subject.txt | openssl enc -e -base64  -A`
BODY=`cat body.txt | openssl enc -e -base64`

/usr/sbin/sendmail -f ${FROM} ${TO} << EOF
To: ${TO}
From: ${FROM}
Subject: =?UTF-8?B?${SUBJECT}?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: Base64

${BODY}
EOF

参考
UTF-8のメールをbashで作ってみる | ハックノート
文字列を簡単に base64 エンコードする - Qiita
catで標準入力と連結 - ボクノス