2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

対象OSのポート(開閉状態)を外部からnmapで確認する方法

Last updated at Posted at 2021-03-29

環境

■ポートの開閉を確認したいOS
Amazon Linux2
■ポートの開閉を確認するためのOS
Ubuntu(ver 16.04.4)

初めに

Amzon linux2にiptablesを用いてポートにフィルタリングをかけます。
その後、他OS(Ubuntu)のNmapを利用してポートのフィルタリングを確認する方法を備忘録として記載します。
今回は、25、80、111番のポートにフィルタリングをかけます。

参照サイト
https://knowledge.sakura.ad.jp/4048/
https://qiita.com/Tocyuki/items/6d90a1ec4dd8e991a1ce

iptables、Nmapの導入

まず初めにAmazon Linux2にiptablesを導入します。
Amazon Linux2でiptablesを利用するにはiptables-servicesが必要ですのでyumを使ってインストールします。

yum -y install iptables-services

そうすると、/etc/sysconfigの配下に2つのファイルが生成されます。

/etc/sysconfig
$ ls | grep iptables
iptables
iptables-config

次に、Ubuntu(ポートの開閉を確認するためのOS)にNmapを導入します。

sudo apt-get install -y nmap

何も設定していない状態のAmazon Linux2のポート番号を確認

$ nmap localhost

Starting Nmap 6.40 ( http://nmap.org ) at 2021-03-29 12:47 JST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.0000030s latency).
Not shown: 995 closed ports
PORT    STATE SERVICE
22/tcp  open  ssh
25/tcp  open  smtp
80/tcp  open  http
111/tcp open  rpcbind
443/tcp open  https

Nmap done: 1 IP address (1 host up) scanned in 0.03 seconds

ポート番号22、25、80、111、443番がopenになっていることを確認。
25、80、111番のポートにフィルターをかけてみます。

ポート番号にフィルターをかける

viで先ほど生成されたiptablesファイルを編集します。

/etc/sysconfig
$ vi iptables


*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

デフォルトでは上記のような構造になっています。
iptablesには4つのテーブルが存在し、今回はfilterテーブルに記載します。

*filterとCOMMITの間に設定を記載します。

それでは25、80、111番のポートにフィルターをかける設定をしていきます。

/etc/sysconfig
$ vi iptables



-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j DROP   ・・・この行を追記
-A INPUT -p tcp -m state --state NEW -m tcp --dport 25 -j DROP   ・・・この行を追記
-A INPUT -p tcp -m state --state NEW -m tcp --dport 111 -j DROP   ・・・この行を追記

COMMIT

一番右に記載してあるDROPでポート番号にフィルターをかけて、パケットを通さない設定です。
対してACCEPTは通信可能な設定になっています。

viを保存してiptablesを再起動します。

/sbin/service iptables restart

UbuntuのNmapを利用して確認してみます。

$ nmap -p 80,22,111,25,443 -Pn Linux2の使用しているIPアドレス
Host discovery disabled (-Pn). All addresses will be marked 'up' and scan times will be slower.
Starting Nmap 7.91 ( https://nmap.org ) at 2021-03-29 16:02 JST
Nmap scan report for linux-practice.example.com (Linux2の使用しているIPアドレス)
Host is up (0.0013s latency).

PORT    STATE    SERVICE
22/tcp  open     ssh
25/tcp  filtered smtp
80/tcp  filtered http
111/tcp filtered rpcbind
443/tcp open     https

Nmap done: 1 IP address (1 host up) scanned in 1.25 seconds

これで25、80、111番にフィルターがかかっていることを確認できました。

以上になります。
ありがとうございました。

2
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?