Leanpub Header

Skip to main content

Mastering Scapy

Network Packet Construction and Analysis

Mastering Scapy
This book is 100% completeLast updated on 2026-10-20

Learn to build, send, capture and analyze network packets with Scapy, one of Python’s most versatile networking tools. Through practical examples and runnable code, you’ll explore protocols, traffic analysis, PCAP files, custom packet creation and security testing. Whether you’re troubleshooting networks or sharpening your security skills, this book gives you the tools to get hands-on.

Minimum price

$25.00

$35.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
APP
293
Pages
About

About

About the Book

This book teaches you how to use Scapy, the powerful Python packet manipulation library, to build, send, receive, dissect and analyze network packets. Starting from installation and first steps, you will progress through protocol layers, live traffic capture, PCAP analysis, custom protocol development, automation and defensive security applications. Every chapter provides complete, runnable Python code and explains the underlying networking concepts so you can adapt these techniques to real-world engineering and security workflows.

Author

About the Author

Steve Publications

Steve is a technology professional with over 20 years of experience in software development, server infrastructure, cybersecurity, vulnerability research and reverse engineering. He specializes in analyzing complex systems, identifying weaknesses and improving security.

Beyond his professional work, Steve collaborates with researchers and industry experts to create practical technical books covering software development, cybersecurity, cloud computing, DevOps, AI and enterprise technologies. His goal is to help IT professionals, students and enthusiasts develop practical skills through clear explanations, real-world examples and hands-on exercises.

At Steve Publications, we use AI to assist with translation, proofreading and editing but not to replace the research, technical expertise or hands-on experience behind our books. Some authors publish under pseudonyms for privacy or professional reasons.

Every book is written, reviewed and maintained by experienced professionals, supported by a private community of over 420 engineers and researchers. We prioritize technical accuracy, practical value and regularly updated content over mass-produced AI-generated text.

We welcome experienced professionals interested in writing or reviewing books, especially in specialized and emerging technology fields. If you'd like to contribute, contact us with your area of expertise.

We stand behind our work. If a book doesn't meet your expectations, Leanpub offers a 60-day money-back guarantee.

Contents

Table of Contents

Network Packet Construction and Analysis

Preface

  1. Why This Book Exists
  2. Who This Book Is For
  3. What You Will Learn
  4. How This Book Is Organized
  5. Code Examples and Reproducibility
  6. Important Note on Authorization
  7. Conventions
  8. About Scapy Versions

Chapter 1: What Is Scapy and Why It Matters

  1. The Problem Scapy Solves
  2. How Scapy Differs from Other Tools
  3. The Packet-Level Mindset
  4. Historical Context and Community
  5. When to Use Scapy (and When Not To)
  6. Summary

Chapter 2: Installation and Environment Setup

  1. Python and System Requirements
  2. Installing Scapy on Linux
  3. Installing Scapy on macOS
  4. Installing Scapy on Windows
  5. Platform-Specific Drivers and Filters
  6. Configuring the Scapy Environment
  7. Verifying Your Installation
  8. Optional Dependencies
  9. Creating an Isolated Test Environment
  10. Summary

Chapter 3: Running Scapy and First Steps

  1. Launching the Interactive Shell
  2. Using Scapy as a Python Library
  3. Configuration and the conf Object
  4. Exploring Available Layers
  5. Your First Packet
  6. Interactive Workflow Tips
  7. Summary

Chapter 4: Understanding Scapy Architecture

  1. The Packet Class and Layer Model
  2. Field Types and Their Behavior
  3. Building and Dissecting Packets
  4. Layer Bindings and Payload Resolution
  5. Serialization and Raw Bytes
  6. The SuperSocket Abstraction
  7. Summary

Chapter 5: Ethernet, ARP and Layer 2 Fundamentals

  1. The Ethernet Frame
  2. MAC Address Fundamentals
  3. ARP and Address Resolution
  4. ARP Scanning for Host Discovery
  5. Identifying ARP Anomalies
  6. VLAN Tags and 802.1Q
  7. Summary

Chapter 6: IPv4 Packet Construction and Analysis

  1. The IPv4 Header
  2. Time to Live and Hop Behavior
  3. IP Fragmentation Mechanics
  4. IP Options and Rare Fields
  5. Practical IPv4 Packet Manipulation
  6. Summary

Chapter 7: IPv6 in Scapy

  1. IPv6 Header and Extension Headers
  2. IPv6 Addressing in Scapy
  3. Neighbor Discovery Protocol
  4. IPv6 Routing Configuration
  5. Dual-Stack Considerations
  6. Practical IPv6 Packet Examples
  7. Summary

