Create a TCP Port Scanner Using Python

Suraj Singh Bisht
3 min readOct 16, 2024

--

Port scanning is a crucial technique in network security, and it can be easily implemented in Python.

Photo by Rubaitul Azad on Unsplash

This tutorial is about how to build a TCP port scanner that demonstrates the use of sockets, and threading, and even offers a simple GUI. Designed for beginners, this project will strengthen your Python skills and enhance your understanding of networking concepts.

What are TCP and UDP packets?

Transport Layer protocols such as Transmission Control Protocol (TCP) and User Datagram Protocol (UDP) are fundamental for the functioning of the internet. Both protocols define how data packets are transmitted across the network, but they operate differently. TCP is connection-oriented, meaning it establishes a connection before transferring data. In contrast, UDP is connectionless and sends data without establishing a prior connection.

The important point to highlight here is that both protocols utilize port numbers ranging between 0 to 65535. These port numbers are essential for data transfer, as they specify both the source and destination ports necessary for proper communication.

The Role of a Port Scanner

A port scanner is an application designed to probe a server or host for open ports. Such an application may be used by administrators to verify the security policies of their networks and by attackers to identify network services running on a host and exploit vulnerabilities. :- https://en.wikipedia.org/wiki/Port_scanner

A port scan works by sending requests to various server ports, determining their status (open, closed, or filtered), and ultimately revealing the services running on that server. The information gathered during a scan can help in assessing potential vulnerabilities and securing the network against threats.

Port Scanning Methods

Port scanning can broadly be categorized into several types, including:

1. Stealth Scan: Also known as half-open scanning, this technique attempts to identify active ports without completing the TCP handshake.

2. TCP Connect Scan: This involves completing the TCP handshake and is less stealthy but easier to implement.

3. SYN Scan: A common method for quickly identifying open ports by sending SYN packets without completing the handshake.

Regardless of the method chosen, it’s vital to remember the ethical implications of port scanning. Ensure that you only scan servers for which you have permission; unauthorized scanning is illegal and considered a hostile act.

Building a TCP Port Scanner

Required Python modules:

1. Socket: provides the low-level networking interface, enabling communication over the network.

2. Threading: To create multiple threads, speeding up the scanning process.

A simplified version of how the port scanner might look using Python

import socket
import threading

# Author : Suraj Singh Bisht
# Github : surajsinghbisht054
def scan(port):
# create a socket object
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
# set timeout
s.settimeout(1)
# establish connection
result = s.connect_ex((target_ip, port))
# check response
if result == 0:
print(f"Port {port} is open")
else:
print(f"Port {port} is closed")

def start_scan():
for port in range(1, 1024):
thread = threading.Thread(target=scan, args=(port,))
thread.start()

if __name__ == "__main__":
target_ip = input("Enter target IP: ")
start_scan()

Save the script as tcp_scanner.py and run the script using Python.

This script will scan ports from 1 to 1023 on the target IP and display whether each port is open or closed in the console.

While port scanning can provide valuable intelligence about network security and vulnerabilities, it is essential to use this knowledge with care and ethical responsibility.

Happy coding!

--

--

No responses yet