2
1

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.

Raspberry PIでポートスキャン

Last updated at Posted at 2021-04-04

概要

今回は現在使っているLinuxにオープンされているポートを確認するため、
nmapツールをインストールし、表示内容を確認します。
便利なツールですので、紹介いたします。

事前インストール

次のコマンドでnmapツールをインストールします。

sudo apt-get install nmap

実行結果

nmapツールをインストールしている画面です。

pi@raspberrypi:~ $ sudo apt-get install nmap
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  gconf-service gconf2-common libgconf-2-4
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  liblinear3 liblua5.3-0 libpcap0.8 nmap-common
Suggested packages:
  liblinear-tools liblinear-dev ncat ndiff zenmap
The following NEW packages will be installed:
  liblinear3 liblua5.3-0 libpcap0.8 nmap nmap-common
0 upgraded, 5 newly installed, 0 to remove and 1 not upgraded.
Need to get 5,919 kB of archives.
After this operation, 26.0 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://ftp.tsukuba.wide.ad.jp/Linux/raspbian/raspbian buster/main armhf liblinear3 armhf 2.1.0+dfsg-4 [35.5 kB]
Get:2 http://ftp.tsukuba.wide.ad.jp/Linux/raspbian/raspbian buster/main armhf liblua5.3-0 armhf 5.3.3-1.1 [94.4 kB]
Get:3 http://ftp.tsukuba.wide.ad.jp/Linux/raspbian/raspbian buster/main armhf libpcap0.8 armhf 1.8.1-6 [124 kB]
Get:4 http://ftp.tsukuba.wide.ad.jp/Linux/raspbian/raspbian buster/main armhf nmap-common all 7.70+dfsg1-6+deb10u1 [3,898 kB]
Get:4 http://ftp.tsukuba.wide.ad.jp/Linux/raspbian/raspbian buster/main armhf nmap-common all 7.70+dfsg1-6+deb10u1 [3,898 kB]
Get:4 http://ftp.tsukuba.wide.ad.jp/Linux/raspbian/raspbian buster/main armhf nmap-common all 7.70+dfsg1-6+deb10u1 [3,898 kB]
Get:4 http://ftp.tsukuba.wide.ad.jp/Linux/raspbian/raspbian buster/main armhf nmap-common all 7.70+dfsg1-6+deb10u1 [3,898 kB]
Get:5 http://ftp.tsukuba.wide.ad.jp/Linux/raspbian/raspbian buster/main armhf nmap armhf 7.70+dfsg1-6+deb10u1 [1,766 kB]
Fetched 2,621 kB in 1min 43s (25.4 kB/s)
Selecting previously unselected package liblinear3:armhf.
(Reading database ... 163825 files and directories currently installed.)
Preparing to unpack .../liblinear3_2.1.0+dfsg-4_armhf.deb ...
Unpacking liblinear3:armhf (2.1.0+dfsg-4) ...
Selecting previously unselected package liblua5.3-0:armhf.
Preparing to unpack .../liblua5.3-0_5.3.3-1.1_armhf.deb ...
Unpacking liblua5.3-0:armhf (5.3.3-1.1) ...
Selecting previously unselected package libpcap0.8:armhf.
Preparing to unpack .../libpcap0.8_1.8.1-6_armhf.deb ...
Unpacking libpcap0.8:armhf (1.8.1-6) ...
Selecting previously unselected package nmap-common.
Preparing to unpack .../nmap-common_7.70+dfsg1-6+deb10u1_all.deb ...
Unpacking nmap-common (7.70+dfsg1-6+deb10u1) ...
Selecting previously unselected package nmap.
Preparing to unpack .../nmap_7.70+dfsg1-6+deb10u1_armhf.deb ...
Unpacking nmap (7.70+dfsg1-6+deb10u1) ...
Setting up libpcap0.8:armhf (1.8.1-6) ...
Setting up nmap-common (7.70+dfsg1-6+deb10u1) ...
Setting up liblinear3:armhf (2.1.0+dfsg-4) ...
Setting up liblua5.3-0:armhf (5.3.3-1.1) ...
Setting up nmap (7.70+dfsg1-6+deb10u1) ...
Processing triggers for man-db (2.8.5-2) ...
Processing triggers for libc-bin (2.28-10+rpi1) ...
pi@raspberrypi:~ $

ポートスキャン

次のコマンドでLinuxのポートをスキャンします。

nmap localhost
nmap ip address

実行結果

ローカルのポートスキャンと他のRaspbery PIのポートをスキャンします。
ポートの例)
80:HTTPポート(WEB Server:Apache, Nginx等)、22:SSH,SFTPポート、3306:Mysqlポート

