Analyzing Network Packets with Scapy
Overview
Scapy is a Python packet manipulation library that enables crafting, sending, sniffing, and dissecting network packets at granular protocol layers. This skill covers using Scapy for security-relevant tasks including TCP/UDP/ICMP packet crafting, pcap file analysis, protocol field extraction, SYN scan implementation, DNS query analysis, and detecting anomalous traffic patterns such as unusually fragmented packets or malformed headers.
When to Use
- When investigating security incidents that require analyzing network packets with scapy
- When building detection rules or threat hunting queries for this domain
- When SOC analysts need structured procedures for this analysis type
- When validating security monitoring coverage for related attack techniques
Prerequisites
- Python 3.8+ with
scapy library installed (pip install scapy)
- Root/administrator privileges for raw socket operations (sniffing, sending)
- Npcap (Windows) or libpcap (Linux) for packet capture
- Authorization to perform packet operations on target network
Steps
- Read and parse pcap/pcapng files with
rdpcap() for offline analysis
- Extract protocol layers (IP, TCP, UDP, DNS, HTTP) and field values
- Compute traffic statistics: top talkers, protocol distribution, port frequency
- Detect SYN flood patterns by analyzing TCP flag ratios
- Identify DNS exfiltration indicators via query length and entropy analysis
- Craft custom probe packets for authorized network testing
- Export findings as structured JSON report
Expected Output
JSON report containing packet statistics, protocol distribution, top source/destination IPs, detected anomalies (SYN floods, DNS tunneling indicators, fragmentation attacks), and per-flow summaries.
1---2name: analyzing-network-packets-with-scapy3description: Use Scapy to craft, send, sniff, and dissect4version: '1.0'5author: mahipal (vendored by SOCIS)6license: Apache-2.07platforms:8- linux9- macos10- windows11category: security12metadata:13 source: https://skillmd.com/skills/socisio/analyzing-network-packets-with-scapy14 socis:15 tags:16 - scapy17 - packet-analysis18 - network-forensics19 - protocol-dissection20 - pcap21 - traffic-analysis22 subdomain: network-security23 upstream: mukul975/Anthropic-Cybersecurity-Skills24 nist_csf:25 - PR.IR-0126 - DE.CM-0127 - ID.AM-0328 - PR.DS-0229 mitre_attack:30 - T104031 - T107132 - T104633 - T155734---353637# Analyzing Network Packets with Scapy3839## Overview4041Scapy is a Python packet manipulation library that enables crafting, sending, sniffing, and dissecting network packets at granular protocol layers. This skill covers using Scapy for security-relevant tasks including TCP/UDP/ICMP packet crafting, pcap file analysis, protocol field extraction, SYN scan implementation, DNS query analysis, and detecting anomalous traffic patterns such as unusually fragmented packets or malformed headers.424344## When to Use4546- When investigating security incidents that require analyzing network packets with scapy47- When building detection rules or threat hunting queries for this domain48- When SOC analysts need structured procedures for this analysis type49- When validating security monitoring coverage for related attack techniques5051## Prerequisites5253- Python 3.8+ with `scapy` library installed (`pip install scapy`)54- Root/administrator privileges for raw socket operations (sniffing, sending)55- Npcap (Windows) or libpcap (Linux) for packet capture56- Authorization to perform packet operations on target network5758## Steps59601. Read and parse pcap/pcapng files with `rdpcap()` for offline analysis612. Extract protocol layers (IP, TCP, UDP, DNS, HTTP) and field values623. Compute traffic statistics: top talkers, protocol distribution, port frequency634. Detect SYN flood patterns by analyzing TCP flag ratios645. Identify DNS exfiltration indicators via query length and entropy analysis656. Craft custom probe packets for authorized network testing667. Export findings as structured JSON report6768## Expected Output6970JSON report containing packet statistics, protocol distribution, top source/destination IPs, detected anomalies (SYN floods, DNS tunneling indicators, fragmentation attacks), and per-flow summaries.