security
pcapファイルから特定のHTTPRequestとそのResponseを抜き出す
ネットワーク系のエンジニアなら誰しも数GBあるpcapファイルから特定のHTTPRequstとそのResponseだけを抜き出したくなることが何回かあるのではないでしょうか。
やり方を書いてみます。
まずpcapファイルがありますね。
このpcapファイルの中からこのブログ(www.uranari.io)にアクセスしているHTTPのRequestとそのResponseだけを抜き出して別のpcapに保存します。
まず以下のようにtsharkを使用することで特定のHTTPRequestのstream番号が分かります。
$ tshark -r pcap.pcapng -R http.host=="www.uranari.io" -T fields -e tcp.stream
-R の後ろのDisplayFilterの条件を変えることでそれぞれのFilterで引っかかったHTTPのstream番号が分かるはずです。
後は出力されたstream番号でpcapファイルをfilterすればfilterしたHTTPRequestとそのResponseだけを抜き出すことができます。
以下のようにすれば最初からfilterとして出力できる。
$ tshark -r pcap.pcapng -R http.host=="www.uranari.io" -T fields -e tcp.stream | sort -un | sed -e ':loop; N; $!b loop; s/\n/ or tcp.stream==/g'
7 or tcp.stream==8 or tcp.stream==9 or tcp.stream==10 or tcp.stream==11 or tcp.stream==12 or tcp.stream==48 or tcp.stream==49 or tcp.stream==52 or tcp.stream==53 or tcp.stream==54 or tcp.stream==61 or tcp.stream==67 or tcp.stream==68 or tcp.stream==74 or tcp.stream==75 or tcp.stream==76 or tcp.stream==77 or tcp.stream==79 or tcp.stream==80 or tcp.stream==85
先頭にtcp.stream==を追加しpcapをfilterして別のpcapファイルとして保存します。
$ tshark -r pcap.pcapng -R "tcp.stream==7 or tcp.stream==8 or tcp.stream==9 or tcp.stream==10 or t
cp.stream==11 or tcp.stream==12 or tcp.stream==48 or tcp.stream==49 or tcp.stream==52 or tcp.stream==53 or tcp.stream==54 o
r tcp.stream==61 or tcp.stream==67 or tcp.stream==68 or tcp.stream==74 or tcp.stream==75 or tcp.stream==76 or tcp.stream==7
7 or tcp.stream==79 or tcp.stream==80" -w filter.pcapng
$ ls
filter.pcapng pcap.pcapng
これでHTTPRequestHeaderのHostFieldに「www.uranari.io」を含むHTTPのRequestとResponseがfilter.pcapngとして保存できました。
ちなみに皆さんがこのサイトを見る際は強制的にHTTPSになるはずなのでそもそもfilterできません。
ふだん見てるサイトでやろうとしたけど今ではどこもHTTPSが標準なのでこれのために自分のアクセスだけ一時的にHTTPにしてCaptureしました。
参考サイト:
tshark オプションメモ