# Analyzing Network Packets With Scapy

> Perform analyzing network packets with scapy assessment during authorized security testing. Use this skill when indicators of the vulnerability class are present in the target environment.

- Skill: `wufufu770/analyzing-network-packets-with-scapy` (Agent Skill)
- Install (CLI): `npx skillmds@latest add wufufu770/analyzing-network-packets-with-scapy`
- Raw SKILL.md: https://api.skillmd.com/api/skills/wufufu770/analyzing-network-packets-with-scapy/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Security
- License: Apache-2.0
- Author: wufufu770 (https://skillmd.com/u/wufufu770)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/wufufu770/analyzing-network-packets-with-scapy

---


## TL;DR

- **目的**：Use Scapy to craft, send, sniff, and dissect TCP/UDP/ICMP/DNS packets, analyze pcap files, implement SYN scans, and detect anomalous traffic…
- **适用**：辅助/通用
- **输入**：PCAP 文件路径 + 分析目标（协议分布/IOC 提取）
- **输出**：资产清单/子域/端口表
- **红线**：仅限授权范围内；扫描限速 `-c 10 -rl 10`；所有动作记 oplog
- **关联**：上游：003-src-session-start → 下游：179-performing-network-traffic-analysis-with-tshark, 180-performing-network-traffic-analysis-with-zeek, 175-analyzing-security-logs-with-splunk

# Analyzing Network Packets with Scapy

## Quick Start

```bash
# Scapy 嗅探示例
from scapy.all import sniff, IP, TCP
packets = sniff(filter="tcp", count=10)
packets.summary()
# 读 pcap
from scapy.all import rdpcap
pkts = rdpcap("capture.pcap")
```

## 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

## Workflow
1. Read and parse pcap/pcapng files with `rdpcap()` for offline analysis
2. Extract protocol layers (IP, TCP, UDP, DNS, HTTP) and field values
3. Compute traffic statistics: top talkers, protocol distribution, port frequency
4. Detect SYN flood patterns by analyzing TCP flag ratios
5. Identify DNS exfiltration indicators via query length and entropy analysis
6. Craft custom probe packets for authorized network testing
7. Export findings as structured JSON report

## Output Format

JSON report containing packet statistics, protocol distribution, top source/destination IPs, detected anomalies (SYN floods, DNS tunneling indicators, fragmentation attacks), and per-flow summaries.

## Advanced Techniques

### Custom Protocol Detection
```python
from scapy.all import *

# 检测 Heartbleed 攻击
def detect_heartbleed(pcap_file):
    pkts = rdpcap(pcap_file)
    for pkt in pkts:
        if pkt.haslayer(TCP) and pkt[TCP].dport == 443:
            if len(pkt[TCP].payload) > 100:  # 异常大 payload
                print(f"Heartbleed suspect: {pkt[IP].src}")
```

### PCAP Anomaly Detection
```python
# 检测端口扫描
pkts = rdpcap("capture.pcap")
syn_packets = [p for p in pkts if p.haslayer(TCP) and p[TCP].flags == 'S']
scan_threshold = 20
if len(syn_packets) > scan_threshold:
    print("Possible port scan detected!")
```

