Skip to main content

Python multi-engine PCAP analyse kit.

Project description

PyPCAPKit

  The pcapkit project is an open source Python program focus on PCAP parsing and analysis, which works as a stream PCAP file extractor. With support of dictdumper, it shall support multiple output report formats.

Note that the whole project supports Python 3.4 or later.


About

pcapkit is an independent open source library, using only dictdumper as its formatted output dumper.

There is a project called jspcapy works on pcapkit, which is a command line tool for PCAP extraction but now DEPRECATED.

  Unlike popular PCAP file extractors, such as Scapy, dpkt, pyshark, and etc, pcapkit uses streaming strategy to read input files. That is to read frame by frame, decrease occupation on memory, as well as enhance efficiency in some way.

Module Structure

  In pcapkit, all files can be described as following six parts.

  • Interface (pcapkit.interface) -- user interface for the pcapkit library, which standardise and simplify the usage of this library
  • Foundation (pcapkit.foundation) -- synthesise file I/O and protocol analysis, coordinate information exchange in all network layers
  • Reassembly (pcapkit.reassembly) -- base on algorithms described in RFC 815, implement datagram reassembly of IP and TCP packets
  • IPSuite (pcapkit.ipsuite) -- collection of constructors for Internet Protocol Suite
  • Protocols (pcapkit.protocols) -- collection of all protocol family, with detail implementation and methods
  • Utilities (pcapkit.utilities) -- collection of four utility functions and classes
  • CoreKit (pcapkit.corekit) -- core utilities for pcapkit implementation
  • ToolKit (pcapkit.toolkit) -- capability tools for pcapkit implementation
  • DumpKit (pcapkit.dumpkit) -- dump utilities for pcapkit implementation

Engine Comparison

  Besides, due to complexity of pcapkit, its extraction procedure takes around 0.01 seconds per packet, which is not ideal enough. Thus, pcapkit introduced alternative extraction engines to accelerate this procedure. By now, pcapkit supports Scapy, DPKT, and PyShark. Plus, pcapkit supports two strategies of multiprocessing (server & pipeline). For more information, please refer to the document.

Engine Performance (seconds per packet)
dpkt 0.0003609057267506917
scapy 0.002443440357844035
default 0.017523006995519
pipeline 0.014550424114863079
server 0.04667099356651306
pyshark 0.0792640733718872

 

Installation

Note that pcapkit supports Python versions since 3.4

  Simply run the following to install the current version from PyPI:

pip install pypcapkit

  Or install the latest version from the git repository:

git clone https://github.com/JarryShaw/PyPCAPKit.git
cd pypcapkit
pip install -e .
# and to update at any time
git pull

  And since pcapkit supports various extraction engines, and extensive plug-in functions, you may want to install the optional ones:

# for DPKT only
pip install pypcapkit[DPKT]
# for Scapy only
pip install pypcapkit[Scapy]
# for PyShark only
pip install pypcapkit[PyShark]
# and to install all the optional packages
pip install pypcapkit[all]
# or to do this explicitly
pip install pypcapkit dpkt scapy pyshark

 

Usage

Documentation

Interfaces

NAME DESCRIPTION
extract extract a PCAP file
analyse analyse application layer packets
reassemble reassemble fragmented datagrams
trace trace TCP packet flows

Macros

Formats
NAME DESCRIPTION
JSON JavaScript Object Notation (JSON) format
PLIST macOS Property List (PLIST) format
TREE Tree-View text format
PCAP PCAP format
Layers
NAME DESCRIPTION
RAW no specific layer
LINK data-link layer
INET internet layer
TRANS transport layer
APP application layer
Engines
NAME DESCRIPTION
PCAPKit the default engine
MPServer the multiprocessing engine with server process strategy
MPPipeline the multiprocessing engine with pipeline strategy
DPKT the DPKT engine
Scapy the Scapy engine
PyShark the PyShark engine

Protocols

NAME DESCRIPTION
NoPayload No-Payload
Raw Raw Packet Data
ARP Address Resolution Protocol
Ethernet Ethernet Protocol
L2TP Layer Two Tunnelling Protocol
OSPF Open Shortest Path First
RARP Reverse Address Resolution Protocol
VLAN 802.1Q Customer VLAN Tag Type
AH Authentication Header
HIP Host Identity Protocol
HOPOPT IPv6 Hop-by-Hop Options
IP Internet Protocol
IPsec Internet Protocol Security
IPv4 Internet Protocol version 4
IPv6 Internet Protocol version 6
IPv6_Frag Fragment Header for IPv6
IPv6_Opts Destination Options for IPv6
IPv6_Route Routing Header for IPv6
IPX Internetwork Packet Exchange
MH Mobility Header
TCP Transmission Control Protocol
UDP User Datagram Protocol
HTTP Hypertext Transfer Protocol

  Documentation can be found in submodules of pcapkit. Or, you may find usage sample in the test folder. For further information, please refer to the source code -- the docstrings should help you :)

