-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsocketsquirrel.py
410 lines (358 loc) · 16.2 KB
/
socketsquirrel.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/usr/bin/env python
import os
import sys
import math
import json
import errno
import paramiko
import argparse
import pandas as pd
import flask as flask
from flask import Flask, render_template
from getpass import getpass
from datetime import datetime
from multiprocessing.pool import ThreadPool
app = Flask(__name__, template_folder='templates')
class htmlDisplay:
def __init__(self, data):
self.data = data
def displayJSON(self):
return render_template('index.html', data=self.data)
def startFlask(data):
app.route('/')(htmlDisplay(data).displayJSON)
app.run()
def verifyUpload(sftp, location, file, host, port):
return_values = {}
try:
sftp.stat(f'{location}{file}')
return_values[f'{host}:{port}'] = {
'sftp': f'upload {location}{file} success'
}
return return_values
except Exception as e:
if e.errno == errno.ENOENT:
return_values[f'{host}:{port}'] = {
'sftp exception': f'upload {location}{file} failed'
}
return return_values
def verifyDownload(host, port, downloaded_file):
return_values = {}
for file in os.listdir("."):
if f"{host}-{downloaded_file}" == file:
return_values[f'{host}:{port}'] = {
'sftp': f'download {downloaded_file} success'
}
return return_values
return_values[f'{host}:{port}'] = {
'sftp exception': f'download {downloaded_file} failed'
}
return return_values
def sftpConnect(host, port, username, password, key, timeout, files, location, upload, download):
print(f" -> [{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}] {username}@{host}:{port}")
return_values = {}
ssh, return_values = sshEstablish(host, port, username, password, key, timeout)
if return_values:
return return_values
try:
sftp = ssh.open_sftp()
except Exception as e:
return_values[f'{host}:{port}'] = {'sftp exception':str(e)}
if download:
server_files = sftp.listdir(path=location)
if len(server_files) == 0:
return_values[f'{host}:{port}'] = {'sftp exception': f'no files found at {host}:{location}'}
return return_values
for file in files:
if file in server_files:
try:
sftp.get(f'{location}{file}', f'./{host}-{file}', callback=None)
return verifyDownload(host, port, file)
except Exception as e:
return_values[f'{host}:{port}'] = {'sftp exception':str(e)}
if len(return_values) == 0:
return_values[f'{host}:{port}'] = {'sftp exception': f'no files found location:{location} files:{files}'}
return return_values
if upload:
for file in files:
remote_file = file.split('/')[-1]
try:
sftp.put(localpath=file, remotepath=f"{location}{remote_file}", callback=None, confirm=False)
return verifyUpload(sftp, location, remote_file, host, port)
except Exception as e:
return_values[f'{host}:{port}'] = {'sftp exception':str(e)}
sftp.close()
ssh.close()
return return_values
def outputWriter(json):
file_name = datetime.now().strftime("%Y-%m-%d_%H-%M-%S-socketsquirrel-output.json")
with open(file_name, 'w') as file:
file.write(json.replace('\\n"','"'))
print(f"[+] output written to file: {file_name}")
def sshEstablish(host, port, username, password, key, timeout):
return_values = {}
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
ssh.load_system_host_keys()
try:
ssh.connect(host, port, username=username, password=password, pkey=key, timeout=timeout)
except Exception as e:
return_values[f'{host}:{port}'] = {'ssh exception':str(e)}
return ssh, return_values
def sshConnect(host, port, username, password, key, timeout, command, sudo):
print(f" -> [{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}] {username}@{host}:{port}")
return_values = {}
ssh, return_values = sshEstablish(host, port, username, password, key, timeout)
feed_password = False
if return_values:
return return_values
else:
try:
if sudo and username != "root":
command = f"sudo -S -p '' {command}"
feed_password = password is not None and len(password) > 0
stdin, stdout, stderr = ssh.exec_command(command)
if feed_password:
stdin.write(password + "\n")
stdin.flush()
stdin.close()
return_values[f'{host}:{port}'] = {
'ssh':{
'command': command,
'output': stdout.readlines(),
'error': stderr.readlines(),
'retvalue': stdout.channel.recv_exit_status()
}
}
except Exception as e:
return_values[f'{host}:{port}'] = {'ssh exception':str(e)}
ssh.close()
return return_values
def engine(cmd_args):
cmd_args['port'] = [22 if math.isnan(item) else item for item in cmd_args['port']]
if cmd_args['logging']:
paramiko.util.log_to_file(f'{datetime.now().strftime("%Y-%m-%d_%H-%M-%S")}-socketsquirrel.log')
with ThreadPool(cmd_args['threads']) as pool:
if cmd_args['protocol'] == 'ssh':
print("[+] starting ssh engine")
results = pool.starmap(sshConnect, [(host, int(port), cmd_args['username'], cmd_args['password'], cmd_args['key'], cmd_args['timeout'], cmd_args['exec'], cmd_args['sudo']) for host, port in zip(cmd_args['hosts'], cmd_args['port'])])
elif cmd_args['protocol'] == 'scp':
print("[+] starting scp engine")
results = pool.starmap(sftpConnect, [(host, int(port), cmd_args['username'], cmd_args['password'], cmd_args['key'], cmd_args['timeout'], cmd_args['files'], cmd_args['location'], cmd_args['upload'], cmd_args['download']) for host, port in zip(cmd_args['hosts'], cmd_args['port'])])
pool.close()
pool.join()
outputWriter(json.dumps(results, indent=4))
if cmd_args['webui']:
startFlask(results)
def csvParser(cmd_args, hosts):
print(f"[+] verifying {hosts} headers")
column_names = []
correct_headers = {
1: "hosts",
2: "hosts,port"
}
try:
df = pd.read_csv(hosts)
headers = df.columns.tolist()
header_count = len(headers)
header_values = ','.join(headers).replace(" ","")
except Exception as e:
print(f"[!] {e}")
sys.exit()
if header_count in correct_headers:
if header_values in correct_headers[header_count]:
print(f" -> headers: {header_values}")
else:
print(f"[!] incorrect format of headers")
print(f" -> {header_values}")
print(" -> correct header options:")
print(f"{correct_headers}")
sys.exit()
else:
print("[!] invalid count of headers")
print(f" -> {header_count}")
sys.exit()
print(f" -> host: {len(df)}")
column_names = correct_headers[header_count].split(',')
for column in column_names:
cmd_args[column].extend(df[column])
return cmd_args
def argumentParser():
print("[+] parsing cmd arguments")
VERSION = '1.0'
parser = argparse.ArgumentParser(
description=f"""
_____ _ _ _____ _ _
/ ____| | | | | / ____| (_) | |
| (___ ___ ___| | _____| |_| (___ __ _ _ _ _ _ __ _ __ ___| |
\___ \ / _ \ / __| |/ / _ \ __|\___ \ / _` | | | | | '__| '__/ _ \ |
____) | (_) | (__| < __/ |_ ____) | (_| | |_| | | | | | | __/ |
|_____/ \___/ \___|_|\_\___|\__|_____/ \__, |\__,_|_|_| |_| \___|_|
_________________________________________| |________________________
+++++++++++++++++++++++++++++++++++++++++|_|++++++++++++++++++++++++
©2023 masterofbrainstorming
Tool to help loop multiple SSH endpoints with ease.
Created for the breed of lazy auditors
Access, Upload, Dowload, Execute.
Version: {(VERSION)}
""",
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('command', choices=['ssh', 'scp'], help='The command to execute (ssh or scp)')
parser.add_argument('-d', '--destination', dest='host', nargs='+', help='Destination IP address or hostname', type=str)
parser.add_argument('-H','--hosts', dest='hosts', nargs='?', help='File containing IP:port separated by newline, if the port value is missing, default 22', type=str)
parser.add_argument('-p', '--port', dest='port', nargs='?', help='Will overwrite SSH port, default 22', type=int)
parser.add_argument('-u', '--user', dest='username', nargs='?', help='SSH username', type=str)
parser.add_argument('--password', dest='password', nargs='?', help='SSH password', type=str)
parser.add_argument('--rsa', dest='rsa', nargs='?', help='File location for RSA private key', type=str)
parser.add_argument('--enc', dest='enc', action='store_true', help='Add only if the private key requires a password', default=False)
parser.add_argument('-e', '--execute', dest='execute', nargs='*', help='Owerwrite command to be executed', type=str)
parser.add_argument('-S', '--sudo', dest='sudo', action='store_true', help='Execute command with sudo', default=False)
# SCP only
parser.add_argument('--up', '--upload', dest='upload', action='store_true', help='Files to be uploaded to hosts', default=False)
parser.add_argument('--dl', '--download', dest='download', action='store_true', help='Files to be downloaded from hosts', default=False)
parser.add_argument('-f', '--files', dest='files', nargs='+', help='Files to be downloaded from hosts')
parser.add_argument('-l', '--location', dest='location', nargs='?', help='Files to be uploaded or downloaded from hosts')
parser.add_argument('-T', '--timeout', dest='timeout', nargs='?', help='Timeout for the connection. Default is 1s', type=int)
parser.add_argument('-t', '--threads', dest='threads', nargs='?', help='Thread pool count for ssh client, default is 8', type=int)
parser.add_argument('-L', '--log', dest='logging', action='store_true', help='Enable log file, default False', default=False)
parser.add_argument('-w', '--web', dest='webui', action='store_true', help='Enable webUI, default False', default=False)
args = parser.parse_args()
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
args = parser.parse_args()
return args
def optionsPrinter(cmd_args):
print(f"[+] {cmd_args['protocol']}")
print(f" -> targets: {cmd_args['hosts']}")
print(f" -> ports: {cmd_args['port']}")
print(f" -> username: {cmd_args['username']}")
print(f" -> password: {cmd_args['password']}")
print(f" -> private key: {cmd_args['rsa']}")
print(f" --> encrypted: {cmd_args['enc']}")
if cmd_args['protocol'] == 'ssh':
print(f" -> sudo: {cmd_args['enc']}")
print(f" -> command: {cmd_args['enc']}")
elif cmd_args['protocol'] == 'scp':
print(f" -> files: {cmd_args['files']}")
print(f" -> location: {cmd_args['location']}")
print(f" -> upload: {cmd_args['upload']}")
print(f" -> download: {cmd_args['download']}")
print(f" -> timeout: {cmd_args['timeout']}")
print(f" -> threads: {cmd_args['threads']}")
print(f" -> threads: {cmd_args['logging']}")
def optionsParser(args):
print(" -> parsing options")
cmd_args = {
'protocol':'',
'hosts':[],
'port':[],
'username':'',
'password':'',
'rsa':None,
'enc':None,
'key':None,
'sudo':None,
'exec':'pwd',
'timeout':1,
'threads':8,
'webui':None,
'logging':None,
'files':[],
'upload':None,
'download':None,
'location':''
}
cmd_args['protocol'] = args.command
if args.host or args.hosts:
if args.host:
cmd_args['hosts'] = args.host
if args.hosts:
print(f"[+] using hosts file: {args.hosts} contents")
if os.path.exists(args.hosts):
print(f" -> {args.hosts} exists")
else:
print(f"[!] file does not exist {args.hosts}")
sys.exit()
cmd_args = csvParser(cmd_args, args.hosts)
if args.port:
print(f"[!] port override: {args.port}")
cmd_args['port'].clear()
length = len(cmd_args['hosts'])
cmd_args['port'].extend([args.port] * length)
if args.username:
cmd_args['username'] = args.username
if args.password or args.rsa:
if args.password:
cmd_args['password'] = args.password
if args.rsa:
if args.enc:
cmd_args['enc'] = args.enc
cmd_args['rsa'] = args.rsa
if args.timeout:
cmd_args['timeout'] = args.timeout
if args.threads:
cmd_args['threads'] = args.threads
if args.logging:
cmd_args['logging'] = args.logging
if args.webui:
cmd_args['webui'] = args.webui
else:
print(f"[!] no hosts were provided")
if args.command == 'ssh':
if args.sudo:
print(f"[!] privilege override: {args.sudo}")
cmd_args['sudo'] = args.sudo
if args.execute:
print(f"[!] command override: {args.execute}")
cmd_args['exec'] = ' '.join(args.execute)
print(cmd_args['exec'])
elif args.command == 'scp':
if not args.location:
print('[!] location must be set when using scp')
sys.exit()
elif args.location.endswith('/'):
cmd_args['location'] = args.location
else:
cmd_args['location'] = args.location + '/'
if (args.upload or args.download) and (args.files):
if args.upload:
cmd_args['upload'] = args.upload
for file in args.files:
if os.path.exists(file):
cmd_args['files'] = args.files
else:
print(f"[!] file {file} does not exist!")
if len(cmd_args['files']) == 0:
print("[!] no files to upload")
sys.exit()
if args.download:
cmd_args['download'] = args.download
cmd_args['files'] = args.files
else:
print("[!] files and download or upload must be defined")
sys.exit()
else:
print("[!] error occured during parsing options")
return cmd_args
def main(cmd_args):
optionsPrinter(cmd_args)
if cmd_args['rsa']:
if cmd_args['enc']:
print('[!] unlock rsa key')
passphrase = getpass(prompt=' -> password: ', stream=None)
cmd_args['key'] = paramiko.RSAKey.from_private_key_file(cmd_args['rsa'], password=passphrase)
else:
cmd_args['key'] = paramiko.RSAKey.from_private_key_file(cmd_args['rsa'], None)
if ((cmd_args['password'] or cmd_args['rsa']) and not cmd_args['username']):
print("[!] missing username")
cmd_args['username'] = input(" -> username: ")
elif (cmd_args['username'] and not cmd_args['password'] or ((cmd_args['username'] and cmd_args['rsa'] and cmd_args['sudo']) and not cmd_args['password'])):
print("[!] missing password")
cmd_args['password'] = getpass(prompt=' -> password: ', stream=None)
else:
engine(cmd_args)
print("[+] execution finished")
if __name__ == "__main__":
args = argumentParser()
cmd_args = optionsParser(args)
main(cmd_args)