概要
この記事ではPortSentry
を使用したポートスキャンの遮断について解説します。
環境
- PortSentryインストール
- Ubuntu 20.04.5
- 攻撃側
- Kali Linux 2023
PortSentryとは
PortSentry
はポートスキャンを検出し、遮断、保護するツールです。
主な機能
- ポートスキャン検出: PortSentryは、TCPおよびUDPのポートスキャンをリアルタイムで監視し、検出します
- アクティブな防御: ポートスキャンが検出された場合、PortSentryは攻撃元IPをブロックするための対策を自動的に実行します(例: iptablesによるブロック)
- ログ記録: 検出されたスキャン活動や防御措置を詳細にログに記録します
- カスタマイズ可能な設定: 各種設定ファイルを通じて、監視ポートの指定や防御アクションのカスタマイズが可能です
PortSentryのインストール
まずはパッケージのアップグレードをします。
$ sudo apt update
$ sudo apt upgrade
PortSentry
をインストールします。
$ sudo apt -y install portsentry
インストールすると注意書きが出ます。
「デフォルトでは/var/log/syslog
にログを出力します。設定ファイルは/etc/portsentry/portsentry.conf
です。」
ということが書かれています。
デフォルト状態の動作確認
PortSentry
をインストール後、デフォルト状態でポートスキャンを受けてみます。
- Kali Linux
$ nmap 192.168.178.18
Starting Nmap 7.94SVN ( https://nmap.org ) at 2024-06-05 01:41 JST
Stats: 0:00:04 elapsed; 0 hosts completed (0 up), 1 undergoing Ping Scan
Parallel DNS resolution of 1 host. Timing: About 0.00% done
Nmap scan report for 192.168.178.18
Host is up (0.00078s latency).
Not shown: 981 closed tcp ports (conn-refused)
PORT STATE SERVICE
1/tcp open tcpmux
21/tcp open ftp
22/tcp open ssh
79/tcp open finger
80/tcp open http
(省略)
Ubuntuでは/var/log/syslog
にログが出てきました。
$ sudo tail -f /var/log/syslog
Jun 5 01:41:29 ubuntu20 portsentry[814]: attackalert: Connect from host: 192.168.178.17/192.168.178.17 to TCP port: 32774
Jun 5 01:41:29 ubuntu20 portsentry[814]: attackalert: Host: 192.168.178.17 is already blocked. Ignoring
Jun 5 01:41:29 ubuntu20 portsentry[814]: attackalert: Connect from host: 192.168.178.17/192.168.178.17 to TCP port: 1080
Jun 5 01:41:29 ubuntu20 portsentry[814]: attackalert: Host: 192.168.178.17 is already blocked. Ignoring
Jun 5 01:41:29 ubuntu20 portsentry[814]: attackalert: Connect from host: 192.168.178.17/192.168.178.17 to TCP port: 12345
Jun 5 01:41:29 ubuntu20 portsentry[814]: attackalert: Host: 192.168.178.17 is already blocked. Ignoring
デフォルト状態なのでログを出力するのみで、Kali Linux側ではポートスキャンを成功させることができました。
PortSentryの設定
ポートスキャンを遮断するように設定を行います。
/etc/portsentry
配下に設定ファイルがあります。
$ ls -l
total 20
-rw-r--r-- 1 root root 11681 4月 3 2018 portsentry.conf
-rw-r--r-- 1 root root 467 6月 5 01:08 portsentry.ignore
-rw-r--r-- 1 root root 699 4月 3 2018 portsentry.ignore.static
/etc/portsentry.conf
ファイルを編集します。
BLOCK_UDP
,BLOCK_TCP
ではポートをブロックするかの設定を出来ます。
両方の値を1
にしてブロックできるようにします。
# 0 = Do not block UDP/TCP scans.
# 1 = Block UDP/TCP scans.
# 2 = Run external command only (KILL_RUN_CMD)
BLOCK_UDP="0" ← "1"に変更
BLOCK_TCP="0" ← "1"に変更
TCO_PORTS
,UDP_PORTS
では監視するポートの設定を行います。
# Use these if you just want to be aware:
TCP_PORTS="1,11,15,79,111,119,143,540,635,1080,1524,2000,5742,6667,12345,12346,20034,27665,31337,32771,32772,32773,32774,40421,49724,54320"
UDP_PORTS="1,7,9,69,161,162,513,635,640,641,700,37444,34555,31335,32770,32771,32772,32773,32774,31337,54321"
KILL_HOSTS_DENY
の設定を行います。
KILL_HOSTS_DENY - TCP ラッパーが使う
hosts.deny
ファイルに挿入する文字列の書式を定義します。$TARGET$
マクロで攻撃者の IP アドレスを追放するために再び必要とされます。PortSentry/README 日本語訳 より
KILL_HOSTS_DENY="ALL: $TARGET$ : DENY"
また、/etc/portsentry.ignore.static
を編集することでポートスキャンを許可するアドレスを指定できます。
ここで指定したアドレスは遮断対象から除外されます。
127.0.0.1/32
0.0.0.0
設定後、PortSentry
の再起動をします。
$ sudo /etc/init.d/portsentry restart
設定後の動作確認
ポートスキャンを遮断できるかテストします。
Kaliでポートスキャンを実行します。
$ nmap -Pn 192.168.178.18
Starting Nmap 7.94SVN ( https://nmap.org ) at 2024-06-05 10:06 JST
Nmap scan report for 192.168.178.18
Host is up (0.057s latency).
All 1000 scanned ports on 192.168.178.18 are in ignored states.
Not shown: 990 filtered tcp ports (no-response), 10 filtered tcp ports (host-unreach)
Nmap done: 1 IP address (1 host up) scanned in 18.90 seconds
ポートスキャンがブロックされ、どのポートも検出されませんでした。
参考サイト