Chapter 8: ICMP and Network Diagnostics

  1. ICMP Message Types
  2. Ping and Echo Requests
  3. Building a Traceroute Implementation
  4. Path MTU Discovery
  5. ICMP Redirects and Errors
  6. ICMP-Based Network Diagnostics
  7. Summary

Chapter 9: TCP Packets and Connection Behavior

  1. The TCP Segment Structure
  2. TCP Flags and Their Meanings
  3. Three-Way Handshake Analysis
  4. Sequence and Acknowledgment Numbers
  5. Window Size and Flow Control
  6. Retransmissions and Connection Teardown
  7. Summary

Chapter 10: UDP, DNS and Application Protocols

  1. UDP Packets and Checksums
  2. DNS Message Structure
  3. Querying DNS with Scapy
  4. DNS Record Types and Responses
  5. DNS Caching Behavior Analysis
  6. Detecting Suspicious DNS Traffic
  7. Summary

Chapter 11: DHCP and Network Boot Protocols

  1. DHCP Message Flow
  2. BOOTP and DHCP Packet Structure
  3. DHCP Discover with Scapy
  4. Lease Analysis and Monitoring
  5. Detecting Rogue DHCP Servers
  6. DHCPv6 Basics
  7. Summary

Chapter 12: HTTP and Higher-Level Protocols

  1. HTTP Over Raw TCP
  2. Building HTTP Requests
  3. Parsing HTTP Responses
  4. TCP Session and Stream Reconstruction
  5. HTTP Compression Handling
  6. HTTP/2 Support
  7. Summary

Chapter 13: Sending and Receiving Packets

  1. Layer 3 vs Layer 2 Sending
  2. The sr and srp Functions
  3. The sr1 and srp1 Functions
  4. The srloop Function
  5. Advanced sr/srp Options
  6. When to Use Each Function
  7. Error Handling and Best Practices
  8. Summary

Chapter 14: Sniffing and Live Traffic Capture

  1. The sniff Function
  2. BPF Capture Filters vs Python Filters
  3. Callback Functions and Inline Processing
  4. Asynchronous Sniffing with AsyncSniffer
  5. Flow Sessions and Stream Decoding
  6. Performance and Memory Management
  7. Summary

Chapter 15: PCAP Files and Capture Analysis

  1. Reading PCAP Files Efficiently
  2. Writing and Appending PCAP Files
  3. Filtering and Summarizing Captures
  4. Reconstructing Conversations
  5. Extracting Metadata
  6. Comparing Multiple Captures
  7. Summary

Chapter 16: Building Custom Protocol Layers

  1. Defining a Custom Layer
  2. Custom Field Types
  3. Binding Layers Together
  4. Dissecting Proprietary Protocols
  5. Variable-Length and Nested Structures
  6. Contributing to Scapy
  7. Summary

Chapter 17: Network Automation and Monitoring Tools

  1. Automated Host Discovery
  2. Port and Service Scanning
  3. Protocol Distribution Analysis
  4. Network Inventory Tools
  5. Traffic Statistics and Monitoring
  6. Summary

Chapter 18: Defensive Security and Anomaly Detection

  1. ARP Spoofing Detection
  2. TCP Scan Detection
  3. DNS Anomaly Analysis
  4. ICMP Misuse Detection
  5. Rule-Based Alerting System
  6. Important Contextual Notes
  7. Summary

Chapter 19: Advanced Patterns and Integrations

  1. Synthetic Traffic Generation
  2. PCAP Replay Techniques
  3. Packet Fuzzing and Testing
  4. Data Science Integration with Pandas
  5. Visualization with Matplotlib
  6. Toolchain Integration with Wireshark and tshark
  7. Summary

Chapter 20: Troubleshooting and Best Practices

  1. Common Scapy Errors
  2. Interface and Permission Problems
  3. Performance Optimization
  4. Testing in Isolated Environments
  5. Legal and Ethical Considerations
  6. Choosing the Right Tool
  7. Summary

Conclusion

  1. The Packet-Level Mindset
  2. Scapy’s Place in Your Toolkit
  3. Continuing Your Learning
  4. A Final Note

Appendix A: Glossary

Appendix B: API Quick Reference

  1. Core Functions
  2. Key Classes
  3. Configuration

Appendix C: Protocol Reference

  1. Ethernet (Ether)
  2. ARP
  3. IPv4 (IP)
  4. TCP
  5. UDP
  6. ICMP
  7. DNS
  8. DHCP

References

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub