A Deep Packet Inspection (DPI) tool that reads PCAP captures, classifies traffic by application (via TLS SNI and HTTP Host), and filters packets based on rules. Itβs a learning/reference implementationβnot intended for production use.
- A fun tool to look into our packets an decide where all that data goes !!
- Reads PCAP files (e.g. from Wireshark) and parses Ethernet β IPv4 β TCP/UDP.
- Classifies flows by extracting SNI from TLS Client Hello (HTTPS) and Host from plain HTTP.
- Maps domains to app types (YouTube, Facebook, Google, Netflix, etc.) using pattern matching.
- Applies block rules by source IP, app type, or domain (substring).
- Writes a filtered PCAP and prints a summary report (forwarded/dropped, app breakdown, detected domains).
- Python 3.8+
- No third-party dependencies for the main app; tests use pytest.
git clone <repo-url>
cd PacketLens
pip install -r requirements.txt # optional, for testspython main.py <input.pcap> <output.pcap> [options]# Filter only (no blocking)
python main.py capture.pcap filtered.pcap
# Block by app
python main.py capture.pcap out.pcap --block-app YouTube --block-app TikTok
# Block by source IP
python main.py capture.pcap out.pcap --block-ip 192.168.1.50
# Block by domain (substring match)
python main.py capture.pcap out.pcap --block-domain facebook
# Combine rules, quiet output
python main.py capture.pcap out.pcap --block-app YouTube --block-domain twitter -q| Option | Description |
|---|---|
--block-ip IP |
Block all traffic from this source IP (repeatable). |
--block-app APP |
Block by app name (e.g. YouTube, Facebook, Google). |
--block-domain DOM |
Block if SNI/host contains this string (repeatable). |
-q, --quiet |
Less output (no banner/report). |
PacketLens/
βββ main.py # CLI entry point
βββ dpi_engine.py # Pipeline: read β parse β classify β filter β write
βββ dpi_types.py # FiveTuple, AppType, Flow, IP/SNI helpers
βββ pcap_reader.py # PCAP file read (global header, packet records)
βββ packet_parser.py # Ethernet / IPv4 / TCP-UDP parsing
βββ sni_extractor.py # TLS SNI + HTTP Host extraction
βββ rule_manager.py # Block rules (IP, app, domain)
βββ requirements.txt # pytest for tests
βββ Dockerfile # Run tests or app in container
βββ pyproject.toml # pytest config (pythonpath)
βββ tests/
βββ test_dpi_types.py
βββ test_pcap_reader.py
βββ test_packet_parser.py
βββ test_sni_extractor.py
βββ test_rule_manager.py
βββ test_dpi_engine.py
βββ helpers.py # Shared test packet builders
From the project root:
pip install -r requirements.txt
pytest tests/ -vWith project root on PYTHONPATH:
PYTHONPATH=. pytest tests/ -vBuild and run tests:
docker build -t packetlens .
docker run --rm packetlensRun the DPI engine on host files by mounting a directory:
docker run --rm -v "$(pwd):/data" packetlens python main.py /data/input.pcap /data/output.pcap --block-app YouTube(Override the default CMD so the container runs main.py instead of pytest.)
- PcapReader reads the PCAP global header and packet records (with byte-order handling).
- PacketParser parses each packet into Ethernet, IP, and TCP/UDP and exposes a 5-tuple and payload.
- Flows are keyed by 5-tuple (src_ip, dst_ip, src_port, dst_port, protocol).
- For port 443, SNIExtractor parses the TLS Client Hello and extracts the SNI hostname.
- For port 80, HTTPHostExtractor finds the
Hostheader. - sni_to_app_type() maps hostnames to app labels (e.g.
youtube.comβ YouTube). - RuleManager decides whether to block (IP, app, or domain rule).
- Non-blocked packets are written to the output PCAP; a text report is printed.
PacketLens is for education and experimentation. It is not a production-grade DPI or security product. Use at your own risk.
If you like my work please drop a βοΈ