あいしんくいっとオススメ商品レビュー

tcpdumpの使い方。コマンドラインでパケット確認出来て便利なコマンド。

この記事には広告を含む場合があります。

記事内で紹介する商品を購入することで、当サイトに売り上げの一部が還元されることがあります。

本記事対象
Linuxでtcpdumpの基本的な使い方が知りたい方

ども。あいしんくいっと(@ithinkitnet)です。

Linuxには、ネットワーク上の通信データを簡単にキャプチャ出来る「tcpdump」という便利なコマンドが標準搭載されてます。

Windows用のアプリであれば、WireSharkといったアプリみたいなものでしょうか。ただ、WireSharkはGUIベースで視覚的に見やすいですが、このtcpdumpはCUIベースなので正直見にくいです・・・。とは言え、コマンドで簡単に扱うことが出来ますので非常に便利です。

それではtcpdumpの使い方を書いておきたいと思います。

tcpdumpの使い方。コマンドラインでパケット確認出来て便利なコマンド。

tcpdumpは非常に便利なコマンドなのですが、利用出来るオプションがたくさんあります。
全て紹介するのは困難(面倒w)なので、基本的な使い方からよく使いそうな利用例をいくつか紹介しておきたいと思います。

tcpdumpの基本的な使い方

tcpdumpの基本的な使い方は以下の通り。

tcpdump -i <NIC名> <オプション多数>

-i ・・・ NICが複数ある場合、監視するデバイスを指定
-n ・・・ 名前解決せず、IPベースで出力

例) tcpdump -ni eth0 src host 192.168.11.101

上記の場合、自身のNIC「eth0」を通って192.168.11.101宛に送信されるパケットのみが表示されます。

プロトコル除外

基本的にsshで繋いでtcpdumpコマンドを発行することが多いので、とりあえずSSHは除外で。あと、ARPも必要無いので除外で。
例えば、普通にtcpdumpを実行するとssh接続(22番ポート)まで出力されます。


# tcpdump -ni eth0
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
11:40:54.047851 IP 192.168.11.6.53963 > 192.168.11.101.22: Flags [.], ack 2648443528, win 61, length 0
11:40:54.048036 IP 192.168.11.101.22 > 192.168.11.6.53963: Flags [P.], seq 1:513, ack 0, win 566, length 512
11:40:54.048782 IP 192.168.11.101.22 > 192.168.11.6.53963: Flags [P.], seq 513:705, ack 0, win 566, length 192

sshは除外したい。そんな時のコマンド例はこんな感じ。

tcpdump -ni eth0 not port ssh

これでssh接続は除外されます。


# tcpdump -ni eth0 not port ssh
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
11:43:37.031543 IP 192.168.11.101.51356 > 54.219.151.195.443: Flags [P.], seq 3060118840:3060118887, ack 681560386, win 457, options [nop,nop,TS val 1539287986 ecr 903673981], length 47

良く使うのはsshとarpの除外ですかねー。


# tcpdump -ni eth0 not port ssh and not arp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
11:45:05.364608 IP 192.168.11.2.62465 > 239.255.255.250.1900: UDP, length 125
11:45:05.462250 IP 192.168.11.2.62465 > 239.255.255.250.1900: UDP, length 125
11:45:05.566967 IP 192.168.11.2.62465 > 239.255.255.250.1900: UDP, length 125

面倒であれば、grepでパイプして除外するのも一つの手かも知れません。邪道でしょうけどね(笑)


# tcpdump -ni eth0 | grep -v 22
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
11:46:10.561010 IP 192.168.11.123 > 192.168.11.101: ICMP echo request, id 18698, seq 1, length 64
11:46:10.561182 IP 192.168.11.101 > 192.168.11.123: ICMP echo reply, id 18698, seq 1, length 64

特定のプロトコルのみ

特定のプロトコルしかみたくない。そんな時は以下。

  • http接続のみ
  • 
    # tcpdump -ni venet0 port http
    tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
    listening on venet0, link-type LINUX_SLL (Linux cooked), capture size 65535 bytes
    11:53:20.727625 IP 202.169.66.194.57263 > 183.181.22.44.http: Flags [.], ack 307788494, win 4096, options [nop,nop,TS val 765681057 ecr 3806378223,nop,nop,sack 3 {10137:11585}{7241:8689}{1449:5793}], length 0
    
  • httpかhttps接続(OR)
  • 
    # tcpdump -ni venet0 port http or port https
    tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
    listening on venet0, link-type LINUX_SLL (Linux cooked), capture size 65535 bytes
    11:54:34.648644 IP 183.181.22.44.http > 122.21.70.18.58213: Flags [P.], seq 1945525994:1945526310, ack 3357527480, win 28408, length 316
    
  • httpとhttps接続(AND)
  • 
    # tcpdump -ni venet0 port http and port https
    tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
    listening on venet0, link-type LINUX_SLL (Linux cooked), capture size 65535 bytes
    11:54:34.648644 IP 183.181.22.44.http > 122.21.70.18.58213: Flags [P.], seq 1945525994:1945526310, ack 3357527480, win 28408, length 316
    

    送信元、送信先で絞る

    特定のホストへ送信されるパケット、特定のホストから受信するパケットでそれぞれ絞る。
    そんな時のコマンド例は以下。

  • 送信元で絞る場合(src)
  • 
    # tcpdump -ni eth0 src host 192.168.11.101
    tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
    listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
    11:47:25.176728 IP 192.168.11.101.22 > 192.168.11.6.53963: Flags [P.], seq 2648695464:2648695976, ack 3039006084, win 566, length 512
    11:47:25.177576 IP 192.168.11.101.22 > 192.168.11.6.53963: Flags [P.], seq 512:736, ack 1, win 566, length 224
    
  • 送信先で絞る場合(dst)
  • 
    # tcpdump -ni eth0 dst host 192.168.11.123
    tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
    listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
    11:49:43.988440 IP 192.168.11.101 > 192.168.11.123: ICMP echo reply, id 18699, seq 1, length 64
    11:49:44.987262 IP 192.168.11.101 > 192.168.11.123: ICMP echo reply, id 18699, seq 2, length 64
    11:49:45.986614 IP 192.168.11.101 > 192.168.11.123: ICMP echo reply, id 18699, seq 3, length 64
    

    ファイル出力

    tcpdumpの結果は標準出力されます。
    バッ〜〜〜っと流れていくので到底追い切れません。
    なので、そんな時は-wオプションでファイル出力するか、リダイレクトするかで出力すると良いと思います。

  • -wオプションでファイル出力
  • 
    # tcpdump -ni eth0 -w tcpdump.txt
    tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
    ^C2 packets captured
    4 packets received by filter
    0 packets dropped by kernel
    
  • リダイレクトで出力
  • 
    # tcpdump -ni eth0 > tcpdump.txt
    tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
    listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
    

    ちなみに-wオプションでファイル出力したものはWindowsのWireSharkで開いて確認することが可能となります。

    tcpdumpはネットワークのトラブルシューティングに役立つので、少なくとも今回紹介した使い方位は知っておくと何かの役に立つかもしれません。

    以上、あいしんくいっとでした(*´Д`)時

    コメントを残す

    メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

    日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)