PDA

View Full Version : My First Python Program Is Ready



bobbocanfly
October 6th, 2007, 10:13 AM
Been teaching myself Python through the link in one of the people here's sig and made my first useful program, a port scanner.



#Tiny-Scan
#Version 0.2.0
#A tiny Python port scanner with basic port use info
#Copyright (C) Bobbocanfly

import socket,sys
i = raw_input("IP? ")
a = int(raw_input("Start Port? "))
b = int(raw_input("End Port? "))
c = {'20': 'FTP Data', '21': 'FTP', '22': 'SSH', '23' : 'Telnet', '25' : 'SMTP', '53' : 'DNS', '66' : 'SQL*NET', '79' : 'Finger', '80' : 'HTTP', '110' : 'Pop3' }
for p in range(a, b + 1):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((i, p))
print p," Open"
if c.has_key(str(p)):
print p, " Normally ", c[str(p)]
except socket.error:
print p," Closed"


The only thing i could think of doing to it would be to get options from the command line and extend the "port-dictionary".

Compyx
October 6th, 2007, 11:23 AM
Well, if you want to look at parsing command line options, I would recommend looking into optparse: http://docs.python.org/lib/module-optparse.html, it's a pretty nice module for scanning the command line for options and arguments.

It's quite powerful and flexible and follows the standard GNU/POSIX syntax for specifying options and arguments.

zanglang
October 6th, 2007, 11:25 AM
The only thing i could think of doing to it would be to get options from the command line and extend the "port-dictionary".

Just a thought, you can probably try and parse /etc/services and read the port mappings and comments from there.

bobbocanfly
October 6th, 2007, 01:09 PM
Just a thought, you can probably try and parse /etc/services and read the port mappings and comments from there.

Thanks for the suggestion but im trying to keep it as cross platform as possible (Tested working on Windows about 10 minutes ago).

Compyx, thanks for the link ill try and work that in later. :)

slavik
October 6th, 2007, 03:27 PM
if you want something fun, take a look at nmap source code ;)

Billy the Kid
October 6th, 2007, 10:44 PM
If you want something simple, try replacing the values of variables i, a and b with:


i = sys.argv[1]
a = int(sys.argv[2])
b = int(sys.argv[3])

You would then be able to specify the options from the command line, for example:
python tiny-scan.py 127.0.0.1 80 120
Where "127.0.0.1" would be the host to scan, 80 would be the start port and 120 would be the end port

slavik
October 7th, 2007, 03:52 AM
If you want something simple, try replacing the values of variables i, a and b with:


i = sys.argv[1]
a = int(sys.argv[2])
b = int(sys.argv[3])

You would then be able to specify the options from the command line, for example:
python tiny-scan.py 127.0.0.1 80 120
Where "127.0.0.1" would be the host to scan, 80 would be the start port and 120 would be the end port
I would suggest using a getopt library.

cwaldbieser
October 8th, 2007, 03:44 AM
Thanks for the suggestion but im trying to keep it as cross platform as possible (Tested working on Windows about 10 minutes ago).

Compyx, thanks for the link ill try and work that in later. :)

I think Windows has an /etc/services file buried in Windows/system32/drivers (or something similar depending on the version of Windows).