2017-01-11 4 views
0

J'ai suivi un guide sur la façon de faire un simple scanner de port, j'essaie de numériser mon propre IP, mais il se coince dans une boucle et n'imprime pas de ports. Il est difficile de comprendre à ce qu'il ne donne aucune erreur et se coince dans une boucle.Python Port Scanner coincé en boucle

Toute aide serait grandement appréciée.

import socket 
import subprocess 
import sys 
from datetime import datetime 

#clears the shell screen 
subprocess.call('clear', shell=True) 

#ask for input 
remoteServer = raw_input("Please enter a host to scan:") 
remoteServerIP = socket.gethostbyname(remoteServer) 

#print a banner saying we are scanning 
print "-" * 60 
print "now scanning your host...", remoteServerIP 
print "-" * 60 

#Check what time the scan started 
t1 = datetime.now() 

# Using the range function to specify which ports (1 - 1025) 

#Errors. 

try: 
    for port in range(1, 1025): 
    sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) 
    result = sock.connect_ex((remoteServerIP, port)) 
    if result == 0: 
     #if the socket is listening it will print out the port 
     print("Port{}:\t Open".format(port)) 
    sock.close() 

except KeyboardInterrupt: 
    print "You pressed ctrl+c" 
    sys.exit() 

except socket.gaierror: 
    print 'Hostname could not be resolved to IP. Exiting' 
    sys.exit() 

except socket.error: 
    print "couldn't connect to server" 
    sys.exit() 

# checking the time again 
t2 = datetime.now() 

#calculates the differnce of time, to see how long it took to run the script 
total = t2 - t1 

#printing the info to screen 
print "scanning compelte in :", total 
+0

vous êtes sûr que vous ne recevez pas de message d'erreur lorsque vous exécutez dans la console/Termina/cmd.exe/Powershell? Peut-être mettre plus 'print()' pour voir quel port pose problème. – furas

+0

BTW: quant à moi l'écran de nettoyage est inutile - vous ne pouvez pas comparer le résultat actuel avec le précédent. – furas

+0

que voulez-vous dire par "coincé dans une boucle"? –

Répondre

-1

Au moins sur ma machine (Ubuntu 16.quelque chose) cela fonctionne. Sortie:

Please enter a host to scan:localhost 
------------------------------------------------------------ 
now scanning your host... 127.0.0.1 
------------------------------------------------------------ 
Port21: Open 
Port22: Open 
Port25: Open 
Port80: Open 
Port139:   Open 
Port443:   Open 
Port445:   Open 
Port631:   Open 
scanning compelte in : 0:00:00.047478 

Cependant, il ne scanne que les ports 1-1024, alors que les ports vont jusqu'à 65535. Pour faire analyser tous les ports, changer for port in range(1, 1025):-for port in range(1, 65536):

+0

il ne résout pas le problème - vous pourriez mettre cela comme un commentaire. – furas

0

Vous pouvez utiliser sock.timeout(0.1) il sera pas d'attente pour la connexion. J'ai mis print port pour voir quel port est analysé.

Vous pouvez essayer avec 8.8.8.8 - sans sock.timeout(0.1) il se bloquer sur le premier port.

Peut-être avez-vous un bon ordinateur sécurisé et bloque les connexions pour fermer les ports.

import sys 
from datetime import datetime 
import socket 

#ask for input 
remoteServer = raw_input("Please enter a host to scan: ") 
remoteServerIP = socket.gethostbyname(remoteServer) 

#print a banner saying we are scanning 
print "-" * 60 
print "now scanning host ...", remoteServerIP 
print "-" * 60 

#Check what time the scan started 
t1 = datetime.now() 

# Using the range function to specify which ports (1 - 1025) 

#Errors. 

try: 
    for port in range(1, 1025): 
    print port 
    sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) 

    sock.settimeout(0.1) 

    result = sock.connect_ex((remoteServerIP, port)) 
    if result == 0: 
     #if the socket is listening it will print out the port 
     print("Port {:4d}: Open".format(port)) 
    sock.close() 

except KeyboardInterrupt: 
    print "You pressed ctrl+c" 
    sys.exit() 

except socket.gaierror: 
    print 'Hostname could not be resolved to IP. Exiting' 
    sys.exit() 

except socket.error: 
    print "couldn't connect to server" 
    sys.exit() 

# checking the time again 
t2 = datetime.now() 

#calculates the differnce of time, to see how long it took to run the script 
total = t2 - t1 

#printing the info to screen 
print "scanning compelte in:", total 

BTW:

Vous pouvez comparer vos résultats avec des outils comme nmap

Voir scapy - module python pour travailler avec des paquets réseau. (Livre Black Hat Python)