LoginSignup
5
5

More than 5 years have passed since last update.

tshark でフィルタを組み立てるときは json 表示が便利

Posted at

トラブルシューティングなどでキャプチャしたパケットを詳細に見たいとき、(キャプチャするときには tcpdump を使っても)見るときは各種プロトコルを詳細にデコードできる Wireshark/tshark 等を使うことが多いと思います。

Wireshark は GUI の上からパケットの各フィールドを選択して、"Apply as Filter" などのコマンドを選ぶことで簡単にフィルタを組み立てることができますが、環境によっては pcap ファイルを簡単に Wireshark の使える場所まで持っていけない場合もあるかと思います。
ただ、その場合に使える tshark の 詳細表示モード (-V) から Filter として使えるフィールド名に読み替えるのはちょっと面倒です。

$ tshark -nr test.pcap -V | less
<中略>
Internet Control Message Protocol
    Type: 8 (Echo (ping) request) 
    Code: 0
    Checksum: 0xc536 [correct]
    [Checksum Status: Good]
    Identifier (BE): 28260 (0x6e64)        ← が 28260 であることを表現するフィルタは 
    Identifier (LE): 25710 (0x646e)          icmp.ident == 28260 だけど、覚えていないと
    Sequence number (BE): 1 (0x0001)         なかなか出てこない
    Sequence number (LE): 256 (0x0100)
    Timestamp from icmp data: Oct 24, 2018 12:16:36.000000000 UTC
    [Timestamp from icmp data (relative): 0.381598000 seconds]
    Data (48 bytes)
<省略>

そういうときは tshark 2.1.0 以降のみですが、-T json で json 出力するとわかりやすくなります。
json のキーがそのままフィルタに使えるフィールド名になっているのです。

$ tshark -nr test.pcap -T json | less
<中略>
        "icmp": {
          "icmp.type": "8",
          "icmp.code": "0",
          "icmp.checksum": "0x0000c536",
          "icmp.checksum.status": "1",
          "icmp.ident": "28260",
          "icmp.ident": "25710",
          "icmp.seq": "1",
          "icmp.seq_le": "256",
          "icmp.data_time": "Oct 24, 2018 12:16:36.000000000 UTC",
          "icmp.data_time_relative": "0.381598000",
<省略>

これで、tshark しか使えない環境でも簡単に Filter を組み立てることができます。
-V でざっくり眺めて -T json でフィルタに使うフィールド名を確認して、とやると tshark だけで完結して楽です。

$ tshark -nr test.pcap -Y "icmp.ident == 28260"
   41  10.032921    127.0.0.1 → 127.0.0.1    ICMP 98 Echo (ping) request  id=0x6e64, seq=1/256, ttl=64
   42  10.032985    127.0.0.1 → 127.0.0.1    ICMP 98 Echo (ping) reply    id=0x6e64, seq=1/256, ttl=64 (request in 41)
   51  11.032635    127.0.0.1 → 127.0.0.1    ICMP 98 Echo (ping) request  id=0x6e64, seq=2/512, ttl=64
   52  11.032711    127.0.0.1 → 127.0.0.1    ICMP 98 Echo (ping) reply    id=0x6e64, seq=2/512, ttl=64 (request in 51)
   53  12.032624    127.0.0.1 → 127.0.0.1    ICMP 98 Echo (ping) request  id=0x6e64, seq=3/768, ttl=64
   54  12.032693    127.0.0.1 → 127.0.0.1    ICMP 98 Echo (ping) reply    id=0x6e64, seq=3/768, ttl=64 (request in 53)
   63  13.032637    127.0.0.1 → 127.0.0.1    ICMP 98 Echo (ping) request  id=0x6e64, seq=4/1024, ttl=64
   64  13.032726    127.0.0.1 → 127.0.0.1    ICMP 98 Echo (ping) reply    id=0x6e64, seq=4/1024, ttl=64 (request in 63)
<省略>
5
5
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
5
5