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 0.0009 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.

PyPCAPKit finally boosts a bit up thanks to @59e5aaf4 with issue #29 🎉

Test environment

Key Value
Operating System macOS Mojave
Processor Name Intel Core i7
Processor Speed 2.6 GHz
Total Number of Cores 6
Memory 16 GB

Test results

Engine Performance (seconds per packet)
dpkt 0.00017389218012491862
scapy 0.00036091208457946774
default 0.0009537641207377116
pipeline 0.0009694552421569824
server 0.018088217973709107
pyshark 0.04200994372367859

 

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.4.tar.gz (190.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.4-py3.8.egg (651.7 kB view details)

Uploaded Egg

pypcapkit-0.14.4-py3.7.egg (650.6 kB view details)

Uploaded Egg

pypcapkit-0.14.4-py3.6.egg (659.6 kB view details)

Uploaded Egg

pypcapkit-0.14.4-py3.5.egg (667.0 kB view details)

Uploaded Egg

pypcapkit-0.14.4-py3.4.egg (660.4 kB view details)

Uploaded Egg

pypcapkit-0.14.4-pp36-none-any.whl (312.6 kB view details)

Uploaded PyPy

pypcapkit-0.14.4-pp35-none-any.whl (313.4 kB view details)

Uploaded PyPy

pypcapkit-0.14.4-cp38-none-any.whl (312.6 kB view details)

Uploaded CPython 3.8

pypcapkit-0.14.4-cp37-none-any.whl (312.6 kB view details)

Uploaded CPython 3.7

pypcapkit-0.14.4-cp36-none-any.whl (312.6 kB view details)

Uploaded CPython 3.6

pypcapkit-0.14.4-cp35-none-any.whl (313.4 kB view details)

Uploaded CPython 3.5

pypcapkit-0.14.4-cp34-none-any.whl (313.4 kB view details)

Uploaded CPython 3.4

File details

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

File metadata

  • Download URL: pypcapkit-0.14.4.tar.gz
  • Upload date:
  • Size: 190.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.4.tar.gz
Algorithm Hash digest
SHA256 dde8af70331d6225fc6e8a88b92709e1ccd39b9bce572df022060cc30892040e
MD5 c01951f4b7a0121fce33c58e2aff18f4
BLAKE2b-256 bf4ac19af18c7a2513f2640ecc4b8e77b82351d8e9fadf208c6e2ad2519615f9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pypcapkit-0.14.4-py3.8.egg
  • Upload date:
  • Size: 651.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.4-py3.8.egg
Algorithm Hash digest
SHA256 3e432b2c94d6032a97d832256c4468c20944c872d87208d83244021592d22529
MD5 8c2ca0e5b33cdbc399d1163f37681f9b
BLAKE2b-256 b2fddfeab09262cdb0404529350445f6f8d35aab9ff7e6733be9eb1c41c98434

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pypcapkit-0.14.4-py3.7.egg
  • Upload date:
  • Size: 650.6 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.4-py3.7.egg
Algorithm Hash digest
SHA256 c31bfc2f942a6b24c0b7012b8c79b8b142142bab68801785427c36ab3be34a3d
MD5 db9835bc9567accfc591f508f40678fb
BLAKE2b-256 f254ee15b3168d84e2afd21e491f20cee26aabd8dd37619704a20d04257f313c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pypcapkit-0.14.4-py3.6.egg
  • Upload date:
  • Size: 659.6 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.4-py3.6.egg
Algorithm Hash digest
SHA256 12d792ddf0dafa2c8f889196cd0cb53568bce539a7094cfc3b1193198144485f
MD5 4c4cb90b75cbf362ba764eeb8394c6f5
BLAKE2b-256 ba6009f3e14f50dedb89e2805add120243c657e5823b0f3d2a3a54e3e54fa513

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pypcapkit-0.14.4-py3.5.egg
  • Upload date:
  • Size: 667.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.4-py3.5.egg
Algorithm Hash digest
SHA256 494bb791faee7b76a9cfb32c816abf273f88e8f80370a826c53ba424f266393c
MD5 61520e61a606928c34afc29aa2cfe75b
BLAKE2b-256 77adfbcce7efb918f35d6458d90ca36342624b3e38f27536da6f0efd8130ddbe

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pypcapkit-0.14.4-py3.4.egg
  • Upload date:
  • Size: 660.4 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.4-py3.4.egg
Algorithm Hash digest
SHA256 42a0fab656e36cbfa4f532a280a3e1e26bb952bdb500b2eb4ea26f244aecf04c
MD5 81e68cc8093e046a8abbdb5fb1210478
BLAKE2b-256 c917a11460532976671bd97cbea86355bd1120e6d32dd7176dcb7e9bb97cbf0d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pypcapkit-0.14.4-pp36-none-any.whl
  • Upload date:
  • Size: 312.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.4-pp36-none-any.whl
Algorithm Hash digest
SHA256 93986d0f312b3b3388dd032f45a0f4d58310b35e4e0c1567a6fa312070e8ec25
MD5 8d6393292538be2fa4d97fdb13285227
BLAKE2b-256 3624300dcdadf2ab3a36b45116f556af089d2a3c2ad492e67daa199809ea7669

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pypcapkit-0.14.4-pp35-none-any.whl
  • Upload date:
  • Size: 313.4 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.4-pp35-none-any.whl
Algorithm Hash digest
SHA256 3ca17dd865de679d6371d3874b0724d0fb31e17971d580b50633c0b6bf1099ee
MD5 bdc56cbeca776c874a1b38b5b16d9c4a
BLAKE2b-256 a3229d281e94952dc169073fa48ed5cd5efc955bb7e27617a15615d8749b6805

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pypcapkit-0.14.4-cp38-none-any.whl
  • Upload date:
  • Size: 312.6 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.4-cp38-none-any.whl
Algorithm Hash digest
SHA256 6488a149dc3ab9dd2bd629ae53d088d22e328903ee1d38e1ea2c15e8b77ebcf3
MD5 357338c0c7cf0f450e4ef5336ad61d3c
BLAKE2b-256 f7c5e087649fdbe78b639f7caad95f4abd1380e79e2f57ab293ef92dd754e321

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pypcapkit-0.14.4-cp37-none-any.whl
  • Upload date:
  • Size: 312.6 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.4-cp37-none-any.whl
Algorithm Hash digest
SHA256 5fbbb17dbc17fc49329a8d691a98188ccec9af4df2cdef2ae9eafa7d712fefb2
MD5 7e51433c89652883ddba1f32c9f6ecfc
BLAKE2b-256 75b33531c00d811551ba62cd3e9ab71d71f25b1f7bb3848418e4e909c3a23d46

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pypcapkit-0.14.4-cp36-none-any.whl
  • Upload date:
  • Size: 312.6 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.4-cp36-none-any.whl
Algorithm Hash digest
SHA256 a52b0a01057e712877a4071ce02eddfc6bc5ae3ba5f28bf38140a801add1eb2a
MD5 5dd5904fec2a1bb08e9ca610d7bb8ad1
BLAKE2b-256 36a99f8e8b944d1bcdeffaed4e996a50b6879643d71e19b33b9448285ac40e1d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pypcapkit-0.14.4-cp35-none-any.whl
  • Upload date:
  • Size: 313.4 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.4-cp35-none-any.whl
Algorithm Hash digest
SHA256 9df3b7e20336ec0621da754130b9ef27f9e3ce7ea36bb0455417ad24cedf4f5e
MD5 b83487fe678730bd57e387ff327b3f58
BLAKE2b-256 d7080cd18a3efae44e1fcf852fb9c8b94bbf299de38de4477f4df5e9f115bb5d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pypcapkit-0.14.4-cp34-none-any.whl
  • Upload date:
  • Size: 313.4 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.4-cp34-none-any.whl
Algorithm Hash digest
SHA256 cf81165cd86b33d0d19470d53c081de13ec7b95786d29b493628ed7c80c720a2
MD5 c2fd426b28aad6e4ba31f92515611fbd
BLAKE2b-256 74325901c24e424d0a81a7925975e8f7f5ce7a4ad792fbd67d24e4c0c4e11658

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