LoginSignup
4
7

More than 5 years have passed since last update.

お手軽DeepPacketInspection

Last updated at Posted at 2015-06-23

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

スループットが高いものを分析する場合は専門のハードなりに頼らないとギガのネットをフルでみるのは辛いです。

4
7
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
4
7