PyDivert is a powerful Python binding for WinDivert, a Windows driver that allows user-mode applications to capture, modify, and drop network packets sent to or from the Windows network stack.
- Capture network packets matching a specific filter.
- Modify packet headers and payloads on the fly.
- Drop unwanted packets.
- Inject new or modified packets into the network stack.
- Modern Python Support: Full integration with
asyncioand Structural Pattern Matching (PEP 634). - Support for WinDivert 2.2+ advanced features (FLOW, SOCKET, and REFLECT layers).
- Bundled Binaries: No need to manually install WinDivert; the 64-bit DLL and driver are included.
- Python 3.10+ (64-bit)
- Windows 11 (64-bit)
- Administrator Privileges (required to interact with the WinDivert driver)
Note
Windows Server is currently untested but likely works if it meets the architecture requirements.
Install PyDivert using pip:
pip install pydivertOr using uv:
uv add pydivertThe main entry points are pydivert.WinDivert for capturing and pydivert.Packet for manipulation.
Tip
All code examples in this README are verified by automated integration tests in pydivert/tests/test_readme_examples.py.
import pydivert
# Capture only TCP packets to port 80 (HTTP requests)
with pydivert.WinDivert("tcp.DstPort == 80") as w:
for packet in w:
print(f"Captured: {packet}")
w.send(packet) # Re-inject the packet back into the stackWhen you call .recv() (or iterate over the WinDivert object), the packet is taken out of the Windows network stack. It will not reach its destination unless you explicitly call .send(packet).
PyDivert 3.0+ supports asyncio natively using modern async with and async for syntax.
import asyncio
import pydivert
async def main():
# Asynchronously capture packets
async with pydivert.WinDivert("tcp.DstPort == 80") as w:
async for packet in w:
print(f"Async captured: {packet}")
await w.send_async(packet)
if __name__ == "__main__":
asyncio.run(main())Filter and analyze packets using clean match/case syntax.
import pydivert
from pydivert.packet import Packet
from pydivert.packet.tcp import TCPHeader
with pydivert.WinDivert("tcp") as w:
for packet in w:
match packet:
case Packet(tcp=TCPHeader(dst_port=80)):
print("HTTP Traffic")
case Packet(tcp=TCPHeader(dst_port=443)):
print("HTTPS Traffic")
w.send(packet)By simply not calling .send(packet), the packet is effectively dropped.
import pydivert
# Block all traffic from a specific IP address
with pydivert.WinDivert("ip.SrcAddr == 1.2.3.4") as w:
for packet in w:
print(f"Blocking packet from {packet.src_addr}")
# Packet is dropped hereYou can inspect or modify the raw bytes of the packet payload.
import pydivert
# Filter for TCP packets with payload
with pydivert.WinDivert("tcp.PayloadLength > 0") as w:
for packet in w:
if b"secret-token" in packet.payload:
# Redact the token
packet.payload = packet.payload.replace(b"secret-token", b"REDACTED")
w.send(packet)PyDivert can verify and recalculate network checksums automatically.
packet.is_checksum_valid: ReturnsTrueif all checksums (IP, TCP, UDP, ICMP) in the packet are correct.packet.recalculate_checksums(): Recalculates all checksums based on the current header and payload values.
if not packet.is_checksum_valid:
print("Corrupted packet detected!")
packet.recalculate_checksums()The pydivert.Packet object provides easy access to common fields:
- IP Layer:
packet.src_addr,packet.dst_addr,packet.ip.ttl,packet.ip.protocol - TCP/UDP Layer:
packet.src_port,packet.dst_port,packet.tcp.flags - Payload:
packet.payload(bytes) - Metadata:
timestamp: Capture time (QueryPerformanceCounter).is_loopback,is_impostor,is_sniffed: Boolean flags.interface: Index of the capture interface.direction:Direction.INBOUNDorDirection.OUTBOUND.
Detailed protocol headers are available through packet.ipv4, packet.ipv6, packet.tcp, packet.udp, and packet.icmp.
Layer.NETWORK(default): IP packets.Layer.FLOW: Connection events.Layer.SOCKET: Socket-level events.Layer.REFLECT: Reflected events.
Flag.SNIFF: Monitor mode (sniffing).Flag.DROP: Drop packets by default.Flag.FRAGMENTS: Capture all IP fragments.Flag.RECV_ONLY/Flag.SEND_ONLY: Restricted handles.
PyDivert uses the WinDivert filter language to select which packets to capture. For a detailed reference on the syntax and available fields, see the Filter Language Guide.
For the original technical reference, please visit the official WinDivert documentation.
| PyDivert | WinDivert |
|---|---|
| 3.0.0+ | 2.2.2 (bundled) - Full support for modern metadata and layers |
- Clone the repository.
- Install dependencies:
uv sync --extra test --extra docs - Run tests (requires Admin):
uv run pytest
Since WinDivert requires Windows, use Vagrant to run tests on a Windows 11 VM:
vagrant up
vagrant powershell -c '$env:UV_PROJECT_ENVIRONMENT="C:/pydivert_venv"; cd C:/pydivert; uv run pytest'The full API documentation is available at https://ffalcinelli.github.io/pydivert/.
PyDivert is dual-licensed under LGPL-3.0-or-later and GPL-2.0-or-later.
PyDivert is committed to security and uses Snyk for continuous vulnerability scanning. For more details on our security practices and how to report vulnerabilities, please refer to the Security Policy.