Skip to main content

Generic Python interface for Windows pktmon packet capture.

Project description

pktmon-interface

Generic Python interface for Windows pktmon packet capture.

It provides:

  • a small C++ shim DLL over PktMonApi.dll realtime streams;
  • a Python ctypes wrapper exposed as pktmon_interface.PktmonBackend;
  • a Scapy-shaped async sniffer exposed as pktmon_interface.PktmonSniffer;
  • a single CLI, pktmon-interface.

The live capture path is Windows-only and generally requires an elevated Administrator terminal.

Layout

native/include/              C ABI consumed by Python
native/src/                  Windows C++ pktmon shim
scripts/build.ps1            MSVC build helper
src/pktmon_interface/        Python package

Setup

python -m venv .venv
.\.venv\Scripts\activate
python -m pip install -e .[dev]

Build the native DLL:

powershell -ExecutionPolicy Bypass -File .\scripts\build.ps1

The default output is:

build/pktmon_backend.dll

You can point Python at another DLL with:

$env:PKTMON_BACKEND_DLL = "C:\path\to\pktmon_backend.dll"

Package build

Build a pip-installable wheel with the native DLL bundled:

python -m pip install build
powershell -ExecutionPolicy Bypass -File .\scripts\build.ps1 -OutputDir .\src\pktmon_interface
python -m build

The wheel is written to dist/ and is tagged as a Windows x64 platform wheel, for example:

pktmon_interface-0.1.0-py3-none-win_amd64.whl

GitHub Actions runs tests on Python 3.10 through 3.13, builds the native DLL, builds the wheel/sdist, and uploads the distributions as workflow artifacts. Pushing a tag like v0.1.0 also attaches those distributions to the GitHub release and publishes them to PyPI with the PYPI_API_TOKEN repository secret.

Python API

Scapy-like callback use:

from pktmon_interface import CapturedPacket, PktmonSniffer


def on_packet(packet: CapturedPacket) -> None:
    print(packet.protocol_name, packet.source, packet.destination, len(packet.payload))


sniffer = PktmonSniffer(filter="tcp port 30031 or udp", prn=on_packet, store=False)
sniffer.start()
try:
    sniffer.join(timeout=30)
finally:
    sniffer.stop()

Capture tuning:

sniffer = PktmonSniffer(
    filter="udp",
    store=False,
    queue_size=8192,
    native_queue_capacity=8192,
    buffer_size_multiplier=4,
    truncation_size=9000,
    include_empty_payloads=True,
    drain_batch_size=64,
)

include_empty_payloads=True counts TCP/UDP packets with no payload, such as TCP ACK and handshake packets. This usually makes packet event counts closer to Scapy. Set it to False to keep the older payload-only behavior. For higher event rates, keep callback work small, use store=False, and lower truncation_size if you only need packet metadata instead of full payloads. buffer_size_multiplier=4 is the stable low-latency default. You can try 1 for lower buffering, but some Windows builds do not deliver packets reliably with a multiplier that small.

Synchronous reads:

from pktmon_interface import PktmonBackend

with PktmonBackend() as backend:
    print(backend.probe())
    backend.start(
        "tcp port 30031 or udp",
        queue_capacity=8192,
        buffer_size_multiplier=4,
        include_empty_payloads=True,
    )
    packet = backend.read(timeout_ms=500)
    if packet is not None:
        print(packet.protocol_name, packet.source, packet.destination, len(packet.payload))

Iterator style:

from pktmon_interface import PktmonSniffer

with PktmonSniffer(filter="udp", store=False) as sniffer:
    for packet in sniffer:
        print(packet.flow, packet.payload[:16].hex(" "))

CLI

pktmon-interface probe
pktmon-interface packets --timeout 30 --probe
pktmon-interface packets --timeout 30 --queue-size 8192 --buffer-size-multiplier 4

The native backend defaults to the documented PktMonApi.dll realtime stream and applies capture constraints to its own live session. If PktMonApi.dll is missing or does not expose the required realtime stream exports, startup fails with an error instead of falling back to another capture path.

Notes

The native backend does not use pktmon filter global system state. It only uses session capture constraints through PktMonApi.dll.

Validation

Offline validation:

python -m py_compile (Get-ChildItem -Recurse -Filter *.py src).FullName
powershell -ExecutionPolicy Bypass -File .\scripts\build.ps1

Project details


Download files

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

Source Distribution

pktmon_interface-0.2.0.tar.gz (159.4 kB view details)

Uploaded Source

Built Distribution

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

pktmon_interface-0.2.0-py3-none-win_amd64.whl (148.6 kB view details)

Uploaded Python 3Windows x86-64

File details

Details for the file pktmon_interface-0.2.0.tar.gz.

File metadata

  • Download URL: pktmon_interface-0.2.0.tar.gz
  • Upload date:
  • Size: 159.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pktmon_interface-0.2.0.tar.gz
Algorithm Hash digest
SHA256 55d76221176d7c80b2c7b4dcbe592e82663dc6996e00a6f30297edb752ea4efc
MD5 de82e9a9792c17f013c33632469f5eb2
BLAKE2b-256 291c40546b6ee2bc609b398dd79277dbf95c64d98989f6df85a71d6585f55b51

See more details on using hashes here.

File details

Details for the file pktmon_interface-0.2.0-py3-none-win_amd64.whl.

File metadata

File hashes

Hashes for pktmon_interface-0.2.0-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 db1d16a012a9b5ba39d2144f87bb438795f59d25317eac39278c25c47e4d8c74
MD5 cd7cd0e1a2e391281c555fea24cd6951
BLAKE2b-256 bbc34336d82c4374bac0a234b5b32ef3d2b7c2841181bda47561bb737db09206

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