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.5.tar.gz (190.8 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.5-py3.8.egg (654.8 kB view details)

Uploaded Egg

pypcapkit-0.14.5-py3.7.egg (653.6 kB view details)

Uploaded Egg

pypcapkit-0.14.5-py3.6.egg (662.7 kB view details)

Uploaded Egg

pypcapkit-0.14.5-py3.5.egg (670.2 kB view details)

Uploaded Egg

pypcapkit-0.14.5-py3.4.egg (663.6 kB view details)

Uploaded Egg

pypcapkit-0.14.5-pp36-none-any.whl (313.6 kB view details)

Uploaded PyPy

pypcapkit-0.14.5-pp35-none-any.whl (314.4 kB view details)

Uploaded PyPy

pypcapkit-0.14.5-cp38-none-any.whl (313.6 kB view details)

Uploaded CPython 3.8

pypcapkit-0.14.5-cp37-none-any.whl (313.6 kB view details)

Uploaded CPython 3.7

pypcapkit-0.14.5-cp36-none-any.whl (313.6 kB view details)

Uploaded CPython 3.6

pypcapkit-0.14.5-cp35-none-any.whl (314.4 kB view details)

Uploaded CPython 3.5

pypcapkit-0.14.5-cp34-none-any.whl (314.4 kB view details)

Uploaded CPython 3.4

File details

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

File metadata

  • Download URL: pypcapkit-0.14.5.tar.gz
  • Upload date:
  • Size: 190.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.4

File hashes

Hashes for pypcapkit-0.14.5.tar.gz
Algorithm Hash digest
SHA256 47e9347b5e93509bdc6ee73e8256e77af5a403af19f8c1ad614920d2a3118f82
MD5 485db096b374306310951e9fd386c04f
BLAKE2b-256 998da10be64c9502f7caab3d75dfae3c62ca73d41f966f8e3a7ed3f259c30b7e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pypcapkit-0.14.5-py3.8.egg
  • Upload date:
  • Size: 654.8 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.4

File hashes

Hashes for pypcapkit-0.14.5-py3.8.egg
Algorithm Hash digest
SHA256 b94287d8376f9c1d6df5cf7016939c90eb60980f18b9df857edd0315b18daef9
MD5 35109c141dea7b190e917505321865bd
BLAKE2b-256 bc56b765a9031861b5c112fd0c0857310f6e2ad7437bb44e708c4e7e9e07dcfd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pypcapkit-0.14.5-py3.7.egg
  • Upload date:
  • Size: 653.6 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.4

File hashes

Hashes for pypcapkit-0.14.5-py3.7.egg
Algorithm Hash digest
SHA256 e678f7617f3802fc159c964b3e2d013855ad809f7c10f7f11252e319181498db
MD5 e83d3067a8e902982a14fa769b97ddbf
BLAKE2b-256 5bdaf8eca1ffff53795f0b4f6b273a1215640b5ad1d54a4fe1762965b1a18670

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pypcapkit-0.14.5-py3.6.egg
  • Upload date:
  • Size: 662.7 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.4

File hashes

Hashes for pypcapkit-0.14.5-py3.6.egg
Algorithm Hash digest
SHA256 19c3a107bfc6705f039492d03280368c4359385ae01adf67ffed51b9d13dfff8
MD5 a4b70590a55120d3c9fdd80375361342
BLAKE2b-256 5c70bda35962ccf67d446acb0f6d08dbe62913e918549ba1abb378727825af97

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pypcapkit-0.14.5-py3.5.egg
  • Upload date:
  • Size: 670.2 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.4

File hashes

Hashes for pypcapkit-0.14.5-py3.5.egg
Algorithm Hash digest
SHA256 ecc7d348d3a617b2186aa708adb7fe2640037142f4c021b4e0d847e3a341f1a6
MD5 462e95fb81ad43183d7cde3c68709be6
BLAKE2b-256 b0213c32ecccd8e3249568689d9b56671ddd00bbcd1c42296190ce89e92652b1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pypcapkit-0.14.5-py3.4.egg
  • Upload date:
  • Size: 663.6 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.4

File hashes

Hashes for pypcapkit-0.14.5-py3.4.egg
Algorithm Hash digest
SHA256 f8a4f293138dee9e9f2f21c354bb025c933ddf9b7c49ef29d8136d9834932f7b
MD5 980758ec7b14859a07d8927e4fcc0900
BLAKE2b-256 e4cfcb7e502dd478ef80a11ef5b98fad54d798bafa31345182d556d19f63096a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pypcapkit-0.14.5-pp36-none-any.whl
  • Upload date:
  • Size: 313.6 kB
  • Tags: PyPy
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.4

File hashes

Hashes for pypcapkit-0.14.5-pp36-none-any.whl
Algorithm Hash digest
SHA256 62c585a5921d579f1c9aad2ca92fe9ed0071cd006c13aa394fcbfa2dac56f549
MD5 122c2be8cee09cd9f07ac451be0f33ee
BLAKE2b-256 46d81d322aeac34e3745f42df976675fb55a603f2e7c72315a0843715cdf1737

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pypcapkit-0.14.5-pp35-none-any.whl
  • Upload date:
  • Size: 314.4 kB
  • Tags: PyPy
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.4

File hashes

Hashes for pypcapkit-0.14.5-pp35-none-any.whl
Algorithm Hash digest
SHA256 cc36d9cabb137d3bf33682f102190255ed89b47918bb4278dea60f80285071e1
MD5 9a9a34c6968fc2f17b4903d23bee0c16
BLAKE2b-256 e3637b45252fe296e586ab649ce16135eac078deee3d43fdc45eb8ff44075074

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pypcapkit-0.14.5-cp38-none-any.whl
  • Upload date:
  • Size: 313.6 kB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.4

File hashes

Hashes for pypcapkit-0.14.5-cp38-none-any.whl
Algorithm Hash digest
SHA256 00b2b1126696e62e7922158f6b83bda55ad71456af0bfe96db82c33d315c55ce
MD5 aea41a33119d5bc447306e06ec99d827
BLAKE2b-256 6d568d41027d75747520daac9b9847bace7deb3f3cba94924eca37a661b18f52

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pypcapkit-0.14.5-cp37-none-any.whl
  • Upload date:
  • Size: 313.6 kB
  • Tags: CPython 3.7
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.4

File hashes

Hashes for pypcapkit-0.14.5-cp37-none-any.whl
Algorithm Hash digest
SHA256 92468deed403842ed258191af30521bee4f2c8729334f2498349be368219bc06
MD5 397b712d51bac66e40c7a2b70350a4e0
BLAKE2b-256 90f01db95cb50c4afe09dd38846937c72c0a5a88d4fdaf94e5accee70a33bc8f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pypcapkit-0.14.5-cp36-none-any.whl
  • Upload date:
  • Size: 313.6 kB
  • Tags: CPython 3.6
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.4

File hashes

Hashes for pypcapkit-0.14.5-cp36-none-any.whl
Algorithm Hash digest
SHA256 1cebe35c1e008f438f61b69dbcb9ad907efb9bd8ab812265efc0fd37cc5eb085
MD5 cdcd86be9b5bcf2d3fb18497850f893e
BLAKE2b-256 717dd8d8b3990230ad443000c5b1bd20845fb4ff4971c3a9d6f95421d6bcd271

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pypcapkit-0.14.5-cp35-none-any.whl
  • Upload date:
  • Size: 314.4 kB
  • Tags: CPython 3.5
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.4

File hashes

Hashes for pypcapkit-0.14.5-cp35-none-any.whl
Algorithm Hash digest
SHA256 2e97ce469b5a6e3333714b054635d97eb71a3ee95d0ee2b5fbbbbebecbfa9d5e
MD5 c5e0bca4b294bbb510b7e1ea212dabce
BLAKE2b-256 73706e49cdc1b7a4d79389fda571981bf62d34db02e6f53c94ff43f5a968404f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pypcapkit-0.14.5-cp34-none-any.whl
  • Upload date:
  • Size: 314.4 kB
  • Tags: CPython 3.4
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.4

File hashes

Hashes for pypcapkit-0.14.5-cp34-none-any.whl
Algorithm Hash digest
SHA256 c3634b9896f12fe101477b5a29cbd5d1cb7030ef2479574558ac6ba9f23242bf
MD5 f1b226f36336ecba3ab7f9c208dac737
BLAKE2b-256 d4bf774a6d815d5c682cc3ed7f5865d0157207a55de037605cff86f675099940

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