or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

automation.mdconfig-utilities.mdcore-packet-system.mdindex.mdpacket-analysis.mdprotocol-layers.mdsend-receive.md
tile.json

tessl/pypi-scapy

Interactive packet manipulation program and library for network security research and testing

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/scapy@2.6.x

To install, run

npx @tessl/cli install tessl/pypi-scapy@2.6.0

index.mddocs/

Scapy

A powerful Python-based interactive packet manipulation program and library designed for network security, research, and testing. Scapy provides comprehensive capabilities for forging, decoding, sending, and capturing network packets across a wide range of protocols, enabling tasks like network scanning, tracerouting, probing, unit testing, security assessments, and network discovery.

Package Information

  • Package Name: scapy
  • Language: Python
  • Installation: pip install scapy
  • Version: 2.6.1
  • License: GPL-2.0-only

Core Imports

from scapy.all import *

For specific functionality:

from scapy.all import Ether, IP, TCP, UDP, ICMP, ARP
from scapy.all import sr, sr1, send, sendp, sniff
from scapy.all import rdpcap, wrpcap
from scapy.all import conf

Basic Usage

from scapy.all import *

# Create packets
packet = IP(dst="8.8.8.8")/ICMP()
ethernet_packet = Ether()/IP(dst="192.168.1.1")/TCP(dport=80)

# Send packets and receive responses
response = sr1(packet, timeout=2)
if response:
    response.show()

# Capture packets
packets = sniff(count=10, filter="tcp port 80")
packets.summary()

# Read/write pcap files
packets = rdpcap("capture.pcap")
wrpcap("output.pcap", packets)

# Create complex protocols
dns_query = IP(dst="8.8.8.8")/UDP(dport=53)/DNS(rd=1, qd=DNSQR(qname="example.com"))
answer = sr1(dns_query)

Architecture

Scapy is built around a flexible packet manipulation architecture:

  • Packet Class: Universal packet representation supporting layered protocols
  • Field System: Strongly-typed field definitions for protocol structures
  • Layer Binding: Automatic protocol layer recognition and chaining
  • Protocol Layers: 100+ implemented network protocols from L2 to L7
  • Send/Receive Engine: Cross-platform network I/O with multiple socket types
  • Analysis Framework: Packet lists, filtering, and statistical analysis tools

This design enables Scapy to handle any network protocol, craft custom packets with precise control, and serve as both an interactive tool and a programmatic library for network security research, testing, and analysis.

Capabilities

Core Packet System

Fundamental packet creation, manipulation, and field system. Provides the Packet base class, field types, and core operations for building and dissecting network packets.

class Packet:
    def __init__(self, *args, **kwargs): ...
    def show(self): ...
    def summary(self) -> str: ...
    def build(self) -> bytes: ...
    def copy(self): ...
    def getlayer(self, layer): ...
    def haslayer(self, layer) -> bool: ...

class Raw(Packet):
    def __init__(self, load: bytes = b""): ...

def bind_layers(lower, upper, **kwargs): ...

Core Packet System

Protocol Layers

Comprehensive implementation of network protocols from Layer 2 to Layer 7, including Ethernet, IP, TCP, UDP, wireless protocols, and application-layer protocols.

class Ether(Packet):
    def __init__(self, dst: str = "ff:ff:ff:ff:ff:ff", src: str = None, type: int = None): ...

class IP(Packet):
    def __init__(self, dst: str = "127.0.0.1", src: str = None, ttl: int = 64, **kwargs): ...

class TCP(Packet):
    def __init__(self, sport: int = 20, dport: int = 80, seq: int = 0, 
                 ack: int = 0, flags: int = 2, **kwargs): ...

class UDP(Packet):
    def __init__(self, sport: int = 53, dport: int = 53, **kwargs): ...

class ICMP(Packet):
    def __init__(self, type: int = 8, code: int = 0, **kwargs): ...

class DNS(Packet):
    def __init__(self, rd: int = 1, qd: DNSQR = None, **kwargs): ...

Protocol Layers

Send/Receive Operations

Network I/O functions for sending packets, receiving responses, capturing traffic, and managing network communication across different platforms.

def sr(x, promisc: bool = None, filter: str = None, timeout: float = None, 
       inter: float = 0, verbose: int = None, chainCC: bool = False, 
       retry: int = 0, multi: bool = False, **kwargs) -> tuple[SndRcvList, PacketList]: ...
def sr1(x, promisc: bool = None, filter: str = None, timeout: float = None, 
        verbose: int = None, retry: int = 0, **kwargs) -> Packet: ...
def send(x, inter: float = 0, loop: int = 0, count: int = None,
         verbose: int = None, realtime: bool = None, **kwargs) -> None: ...
def sendp(x, inter: float = 0, loop: int = 0, count: int = None,
          verbose: int = None, realtime: bool = None, iface: str = None, **kwargs) -> None: ...
def sniff(count: int = 0, store: bool = True, prn: callable = None, 
          filter: str = None, lfilter: callable = None, timeout: float = None, 
          iface: str = None, **kwargs) -> PacketList: ...

class AsyncSniffer:
    def __init__(self, count: int = 0, store: bool = True, prn: callable = None,
                 filter: str = None, lfilter: callable = None, **kwargs): ...
    def start(self): ...
    def stop(self): ...
    def join(self, timeout: float = None): ...
    @property
    def results(self) -> PacketList: ...

Send/Receive Operations

Packet Analysis

Packet collection management, filtering, analysis, and visualization tools for working with captured network traffic and packet sequences.

class PacketList:
    def summary(self) -> None: ...
    def show(self) -> None: ...
    def filter(self, func) -> PacketList: ...
    def plot(self, **kwargs): ...
    def conversations(self) -> dict: ...

class SndRcvList(PacketList):
    def make_table(self, **kwargs): ...

def rdpcap(filename: str, count: int = -1) -> PacketList: ...
def wrpcap(filename: str, pkt: PacketList, **kwargs) -> None: ...

Packet Analysis

Configuration and Utilities

Global configuration management, utility functions for data conversion, validation, file operations, and platform-specific functionality.

class Conf:
    def configure(self, **kwargs): ...

# Global configuration object
conf: Conf

def hexdump(x: bytes) -> None: ...
def checksum(data: bytes) -> int: ...
def get_if_list() -> list[str]: ...
def get_if_addr(iff: str) -> str: ...
def valid_ip(ip: str) -> bool: ...
def valid_mac(mac: str) -> bool: ...

Configuration and Utilities

Automation Framework

State machine framework for building automated network protocols, responding to network events, and creating interactive network services.

class Automaton:
    def __init__(self): ...
    def start(self): ...
    def stop(self): ...

class ATMT:
    @staticmethod
    def state(name: str): ...
    @staticmethod
    def action(func): ...
    @staticmethod
    def receive(func): ...
    @staticmethod
    def timeout(func, timeout: float): ...

Automation Framework