Network Scanning using scapy module - Python
Last Updated :
01 Mar, 2020
Scapy is a library supported by both Python2 and Python3. It is used for interacting with the packets on the network. It has several functionalities through which we can easily forge and manipulate the packet. Through scapy module we can create different network tools like ARP Spoofer, Network Scanner, packet dumpers etc. This module can be used to create more advanced tools related network security and ethical hacking.
Installation of scapy module:
As scapy module is not included in Python3 library by default, we have to add it into our Python library using pip. Execute this command in your Linux terminal to get the scapy module for Python3.
pip3 install scapy-python3
What is network scanning ?
Network scanning refers to scanning of whole network to which we are connected and try to find out what are all the clients connected to our network. We can identify each and every client using their IP and MAC address. We can use ARP ping to find out the alive systems in our network.
Some important functions for creating Network scanner -
ARP(): This function defined in scapy module which allows us to create ARP packets (request or response). By default, if we are calling it, it will create an ARP request packet for us.
Python3 1==
import scapy.all as scapy
request = scapy.ARP()
summary(): This method provide us the status of the packet that we have created. It does not provide the detailed information about the packet, it just gives us the basic idea like what is the type of packet, what is the destination of the packet etc.
For example if we want to create an ARP packet using
ARP()
method which is present in the scapy module and want to see the summary of the packet then we can do this by creating the object of ARP class.
Python3 1==
import scapy.all as scapy
request = scapy.ARP()
print(request.summary())
Now we have created a request packet of ARP. Here the output of the program will be like this -
show() method: This method is very similar to
summary()
method. It gives more detailed information about the packet. The usage of this function is also much similar to as
summary()
method.
Python3 1==
import scapy.all as scapy
request = scapy.ARP()
print(request.show())
ls() function: This method is present in the scapy class. By using this method, we can see what are the fields that we can set for a specific packet.
In our example we will create an ARP packet and the with the help of ls() function, we will see what are the available fields for this packet.
Python3 1==
import scapy.all as scapy
request = scapy.ARP()
print(scapy.ls(scapy.ARP()))
Steps for creating Network Scanner -
1. Create an ARP packet using ARP() method.
2. Set the network range using variable.
3. Create an Ethernet packet using Ether() method.
4. Set the destination to broadcast using variable hwdst.
5. Combine ARP request packet and Ethernet frame using '/'.
6. Send this to your network and capture the response from different devices.
7. Print the IP and MAC address from the response packets.
Below is the Python implementation -
Python3 1==
import scapy.all as scapy
request = scapy.ARP()
request.pdst = 'x'
broadcast = scapy.Ether()
broadcast.dst = 'ff:ff:ff:ff:ff:ff'
request_broadcast = broadcast / request
clients = scapy.srp(request_broadcast, timeout = 1)[0]
for element in clients:
print(element[1].psrc + " " + element[1].hwsrc)
Here x = Network range. For example x = 192.168.1.1/24, 172.16.5.1/16 etc
Output:
Similar Reads
Scraping dynamic content using Python-Scrapy Let's suppose we are reading some content from a source like websites, and we want to save that data on our device. We can copy the data in a notebook or notepad for reuse in future jobs. This way, we used scraping(if we didn't have a font or database, the form brute removes the data in documents, s
4 min read
How to Build a WiFi Scanner in Python using Scapy? In this article, we are going to build a WiFi Scanner in Python using Scapy. WiFi Scanning or Network scanning refers to the scanning of the whole network to which we are connected and try to find out what are all the clients connected to our network. We can identify each client using their IP and M
3 min read
Network Scanner in Python A network scanner is one major tool for analyzing the hosts that are available on the network. A network scanner is an IP scanner that is used for scanning the networks that are connected to several computers. To get the list of the available hosts on a network, there are two basic methods - ICMP Ec
3 min read
Port Scanner using Python Prerequisites: Socket Programming in Python This article is just to provide a sample code to generate a Port Scanner. This Port Scanner will work for both the Web Applications as well as remote Host. This tool has been created to provide the basic functionality of a Port Scanner. The general concept
2 min read
Simple Port Scanner using Sockets in Python Prerequisites: Socket Programming in Python Before going to the programming, let us discuss about ports. In this article, we will check the virtual ports of a server or websites, or localhost. Every port has a unique number. There are 65,535 ports available in a host starting from 0. We can assign t
3 min read
Sending Email using FastAPI Framework in Python Before jumping into the topic directly, let's have a small intro about the technologies we are going to use. As the name suggests, we will be using FastAPI, a Python language framework. FastAPI: FastAPI is a python framework to develop REST Apis. It is very easy to build, Â high performance, easy to
3 min read
Introduction to Social Networks using NetworkX in Python Prerequisite - Python Basics Ever wondered how the most popular social networking site Facebook works? How we are connected with friends using just Facebook? So, Facebook and other social networking sites work on a methodology called social networks. Social networking is used in mostly all social m
4 min read
File Sharing App using Python Computer Networks is an important topic and to understand the concepts, practical application of the concepts is needed. In this particular article, we will see how to make a simple file-sharing app using Python. Â An HTTP Web Server is software that understands URLs (web address) and HTTP (the proto
4 min read
Spoofing IP address when web scraping using Python In this article, we are going to scrap a website using Requests by rotating proxies in Python. Modules RequiredRequests module allows you to send HTTP requests and returns a response with all the data such as status, page content, etc. Syntax:Â requests.get(url, parameter)Â JSON JavaScript Object No
3 min read
Automated Website Scraping using Scrapy Scrapy is a Python framework for web scraping on a large scale. It provides with the tools we need to extract data from websites efficiently, processes it as we see fit, and store it in the structure and format we prefer. Zyte (formerly Scrapinghub), a web scraping development and services company,
5 min read