Let’s Build a Network Scanner Using Python

One of the most powerful tools in a cybersecurity professional’s arsenal is a network scanner, which can help identify hosts, open ports, and potential security issues within a network. In this blog post, we’ll embark on a journey to build a simple network scanner using Python.

Paritosh
2 min readOct 10, 2023

Prerequisites

Before we dive into coding, let’s ensure we have the necessary tools and knowledge in place:

- Python installed on your system (preferably Python 3).
- Basic knowledge of networking concepts and Python programming.

Choosing the Libraries

Python offers several libraries that can assist in building a network scanner. For this project, we’ll use the Scapy library, a powerful packet manipulation tool that allows us to craft, send, and receive packets.

You can install Scapy using pip:

pip install scapy

Lets write the Code..

Let’s start by creating a Python script that will scan a range of IP addresses for open ports. This script will use Scapy for sending and receiving packets.

import scapy.all as scapy

def scan(ip):
arp_request = scapy.ARP(pdst=ip)
broadcast = scapy.Ether(dst="ff:ff:ff:ff:ff:ff")
arp_request_broadcast = broadcast / arp_request
answered_list = scapy.srp(arp_request_broadcast, timeout=1, verbose=False)[0]

results = []

for element in answered_list:
result = {"ip": element[1].psrc, "mac": element[1].hwsrc}
results.append(result)

return results

def display_results(results):
print("IP Address\t\tMAC Address")
print("-----------------------------------------")
for result in results:
print(result["ip"] + "\t\t" + result["mac"])

target_ip = "192.168.1.1/24"
scan_results = scan(target_ip)
display_results(scan_results)

In this code, we’re sending ARP requests to a range of IP addresses and collecting the IP and MAC addresses of the devices that respond.

Image Credits : Here

Running the Scanner

To run the network scanner, save the code in a Python file (e.g., `network_scanner.py`) and execute it:

python network_scanner.py

The scanner will send ARP requests to the specified IP range and display the results, including the IP and MAC addresses of the devices.

In this blog post, we built a simple network scanner using Python and the Scapy library. This is just a basic example, and you can expand on it to include more advanced features such as port scanning, banner grabbing, and vulnerability assessment.

Network scanning is a critical skill in the field of cybersecurity, and building your own scanner is a great way to understand the inner workings of network reconnaissance tools. Remember always to use such tools responsibly and with proper authorization.

Happy scanning, and stay curious about the world of cybersecurity!

Feel free to customize and expand this blog post to include more details, explanations, and potentially add more features to your network scanner.

Found this article intersting..? Show your appreciation by clapping (as many times as you can), commenting, and following for more insightful content!”

--

--