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.14.3.tar.gz (170.1 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.14.3-py3.8.egg (512.8 kB view details)

Uploaded Egg

pypcapkit-0.14.3-py3.7.egg (511.8 kB view details)

Uploaded Egg

pypcapkit-0.14.3-py3.6.egg (519.0 kB view details)

Uploaded Egg

pypcapkit-0.14.3-py3.5.egg (524.7 kB view details)

Uploaded Egg

pypcapkit-0.14.3-py3.4.egg (519.8 kB view details)

Uploaded Egg

pypcapkit-0.14.3-pp36-none-any.whl (251.3 kB view details)

Uploaded PyPy

pypcapkit-0.14.3-pp35-none-any.whl (251.6 kB view details)

Uploaded PyPy

pypcapkit-0.14.3-cp38-none-any.whl (251.3 kB view details)

Uploaded CPython 3.8

pypcapkit-0.14.3-cp37-none-any.whl (251.3 kB view details)

Uploaded CPython 3.7

pypcapkit-0.14.3-cp36-none-any.whl (251.3 kB view details)

Uploaded CPython 3.6

pypcapkit-0.14.3-cp35-none-any.whl (251.6 kB view details)

Uploaded CPython 3.5

pypcapkit-0.14.3-cp34-none-any.whl (251.6 kB view details)

Uploaded CPython 3.4

File details

Details for the file pypcapkit-0.14.3.tar.gz.

File metadata

  • Download URL: pypcapkit-0.14.3.tar.gz
  • Upload date:
  • Size: 170.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4

File hashes

Hashes for pypcapkit-0.14.3.tar.gz
Algorithm Hash digest
SHA256 5d4ebd21b187572be0d062231d085b6bf90fe49e6dd10a6c04db0657b1d8cf09
MD5 c9266bd188a312b27a7d965f10454799
BLAKE2b-256 60e4bf83d0e6ff7356675e640dce42bf0a6829d7a6fc946bf05409348713bab1

See more details on using hashes here.

File details

Details for the file pypcapkit-0.14.3-py3.8.egg.

File metadata

  • Download URL: pypcapkit-0.14.3-py3.8.egg
  • Upload date:
  • Size: 512.8 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4

File hashes

Hashes for pypcapkit-0.14.3-py3.8.egg
Algorithm Hash digest
SHA256 b82d55c4e69c1ab11624e09755a124f9b51dba175185d99dfeb0754e63f15843
MD5 97e63a2910cce25482cdcc31148d722a
BLAKE2b-256 cfc99bf8e2a1daa095d461e973d4eda255c712acb5721d9df654cc7fae91c7d8

See more details on using hashes here.

File details

Details for the file pypcapkit-0.14.3-py3.7.egg.

File metadata

  • Download URL: pypcapkit-0.14.3-py3.7.egg
  • Upload date:
  • Size: 511.8 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4

File hashes

Hashes for pypcapkit-0.14.3-py3.7.egg
Algorithm Hash digest
SHA256 4b977e8f0c9fc9febbddf299fec2daa9318aac9588e9fc4dd5296927a3559d1a
MD5 b1992d7a033ee9dc62262bec97d2b755
BLAKE2b-256 eb05763cf6d093c90ef9d7801860090824dbb933b28847eb4637cbe9cc641576

See more details on using hashes here.

File details

Details for the file pypcapkit-0.14.3-py3.6.egg.

File metadata

  • Download URL: pypcapkit-0.14.3-py3.6.egg
  • Upload date:
  • Size: 519.0 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4

File hashes

Hashes for pypcapkit-0.14.3-py3.6.egg
Algorithm Hash digest
SHA256 74b21772ef0569dbd6b5381c5ee07e95d5e582164c784ce19b3391b1809365b0
MD5 c44176cef69e36ac47684adf9b6329ac
BLAKE2b-256 085193ae0cbc7e6b1b9b5d4edcd7618a0b1876ed053f1a2749caa393d477b518

See more details on using hashes here.

File details

Details for the file pypcapkit-0.14.3-py3.5.egg.

File metadata

  • Download URL: pypcapkit-0.14.3-py3.5.egg
  • Upload date:
  • Size: 524.7 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4

File hashes

Hashes for pypcapkit-0.14.3-py3.5.egg
Algorithm Hash digest
SHA256 427ceec97111b257974676501a5317ca54ddc18ba18b3e1e7cc6f82b558d77ef
MD5 d156d890c28fc9664ce61fe73f395b43
BLAKE2b-256 7c2bae457fb0a79f27f751b872e1ccb5f74130aa0e7dd4fc4c0a161a4d242a96

See more details on using hashes here.

File details

Details for the file pypcapkit-0.14.3-py3.4.egg.

File metadata

  • Download URL: pypcapkit-0.14.3-py3.4.egg
  • Upload date:
  • Size: 519.8 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4

File hashes

Hashes for pypcapkit-0.14.3-py3.4.egg
Algorithm Hash digest
SHA256 704d0d15b79f558793844812b2a866e05027bcfc00cfff33e0087a626ccf5c31
MD5 484a6473748b5bfcd6666bb92c3e335f
BLAKE2b-256 24a86b2ae09196c2bfa7e364a8e705ff95d36b1e3f5824ef376dcf99fc428612

See more details on using hashes here.

File details

Details for the file pypcapkit-0.14.3-pp36-none-any.whl.

File metadata

  • Download URL: pypcapkit-0.14.3-pp36-none-any.whl
  • Upload date:
  • Size: 251.3 kB
  • Tags: PyPy
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4

File hashes

Hashes for pypcapkit-0.14.3-pp36-none-any.whl
Algorithm Hash digest
SHA256 fb31faa02487dab4a3328fa1c742206794c9b937c0271c7f06d1f00b146b74ae
MD5 434087f852de8a3b04b56e87471187d1
BLAKE2b-256 1f9486f90042bcf00b8b7dcea523fc23c1d0ea6fe31ffc12309e6ccadc2f36f3

See more details on using hashes here.

File details

Details for the file pypcapkit-0.14.3-pp35-none-any.whl.

File metadata

  • Download URL: pypcapkit-0.14.3-pp35-none-any.whl
  • Upload date:
  • Size: 251.6 kB
  • Tags: PyPy
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4

File hashes

Hashes for pypcapkit-0.14.3-pp35-none-any.whl
Algorithm Hash digest
SHA256 7f5d881fafe69063699194f75254001db769f871e0ed2b6bfae7fba49f489b5b
MD5 da2ab230785b3c7684d854bd61a29f92
BLAKE2b-256 269e4c3c8d65205c6dac47db63eb53bee2830dff3e41a15a7a06bddb971e06d3

See more details on using hashes here.

File details

Details for the file pypcapkit-0.14.3-cp38-none-any.whl.

File metadata

  • Download URL: pypcapkit-0.14.3-cp38-none-any.whl
  • Upload date:
  • Size: 251.3 kB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4

File hashes

Hashes for pypcapkit-0.14.3-cp38-none-any.whl
Algorithm Hash digest
SHA256 047226e1fe849a0a184af0f1dd22969a723f83d0765cf0e071d102b52712014b
MD5 4aa83682b4f4e3efdd7616d38e8b17be
BLAKE2b-256 3e09fc3fad0929b7a1519bee8263c484a56fc447d1bbd58abc890bd5c2f6f2aa

See more details on using hashes here.

File details

Details for the file pypcapkit-0.14.3-cp37-none-any.whl.

File metadata

  • Download URL: pypcapkit-0.14.3-cp37-none-any.whl
  • Upload date:
  • Size: 251.3 kB
  • Tags: CPython 3.7
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4

File hashes

Hashes for pypcapkit-0.14.3-cp37-none-any.whl
Algorithm Hash digest
SHA256 30ec2f1f4dd73de805ddbe4bd1a040e318e1905ba1e7038a1e5d3f91ca6e00e4
MD5 08dbf4f9cec87d6fc4912fc10611928a
BLAKE2b-256 7f2dc37a41552c763b7e3cb9b3f7f655b5bb96c200bcd93946a2772f486b27c8

See more details on using hashes here.

File details

Details for the file pypcapkit-0.14.3-cp36-none-any.whl.

File metadata

  • Download URL: pypcapkit-0.14.3-cp36-none-any.whl
  • Upload date:
  • Size: 251.3 kB
  • Tags: CPython 3.6
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4

File hashes

Hashes for pypcapkit-0.14.3-cp36-none-any.whl
Algorithm Hash digest
SHA256 e5bf66ba412217ec45df787884416bf0073ff6012339fa845a29a2a11d9f9729
MD5 5962ed05a3bccb6c88f663388ff95409
BLAKE2b-256 2ae75b06bcac8ffd97c0169946d03d2ab9374699b803df7a03f076ed565f6930

See more details on using hashes here.

File details

Details for the file pypcapkit-0.14.3-cp35-none-any.whl.

File metadata

  • Download URL: pypcapkit-0.14.3-cp35-none-any.whl
  • Upload date:
  • Size: 251.6 kB
  • Tags: CPython 3.5
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4

File hashes

Hashes for pypcapkit-0.14.3-cp35-none-any.whl
Algorithm Hash digest
SHA256 75b12065d3a9cd0d21a5a28c079dfc1074c2e45582aeedaaa3c6fd335d54007c
MD5 9b33ca2acf6b4b37d281543736ec829c
BLAKE2b-256 5730fe2463b6acc27fff222739429c6060779818fa9391ce5d3765c8a06624d6

See more details on using hashes here.

File details

Details for the file pypcapkit-0.14.3-cp34-none-any.whl.

File metadata

  • Download URL: pypcapkit-0.14.3-cp34-none-any.whl
  • Upload date:
  • Size: 251.6 kB
  • Tags: CPython 3.4
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4

File hashes

Hashes for pypcapkit-0.14.3-cp34-none-any.whl
Algorithm Hash digest
SHA256 74439ba5dde2913575576bb19b5257e0be9c083ef1cb171b0fa79d33df5a30ee
MD5 40533ac39318c83bda0d3e09162213bf
BLAKE2b-256 7c48c4d79fdd18d7db6bd61a44a57d3fd3a1b17015f47c3dd96615b2daf7c76b

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