108k views
5 votes
Analyze the following script. Can you tell what scan this script is running when it is executed? If so what is it doing?

import socket, threading
def TCP_connect(ip, port_number, delay, output):
TCPsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
TCPsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
TCPsock.settimeout(delay)
try:
TCPsock.connect((ip, port_number))
output[port_number] = 'Listening'
except:
output[port_number] = ''
def scan_ports(host_ip, delay):
threads = [] # To run TCP_connect concurrently
output = {} # For printing purposes
# Spawning threads to scan ports
for i in range(10000):
t = threading.Thread(target=TCP_connect, args=(host_ip, i, delay, output))
threads.append(t)
# Starting threads
for i in range(10000):
threads[i].start()
# Locking the main thread until all threads complete
for i in range(10000):
threads[i].join()
# Printing listening ports from small to large
for i in range(10000):
if output[i] == 'Listening':
print(str(i) + ': ' + output[i])

1 Answer

4 votes

Final answer:

The script is running a port scanning operation to check which ports are open and accepting connections.

Step-by-step explanation:

The script shown is running a port scanning operation when executed. It uses the socket module in Python to create a TCP socket and connect to different ports on a given IP address. It spawns multiple threads to scan ports concurrently, and stores the result of each port scan in the output dictionary. The script then prints the ports that are listening, indicating that they are open and accepting connections.

User Gengns
by
8.1k points