Scapy

2.7.0 · active · verified Thu Apr 09

Scapy is a powerful Python-based interactive packet manipulation tool that enables users to forge, decode, send, and sniff network packets. It supports a wide array of protocols and can function as an interactive shell (REPL) or as a library within Python scripts. Scapy runs on Linux, macOS, most Unix-like systems, and Windows (requiring Npcap). The current version is 2.7.0, and it maintains an active release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import Scapy, craft a basic IP/ICMP packet, send it, and process the response. It also includes a commented-out example for sniffing packets. Sending and sniffing raw packets with Scapy typically requires root or administrator privileges. The `sr1` function sends one packet and waits for a single response, while `sniff` can capture multiple packets, using a callback function for processing.

from scapy.all import *

# Craft an IP packet with an ICMP payload
packet = IP(dst="8.8.8.8")/ICMP()

# Send the packet and receive a response
# Note: Requires root/admin privileges to send/receive raw packets
# Use os.environ.get('SCAPY_IFACE', 'eth0') to specify an interface if needed
# For simple testing, can often run as sudo python your_script.py

# sr1 sends one packet and waits for one answer
# timeout is crucial for non-blocking execution in scripts
resp = sr1(packet, timeout=1, verbose=0)

if resp:
    print(f"Received response from: {resp.src}")
    resp.show()
else:
    print("No response received.")

# Example of sniffing packets (run for 2 packets or 5 seconds)
# Sniffing often requires elevated privileges
def print_packet_summary(pkt):
    print(pkt.summary())

# sniff(prn=print_packet_summary, count=2, timeout=5)
# print("Sniffing complete.")

view raw JSON →