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

nmapはrootで利用?

Posted at

nmapを管理者権限で利用しないと動作しない?

Mac OSにて、nmapのarp scanを実施していた時のメモ。root権限で使わないと正しい結果が得られないことあり。

arp scan

ARPには、ブロードキャストパケットを送信し、IPアドレスと紐づくMacアドレスを得る役割がある。これを利用して、”arp scan”は、ネットワーク内の機器の存在を見つけるものである。nmapのコマンド的には下記となる。

$ nmap -sn 192.168.20.0/24

この例では、ネットワーク”192.168.20.0/24”内の254台のネットワーク機器が存在するかを見つける。

non-root

”arp scan”を実施中、接続されている、ある特定の機器が見つからなかった。下記は連続してnmapを実行した時の結果。

$ nmap -sn 192.168.2.24/29
Starting Nmap 7.92 ( https://nmap.org ) at 2024-05-12 09:53 JST
Nmap scan report for 192.168.2.29
Host is up (1.0s latency).
Nmap done: 8 IP addresses (1 host up) scanned in 9.14 seconds
$
$ nmap -sn 192.168.2.24/29
Starting Nmap 7.92 ( https://nmap.org ) at 2024-05-12 09:53 JST
Nmap done: 8 IP addresses (0 hosts up) scanned in 2.42 seconds

”192.168.2.29”なる機器、最初は見つかっていたが、後に見つからなくなる。Wiresharkデータは下記。
nonSudo.png
なぜか、”192.168.2.29”への問い合わせを実施していない。そこで、試しに、ARPテーブルを削除してみた。

$ sudo arp -d -a
Password:
192.168.2.29 (192.168.2.29) deleted
192.168.2.41 (192.168.2.41) deleted
192.168.10.1 (192.168.10.1) deleted
192.168.10.107 (192.168.10.107) deleted
224.0.0.251 (224.0.0.251) deleted
239.255.255.250 (239.255.255.250) deleted

削除直後は正常。

$ nmap -sn 192.168.2.24/29
Starting Nmap 7.92 ( https://nmap.org ) at 2024-05-12 09:58 JST
Nmap scan report for 192.168.2.29
Host is up (1.0s latency).
Nmap done: 8 IP addresses (1 host up) scanned in 6.13 seconds

Wiresharkデータは下記。
nonSudoAfterRemovalARPtable.png
見つかっているが、”192.168.2.29”への問い合わせが見えない。nmapは他の情報から表示するのであろうか、、OSのふるまいか??

しかし、時間がたつと、見つからなくなる。

$ nmap -sn 192.168.2.24/29
Starting Nmap 7.92 ( https://nmap.org ) at 2024-05-12 10:01 JST
Nmap done: 8 IP addresses (0 hosts up) scanned in 1.31 seconds

全くよくわからず。

rootとnon-root

次にroot権限も含めて実行。まずは、rootで実行。

$ sudo nmap -sn 192.168.2.24/29
Password:
Starting Nmap 7.92 ( https://nmap.org ) at 2024-05-12 10:06 JST
Nmap scan report for 192.168.2.29
Host is up (0.00024s latency).
MAC Address: 9C:GG:00:XX:YY:ZZ (Unknown)
Nmap done: 8 IP addresses (1 host up) scanned in 1.40 seconds

見つかる。次に、non-rootで実行。

$ nmap -sn 192.168.2.24/29
Starting Nmap 7.92 ( https://nmap.org ) at 2024-05-12 10:06 JST
Nmap scan report for 192.168.2.29
Host is up (1.0s latency).
Nmap done: 8 IP addresses (1 host up) scanned in 6.13 seconds
$
$ nmap -sn 192.168.2.24/29
Starting Nmap 7.92 ( https://nmap.org ) at 2024-05-12 10:06 JST
Nmap done: 8 IP addresses (0 hosts up) scanned in 1.32 seconds

最初は見つかるが、そのうち見つからなくなる。

root

rootで連続実行。

$ sudo nmap -sn 192.168.2.24/29
Password:
Starting Nmap 7.92 ( https://nmap.org ) at 2024-05-12 10:27 JST
Nmap scan report for 192.168.2.29
Host is up (0.00024s latency).
MAC Address: 9C:GG:00:XX:YY:ZZ (Unknown)
Nmap done: 8 IP addresses (1 host up) scanned in 1.39 seconds
$ 
$ sudo nmap -sn 192.168.2.24/29
Starting Nmap 7.92 ( https://nmap.org ) at 2024-05-12 10:27 JST
Nmap scan report for 192.168.2.29
Host is up (0.00025s latency).
MAC Address: 9C:GG:00:XX:YY:ZZ (Unknown)
Nmap done: 8 IP addresses (1 host up) scanned in 1.41 seconds
$ 
$ sudo nmap -sn 192.168.2.24/29
Starting Nmap 7.92 ( https://nmap.org ) at 2024-05-12 10:27 JST
Nmap scan report for 192.168.2.29
Host is up (0.00025s latency).
MAC Address: 9C:GG:00:XX:YY:ZZ (Unknown)
Nmap done: 8 IP addresses (1 host up) scanned in 1.39 seconds

必ず見つかるようだ。Wiresharkのデータは下記。
Sudoのコピー.png
”192.168.2.29”への問い合わせあり。何度も実行したが、必ず問い合わせを実施し、結果も表示される。

なお、Linux(ubuntu)でも同じような傾向が見られた。

scapyでもトライ

Lowレベルで確実にARPパケットを創出するには、scapyがいいだろう。

from scapy.all import *

intf = 'en0'
range = "192.168.2.24/29"

packet = Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=range)
ans,uans = srp(packet, iface=intf, timeout=1)
ans.summary()

Mac OSでの実行結果は下記となる。

$ python arp_scan3.py 
Begin emission:
Finished sending 8 packets.
......*...
Received 10 packets, got 1 answers, remaining 7 packets
Ether / ARP who has 192.168.2.29 says 192.168.2.1 ==> Ether / ARP is at 9c:XX:YY:ZZ:54:81 says 192.168.2.29 / Padding

ただし、ubuntuでは上記実行にはroot権限が必要であった。

最後に

nmapをrootで実行する点については、下記情報を参考にされたし。

root権限をもたないと arpを出さない理由がよくわからずであるが、rootで利用すべき、、、というのが結論であった。

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