-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindSSH.py
More file actions
96 lines (76 loc) · 2.72 KB
/
findSSH.py
File metadata and controls
96 lines (76 loc) · 2.72 KB
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
import socket
import threading
import sys
class bruteThread (threading.Thread):
def __init__(self, threadId, numThreads, listIPs):
threading.Thread.__init__(self)
self.threadId = threadId
self.numThreads = numThreads
self.listIPs = listIPs
def run(self):
findSSH(self.threadId, self.numThreads, self.listIPs)
def iterIPs(start, end):
resultList = []
print (start, end)
class0 = list(map(int, start.split(".")))
class1 = list(map(int, end.split(".")))
for classA in range(class0[0], class1[0] + 1, 1):
for classB in range(class0[1], class1[1] + 1, 1):
for classC in range(class0[2], class1[2] + 1, 1):
for classD in range(class0[3], class1[3] + 1, 1):
resultList.append(str(classA) + "." + str(classB) + "." + str(classC) + "." + str(classD))
return resultList
def findSSH(threadId, numThreads, listIPs):
closedPorts = open("closed-" + str(threadId), "a+")
openPorts = open("open-" + str(threadId), "a+")
seenIP = True
for j in range(threadId, len(listIPs), numThreads):
ip = listIPs[j]
if seenIP == True:
with open("closed-" + str(threadId), 'r') as f:
broken = False
for line in f:
if ip in line:
broken = True
break
if broken == True:
continue
else:
seenIP = False
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(2)
result = sock.connect_ex((ip, 22))
if result == 0:
print("port open: " + ip)
openPorts.write(ip + "\n")
openPorts.flush()
else:
closedPorts.write(ip + "\n")
closedPorts.flush()
closedPorts.close()
openPorts.close()
def main(numThreads):
rangeIPs = []
with open(sys.argv[2]) as f:
for line in f:
rangeIPs.append([line.split(",")[0], line.split(",")[1]])
for i in range(len(rangeIPs)):
start = rangeIPs[i][0]
end = rangeIPs[i][1]
listIPs = iterIPs(start, end.strip())
threadList = []
for i in range(numThreads):
threadList.append(bruteThread(i, numThreads, listIPs))
threadList[i].start()
for t in threadList:
t.join()
# remove the first line of the range IPs.
f = open(sys.argv[2], 'w')
for j in range(1, len(rangeIPs), 1):
ips = rangeIPs[j]
f.write(ips[0] + "," + ips[1])
f.close()
if len(sys.argv) == 3:
main(int(sys.argv[1]))
else:
print("Usage: python3 findSSH.py NUM_THREADS COUNTRY")