forked from chprice/PortScanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportscan.py
126 lines (113 loc) · 3.6 KB
/
portscan.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import socket, sys, random, threading, time
#---global---
locky = threading.Lock()
master = []
numThreads = 0
class Scanner(threading.Thread):
def __init__(self, addr, portList):
threading.Thread.__init__(self)
self.addr = addr
self.portList = portList
self.goodPort = []
def run(self):
global locky
print("Scanning: "+self.addr)
for port in self.portList:
if(scan(self.addr, port)):
self.goodPort.append(port)
global numThreads
global master
locky.acquire()
if(len(self.goodPort)!=0):
master.append([self.addr, self.goodPort])
numThreads=numThreads-1
locky.release()
print("Thread for", self.addr, "ending now")
def scan(address, port):
s = socket.socket()
s.settimeout(1.0)
try:
s.connect((address, port))
return True
except socket.error:
return False
except socket.timeout:
return False
s.close()
def scanRange(ip, numComputers, portList):
count=0
global master
global numThreads
addr = ip
while count < numComputers:
numThreads = numThreads + 1 # this may be a race condition
Scanner(addr, portList).start()
addr = incr(addr.split("."))
count = count+1
while (numThreads != 0):
#print(numThreads)
derp = False
print("Done scanning")
return master, addr
def incr(ip):
d = int(ip[3])+1
c = int(ip[2])
b = int(ip[1])
a = int(ip[0])
if(d==256):
c = c+1
d=0
if(c==256):
b= b+1
c=0
if(b==256):
a=a+1
b=0
if(a==256):
print("Fuck you >__>")
return str(a) +"."+ str(b) +"."+ str(c) +"."+ str(d)
def portListToString(portList):
s = ""
for port in portList:
s = s+" "+str(port)
return s
def listDump(name, ipList, timeTaken):
try:
filehandle = open(name, "w")
except IOError:
print("Can't dump ips")
return
filehandle.write("Time taken "+str(timeTaken)+"\n")
for ip in ipList:
filehandle.write(ip[0]+" "+portListToString(ip[1])+"\n")
filehandle.close()
print("Welcome to the port scanner I wrote in like 15 minutes.")
choice = raw_input("Pick a type of scan: scan, random, (or type help) : ")
if(choice == "scan"):
I = raw_input("Input a starting ip address ")
N = int(raw_input("Input the number of computers you want to scan "))
tempP = raw_input("Input a list of ports you want to be scanned seperated by a comma (but no spaces, I'm lazy) ").split(",")
P=[]
for eh in tempP:
P.append(int(eh))
elif(choice == "random"):
N = random.randint(100, 5000)
P = [21, 80]
I=str(random.randint(0,255)) + "." + str(random.randint(0,255)) + "." + str(random.randint(0,255)) + "." + str(random.randint(0,255))
else:
print("scan: scan a specific range of computers.")
print("random: scans a random set of computers, and scans 10-200 computers")
print("help: prints this message")
exit(0)
fileName = raw_input("Input a filename you want me to dump the results to (or - to use standard output) (just hit return to default) ")
startTime = time.time()
data, finalIP = scanRange(I, N, P)
timeTaken = time.time() - startTime
if(fileName == "-"):
print("Time taken:", timeTaken)
for line in data:
print(line[0]+" "+portListToString(line[1]))
elif(fileName == "" or fileName == "\n"):
listDump(I+"-"+finalIP+".txt", data, timeTaken)
else:
listDump(fileName, data, timeTaken)