ps: help function in Python should always help you out.

CLI Usage

The following part was originally described in jspcapy, which is now deprecated and merged into this repository.

  As it shows in the help manual, it is quite easy to use:

$ pcapkit --help
usage: pcapkit [-h] [-V] [-o file-name] [-f format] [-j] [-p] [-t] [-a] [-v]
               [-F] [-E PKG] [-P PROTOCOL] [-L LAYER]
               input-file-name

PCAP file extractor and formatted exporter

positional arguments:
  input-file-name       The name of input pcap file. If ".pcap" omits, it will
                        be automatically appended.

optional arguments:
  -h, --help            show this help message and exit
  -V, --version         show program's version number and exit
  -o file-name, --output file-name
                        The name of input pcap file. If format extension
                        omits, it will be automatically appended.
  -f format, --format format
                        Print a extraction report in the specified output
                        format. Available are all formats supported by
                        dictdumper, e.g.: json, plist, and tree.
  -j, --json            Display extraction report as json. This will yield
                        "raw" output that may be used by external tools. This
                        option overrides all other options.
  -p, --plist           Display extraction report as macOS Property List
                        (plist). This will yield "raw" output that may be used
                        by external tools. This option overrides all other
                        options.
  -t, --tree            Display extraction report as tree view text. This will
                        yield "raw" output that may be used by external tools.
                        This option overrides all other options.
  -a, --auto-extension  If output file extension omits, append automatically.
  -v, --verbose         Show more information.
  -F, --files           Split each frame into different files.
  -E PKG, --engine PKG  Indicate extraction engine. Note that except default
                        engine, all other engines need support of corresponding
                        packages.
  -P PROTOCOL, --protocol PROTOCOL
                        Indicate extraction stops after which protocol.
  -L LAYER, --layer LAYER
                        Indicate extract frames until which layer.

  Under most circumstances, you should indicate the name of input PCAP file (extension may omit) and at least, output format (json, plist, or tree). Once format unspecified, the name of output file must have proper extension (*.json, *.plist, or *.txt), otherwise FormatError will raise.

  As for verbose mode, detailed information will print while extraction (as following examples). And auto-extension flag works for the output file, to indicate whether extensions should be appended.

 

Samples

Usage Samples

  As described in test folder, pcapkit is quite easy to use, with simply three verbs as its main interface. Several scenarios are shown as below.

  • extract a PCAP file and dump the result to a specific file (with no reassembly)

    import pcapkit
    # dump to a PLIST file with no frame storage (property frame disabled)
    plist = pcapkit.extract(fin='in.pcap', fout='out.plist', format='plist', store=False)
    # dump to a JSON file with no extension auto-complete
    json = pcapkit.extract(fin='in.cap', fout='out.json', format='json', extension=False)
    # dump to a folder with each tree-view text file per frame
    tree = pcapkit.extract(fin='in.pcap', fout='out', format='tree', files=True)
    
  • extract a PCAP file and fetch IP packet (both IPv4 and IPv6) from a frame (with no output file)

    >>> import pcapkit
    >>> extraction = pcapkit.extract(fin='in.pcap', nofile=True)
    >>> frame0 = extraction.frame[0]
    # check if IP in this frame, otherwise ProtocolNotFound will be raised
    >>> flag = pcapkit.IP in frame0
    >>> tcp = frame0[pcapkit.IP] if flag else None
    
  • extract a PCAP file and reassemble TCP payload (with no output file nor frame storage)

    import pcapkit
    # set strict to make sure full reassembly
    extraction = pcapkit.extract(fin='in.pcap', store=False, nofile=True, tcp=True, strict=True)
    # print extracted packet if HTTP in reassembled payloads
    for packet in extraction.reassembly.tcp:
        for reassembly in packet.packets:
            if pcapkit.HTTP in reassembly.protochain:
                print(reassembly.info)
    

CLI Samples

  The CLI (command line interface) of pcapkit has two different access.

  • through console scripts -- use command name pcapkit [...] directly (as shown in samples)
  • through Python module -- python -m pypcapkit [...] works exactly the same as above

Here are some usage samples:

  • export to a macOS Property List (Xcode has special support for this format)
$ pcapkit in --format plist --verbose
🚨Loading file 'in.pcap'
 - Frame   1: Ethernet:IPv6:ICMPv6
 - Frame   2: Ethernet:IPv6:ICMPv6
 - Frame   3: Ethernet:IPv4:TCP
 - Frame   4: Ethernet:IPv4:TCP
 - Frame   5: Ethernet:IPv4:TCP
 - Frame   6: Ethernet:IPv4:UDP
