1. 概要
「portsentry」は、ポートスキャン、ステルス・スキャンを検知して、接続してくる IP アドレスからの通信を遮断するプログラムです。
単に遮断するのみでなく、相手からは通信が成功しているように見せかけて実際は、遮断しているという、、「なかなかやりおるな」というプログラムなのです。
本ページは、下記のサイトを参考にさせていただきました。
「PortSentry - Pocketstudio.jp Linux Wiki」
2. インストール
cd /usr/ports/security/portsentry
make
make install
3. 構成ファイルの編集
/usr/local/etc/portsentry.conf
/usr/local/etc/portsentry.ignore
この2つを編集します。
「/usr/local/etc/portsentry.conf」は
#KILL_ROUTE="/bin/echo 'block in log on external_interface from $TARGET$/32 to any' | /sbin/ipf -f -"
の先頭の # を削除して生かします。これで、攻撃してきた IP を遮断します。
ただこの行のコメントが「For those running ipfilt (OpenBSD, etc.)」となっていて上の方に「FreeBSD」というコメントのある行があるのが気になりますわなぁ。
「/usr/local/etc/portsentry.ignore」は
0.0.0.0
の行を削除して、遮断しないアドレス(つまり自分のローカルやワールドのアドレス)を記述します。
/(スラッシュ)を使うことによってサブネットマスクを使ってネットワークアドレスでの記述ができます。
4. 起動
起動スクリプトが作成されています。
/usr/local/etc/rc.d/portsentry.sh
操作コマンドは、以下になります。
service portsentry.sh start ← 起動
service portsentry.sh stop ← 停止
ステータスを見るオプションがないので、起動しているか見るのはとりあえず
> pgrep -lf portsentry
17304 /usr/local/bin/portsentry -udp
17302 /usr/local/bin/portsentry -tcp
で良しとしましょう。
5. 試験
「portsentry」を実行している状態で、ポートスキャンをかけて、「/var/log/message」を見てみます。
Aug 26 14:50:23 vm portsentry[1200]: attackalert: Connect from host: 192.168.174.1/192.168.174.1 to TCP port: 1
Aug 26 14:50:23 vm portsentry[1200]: attackalert: Host 192.168.174.1 has been blocked via wrappers with string: "ALL: 192.168.174.1"
Aug 26 14:50:23 vm portsentry[1200]: attackalert: Host 192.168.174.1 has been blocked via dropped route using command: "/bin/echo
'block in log on external_interface from 192.168.174.1/32 to any' | /sbin/ipf -f -"
Aug 26 14:50:25 vm portsentry[1200]: attackalert: Connect from host: 192.168.174.1/192.168.174.1 to TCP port: 11
Aug 26 14:50:25 vm portsentry[1200]: attackalert: Host: 192.168.174.1 is already blocked. Ignoring
Aug 26 14:50:26 vm portsentry[1200]: attackalert: Connect from host: 192.168.174.1/192.168.174.1 to TCP port: 15
Aug 26 14:50:26 vm portsentry[1200]: attackalert: Host: 192.168.174.1 is already blocked. Ignoring
Aug 26 14:50:38 vm portsentry[1200]: attackalert: Connect from host: 192.168.174.1/192.168.174.1 to TCP port: 79
Aug 26 14:50:38 vm portsentry[1200]: attackalert: Host: 192.168.174.1 is already blocked. Ignoring
・・・
おお、出てる出てる。
/usr/local/etc/portsentry.ignore
を編集して、「192.168.174.1」を遮断しないようにすると、ログが出てこなくなったのでうまく動作しているようです。
6. メンテナンス
最近(2021年7月19日)まで、意識していなかったのですが、「portsentry」は、アタックを受けたとみなした IP アドレスを
/etc/hosts.deny
へ記録します。
「/etc/hosts.allow」「/etc/hosts.deny」によってセキュリティ制限をかけている場合は、注意が必要です。
逆に、「FreeBSD」のデフォルトで
/etc/hosts.allow
の有効行の先頭に
# Start by allowing everything (this prevents the rest of the file
# from working, so remove it when you need protection).
# The rules here work on a "First match wins" basis.
ALL : ALL : allow
の記述があれば、「/etc/hosts.deny」に記録されてもなんの効力もありません。
「/etc/hosts.deny」によってやたら制限がかかったり、ちっともかからないとしても、中身のチェックはときどきおこないましょう。
やたら、行数が増えていたりしますし、同じアドレスが、複数回記録されていたりしますので、ソートしたり、重複行を削除したりすることが必要です。