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()

Synchronous reads:

from pktmon_interface import PktmonBackend

with PktmonBackend() as backend:
    print(backend.probe())
    backend.start("tcp port 30031 or udp")
    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

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.1.0.tar.gz (155.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.1.0-py3-none-win_amd64.whl (145.4 kB view details)

Uploaded Python 3Windows x86-64

File details

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

File metadata

  • Download URL: pktmon_interface-0.1.0.tar.gz
  • Upload date:
  • Size: 155.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.1.0.tar.gz
Algorithm Hash digest
SHA256 d6f7e9e8e521f410fda545ab345c20022c8ee133bea6335284cbb3ca5de2ce8b
MD5 e4b1b93891f01910701f3eebc7806ab3
BLAKE2b-256 5beba37dd5af0ac00572797ed95294791757653f8bcff099e5449beeeaf37135

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pktmon_interface-0.1.0-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 a4df98fe88362bbf05a88f86c98fdcbfc7c8196972a4416b04eb64be48a2d78c
MD5 e486c32295c21d189bd47febabdec1bb
BLAKE2b-256 628978a3234deb78343c91ab0277af453dd3baa3eb34ec73caad2986a071c75c

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