🍺Report file stored in 'out.plist'
  • export to a JSON file (with no format specified)
$ pcapkit in --output out.json --verbose
🚨Loading file 'in.pcap'
 - Frame   1: Ethernet:IPv6:ICMPv6
 - Frame   2: Ethernet:IPv6:ICMPv6
 - Frame   3: Ethernet:IPv4:TCP
 - Frame   4: Ethernet:IPv4:TCP
 - Frame   5: Ethernet:IPv4:TCP
 - Frame   6: Ethernet:IPv4:UDP
🍺Report file stored in 'out.json'
  • export to a text tree view file (without extension autocorrect)
$ pcapkit in --output out --format tree --verbose
🚨Loading file 'in.pcap'
 - Frame   1: Ethernet:IPv6:ICMPv6
 - Frame   2: Ethernet:IPv6:ICMPv6
 - Frame   3: Ethernet:IPv4:TCP
 - Frame   4: Ethernet:IPv4:TCP
 - Frame   5: Ethernet:IPv4:TCP
 - Frame   6: Ethernet:IPv4:UDP
🍺Report file stored in 'out'

 

TODO

  • specify Raw packet
  • interface verbs
  • review docstrings
  • merge jspcapy
  • write documentation
  • implement IP and MAC address containers
  • implement option list extractors
  • implement more protocols

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pypcapkit-0.13.0.post2.tar.gz (168.7 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pypcapkit-0.13.0.post2-py3.7.egg (508.1 kB view details)

Uploaded Egg

pypcapkit-0.13.0.post2-py3.6.egg (508.0 kB view details)

Uploaded Egg

pypcapkit-0.13.0.post2-py3.5.egg (509.6 kB view details)

Uploaded Egg

pypcapkit-0.13.0.post2-py3.4.egg (510.4 kB view details)

Uploaded Egg

pypcapkit-0.13.0.post2-pp35-none-macosx_10_14_x86_64.whl (250.0 kB view details)

Uploaded PyPymacOS 10.14+ x86-64

pypcapkit-0.13.0.post2-cp37-none-macosx_10_14_x86_64.whl (249.8 kB view details)

Uploaded CPython 3.7macOS 10.14+ x86-64

pypcapkit-0.13.0.post2-cp36-none-macosx_10_14_x86_64.whl (249.8 kB view details)

Uploaded CPython 3.6macOS 10.14+ x86-64

pypcapkit-0.13.0.post2-cp35-none-macosx_10_14_x86_64.whl (250.0 kB view details)

Uploaded CPython 3.5macOS 10.14+ x86-64

pypcapkit-0.13.0.post2-cp34-none-macosx_10_14_x86_64.whl (250.0 kB view details)

Uploaded CPython 3.4macOS 10.14+ x86-64

File details

Details for the file pypcapkit-0.13.0.post2.tar.gz.

File metadata

  • Download URL: pypcapkit-0.13.0.post2.tar.gz
  • Upload date:
  • Size: 168.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.1

File hashes

Hashes for pypcapkit-0.13.0.post2.tar.gz
Algorithm Hash digest
SHA256 73a3f0fb943ca4caba0de7f7d6001a6ac1ad0c58c27d8d0f0d17c244efabf42a
MD5 42727329d96d5f757d2e0786576f8c97
BLAKE2b-256 f307cb5a5b3ab924690fc431172d3df7012f8ddde69b147a12c5a8d238a76d57

See more details on using hashes here.

File details

Details for the file pypcapkit-0.13.0.post2-py3.7.egg.

File metadata

  • Download URL: pypcapkit-0.13.0.post2-py3.7.egg
  • Upload date:
  • Size: 508.1 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.1

File hashes

Hashes for pypcapkit-0.13.0.post2-py3.7.egg
Algorithm Hash digest
SHA256 240ebbcdc3b96f3d57d05ea4f28ed67459b035303215681bd84ca229cc09c73d
MD5 2cfe86c304c4416ed4b0225b69eba98c
BLAKE2b-256 d300c3b811ab8ed95d74b7525a64300be41ad6bd3ad6e9ed2ad0b37dafcd717d

See more details on using hashes here.

File details

Details for the file pypcapkit-0.13.0.post2-py3.6.egg.

File metadata

  • Download URL: pypcapkit-0.13.0.post2-py3.6.egg
  • Upload date:
  • Size: 508.0 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.1

File hashes

Hashes for pypcapkit-0.13.0.post2-py3.6.egg
Algorithm Hash digest
SHA256 9a68b059b85af7204bdd0ea2d91c4e694a378f95986c7a88ad186bac3c872d04
MD5 9533d1e08419372b74bf54c36bdee300
BLAKE2b-256 21d3112237ccb777b5cd8250e91040604e117ff674ce16b2761affa911a7681f

See more details on using hashes here.

File details

Details for the file pypcapkit-0.13.0.post2-py3.5.egg.

File metadata

  • Download URL: pypcapkit-0.13.0.post2-py3.5.egg
  • Upload date:
  • Size: 509.6 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.1

File hashes

Hashes for pypcapkit-0.13.0.post2-py3.5.egg
Algorithm Hash digest
SHA256 752d23f847eaab5c22369064580000880f032d9d7d2e4c179038b4a34438ea51
MD5 5a5f75797602a26ac9e60c730f48df4d
BLAKE2b-256 d3fb63436468452a17aa778bba9e974b6f3f3cc0a4d3fcbf0619e594e105423d

See more details on using hashes here.

File details

Details for the file pypcapkit-0.13.0.post2-py3.4.egg.

File metadata

  • Download URL: pypcapkit-0.13.0.post2-py3.4.egg
  • Upload date:
  • Size: 510.4 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.1

File hashes

Hashes for pypcapkit-0.13.0.post2-py3.4.egg
Algorithm Hash digest
SHA256 447f13b1c2bdffaf79aa09e1b13c6ebef71d1b62bcf45467b442e5bffb053103
MD5 d8ceb5fad72e10c6e1d0b74fd405df05
BLAKE2b-256 2acad6d6bfab910f38ed713077969bbb9b8d5fedefc00c273d64cadb8ea73940

See more details on using hashes here.

File details

Details for the file pypcapkit-0.13.0.post2-pp35-none-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: pypcapkit-0.13.0.post2-pp35-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 250.0 kB
  • Tags: PyPy, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.1

File hashes

Hashes for pypcapkit-0.13.0.post2-pp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 b31a8e3e9bfdda1739477cbc4329f2b25050168ff9246f17ce47801b0c0a65aa
MD5 bbdfd92528c86e4c1ade172e5cbfb26e
BLAKE2b-256 b2a9edb0ce6493814054cd4ccfee63cd3165c3b6187c194086d46a099d791e71

See more details on using hashes here.

File details

Details for the file pypcapkit-0.13.0.post2-cp37-none-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: pypcapkit-0.13.0.post2-cp37-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 249.8 kB
  • Tags: CPython 3.7, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.1

File hashes

Hashes for pypcapkit-0.13.0.post2-cp37-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 348bab4a7a69bd0748c30c185dbc279e504a8541366f11947ad950ceb253ba1a
MD5 0ac058f67dd822a1bab8df1945c27577
BLAKE2b-256 e6fce5c458100e93c261cc1b7e938b148ccd66b9da6803aada6610b33fbcc0a3

See more details on using hashes here.

File details

Details for the file pypcapkit-0.13.0.post2-cp36-none-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: pypcapkit-0.13.0.post2-cp36-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 249.8 kB
  • Tags: CPython 3.6, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.1

File hashes

Hashes for pypcapkit-0.13.0.post2-cp36-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 e1d778dd40737465d87d250a1435932b94976e9c5901c0576f17469b8ee69d43
MD5 30a0a786862e6bc861d07c6ea562a161
BLAKE2b-256 f93fb1a8ae84b05b27019c607a07b90a1ab574edc05414ecb29037373d60c4c0

See more details on using hashes here.

File details

Details for the file pypcapkit-0.13.0.post2-cp35-none-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: pypcapkit-0.13.0.post2-cp35-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 250.0 kB
  • Tags: CPython 3.5, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.1

File hashes

Hashes for pypcapkit-0.13.0.post2-cp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 653993401829c06a29e2ab3a8239037f1e1f380cb6544114c6f27256635b8855
MD5 a82233a49be871afd0082dd1380307d8
BLAKE2b-256 40c233cb86e342f5d0d75a4f063c644ab780c45413b4c4b7d3ec8737fe0d77bf

See more details on using hashes here.

File details

Details for the file pypcapkit-0.13.0.post2-cp34-none-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: pypcapkit-0.13.0.post2-cp34-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 250.0 kB
  • Tags: CPython 3.4, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.1

File hashes

Hashes for pypcapkit-0.13.0.post2-cp34-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 d2e8a7b83f6fd6832fd16f23a57d04040cb81b6f9893fee648fb32aed822ed78
MD5 4e60814c954609c825c6254c6b6dc626
BLAKE2b-256 e5a7df31bc020f18b583d641760e59c3bf0c614e58f5c5387e5bcf03074daf96

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page