Python

05
avril 2011

Python - Lilpy Scanner

Rédigé par Matthieu CERDA   21 commentaires   Mis à jour le  05/04/2011

Je release au passage un petit code python que j'ai fait a l'arrache dans le cadre de mes cours de sécurité, j'espère que ça sera utile a quelqu'un :)

#!/usr/bin/env python

import socket
import sys

print "Little Python port scanner - fast, buggy and noisy - i'm running on python %s\n" % sys.version # Nice friendly greeting

if len(sys.argv) < 6: # Uh ? what did you say ?
sys.exit("""You're obviously trying to get me to run without the required arguments. I'm expecting to receive:

%s host range_start range_end timeout mode ( intrusive or stealth )

Ex: ./lilpy 192.168.1.254 1 1024 0.2 intrusive
""" % sys.argv[0])

if socket.has_ipv6: # A nice polished touch though.
print "IPv6 support is available."
else:
print "IPv6 is not supported on this system."

print "Testing " + sys.argv[1] # Confirming hostname
print "Testing range from " + sys.argv[2] + " to " + sys.argv[3] # Confirming range
print "TimeOut set to " + sys.argv[4] # Confirming timeout

if sys.argv[5] == "intrusive": # What mode will I use ?
print "Switching to intrusive mode\n" # Lock'n'load !
else:
print "Switching to stealth mode\n" # Shhhhh ...

hack = int(sys.argv[3]) + 1 # Ugly hack to prevent timeout issues on the last port analyzed.

for port in range(int(sys.argv[2]),hack): # Main loop
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Creating socket
sock.settimeout(float(sys.argv[4])) # Setting timeout
sock.connect((sys.argv[1], port)) # knock knock ?
print "Port open: %d" % port # "Who's there ?"
if sys.argv[5] == "intrusive": # Sarah connor ?
if port == 80: # Little extra
sock.send('GET / HTTP/1.1\nHost: %s\n\n' % sys.argv[1])
print "- Port successfully connected, attempting to get something ... :\n\n ---BEGIN SERVICE ---\n%s\n ---END SERVICE---\n" % sock.recv(1024) # Uh nope it's the other door, see ?
sock.close() # Be polite
except socket.error:
pass # If we fail to connect, don't care and go on
except KeyboardInterrupt:
print "\nBailing out ..." # HALP ME GETTING KILLED !!!
quit()

Classé dans : Python Mots clés : aucun