# ローカルのポートスキャン結果
pi@raspberrypi:~ $ nmap localhost
Starting Nmap 7.70 ( https://nmap.org ) at 2021-04-04 09:26 BST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.0034s latency).
Other addresses for localhost (not scanned): ::1
Not shown: 998 closed ports
PORT    STATE SERVICE
22/tcp  open  ssh
631/tcp open  ipp

Nmap done: 1 IP address (1 host up) scanned in 1.96 seconds
pi@raspberrypi:~ $

# 他のRaspbery PIのポートスキャン結果
pi@raspberrypi:~ $ nmap 192.168.1.XX
Starting Nmap 7.70 ( https://nmap.org ) at 2021-04-04 09:27 BST
Nmap scan report for 192.168.1.xx
Host is up (0.0057s latency).
Not shown: 991 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
80/tcp   open  http
139/tcp  open  netbios-ssn
445/tcp  open  microsoft-ds
3306/tcp open  mysql

Nmap done: 1 IP address (1 host up) scanned in 2.13 seconds
pi@raspberrypi:~ $

その他

nmapツールを使わない場合は、「netstat -na|grep LISTEN」コマンドで確認は可能です。
「LISTEN」と記載されている行がオープンされているポートです。
※ローカルのポートしか確認ができないです。他のPCのIPアドレス指定ができないです。

pi@raspberrypi:~ $ netstat -na|grep LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN
tcp6       0      0 :::22                   :::*                    LISTEN
tcp6       0      0 ::1:631                 :::*                    LISTEN
unix  2      [ ACC ]     STREAM     LISTENING     158221   /run/cups/cups.sock
unix  2      [ ACC ]     STREAM     LISTENING     17179    /run/user/1000/pcmanfm-socket--0
unix  2      [ ACC ]     SEQPACKET  LISTENING     7454     /run/udev/control
unix  2      [ ACC ]     STREAM     LISTENING     15354    @/tmp/.X11-unix/X0
unix  2      [ ACC ]     STREAM     LISTENING     7463     /run/systemd/fsck.progress
unix  2      [ ACC ]     STREAM     LISTENING     92973    /run/systemd/private
unix  2      [ ACC ]     STREAM     LISTENING     15355    /tmp/.X11-unix/X0
unix  2      [ ACC ]     STREAM     LISTENING     7474     /run/systemd/journal/stdout
unix  2      [ ACC ]     STREAM     LISTENING     16475    /tmp/ssh-hJEKNV5hXt4X/agent.485
unix  2      [ ACC ]     STREAM     LISTENING     16897    /tmp/ssh-KSAtbwvKEFLf/agent.565
unix  2      [ ACC ]     STREAM     LISTENING     11870    /var/run/dhcpcd.sock
unix  2      [ ACC ]     STREAM     LISTENING     11872    /var/run/dhcpcd.unpriv.sock
unix  2      [ ACC ]     STREAM     LISTENING     90473    /run/user/1000/menu-cached-:0
unix  2      [ ACC ]     STREAM     LISTENING     11177    /var/run/dbus/system_bus_socket
unix  2      [ ACC ]     STREAM     LISTENING     11190    /run/thd.socket
unix  2      [ ACC ]     STREAM     LISTENING     11193    /run/avahi-daemon/socket
unix  2      [ ACC ]     STREAM     LISTENING     17875    /run/user/1000/.lxterminal-socket-:0.0
unix  2      [ ACC ]     STREAM     LISTENING     16104    /run/user/1000/systemd/private
unix  2      [ ACC ]     STREAM     LISTENING     16112    /run/user/1000/gnupg/S.dirmngr
unix  2      [ ACC ]     STREAM     LISTENING     16115    /run/user/1000/gnupg/S.gpg-agent.extra
unix  2      [ ACC ]     STREAM     LISTENING     16117    /run/user/1000/gnupg/S.gpg-agent.ssh
unix  2      [ ACC ]     STREAM     LISTENING     16119    /run/user/1000/pulse/native
unix  2      [ ACC ]     STREAM     LISTENING     16122    /run/user/1000/gnupg/S.gpg-agent
unix  2      [ ACC ]     STREAM     LISTENING     16124    /run/user/1000/gnupg/S.gpg-agent.browser
unix  2      [ ACC ]     STREAM     LISTENING     16126    /run/user/1000/bus
pi@raspberrypi:~ $

終わりに

Raspbery PIのポートが開けてないことで外部からアクセスができないことやサービスのポートがオープンされていないかなどを調べるには便利なツールだと思います。
今までは「netstat -na|grep LISTEN」コマンドを使っていましたが、これからはこのコマンドを使います。

2
1
1

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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?