scanner Taraudages Python
-
09-10-2019 - |
Question
J'ai des problèmes avec un scanner de port, je suis d'édition à utiliser les threads. Ce sont les principes de base pour le code d'origine:
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()
Cela prend environ 33 minutes pour terminer. Donc, je pensais la passer pour le faire tourner un peu plus vite. Ceci est mon premier projet de filetage il est donc rien de trop extrême, mais j'ai couru le code suivant pour environ une heure et obtenir aucune exception encore aucune sortie. Est-ce que je fais juste le mal de filetage ou quoi?
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()
La solution
est où votre code échoue.
threading.Thread(target = ScanLow).start()
threading.Thread(target = ScanHigh).start()
e = a + b
while e < 2000:
f = raw_input()
Immédiatement après que vos fils, vous définissez la valeur e
. Cependant, vous ne mettez à jour e
après cela, de sorte que la boucle jamais sorties.
Il semble aussi que vous faites cela pour attendre jusqu'à ce que les deux fils aient fini. La méthode de join()
est une meilleure façon de le faire.
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: Non lié à votre question, mais un commentaire utile. Les deux de vos fonctions de numérisation font exactement la même chose. Vous pouvez les remplacer par une fonction qui prend la plage de balayage comme arguments et commencer à deux fils avec une fonction.
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
Et maintenant, vous pouvez facilement ajuster le nombre de threads et des ports à balayer.
Autres conseils
Vous avez une méthode maladroite pour surveiller les fils. L'utilisation join
indiquera quand le fil est terminé. Aucune raison de ne pas tourner de plus threads pour obtenir plus rapidement les résultats ainsi:
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
Sortie
[80, 135, 445, 1028]
6.92199993134