This repository contains a single advanced Python script, DHCP_Logs.py, which is an "ultimate" DHCP sniffer designed to capture and analyze DHCP traffic, fingerprint devices, detect anomalies and potential threats, and persist detailed logs (PCAP, JSONL, and human-readable text). It includes enhanced features such as geolocation lookups, vendor fingerprinting, DNS-tunneling heuristics, and performance monitoring.
This README explains prerequisites, installation steps, and a comprehensive set of PowerShell example commands that exercise all major modes and options supported by the script.
DHCP_Logs.py— main script (capture and analysis). Keep this file in the workspace root.requirements.txt— Python dependencies for the script (use a virtualenv).
- Listens for DHCP-related network traffic (standard DHCP/BOOTP ports and DHCPv6) using Scapy.
- Parses and extracts ALL DHCP options and tries to canonicalize them.
- Produces multiple outputs: PCAP file, JSONL event file, pretty text logs, raw analysis dump, statistics file, and a threat analysis log.
- Performs enhanced analysis: device/vendor fingerprinting, hostname resolution (optional), geolocation (optional via external web services), anomaly detection and basic threat scoring.
- Can operate on a live interface or read an existing PCAP.
- Python 3.8+ (3.10/3.11 recommended)
- Administrator/root privileges for live capture (Windows: run PowerShell as Administrator)
- Recommended Python packages (see
requirements.txt):- scapy
- psutil (optional, used for performance monitoring)
Notes:
- The script calls external tools (e.g.,
curl,arp,avahi-resolve,getent) for certain features. Those tools are typical on Linux/macOS. On Windows:- Recent Windows 10/11 include
curlandnslookupby default.arp -aworks on Windows but parsing differs from Linux output. - Linux-specific helper utilities such as
avahi-resolveandgetentare not available on stock Windows and those code paths will simply skip or log debug messages.
- Recent Windows 10/11 include
Open PowerShell as Administrator and run the commands below to create and activate a virtual environment, then install dependencies.
# change to the project directory
Set-Location -Path "C:\Users\Muhammad Asim\Documents\DHCP Logs"
# create a venv
python -m venv .venv
# activate the venv (PowerShell)
.\.venv\Scripts\Activate.ps1
# upgrade pip and install requirements
python -m pip install --upgrade pip
pip install -r requirements.txtIf you cannot use a virtual environment, you can install packages globally (not recommended):
pip install scapy psutil- Live capture requires Administrator privileges.
- Some features that rely on Linux-only tools (AVAHI, getent, Linux arp parsing) will be skipped on Windows. Hostname resolution still uses Python's
socketfunctions which work cross-platform. - Geolocation uses public web APIs via
curl. Windows 10+ includes acurlbinary; if your system doesn't havecurl, either install it or modifyDHCP_Logs.pyto use Python'srequestslibrary.
Run the following to confirm Scapy is importable and dependencies are installed:
python -c "import scapy.all as scapy; print('scapy', scapy.__version__)"
python -c "import psutil; print('psutil', psutil.__version__)" # optionalImportant: for live capture you must specify an interface via -i / --interface. To get a list of available interfaces, run with --list-interfaces.
General usage:
python .\DHCP_Logs.py -i <interface> [options]If you don't need live capture you can analyze a PCAP file:
python .\DHCP_Logs.py --read-pcap path\to\capture.pcapBelow is a comprehensive set of commands that exercise the script's options. Replace placeholders (like <iface> and paths) with real values.
- List network interfaces (no capture):
python .\DHCP_Logs.py --list-interfaces- Test hostname resolution (offline test mode):
python .\DHCP_Logs.py --test-hostnames- Read and analyze an existing pcap file (safe, no root required):
python .\DHCP_Logs.py --read-pcap .\example_capture.pcap --json output_events.jsonl --txt output_log.txt --pcap output_append.pcap- Live capture (minimum required):
# Run as Administrator
python .\DHCP_Logs.py -i "Ethernet" -v- Live capture with verbose diagnostic mode (very chatty):
python .\DHCP_Logs.py -i "Ethernet" -v -d- Live capture, enable hostname resolution (may slow down processing):
python .\DHCP_Logs.py -i "Ethernet" --enable-hostname-resolution --geo-lookup --threat-detection --performance-monitor- Capture ALL UDP traffic (non-filtered) — use caution (lots of data):
python .\DHCP_Logs.py -i "Ethernet" --capture-all --no-filter- Internet focus (attempt to highlight DHCP traffic that involves non-private IPs):
python .\DHCP_Logs.py -i "Ethernet" --internet-focus --threat-detection --geo-lookup- Increase thread pool for higher throughput (modify
--max-threadsas needed):
python .\DHCP_Logs.py -i "Ethernet" --max-threads 16 --performance-monitor- Change output filenames and batch/flush parameters:
python .\DHCP_Logs.py -i "Ethernet" --pcap my_capture.pcap --json events.jsonl --txt events.txt --batch-size 5 --flush-interval 10- Run in diagnostic + deep analysis mode (for debugging and development):
python .\DHCP_Logs.py -i "Ethernet" -d --deep-analysis --diagnostic --verbose- Read PCAP, generate final enhanced report & threat log (read-only run):
python .\DHCP_Logs.py --read-pcap .\captures\big_capture.pcap --json analysis_events.jsonl --threat-log threats.jsonRun this as Administrator and be prepared for a lot of output and network/Internet lookups:
python .\DHCP_Logs.py -i "Ethernet" --pcap dhcp_capture.pcap --json dhcp_events.jsonl --txt dhcp_log.txt --raw-dump raw_dhcp.json --stats-file dhcp_stats.json --threat-log dhcp_threats.json --geo-cache geo_cache.json -b 10 -f 5 --internet-focus --threat-detection --geo-lookup --performance-monitor --enable-hostname-resolution --max-threads 12 -vdhcp_capture.pcap(default): appended raw packets capturedlog.json(default): line-delimited JSON eventslog.txt(default): human-readable formatted eventsraw_dhcp_dump.txt(default): raw JSON analyses of packetsdhcp_stats.json: computed statistics and summariesthreat_analysis.json: threat events and summaries*_comprehensive_report.json: generated at graceful shutdown
- Permission denied / sniff failed: Run PowerShell as Administrator. Windows requires elevated privileges to capture packets.
- Scapy import failed: ensure Python and pip installed and install
scapyfrom therequirements.txt. - curl not found: install curl or modify the code to use Python
requestsfor geolocation lookups. - Missing Linux-specific tools: The script gracefully falls back for many of these functions; hostname and geolocation features still work with Python standard libs and web APIs.
The script accepts many flags. Here is a concise reference matching the argparse options in DHCP_Logs.py:
-i, --interface— Interface name for live capture (required for live mode).--list-interfaces— Print available interfaces and exit.--test-hostnames— Run offline hostname resolution tests and exit.--pcap— PCAP output filename (default: dhcp_capture.pcap).--json— JSONL events output file (default: log.json).--txt— Human-readable text log (default: log.txt).--summary— Summary JSON file name.--raw-dump— Raw packet analysis dump (default: raw_dhcp_dump.txt).--stats-file— Statistics output file (default: dhcp_stats.json).--threat-log— Threat analysis log filename.--geo-cache— Geolocation cache file.-b, --batch-size— Number of packets/events to buffer before writing to disk.-f, --flush-interval— Seconds between automatic flushes of buffers.--read-pcap— Read and analyze an existing PCAP (no live capture).-v, --verbose— Verbose logging.-d, --diagnostic— Diagnostic mode (extra debug prints).--no-filter— Capture all packets (no BPF filter).--promisc— Enable promiscuous mode on the interface.--capture-all— Capture all DHCP-like traffic, including malformed.--deep-analysis— Enable deeper analysis routines.--internet-focus— Focus on DHCP traffic involving public/internet IPs.--threat-detection— Enable threat detection features.--geo-lookup— Enable geolocation lookups for public IPs.--performance-monitor— Enable background performance monitoring (psutil required).--max-threads— Maximum worker threads for analysis (default 8).--enable-hostname-resolution— Enable reverse DNS/hostname resolution (slower).
For a complete list and short help text, run:
python .\DHCP_Logs.py -hThis section gives examples of the main output types so you know what to expect.
- JSONL events (
--jsonor defaultlog.json): each line is a JSON object describing a DHCP event. A minimal example event looks like:
{
"time": "2025-10-07T12:34:56.789+00:00",
"message_type": "OFFER",
"message_type_code": 2,
"chaddr": "00:11:22:33:44:55",
"yiaddr": "192.168.1.101",
"server_id": "192.168.1.1",
"raw_options": {"53": "02", "1": "ffffff00"}
}-
Human-readable text log (
--txtor defaultlog.txt): contains formatted blocks produced by thepretty_line()function in the script. It includes CLIENT INFO, SERVER INFO, IP ASSIGNMENT, FINGERPRINT INFO, RAW DHCP OPTIONS and more. Expect multi-page human-friendly entries. -
PCAP (
--pcap): standard packet capture file that can be opened with Wireshark or tcpdump. -
Raw analyses (
--raw-dump): JSON serialized results of deep packet inspection for each packet analyzed — useful for programmatic post-processing. -
Statistics (
--stats-file): a JSON snapshot containing counters (packets, unique vendors, option frequency, etc.).
================================================================================
[2025-10-07T12:34:56.789+00:00] DHCP OFFER
--------------------------------------------------------------------------------
CLIENT INFO:
MAC Address: 00:11:22:33:44:55
DHCP Hostname: client-host
Client IP: 0.0.0.0
Source MAC: 00:11:22:33:44:55
Source IP: 192.168.1.101
SERVER INFO:
Server ID: 192.168.1.1
Server IP: 192.168.1.1
RAW DHCP OPTIONS:
[ 53] message-type = 2
[ 1] subnet_mask = ffffff00
...
================================================================================
For live packet capture on Windows you need Npcap installed. Steps:
- Download Npcap from https://nmap.org/npcap/ (use the latest stable installer).
- Run the installer as Administrator and enable the "Install Npcap in WinPcap API-compatible mode" option if you need older software compatibility.
- Reboot if the installer recommends it.
- Run PowerShell as Administrator and run the capture command (e.g.,
--list-interfacesthen-i <Your Interface>).
If you see permission or device errors when running sniff() in Scapy, ensure Npcap is installed and that PowerShell was launched with Administrator privileges.
If you don't have live DHCP traffic, create a small test pcap or use Scapy to craft packets and then run --read-pcap:
# Example: create a tiny pcap using a Python snippet
python - <<'PY'
from scapy.all import Ether, IP, UDP, BOOTP, DHCP, wrpcap
pkt = Ether()/IP(src='192.168.1.2',dst='192.168.1.1')/UDP(sport=68,dport=67)/BOOTP(chaddr=b'\x00\x11\x22\x33\x44\x55')/DHCP(options=[('message-type', 'discover'), ('end', b'')])
wrpcap('test_dhcp.pcap', [pkt])
print('wrote test_dhcp.pcap')
PY
python .\DHCP_Logs.py --read-pcap .\test_dhcp.pcapUse these fast one-liners to validate environment and imports before a live capture:
# scapy import
python -c "import scapy.all as scapy; print('scapy OK', getattr(scapy, '__version__', 'unknown'))"
# psutil (optional)
python -c "import psutil; print('psutil OK', psutil.virtual_memory().percent)"
# curl availability (for geo lookups)
curl --version-
Q: "I get PermissionError / can't sniff" — A: Run PowerShell as Administrator and ensure Npcap is installed. Also close other applications that may bind to network devices.
-
Q: "Geolocation lookups fail or are slow" — A: The script uses
curlto query third-party APIs. Ifcurlis missing, install it or request that I replace the code to userequests(included inrequirements.txt). Also consider disabling--geo-lookupfor large captures. -
Q: "Hostname resolution is slow and times out" — A: Don't run with
--enable-hostname-resolutionon high-traffic captures. Use--diagnosticonly for low-volume debugging. -
Q: "The script writes nothing to disk" — A: Check the output directory permissions and validate the file paths you provided. By default the script attempts to create the output files on startup; any failure is logged and the script exits.
-
Q: "I see a lot of UDP packets but no DHCP events" — A: Use
--capture-allto capture non-standard DHCP traffic, or confirm the interface is correct. Also ensure no firewall rules are blocking UDP 67/68 visibility.
- Replace
curlsubprocess calls with Pythonrequestsfor cross-platform geolocation lookups and better error handling. (I can do this change and run a local test.) - Add a small unit test suite for parsing functions (e.g.,
dhcp_options_to_dict) and run them automatically. - Add a small Windows-specific module to parse
arp -aoutput and map IP->hostnames more reliably.
- This tool captures network traffic; use it only on networks you own or have explicit permission to monitor.
- Geolocation and threat lookups may send IP addresses to 3rd-party services; be mindful of privacy and compliance requirements.
- If you want to add platform-specific features (Windows support for ARP parsing, using WinPcap/Npcap APIs), modify
DHCP_Logs.pyaccordingly. - To replace
curlcalls withrequestsfor cross-platform compatibility, addrequeststorequirements.txtand update the geolocation functions.
- This README and the
requirements.txtprovide a quick-start and full command matrix to exercise the script. Some advanced features (e.g., avahi, getent, Linux ARP parsing) are platform-specific and will not run on Windows without changes. The script includes many defensive checks and will continue to run even if optional utilities are missing.
If you want, I can also:
- Run one example command in your PowerShell terminal here (you must confirm which one and accept elevated permissions), or
- Replace
curlcalls with Pythonrequestsfor cross-platform compatibility and updaterequirements.txt.