Network scanning is a fundamental skill in networking and cybersecurity. It allows you to discover devices connected to a network, identify open ports, and detect potential vulnerabilities. In this tutorial, we’ll walk you through building a simple network scanner in Python using the scapy library. This tool will help you scan your local network and list all connected devices.
What is a Network Scanner?
A network scanner is a tool that identifies active devices on a network by sending and analyzing network packets. It’s commonly used for:
- Network troubleshooting.
- Security audits.
- Monitoring connected devices.
Prerequisites
To follow this tutorial, you’ll need:
- Python installed on your system.
- The
scapylibrary installed.
Step 1: Install Scapy
Scapy is a powerful Python library for network packet manipulation. Install it using pip:
pip install scapy
Step 2: Writing the Network Scanner
Here’s the Python code for a basic network scanner:
from scapy.all import ARP, Ether, srp
# Define the target network (replace with your network)
target_ip = "192.168.1.1/24"
# Create an ARP request packet
arp = ARP(pdst=target_ip)
ether = Ether(dst="ff:ff:ff:ff:ff:ff")
packet = ether/arp
# Send the packet and receive the response
result = srp(packet, timeout=2, verbose=0)[0]
# Parse the response
devices = []
for sent, received in result:
devices.append({"ip": received.psrc, "mac": received.hwsrc})
# Print the results
print("Available devices on the network:")
print("IP Address\t\tMAC Address")
for device in devices:
print(f"{device['ip']}\t\t{device['mac']}")
Step 3: Running the Network Scanner
Save the script as network_scanner.py and run it. The program will scan the specified network and list all connected devices along with their IP and MAC addresses.
How It Works
The network scanner works by sending an ARP (Address Resolution Protocol) request to all devices on the network. Devices that respond to the ARP request are considered active, and their IP and MAC addresses are recorded.
Enhancing the Network Scanner
You can enhance the network scanner by adding features like:
- Scanning multiple networks.
- Detecting open ports on devices.
- Exporting results to a file.
Here’s an example of detecting open ports using the socket library:
import socket
def scan_port(ip, port):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(0.5)
result = sock.connect_ex((ip, port))
if result == 0:
print(f"Port {port} is open on {ip}")
sock.close()
except Exception as e:
print(f"Error scanning port {port} on {ip}: {e}")
# Scan ports 1 to 1024 on a target IP
target_ip = "192.168.1.1"
for port in range(1, 1025):
scan_port(target_ip, port)
Ethical Considerations
When using a network scanner, it’s important to:
- Obtain permission before scanning any network.
- Use the tool responsibly and ethically.
- Comply with local laws and regulations.
Conclusion
Building a network scanner in Python is a great way to learn about networking and cybersecurity. With just a few lines of code, you can create a tool that detects devices on your network. Remember to use this knowledge responsibly and always obtain permission before scanning any network.
Have you tried building a network scanner or any other networking tool in Python? Share your experiences in the comments below!
Disclaimer: This blog post is for educational purposes only. The author does not condone or encourage the misuse of network scanners or any other tools for malicious purposes.