Writting a Network Scanner Tool in Python !


Let’s write a very simple Network scanning tool in Python !

Full code : Github

Concept of Network Scaning

ARP ( address resolution Protocol ) : This network protocol is used to map IP addresses to MAC addresses for the devices present in the network .

We are going to send an ARP packet to all the ip addresses in the network . If the machine is alive, it will send back an ARP response packet giving its MAC address . This is how we will map all the devices in the network .

Now , lets begin with the program .

First we imported , two modules :

OS : we will use it to run shell commands from our program .

argparse : we will use this module to parse arguments given from the command line .

Scapy is the most important module that we are going to use in the program . This module can create customized packets as per our requirements . Scapy is not pre installed with python .

So , first we tried to import it ,if it is already installed it will get imported . But if it is not installed , it will raise an exception , that is why we have taken care of the exception . If the exception rises , our program will first install the scapy module with the help of pip3 and then it will import it .

Next , we created a function named argument . first we created an instance ti the ArgumentParser() class . Then we added an option into it – “-t” or “–target” , and its destination variable is set to be target . A help message is also included for usage information . Finally we parsed the arguments given from the command line and returned them as argument function’s return value .

Then , we created a function named to checker to check if the supplied argument target is not empty . It accepts an argument target , which is going to be the ip or ip range . If it is empty , an informational message is displayed and the program is exited .

scan is the most important function of our program . This function accepts an argument , target ip or ip range . First we created an object from scapy’s ARP class , and set the IP field to our target IP or IP range .

Then we created an another object from scapy’s Ether class.ff:ff:ff:ff:ff:ff is set as destination MAC field of this object . This will make packet reach to all computers on the network .

An ARP packet has two layers , we need to glue this two layers . “/” is used to glue two layers in the scapy module . we stored the final packet in a variable named c.

finally , scapy’s srp method is used to send this packet . timeout is set to 1 second , which means if the response doesnt comeback within one second of request then move on to the next address . This function will give two lists , answered response and unanswered requests . We are only going to store answered response in a variable named result .

Finally we iterating the answered list and printing the source IP and corresponding source MAC field .

argument function is called and the parsed arguments are stored in ip variable. Next , the target argument is checked by checker module. Finally , the scan function is called by supplying the target IP or target IP range .

Full code : Github

For more info on the tool : python3 network_scanner.py -h or –help

Usage : python3 network_scanner.py -t TARGET_IP_RANGE

Voila , A simple Network Scanner in Python !