Scapy network stack

Scapy maintains its own network stack, which is independent from the one of your operating system. It possesses its own interfaces list, routing table, ARP cache, IPv6 neighbour cache, nameservers config… and so on, all of which is configurable.

Here are a few examples of where this is used:

- When you use ``sr()/send()``, Scapy will use internally its own routing table (``conf.route``) in order to find which interface to use, and eventually send an ARP request.
- When using ``dns_resolve()``, Scapy uses its own nameservers list (``conf.nameservers``) to perform the request
- etc.


What’s important to note is that Scapy initializes its own tables by querying the OS-specific ones. It has therefore implemented bindings for Linux/Windows/BSD.. in order to retrieve such data, which may also be used as a high-level API, documented below.

Interfaces list

Scapy stores its interfaces list in the conf.ifaces object. It provides a few utility functions such as dev_from_networkname(), dev_from_name() or dev_from_index() in order to access those.

>>> conf.ifaces
Source  Index  Name  MAC                IPv4          IPv6
sys     1      lo    00:00:00:00:00:00     ::1
sys     2      eth0  Microsof:12:cb:ef  fe80::10a:2bef:dc12:afae
>>> conf.ifaces.dev_from_index(2)
<NetworkInterface eth0 [UP+BROADCAST+RUNNING+SLAVE]>

You can also use the older get_if_list() function in order to only get the interface names.

>>> get_if_list()
['lo', 'eth0']

Extcap interfaces

Scapy supports sniffing on Wireshark’s extcap interfaces. You can simply enable it using load_extcap() (from scapy.libs.extcap).

>>> load_extcap()
>>> conf.ifaces
Source       Index  Name                                     Address
ciscodump    100    Cisco remote capture                     ciscodump
dpauxmon     100    DisplayPort AUX channel monitor capture  dpauxmon
randpktdump  100    Random packet generator                  randpkt
sdjournal    100    systemd Journal Export                   sdjournal
sshdump      100    SSH remote capture                       sshdump
udpdump      100    UDP Listener remote capture              udpdump
wifidump     100    Wi-Fi remote capture                     wifidump
Source  Index  Name  MAC                IPv4          IPv6
sys     1      lo    00:00:00:00:00:00     ::1
sys     2      eth0  Microsof:12:cb:ef  fe80::10a:2bef:dc12:afae

Here’s an example of how to use sshdump. As you can see you can pass arguments that are properly converted:

>>> load_extcap()
>>> sniff(
...     iface="sshdump",
...     prn=lambda x: x.summary(),
...     remote_host="",
...     remote_username="root",
...     remote_password="SCAPY",
... )

You can check the available options by using the following.

>>> conf.ifaces.dev_from_networkname("sshdump").get_extcap_config()

IPv4 routes


If you want to change or edit the routes, have a look at the “Routing” section in Usage

The routes are stores in conf.route. You can use it to display the routes, or get specific routing

>>> conf.route

Network          Netmask          Gateway   Iface  Output IP  Metric  eth0   100   eth0   0   lo  1  eth0   100  eth0   100

Get the route for a specific IP: conf.route.route() will return (interface, outgoing_ip, gateway)

>>> conf.route.route("")
('lo', '', '')

IPv6 routes

Same as IPv4 but with conf.route6

Get default gateway IP address

>>> gw = conf.route.route("")[2]
>>> gw

Get the IP of an interface

Use conf.iface

>>> ip = get_if_addr(conf.iface)  # default interface
>>> ip = get_if_addr("eth0")
>>> ip

Get the MAC of an interface

>>> mac = get_if_hwaddr(conf.iface)  # default interface
>>> mac = get_if_hwaddr("eth0")
>>> mac

Get MAC address of the next hop to reach an IP

This basically performs a cached ARP who-has when the IP is on the same local link, returns the MAC of the gateway when it’s not, and handle special cases like multicast.

>>> mac = getmacbyip("")
>>> mac