简体   繁体   中英

Threaded python SSH port scanner doesn't work

So I'm trying to make a program that scans the SSH port for IPs in a list. Because the process is painfully long I'm trying to use threading (I can use multiprocessing if it is more suitable for the program) to make everything faster but I'm running in a problem where the program says "Trying IP" (that's what it's meant to say every time it scans an IP) a lot of times without giving a result and then it gives the results (significantly fewer results than IP scans) and having other weird patterns. It should say Trying IP and then the result but it doesn't and even the result is always failing even if it does find IPs with the SSH port open. At some point I was curious if it misses IPs with SSH so I searched for an IP range that should have a lot of them and it only caught 2000 of them even if the guy who posted the class said he got 45000, yeah I know, maybe something happened and an insane amount of people closed SSH but no, I downloaded something called a "scanning archive" made by some Romanian hackers that had a SSH port scanner in it and when I scanned the same IP range I caught 6600. So can someone help me figure out what is wrong with the code and tell me how to improve it?

import socket
import threading
from queue import Queue
from datetime import datetime

time_start = datetime.now()

SSH_ips = []

def scan(ip_number):
    ip_try = ip_list[ip_number]
    port = 22
    try:
        print("Trying IP")
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        result = sock.connect_ex((ip_try,port))
        if result == 0:
            SSH_ips.append(ip_try)
            print("Found %d" % (ip_try))

        else:
            print("Didn't work")

    except:
        pass

def gulag():
    while True:
        worker = q.get()
        scan(worker)
        q.task_done()

q = Queue()

for x in range(15000):
    t = threading.Thread(target=gulag)
    t.daemon = True
    t.start()

for worker in range(0, 65026):
    q.put(worker)

q.join()

time_finish = datetime.now()

time_elapsed = time_finish - time_start

ip_list_lenght = len(SSH_ips)

SSH_ips.sort()

print("Found %s IPs in %s." % (ip_list_lenght, time_elapsed));

print(SSH_ips)

... what is wrong with the code and tell me how to improve it?

try:
    print("Trying IP")
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    ...
except:
    pass

Please don't just pass on exception but actually check why it failed. It is likely that you run against the limit of open file descriptors and thus creation of a new socket failed. In this case of course no connection will be attempted to this IP address.

    result = sock.connect_ex((ip_try,port))
    if result == 0:
        SSH_ips.append(ip_try)
        print("Found %d" % (ip_try))

    else:
        print("Didn't work")

Similar here check why exactly connect_ex failed instead of just assuming that it failed because there is no open port on the other end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM