Keijir Blog

やっぱハワイ行きたいよね

Wireshark(コマンド)の使い方まとめ

ネットワークに流れているパケットデータを表示して解析できるソフト

大量のパケットファイルを解析する際には、コマンドでの解析が欠かせない。

使い方を簡単に解説。

Wiresharkで使えるコマンド一覧

capinfos
dumpcap
editcap ←これを解説
mergecap←これを解説
rawshark
text2pcap
tshark ←これを解説
wireshark


コマンドのヘルプ表示(option -h)

$ tshark -h

インターフェースの表示 (option -D)

$ tshark -D

パケットのキャプチャ

Ctrl+Cまでパケット採取 10MBづつファイル作成

$ tshark -i 1 -n -B 1024 -b filesize:10240 -w filename.pcap

-i                 :インターフェースの番号 tshark -D で確認できる
-B 1024            :1024MBのバッファサイズ
-b filesize:10240 :10MBづつファイルを分割
-b files:1000      :1000個でサイクリックして保存
-w                 :保存するファイル名

※2017/06/20追記 オプションの説明

パケットのキャプチャ(リングバッファ)

Ctrl+Cまでパケット採取 10MBづつファイル作成 最新1000ファイル保持

$ tshark -i 1 -n -B 1024 -b filesize:10240 -b files:1000 -w filename.pcap

※2012/06/20追記 繰り返しキャプチャ

ファイルの分割

$ editcap filename.pcap newfilename -c paketsize

ファイルの結合

$ margecap -w outputfilename.pcap inputfile1 inputfile2 ...

パケット表示 (option -r)

$ tshark -r filename.pcap

パケット表示のフィルタリング (option -R)

$ tshark -r filename.pcap -R "Display Filter"
よく使うDisplay Filter の例
IPアドレス
 ip.addr==192.168.1.1
ポート番号
 tcp.port==80
resetフラグ
 tcp.flags.reset==1
ストリーム番号
 tcp.stream==123
Frameでバイトを指定(追記)
  frame[XXXX:YYYY] == ZZ:ZZ:ZZ.....

参考:Display Filter Reference
Wireshark(Ethereal)のDisplay Filter


パケット表示カスタム (option -o)

$ tshark -r filename.pcap ^
-o column.format:""Frame_Number","%Cus:frame.number","Frame_Time","%Cus:frame.time_relative", ^
"IP_src","%Cus:ip.src","IP_dst","%Cus:ip.dst","Protocol","%Cus:ip.proto", ^
"TCP_srcport","%Cus:tcp.srcport","TCP_dstport","%Cus:tcp.dstport","Info","%i""

カスタムの列には"%Cus:Display Filter"で記述
参考:Wireshark(Tshark)のpcapファイルを使ってみよう(No.4)

パケット表示カスタム (option -Tfields option -e)

$ tshark -r filename.pcap ^
-T fields ^
-E separator=';' ^
-e frame.number -e frame.time -e ip.src -e ip.dst -e tcp.srcport -e tcp.dstport -e col.Protocol -e col.Info

※2014/06/12 -e col.Protocol -e col.Info 追記

パケットの通信量表示(option -z io,stat,num)

$ tshark -r filename.pcap -q -z io,stat,60,"Filter1"...

※2012/09/25 Filter追記

パケットの通信量表示 絶対時間表示

Wireshark-dev New date format for the iostat dissector to merge multiple files
ここを参考にパッチを当てたWiresharkコンパイルすると-z,io,stat
を利用するときに -t で時間の書式が利用できるようになる。

$ tshark  -r data.cap -q -z io,stat,60,"Filter1"... -t ad

※2012/09/25 Filter追記

ファイルの抽出

$ tshark -r filename.pcap -R "Display Filter" -w output_filename.pcap

※2014/07/08追記

あとは出力をバッチなりシェルなりで加工すればOK!

以上