Threaded Python Port Scanner

I am having problems with a port scanner that I am editing to use streams. These are the basics for the source code:

for i in range(0, 2000):  

    s = socket(AF_INET, SOCK_STREAM)  
    result = s.connect_ex((TargetIP, i))  

    if(result == 0) :  
        c = "Port %d: OPEN\n" % (i,)  

    s.close()

It takes about 33 minutes. So I thought I was trying it so that it worked a little faster. This is my first project with threads, so it is not too extreme, but I skipped the following code for about an hour and did not receive any exceptions, but did not get the output. Am I just doing the wrong thread or what?

import threading
from socket import *
import time

a = 0
b = 0
c = ""
d = ""

def ScanLow():
    global a
    global c

    for i in range(0, 1000):  
        s = socket(AF_INET, SOCK_STREAM)  
        result = s.connect_ex((TargetIP, i))  

        if(result == 0) :  
            c = "Port %d: OPEN\n" % (i,)  

        s.close()  
        a += 1

def ScanHigh():
    global b
    global d

    for i in range(1001, 2000):  
        s = socket(AF_INET, SOCK_STREAM)  
        result = s.connect_ex((TargetIP, i))  

        if(result == 0) :  
            d = "Port %d: OPEN\n" % (i,)  

        s.close()  
        b += 1

Target = raw_input("Enter Host To Scan:")
TargetIP = gethostbyname(Target)

print "Start Scan On Host ", TargetIP
Start = time.time()

threading.Thread(target = ScanLow).start()
threading.Thread(target = ScanHigh).start()

e = a + b

while e < 2000:
    f = raw_input()

End = time.time() - Start
print c
print d
print End

g = raw_input()
+3
source share
2 answers

Your code is not working here.

threading.Thread(target = ScanLow).start()
threading.Thread(target = ScanHigh).start()

e = a + b

while e < 2000:
   f = raw_input()

Immediately after starting your threads, you set the value e. However, after that you never update e, so the loop never exits.

, , . join() - .

from threading import Thread
threads = []
threads.append(Thread(target = ScanLow))
threads.append(Thread(target = ScanHigh))
for thread in threads:
  thread.start()
//both threads are running
for thread in threads:
  thread.join()
//both threads have stopped

Edit: , . . , .

from threading import Thread
def Scan(start, stop):
    global a
    global c

    for i in range(start, stop):  
        s = socket(AF_INET, SOCK_STREAM)  
        result = s.connect_ex((TargetIP, i))  

        if(result == 0) :  
            c = "Port %d: OPEN\n" % (i,)  

        s.close()  
        a += 1

threadCount = 2
totalPorts = 2000
threads = []
for start in xrange(0, totalPorts, totalPorts/threadCount):
    threads.append(Thread(target = Scan, args = (start, totalPorts/threadCount)))

for thread in threads:
  thread.start()
//both threads are running
for thread in threads:
  thread.join()
//both threads have stopped

.

+5

. join , . , :

import threading
import socket
import time

ports = []
def check_port(ip,port):
    s = socket.socket()
    if s.connect_ex((ip,port)) == 0:
        ports.append(port)
    s.close()

target = raw_input('Target? ')
s = time.time()
threads = []
for port in range(2000):
    t = threading.Thread(target=check_port,args=(target,port))
    t.start()
    threads.append(t)
for t in threads:
    t.join()
print ports
print time.time() - s

[80, 135, 445, 1028]
6.92199993134
+1

Source: https://habr.com/ru/post/1784393/


All Articles