by Evangelos Atlasis
In this
post I will show how easy it is to make a “ping-sweep” IPv4 scanner using
Python and the powerful Scapy.
Ping-sweep is an IP scanner than sends an ICMP Echo Request (aka “ping”) to a
range of IP addresses and checks for responses to identify hosts that are present
and respond to these pings. Effectively, this is the same as sending a ping
from the command line.
The Code
The Python code for the program |
First,
we need to import Scapy, as well as the Python library ipaddress. The last one
is used to automatically break down an IPv4 subnet (a set of IP addresses) to a
list of IP addresses that comprise this subnet.
The user
receives a prompt to input the IPv4 subnet he wants to scan (via the input command). Then, the try... except on lines 7 - 12 is used to ensure
the input of a valid IPv4 subnet by the user while avoiding the program
crashing. The try attempts to convert the input into a list of IPv4
addresses. If the process is successful the program continues. Otherwise, an
exception is raised and a small explanatory message is displayed before the
program exits.
The for loop in line 15 iterates the list of IPv4 addresses one by one. Firstly, the
packet which will be sent as a ping is constructed with an IP and ICMP header type 8 (Echo).
Inside the IP header, the destination of the packet is changed to the one of
the current address. We do not need to put a layer-2 (MAC) header since this
will be taken care of automatically from Scapy.
Capturing Traffic with Wireshark
The Wireshark screenshot below shows an outcome of the ping-sweep script in our home network.
Screenshot from Wireshark displaying the ping-sweep in our home net1`2wwwork |
Next Steps
In the future,
I will construct a while loop to allow the user to enter an input until it is a
valid IPv4 network/address, instead of exiting the program. In addition, I will
try to use multithreading or multiprocessing to make the running process
shorter in length and hence less tedious.
Very impressive! Well done!
ReplyDelete