Python Scapy tutorial

This tutorial explains the basics of scapy and how scapy can be integrated with Python to build custom network tools and scripts.

What is scapy

Scapy is a packet crafting framework which works on windows and Linux platforms. Scapy is free to use and can be downloaded from the website www.secdev.org

Scapy is predominantly used for crafting , sending and receiving packets at different layers of the TCP/IP model like L2, L3 and L4. Scapy has libraries inbuilt for most of the networking protocols.

How to install scapy

Scapy can be installed on both the windows and linux platform. On linux, it can be installed using the command line or using the synaptic package manager. The screenshot of how to install scapy using the synaptic manager on linux is shown below

scapy install with synaptic

How to verify scapy installation

Once scapy is installed using the synaptic package manager, the installation needs to be verified on the command line. This is done using the sudo scapy command from the linux command line. The screenshot is shown below.

scapy installation verification

The “Welcome to Scapy(2.2.0) “prompt ensures that scapy is installed properly on the system.

What is Python and how to install

Python is a programming language which is available as a free download. It is the most widely used language for developing scripts and tools. As with scapy, it can be downloaded and installed using the Synaptic package manager.

How to install scapy on python

Scapy is integrated with Python to create custom network tools and scripts. The command from scapy.all import* is used in a Python file to import and use scapy on a Python file. The command could vary based on the version of Python used.

Steps to create a Python file with Scapy

The following example shows how a ping packet is created using Python and scapy by importing scapy into a python file.

The details of the code is included in our course on udemy Python Network Programming Course

Step 1 - Create a python file using the command vi scapyping.py
Step 2 - Type the scapy command from scapy.all import * in the python file.
Step 3 - Create an ICMP header using the command icmp==ICMP()
Step 4 - Assign the type value in the ICMP header as 8 with the code icmp.type=8
Step 5 - Assign the code value in the ICMP header as 0 with the code icmp.code=0
Step 6 - Create a IP header object with the code ip=IP()
Step 7 - Assign the source IP address in the IP header with the value 192.168.1.25.
Step 8- Assign the destination IP address in the IP header with the value 192.168.1.100
Step 9 - Execute the code send(ip/icmp) to send the IP packet appended with the icmp header on the network.

The packet is sent using the command line as shown in the below screenshot.

scapy send ping packet

Check out our Python Network Programming Course on udemy which explains how to use Python & Scapy to build custom network tools & scripts Python Network Programming Course