GUI tool to scan TCP ports

This Python script is a port scanner with a graphical user interface (GUI) built using the customtkinter library for modern UI elements. The port scanner checks for open ports on a specified target host within a given port range. Let’s break down the code:

Importing Libraries

import customtkinter
import socket
import threading
from queue import Queue
import smvalidate
  • The script imports the necessary libraries, including customtkinter for the GUI, socket for socket programming, threading for multithreading, Queue for managing a thread-safe queue, and smvalidate for input validation.

Global Variables

target = ''
queue = Queue()
port_list = []
open_ports = []
closed_ports = []
  • Global variables are defined to store the target host, a queue for managing ports, lists to store open and closed ports.

Port Scan Function

def portscan(target, port):
    # Function to open a TCP socket for the specified target and port
    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.connect((target, port))
        return True
    except:
        return False

Worker Function

def worker():
    # Worker function to process ports from the queue
    while not queue.empty():
        port = queue.get()
        if portscan(target, port):
            print(f'\nport {port} is open')
            open_ports.append(port)
        else:
            print(f'\nport {port} is closed')
            closed_ports.append(port)

Fill Queue Function

def fill_queue(port_list):
    # Function to fill the queue with port numbers
    for port in port_list:
        queue.put(port)

Button Function

def button_function():
    # Function executed when the START button is pressed
    label_status.configure(text="Program Running")
    textbox.delete("0.0", "end")
    app.update()

    global target, port_list, open_ports, closed_ports
    open_ports, closed_ports = [], []
    target = entry_target_host.get()
    from_port = entry_port_range_from.get()
    to_port = entry_port_range_to.get()

    try:
        if smvalidate.validate_website_address(target) or smvalidate.validate_ipv4_address(target):
            port_list = list(range(int(from_port), int(to_port) + 1))
            fill_queue(port_list)

            # Creating multiple threads to run the 'worker' function in parallel
            thread_list = []
            thread_count = min(len(port_list), 1000)  # Limiting thread count to a maximum of 1000
            for _ in range(thread_count):
                thread = threading.Thread(target=worker)
                thread_list.append(thread)

            for thread in thread_list:
                thread.start()
            for thread in thread_list:
                thread.join()

            open_ports.sort()
            textbox.delete("0.0", "end")
            textbox.insert("0.0", '\n'.join(str(e) for e in open_ports))
            label_status.configure(text="Program Completed")
            app.update()
        else:
            label_status.configure(text="Please verify your inputs")
            app.update()
    except:
        label_status.configure(text="ERROR! Please verify your inputs")
        app.update()

UI Code Using customtkinter

customtkinter.set_appearance_mode("system")
customtkinter.set_default_color_theme("blue")

app = customtkinter.CTk()
app.geometry("350x370")
app.title("Port Scanner - schoolofiris.com")

# UI elements using customtkinter
button_start = customtkinter.CTkButton(master=app, text="START", command=button_function, width=200)
# ... (similar blocks for other UI elements)

app.mainloop()

The script defines functions for port scanning, threading, and GUI elements. The button_function is executed when the “START” button is pressed, initiating the port scanning process. The customtkinter library is used for creating a modern and visually appealing user interface. The program uses multithreading to speed up the port scanning process, and the results are displayed in a textbox. Input validation is done using the smvalidate module.

Add a Comment

Your email address will not be published. Required fields are marked *