Created
October 28, 2011 04:46
-
-
Save btoews/1321647 to your computer and use it in GitHub Desktop.
Portscanner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import random | |
#Default source ports | |
default_sport = [80,443,123] | |
#Default source addresses | |
default_saddr = ["192.168.1.1"] | |
def parse_list(string): | |
#We receive an undefined variable | |
if not string: | |
return string | |
#We received stuff in form: "123-234" | |
elif not not string.count('-'): | |
if string.count('.'): | |
raise Exception("Please specify IP address ranges using CIDR (192.168.0.1/24)") | |
r = [int(i) for i in string.split('-')] | |
output = range(r[0],r[1]+1) | |
#We received stuff in form: "123,234,345,456,567" | |
elif not not string.count(','): | |
try: | |
output = [int(i) for i in string.split(',')] | |
#Trying to parse IP addresses | |
except ValueError: | |
output = [parse_list(i) for i in string.split(',')] | |
#Try to proccess CIDR Address | |
elif string.count('/'): | |
#break up mask and addr | |
addr,mask = string.split('/') | |
#convert mask to binary mask | |
netmask = (2**int(mask)-1) << (32 - int(mask)) | |
#make wildcard mask from net mask | |
wildcardmask = netmask ^ 2**32-1 | |
#turn addr to list of ints | |
addr = [int(i) for i in addr.split('.')] | |
int_addr = 0 | |
#bit address from address | |
for i in range(0,4): | |
int_addr += addr[i] << ((3-i)*8) | |
#find range from address and mask | |
int_output = [(int_addr & netmask) + i for i in range(0,wildcardmask+1)] | |
output = ['.'.join([str(int_addr >> (3-x) * 8 & 0xFF) for x in range(0,4)]) for int_addr in int_output] | |
#We received stuff in form: "123" | |
else: | |
output = [int(string)] | |
return output | |
if __name__ == "__main__": | |
dest = [] | |
src = [] | |
dports=[] | |
sports=[] | |
#configure argument parser | |
parser = argparse.ArgumentParser(description = 'Scan some ports... Fuck some IDSs') | |
parser.add_argument('-s',metavar='source_ip',help='IP Address(es) to scan from (single, comma separated list, or slash notated CIDR address)',required=True) | |
parser.add_argument('-d',metavar='destination_ip',help='IP Address(es) to scan (single, comma separated list, or slash notated CIDR address)',required=True) | |
parser.add_argument('-S',metavar='source_ports',help='TCP port(s) to scan from (single, comma separated, or range in form 80-120)') | |
parser.add_argument('-D',metavar='destination_ports',help='TCP port(s) to scan (single, comma separated, or range in form 80-120)',required=True) | |
#parse arguments | |
args = parser.parse_args() | |
#parse integer ranges into list | |
saddr = parse_list(args.s) | |
#if they didn't specify we will assign defaults | |
saddr = saddr if saddr else default_saddr | |
daddr = parse_list(args.d) | |
sport = parse_list(args.S) | |
#if they didn't specify we will assign defaults | |
sport = sport if sport else default_sport | |
dport = parse_list(args.D) | |
for da in daddr: | |
for dp in dport: | |
sa = saddr[random.randint(0,len(saddr)-1)] | |
sp = sport[random.randint(0,len(sport)-1)] | |
print "\ | |
Source Address: %s\n\ | |
Source Port: %d\n\ | |
Destination Address: %s\n\ | |
Destination Port: %d\n\n"%\ | |
(sa,\ | |
sp,\ | |
da,\ | |
dp) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment