pysharkはtsharkのラッパーなので、tsharkはインストールしてあることは前提
Install Pyshark
Gitからインストールすることを推奨 (2015/06/20)
下記のパッチがあったものがリリースされていないから
pipでインストールするとlxml.etree.XMLSyntaxError: internal error: Huge input lookup,
が起こる場合がある。
https://github.com/KimiNewt/pyshark/issues/75
$ git clone https://github.com/KimiNewt/pyshark.git
$ cd pyshark/src
$ python setup.py install
display_filterはtsharkの-R
オプションになる。
https://github.com/KimiNewt/pyshark/blob/master/src/pyshark/tshark/tshark.py#L73
import pyshark
for p in pyshark.FileCapture(pcap_file, display_filter='http'):
try:
if p.http.request_method == 'GET':
print(p.eth.src)
print(p.eth.dst)
print(p.ip.src)
print(p.ip.dst)
print(p.tcp.srcport)
print(p.tcp.dstport)
print(p.http.host)
print(p.http.request_full_uri)
print(p.sniff_time)
except AttributeError as e:
continue
注意
tsharkで見れないものは当然見れないので、全てがDPIできるわけではありません。
libpcapなりを使ってちゃんとパースするのが正確です。
すこしそれますが、tcpdump(libpcapを使っている。tsharkも同様)でetherを直で見張っても300Mbpsを超えるあたりでパケットがカーネルで落ちます。(ハードの性能にある程度依存はある。)
tcpdump -n -i eth0
スループットが高いものを分析する場合は専門のハードなりに頼らないとギガのネットをフルでみるのは辛いです。