LoginSignup
1
0
お題は不問!Qiita Engineer Festa 2024で記事投稿!
Qiita Engineer Festa20242024年7月17日まで開催中!

【セキュリティ入門】PortSentryでポートスキャンを遮断する

Posted at

概要

この記事ではPortSentryを使用したポートスキャンの遮断について解説します。

環境

  • PortSentryインストール
    • Ubuntu 20.04.5
  • 攻撃側
    • Kali Linux 2023

PortSentryとは

PortSentryはポートスキャンを検出し、遮断、保護するツールです。

主な機能

  1. ポートスキャン検出: PortSentryは、TCPおよびUDPのポートスキャンをリアルタイムで監視し、検出します
  2. アクティブな防御: ポートスキャンが検出された場合、PortSentryは攻撃元IPをブロックするための対策を自動的に実行します(例: iptablesによるブロック)
  3. ログ記録: 検出されたスキャン活動や防御措置を詳細にログに記録します
  4. カスタマイズ可能な設定: 各種設定ファイルを通じて、監視ポートの指定や防御アクションのカスタマイズが可能です

PortSentryのインストール

まずはパッケージのアップグレードをします。

$ sudo apt update
$ sudo apt upgrade

PortSentryをインストールします。

$ sudo apt -y install portsentry

インストールすると注意書きが出ます。
「デフォルトでは/var/log/syslogにログを出力します。設定ファイルは/etc/portsentry/portsentry.confです。」
ということが書かれています。

install info.png

デフォルト状態の動作確認

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にしてブロックできるようにします。

/etc/portsentry.conf
# 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では監視するポートの設定を行います。

/etc/portsentry.conf
# 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を編集することでポートスキャンを許可するアドレスを指定できます。
ここで指定したアドレスは遮断対象から除外されます。

/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

ポートスキャンがブロックされ、どのポートも検出されませんでした。

参考サイト

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0