Write A Port Scanner Using Python in 10 Minutes
Write A Port Scanner Using Python in 10 Minutes
in 10 Minutes
July 20, 2023 • 5 min read
Tags: Python
Port scanning is a technique that allows you to discover which ports are open or
closed on a target host or network. Port scanning can be useful for network security,
penetration testing, or ethical hacking.
In this post, we will explore 3 possible ways to create a port scanner in Python using
the socket, python-nmap, and scapy libraries.
import socket
# Usage example
ports = [21, 22, 80, 443, 3306, 5432]
scan_ports(‘localhost’, ports)
Code explanation:
1. The scan_ports function takes two arguments, host and ports. host
represents the target host to scan, and ports is a list of port numbers that
need to be checked.
nashruddinamin.com
Web Development and AI - Python, React, Rust. 1
2. It uses a for loop to iterate over each port number in the ports list.
3. Inside the loop, it tries to create a TCP socket and connect to the target host
and port. If the connection is successful, it means the port is open and
accessible.
import nmap
# Usage example
scan_ports('localhost', 3000, 4001)
Code explanation:
2. The function creates an instance of the PortScanner and perform the scan.
3. For each of the scanned hosts, it iterates through the TCP ports and print the
state for each port.
nashruddinamin.com
Web Development and AI - Python, React, Rust. 2
Using the scapy library
The scapy library is a powerful packet manipulation tool that allows you to create,
send, receive, and analyze network packets. You can use the scapy library to craft
custom packets and perform various types of scans, such as SYN scan, ACK scan,
XMAS scan, and more.
# Usage example
ports = [21, 22, 80, 443, 3306, 5432]
scan_ports('localhost', ports)
Code explanation:
1. The scan_ports function takes two parameters: host and ports. host
represents the target host to scan, and ports is a list of port numbers that
need to be checked.
2. It uses a for loop to iterate over each port number in the ports list.
3. For each port, a SYN packet (TCP SYN) is created using Scapy. The packet is
constructed with the IP() and TCP() functions. The IP() function specifies
the destination IP address (dst=host), and the TCP() function specifies the
destination port (dport=port) and sets the TCP flags to “S” (SYN).
4. The constructed SYN packet is sent using the sr1() function, which sends the
packet and captures the response. The timeout=1 parameter sets the timeout
for the response to 1 second, and verbose=0 suppresses Scapy’s output
during packet sending.
nashruddinamin.com
Web Development and AI - Python, React, Rust. 3
5. After sending the packet, the function checks if a response was received. It
also verifies if the response contains a TCP layer and if the TCP flags of the
response indicate a SYN-ACK response (SA).
Summary
In this blog post, we have shown you how to write a port scanner using the socket,
python-nmap, and scapy libraries.
Each of these methods has its own advantages and disadvantages. The best method
to use will depend on your specific needs. If you need a simple port scanner, the
socket module is a good option. If you need a more efficient port scanner,
python-nmap and scapy are good options.
nashruddinamin.com
Web Development and AI - Python, React, Rust. 4