简体   繁体   English

线程Python端口扫描程序

[英]Threaded Python port scanner

I am having issues with a port scanner I'm editing to use threads. 我正在编辑端口扫描程序,我正在编辑使用线程。 This is the basics for the original 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()

This takes approx 33 minutes to complete. 这大约需要33分钟才能完成。 So I thought I'd thread it to make it run a little faster. 所以我想我会把它设置为让它运行得更快一点。 This is my first threading project so it's nothing too extreme, but I've ran the following code for about an hour and get no exceptions yet no output. 这是我的第一个线程项目,所以它不是太极端,但我运行了以下代码大约一个小时,没有例外但没有输出。 Am I just doing the threading wrong 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()

This is where your code is failing. 这是您的代码失败的地方。

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

e = a + b

while e < 2000:
   f = raw_input()

Immediately after you start your threads, you set the value to e . 启动线程后,立即将值设置为e However, you never update e after that, so the loop never exits. 但是,之后你永远不会更新e ,因此循环永远不会退出。

It also seems like you are doing this to wait until both threads have finished. 你似乎也要等到两个线程都完成了。 The join() method is is a better way to do this. 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: Not related to your question, but a helpful comment. 编辑:与您的问题无关,但是有用的评论。 Both of your scan functions are doing the exact same thing. 两个扫描功能都完全相同。 You can replace them with one function that takes the scan range as arguments and start both threads with the one function. 您可以使用一个将扫描范围作为参数的函数替换它们,并使用一个函数启动两个线程。

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

And now you can easily adjust the number of threads and ports to scan. 现在,您可以轻松调整要扫描的线程和端口数。

You have an awkward method for monitoring the threads. 你有一个监控线程的笨拙方法。 Using join will indicate when the thread is complete. 使用join将指示线程何时完成。 No reason not to spin off more threads to get the results faster as well: 没有理由不剥离更多的线程来获得更快的结果:

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

Output 产量

[80, 135, 445, 1028]
6.92199993134

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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