Network Packet Construction and Analysis
Preface
- Why This Book Exists
- Who This Book Is For
- What You Will Learn
- How This Book Is Organized
- Code Examples and Reproducibility
- Important Note on Authorization
- Conventions
- About Scapy Versions
Chapter 1: What Is Scapy and Why It Matters
- The Problem Scapy Solves
- How Scapy Differs from Other Tools
- The Packet-Level Mindset
- Historical Context and Community
- When to Use Scapy (and When Not To)
- Summary
Chapter 2: Installation and Environment Setup
- Python and System Requirements
- Installing Scapy on Linux
- Installing Scapy on macOS
- Installing Scapy on Windows
- Platform-Specific Drivers and Filters
- Configuring the Scapy Environment
- Verifying Your Installation
- Optional Dependencies
- Creating an Isolated Test Environment
- Summary
Chapter 3: Running Scapy and First Steps
- Launching the Interactive Shell
- Using Scapy as a Python Library
- Configuration and the conf Object
- Exploring Available Layers
- Your First Packet
- Interactive Workflow Tips
- Summary
Chapter 4: Understanding Scapy Architecture
- The Packet Class and Layer Model
- Field Types and Their Behavior
- Building and Dissecting Packets
- Layer Bindings and Payload Resolution
- Serialization and Raw Bytes
- The SuperSocket Abstraction
- Summary
Chapter 5: Ethernet, ARP and Layer 2 Fundamentals
- The Ethernet Frame
- MAC Address Fundamentals
- ARP and Address Resolution
- ARP Scanning for Host Discovery
- Identifying ARP Anomalies
- VLAN Tags and 802.1Q
- Summary
Chapter 6: IPv4 Packet Construction and Analysis
- The IPv4 Header
- Time to Live and Hop Behavior
- IP Fragmentation Mechanics
- IP Options and Rare Fields
- Practical IPv4 Packet Manipulation
- Summary
Chapter 7: IPv6 in Scapy
- IPv6 Header and Extension Headers
- IPv6 Addressing in Scapy
- Neighbor Discovery Protocol
- IPv6 Routing Configuration
- Dual-Stack Considerations
- Practical IPv6 Packet Examples
- Summary
Chapter 8: ICMP and Network Diagnostics
- ICMP Message Types
- Ping and Echo Requests
- Building a Traceroute Implementation
- Path MTU Discovery
- ICMP Redirects and Errors
- ICMP-Based Network Diagnostics
- Summary
Chapter 9: TCP Packets and Connection Behavior
- The TCP Segment Structure
- TCP Flags and Their Meanings
- Three-Way Handshake Analysis
- Sequence and Acknowledgment Numbers
- Window Size and Flow Control
- Retransmissions and Connection Teardown
- Summary
Chapter 10: UDP, DNS and Application Protocols
- UDP Packets and Checksums
- DNS Message Structure
- Querying DNS with Scapy
- DNS Record Types and Responses
- DNS Caching Behavior Analysis
- Detecting Suspicious DNS Traffic
- Summary
Chapter 11: DHCP and Network Boot Protocols
- DHCP Message Flow
- BOOTP and DHCP Packet Structure
- DHCP Discover with Scapy
- Lease Analysis and Monitoring
- Detecting Rogue DHCP Servers
- DHCPv6 Basics
- Summary
Chapter 12: HTTP and Higher-Level Protocols
- HTTP Over Raw TCP
- Building HTTP Requests
- Parsing HTTP Responses
- TCP Session and Stream Reconstruction
- HTTP Compression Handling
- HTTP/2 Support
- Summary
Chapter 13: Sending and Receiving Packets
- Layer 3 vs Layer 2 Sending
- The sr and srp Functions
- The sr1 and srp1 Functions
- The srloop Function
- Advanced sr/srp Options
- When to Use Each Function
- Error Handling and Best Practices
- Summary
Chapter 14: Sniffing and Live Traffic Capture
- The sniff Function
- BPF Capture Filters vs Python Filters
- Callback Functions and Inline Processing
- Asynchronous Sniffing with AsyncSniffer
- Flow Sessions and Stream Decoding
- Performance and Memory Management
- Summary
Chapter 15: PCAP Files and Capture Analysis
- Reading PCAP Files Efficiently
- Writing and Appending PCAP Files
- Filtering and Summarizing Captures
- Reconstructing Conversations
- Extracting Metadata
- Comparing Multiple Captures
- Summary
Chapter 16: Building Custom Protocol Layers
- Defining a Custom Layer
- Custom Field Types
- Binding Layers Together
- Dissecting Proprietary Protocols
- Variable-Length and Nested Structures
- Contributing to Scapy
- Summary
Chapter 17: Network Automation and Monitoring Tools
- Automated Host Discovery
- Port and Service Scanning
- Protocol Distribution Analysis
- Network Inventory Tools
- Traffic Statistics and Monitoring
- Summary
Chapter 18: Defensive Security and Anomaly Detection
- ARP Spoofing Detection
- TCP Scan Detection
- DNS Anomaly Analysis
- ICMP Misuse Detection
- Rule-Based Alerting System
- Important Contextual Notes
- Summary
Chapter 19: Advanced Patterns and Integrations
- Synthetic Traffic Generation
- PCAP Replay Techniques
- Packet Fuzzing and Testing
- Data Science Integration with Pandas
- Visualization with Matplotlib
- Toolchain Integration with Wireshark and tshark
- Summary
Chapter 20: Troubleshooting and Best Practices
- Common Scapy Errors
- Interface and Permission Problems
- Performance Optimization
- Testing in Isolated Environments
- Legal and Ethical Considerations
- Choosing the Right Tool
- Summary
Conclusion
- The Packet-Level Mindset
- Scapy’s Place in Your Toolkit
- Continuing Your Learning
- A Final Note
Appendix A: Glossary
Appendix B: API Quick Reference
- Core Functions
- Key Classes
- Configuration
Appendix C: Protocol Reference
- Ethernet (Ether)
- ARP
- IPv4 (IP)
- TCP
- UDP
- ICMP
- DNS
- DHCP