-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnmpede.py
572 lines (483 loc) · 26.7 KB
/
snmpede.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
from pysnmp.hlapi.v3arch.asyncio import *
#from pysnmp import debug
import argparse
import argcomplete
from csv import DictWriter
from os.path import exists
from sys import platform
import asyncio
from _modules.bulkwalk import *
from _modules import config
from _modules.get_multi import *
from _modules.get_single import *
from _modules.scan_port import *
from _modules.write import *
from _modules.helpers import *
import psutil
# MARK: Target Class
class Target:
def __init__(self, FQDN, IP, IPVersion, Port, SNMPVersion=None, CommunityString=None, Username=None, AuthPwd=None, AuthProto={'Name': None, 'Class': None}, PrivPwd=None, PrivProto={'Name': None, 'Class': None}, Access=False):
self.FQDN = FQDN # Some provided targets may not have a DNS FQDN. If they don't, this will be the IP address also.
self.IP = IP
self.IPVersion = IPVersion
self.SNMPVersion = SNMPVersion
self.CommunityString = CommunityString
self.Username = Username
self.AuthPwd = AuthPwd
self.AuthProto = AuthProto # ['Name': 'NicknameAlgo', 'Class': USMAlgoClass]
self.PrivPwd = PrivPwd
self.PrivProto = PrivProto # ['Name': 'NicknameAlgo', 'Class': USMAlgoClass]
self.Port = Port
# We'll use this below attribute to keep track of if an entry needs more spraying
# Like don't spray usernames on this instance if a community string was successfully found
# And vice versa.
self.Access = Access
# Add the new instance to the instances list
def __str__(self):
if self.PrivProto:
return f"[d] {self.FQDN}:{self.Port}/{self.SNMPVersion} via {self.CommunityString}/{self.Username}/{self.AuthPwd}/{self.AuthProto['Name']}/{self.PrivPwd}/{self.PrivProto['Name']}, Access: {self.Access}"
elif self.AuthProto:
return f"[d] {self.FQDN}:{self.Port}/{self.SNMPVersion} via {self.CommunityString}/{self.Username}/{self.AuthPwd}/{self.AuthProto['Name']}/None/None, Access: {self.Access}"
else:
return f"[d] {self.FQDN}:{self.Port}/{self.SNMPVersion} via {self.CommunityString}/{self.Username}/None/None/None/None, Access: {self.Access}"
@property
def IPVersion(self):
return self._IPVersion
@IPVersion.setter
def IPVersion(self, value):
if value not in ["v4", "v6", 'both']:
# If presented the option between v4 or v6, we mark it based upon the first resolved record's type
raise ValueError("IPVersion must be 'v4' or 'v6'.")
self._IPVersion = value
@property
def SNMPVersion(self):
return self._SNMPVersion
@SNMPVersion.setter
def SNMPVersion(self, value):
if value not in ["v1", "v2c", "v3"]:
raise ValueError("SNMPVersion must be 'v1', 'v2c, or 'v3'.")
self._SNMPVersion = value
def write_fieldnames(filepath, fieldnames):
if not exists(filepath):
with open(filepath, 'w') as file:
file.write(fieldnames)
def append_csv(filepath, fieldnames, data):
with open(filepath, 'a', encoding='utf-8', newline='') as csvfile:
writer = DictWriter(csvfile, fieldnames=fieldnames)
for d in data:
writer.writerow(d)
# MARK: Main
async def main():
parser = argparse.ArgumentParser(description='A modern and intelligent approach to SNMP hacking')
module_group = parser.add_argument_group('Modules')
#module_group.add_argument('--scan', action='store_true', help='Scan for SNMP services')
module_group.add_argument('-c', '--community', type=str, help='Login with a provided community string or line-delimited file')
module_group.add_argument('-u', '--username', type=str, help='Login with a username or line-delimited file')
module_group.add_argument('-p', '--password', type=str, help='Login with a password or line-delimited file') # Is used for auth and priv passwords/keys
module_group.add_argument('--bulkwalk', action='store_true', help='Collect as much information as possible')
#module_group.add_argument('--write', action='store_true', help='Check if write access is possible')
module_group.add_argument('--all', action='store_true', help='CAUTION: Use all above modules and default login dictionaries')
io_group = parser.add_argument_group('I/O Arguments')
io_group.add_argument('-t', '--target', type=str, help='Singular hostname or IPv4/IPv6 address or file containing line-delimited targets')
io_group.add_argument('-pt', '--port', default=161, help='Target port/range (e.g., 161 or 161,162 or 160-165)')
io_group.add_argument('-i', '--interface', type=str, help='Specify network interface (e.g., eth0, Ethernet0)')
io_group.add_argument('-eid', '--engine-id', type=str, help='Specify a hex agent engine ID (e.g., 0x80000000011234567890abcdef)')
io_group.add_argument('-o', '--output', type=str, default='SNMPede_', help='CSV prepended output filename/path')
io_group.add_argument('-d', '--debug', type=int, default=0, choices=[0, 1, 2], help='Debug level to stdout')
io_group.add_argument('-to', '--timeout', type=float, default=0.4, help='Timeout seconds')
io_group.add_argument('-rt', '--retries', type=int, default=0, help='Retries count')
io_group.add_argument('-dl', '--delay', type=float, default=0.3, help='Seconds delay between each request')
io_group.add_argument('-or', '--oid-read', type=str, default='1.3.6.1.2.1.1.1.0', help='OID the Login module will read (default is sysDescr.0)')
io_group.add_argument('-tk', '--tasks', type=int, default=10, help='Number of concurrent tasks')
#io_group.add_argument('-cb', '--combo', help='File containing line-delimited combos (host,port,username,password/string)')
#io_group.add_argument('-ow', '--oid-write', type=str, default='', help='')
argcomplete.autocomplete(parser)
args = parser.parse_args()
Target_instances = []
semaphore = asyncio.Semaphore(args.tasks)
await asyncio.sleep(0.2)
ascii_art = """
.▄▄ · ▐ ▄ • ▌ ▄ ·. ▄▄▄·▄▄▄ .·▄▄▄▄ ▄▄▄ .
▐█ ▀. •█▌▐█·██ ▐███▪▐█ ▄█▀▄.▀·██▪ ██ ▀▄.▀·
▄▀▀▀█▄▐█▐▐▌▐█ ▌▐▌▐█· ██▀·▐▀▀▪▄▐█· ▐█▌▐▀▀▪▄
▐█▄▪▐███▐█▌██ ██▌▐█▌▐█▪·•▐█▄▄▌██. ██ ▐█▄▄▌
▀▀▀▀ ▀▀ █▪▀▀ █▪▀▀▀.▀ ▀▀▀ ▀▀▀▀▀• ▀▀▀
"""
print(ascii_art)
# Update inter-module global variables in config.py with the provided args
config.ARGTIMEOUT, config.ARGRETRIES, config.ARGDELAY, config.ARGDEBUG, config.OID_READ = args.timeout, args.retries, args.delay, args.debug, args.oid_read
# Tier 2 debugging
#if config.ARGDEBUG >= 2:
#debug.setLogger(debug.Debug('all'))
# MARK: Exception Checks
if not args.target:
print("[e] No target was provided. Please add one.")
parser.print_help()
quit()
else:
# Convert the singular or multiple targets to a list
targets = await convert_to_list(args.target)
if config.ARGDEBUG >= 1: print("[d] Detected the following:\n[d] " + str(len(targets)) + ' target(s)')
tasks = []
for target in targets: # Returns (FQDN, IP, Version)
tasks.append(asyncio.create_task(resolve_target(target)))
if not (args.community or args.username or args.password or args.bulkwalk or args.all): #args.scan
print("[e] No module was selected. Please pick one:")
parser.print_help()
quit()
# Tell async task to raise an exception
for task in tasks:
task.add_done_callback(handle_task_result)
# Wait for name resolution to finish before continuing
try:
resolved_targets = await asyncio.gather(*tasks)
except Exception as e:
print(f"[e] A resolution error occurred: {e}")
quit()
# Check interface existance and compatibility
if args.interface:
# Get network interface addresses
interfaces = psutil.net_if_addrs()
# Check if the NIC name exists in the addresses
for interface_name, addresses in interfaces.items():
if interface_name.lower() == (args.interface).lower():
for address in addresses: # Single interface could have both an IPv4 and IPv6 address, or sadly multiple of each
if any('v4' in tup for tup in resolved_targets) and address.family == AF_INET:
config.INTERFACE_ADDR4 = address.address
elif any('v6' in tup for tup in resolved_targets) and address.family == AF_INET6:
config.INTERFACE_ADDR6 = address.address
if not config.INTERFACE_ADDR4 and not config.INTERFACE_ADDR6:
print("[e] Provided interface not found. Interfaces found:")
for interface_name, addresses in interfaces.items():
print(" '" + interface_name + "'")
quit()
elif any('v4' in tup for tup in resolved_targets) and not config.INTERFACE_ADDR4:
print("[e] An IPv4 target was provided, but the provided interface does not support IPv4.")
quit()
elif any('v6' in tup for tup in resolved_targets) and not config.INTERFACE_ADDR6:
print("[e] An IPv6 target was provided, but the provided interface does not support IPv6.")
quit()
elif config.ARGDEBUG >= 1:
print(f"[d] NIC: {args.interface}")
print(f"[d] IPv4 local address: {config.INTERFACE_ADDR4}")
print(f"[d] IPv6 local address: {config.INTERFACE_ADDR6}")
if args.all and not (args.community or args.username or args.password):
print("[e] Although the All module was specified, neither a community, username, or password was provided.")
parser.print_help()
quit()
# Convert the singular or multiple ports to a list
ports = await parse_ports(args.port)
if config.ARGDEBUG >= 1: print("[d] " + str(len(ports)) + ' port(s)')
if args.community:
# Convert the singular or multiple community strings to a list
community_strings = await convert_to_list(args.community)
if config.ARGDEBUG >= 1: print("[d] " + str(len(community_strings)) + ' community string(s)')
if args.username:
# Convert the singular or multiple values to a list
usernames = await convert_to_list(args.username)
if config.ARGDEBUG >= 1: print("[d] " + str(len(usernames)) + ' username(s)')
if args.password:
if not args.username:
print("[e] Password(s) detected, but no usernames detected. Please adjust.")
quit()
# Convert the singular or multiple passwords to a list
passwords = await convert_to_list(args.password)
if config.ARGDEBUG >= 1: print("[d] " + str(len(passwords)) + ' password(s)')
# SNMP v3 does not allow less than 8 character passwords. Error if we have any
if any(len(password) < 8 for password in passwords):
print("[e] SNMP v3 does not allow passwords less than 8 characters. Please adjust.")
quit()
# Agent engine ID requires `0x`, make sure it does:
if args.engine_id and not (args.engine_id).startswith('0x'):
config.ENGINE_ID = '0x' + args.engine_id
else:
config.ENGINE_ID = args.engine_id
# Map custom auth protocol to Name/Class
auth_protocols = [
{'Name': 'HMACMD5', 'Class': usmHMACMD5AuthProtocol},
{'Name': 'HMACSHA', 'Class': usmHMACSHAAuthProtocol},
{'Name': 'HMAC128SHA224', 'Class': usmHMAC128SHA224AuthProtocol},
{'Name': 'HMAC192SHA256', 'Class': usmHMAC192SHA256AuthProtocol},
{'Name': 'HMAC256SHA384', 'Class': usmHMAC256SHA384AuthProtocol},
{'Name': 'HMAC384SHA512', 'Class': usmHMAC384SHA512AuthProtocol}
]
# Map custom priv protocol to Name/Class
priv_protocols = [
{'Name': 'DES', 'Class': usmDESPrivProtocol},
{'Name': 'AESCFB128', 'Class': usmAesCfb128Protocol},
{'Name': 'AESCFB192', 'Class': usmAesCfb192Protocol},
{'Name': 'AESCFB256', 'Class': usmAesCfb256Protocol}
]
# MARK: Scan
# if args.scan or args.all:
# if config.ARGDEBUG >=1: print() # For pretty stdout
# print("[i] Performing SNMP port scanning...")
# tasks = []
# for target in resolved_targets:
# for port in ports:
# print(f'[-] Scanning {target[0]}:{port}')
# tasks.append(scan_port(target, port))
# outputfile = args.output + 'Scan.csv'
# if not exists(outputfile):
# write_fieldnames(outputfile, 'Host,Protocol,Port,Service,Status\n')
# # Tell async task to raise an exception
# for task in tasks:
# task.add_done_callback(handle_task_result)
#
# # Wait for port scanning to finish
# try:
# task_results = await asyncio.gather(*tasks)
# except Exception as e:
# print(f"[e] A scan error occurred: {e}")
# quit()
# print("[-] Writing results...")
# imported_instances = False
# for r, instances in task_results:
# append_csv(outputfile, ['Host', 'Protocol', 'Port', 'Service', 'Status'], r)
# if imported_instances is False:
# for instance in instances:
# Target_instances.append(instance)
# imported_instances = True
# if len(Target_instances) == 0:
# print(f"[i] No open port(s) found{quit_text}.\n")
# quit()
# else:
# print() # For pretty stdout
# If we are not spraying, but instead just checking authentication, let's clarify that:
if config.WASFILEIMPORTED:
intention = 'Spray'
else:
intention = 'Check'
# MARK: Comm_Strings
if args.community:
if config.ARGDEBUG >=1: print() # For pretty stdout # and not args.scan
print(f"[i] {intention}ing SNMP versions 1/2c community string(s)...")
tasks = []
task_results = []
# Perform v1 community string spraying/checking
if len(Target_instances) > 0:
if config.ARGDEBUG >= 1: print(f"[d] Using the existing ({len(Target_instances)}) instances:")
for id, instance in enumerate(Target_instances):
tasks.append(asyncio.create_task(snmp_v12c_get_multi(semaphore, id, (instance.FQDN, instance.IP, instance.IPVersion), instance.port, 'v1', community_strings, instance=instance)))
else:
id = 0
for target in resolved_targets:
for port in ports:
tasks.append(asyncio.create_task(snmp_v12c_get_multi(semaphore, id, target, port, 'v1', community_strings)))
id = id + 1
# Tell async task to raise an exception
for task in tasks:
task.add_done_callback(handle_task_result)
# Wait for community string v1 spraying/checking to finish before beginning v2c
try:
task_results.extend(await asyncio.gather(*tasks))
except Exception as e:
print(f"[e] A v1 community string {intention.lower()}ing error occurred: {e}")
quit()
# Perform v2c community string spraying/checking
task_results = []
if len(Target_instances) > 0:
for id, instance in enumerate(Target_instances):
tasks.append(asyncio.create_task(snmp_v12c_get_multi(semaphore, id, (instance.FQDN, instance.IP, instance.IPVersion), instance.port, 'v2c', community_strings, instance=instance)))
else:
id = 0
for target in resolved_targets:
for port in ports:
tasks.append(asyncio.create_task(snmp_v12c_get_multi(semaphore, id, target, port, 'v2c', community_strings)))
id = id + 1
# Tell async task to raise an exception
for task in tasks:
task.add_done_callback(handle_task_result)
# Wait for community string v2c spraying/checking to finish
try:
task_results.extend(await asyncio.gather(*tasks))
except Exception as e:
print(f"[e] A v2c community string {intention.lower()}ing error occurred: {e}")
quit()
outputfile = args.output + f"v12c_{intention}_Community_Strings.csv"
if not exists(outputfile):
write_fieldnames(outputfile, 'Host,Port,Version,CommunityString,OID,Value,Status\n')
print("[-] Writing results...")
for r, instances in task_results:
append_csv(outputfile, ['Host', 'Port', 'Version', 'CommunityString', 'OID', 'Value', 'Status'], r)
for instance in instances:
Target_instances.append(instance)
if len(Target_instances) == 0:
print(f"[i] No community string(s) found.\n")
quit()
else:
print() # For pretty stdout
# MARK: UserEnum
if args.username:
if config.ARGDEBUG >=1 and not args.community: print() # For pretty stdout # (args.scan or args.community)
print(f"[i] {intention}ing SNMP v3 username(s) with NoAuthNoPriv...")
tasks = []
task_results = []
instances = get_instances_with_attribute(Target_instances, 'Access', False)
if instances:
if config.ARGDEBUG >= 1: print(f"[d] Using the relevant ({len(instances)}) instances:")
for id, instance in enumerate(Target_instances):
tasks.append(asyncio.create_task(snmp_v3_get_multi(semaphore, id, (instance.FQDN, instance.IP, instance.IPVersion), instance.Port, usernames, instance=instance)))
else:
id = 0
for target in resolved_targets:
for port in ports:
tasks.append(asyncio.create_task(snmp_v3_get_multi(semaphore, id, target, port, usernames)))
id = id + 1
outputfile = args.output + f"v3_{intention}_Credentials.csv"
if not exists(outputfile):
write_fieldnames(outputfile, 'Host,Port,Version,Username,AuthPassword,AuthProtocol,PrivPassword,PrivProtocol,OID,Value,Status\n')
# Tell async task to raise an exception
for task in tasks:
task.add_done_callback(handle_task_result)
# Wait for username spraying/checking to finish
try:
task_results.extend(await asyncio.gather(*tasks))
except Exception as e:
print(f"[e] A username {intention.lower()}ing error occurred: {e}")
quit()
print("[-] Writing results...")
imported_instances = False
for r, instances in task_results:
append_csv(outputfile, ['Host', 'Port', 'Version', 'Username', 'AuthPassword', 'AuthProtocol', 'PrivPassword', 'PrivProtocol', 'OID', 'Value', 'Status'], r)
if imported_instances is False:
for instance in instances:
Target_instances.append(instance)
imported_instances = True
if len(Target_instances) == 0 or len(get_instances_with_attribute(Target_instances, 'Username')) == 0:
print(f"[i] No username(s) found.\n")
quit()
else:
print() # For pretty stdout
# MARK: AuthPwd
if args.password:
tasks = []
task_results = []
# Filter for any unfinished targets
instances = get_instances_with_attribute(Target_instances, 'Access', False)
# Filter for those with Username not None
instances = get_instances_with_attribute(instances, 'Username')
if instances:
print(f"[i] {intention}ing SNMP v3 password(s) with AuthNoPriv...")
if config.ARGDEBUG >= 1: print(f"[d] Using the relevant ({len(instances)}) instances:")
for id, instance in enumerate(instances):
tasks.append(asyncio.create_task(snmp_v3_get_multi(semaphore, id, (instance.FQDN, instance.IP, instance.IPVersion), instance.Port, instance.Username, authpasswords=passwords, authprotocols=auth_protocols, instance=instance)))
# Tell async task to raise an exception
for task in tasks:
task.add_done_callback(handle_task_result)
# Wait for auth spraying/checking to finish
try:
task_results.extend(await asyncio.gather(*tasks))
except Exception as e:
print(f"[e] An AuthNoPriv {intention.lower()}ing error occurred: {e}")
quit()
print("[-] Writing results...")
for r in task_results:
append_csv(outputfile, ['Host', 'Port', 'Version', 'Username', 'AuthPassword', 'AuthProtocol', 'PrivPassword', 'PrivProtocol', 'OID', 'Value', 'Status'], r)
if len(Target_instances) == 0 or len(get_instances_with_attribute(Target_instances, 'AuthPwd')) == 0:
print(f"[i] No AuthNoPriv password(s) found.\n")
quit()
else:
print() # For pretty stdout
else:
print(f"[i] Skipping {intention.lower()}ing AuthNoPriv as its unnecessary...\n")
# MARK: PrivPwd
# Doing async like this so that we don't DDOS a specific SNMP agent
# Filter for any unfinished targets
instances = get_instances_with_attribute(Target_instances, 'Access', False)
# Filter for those with AuthPwd not None
instances = get_instances_with_attribute(instances, 'AuthPwd')
if instances:
print(f"[i] {intention}ing SNMP v3 password(s) with AuthPriv...")
tasks = []
task_results = []
if config.ARGDEBUG >= 1: print(f"[d] Using the relevant ({len(instances)}) instances:")
for id, instance in enumerate(instances):
tasks.append(asyncio.create_task(snmp_v3_get_multi(semaphore, id, (instance.FQDN, instance.IP, instance.IPVersion), instance.Port, instance.Username, authpasswords=instance.AuthPwd, authprotocols=instance.AuthProto, privpasswords=passwords, privprotocols=priv_protocols, instance=instance)))
# Tell async task to raise an exception
for task in tasks:
task.add_done_callback(handle_task_result)
# Wait for AuthPriv spraying/checking to finish
try:
task_results.extend(await asyncio.gather(*tasks))
except Exception as e:
print(f"[e] An AuthPriv {intention.lower()}ing error occurred: {e}")
quit()
print("[-] Writing results...")
for r in task_results:
append_csv(outputfile, ['Host', 'Port', 'Version', 'Username', 'AuthPassword', 'AuthProtocol', 'PrivPassword', 'PrivProtocol', 'OID', 'Value', 'Status'], r)
if len(Target_instances) == 0 or len(get_instances_with_attribute(Target_instances, 'PrivPwd')) == 0:
print(f"[i] No AuthPriv password(s) found.\n")
else:
print() # For pretty stdout
else:
print(f"[i] Skipping {intention.lower()}ing AuthPriv as its unnecessary...\n")
# We need to cleanse no Access instances so that our post-access
# activities can be efficient
for instance in Target_instances:
if instance.Access == False:
Target_instances.remove(instance)
del instance
# MARK: BulkWalk
if args.bulkwalk or args.all:
if not Target_instances:
print("[i] No successful access was achieved. Skipping BulkWalk module.")
else:
# Doing async like this so that we don't DDOS a specific SNMP agent
instances = get_instances_with_attribute(Target_instances, 'CommunityString')
tasks = []
task_results = []
print("[i] BulkWalking v1/2c information...")
if instances:
if config.ARGDEBUG >= 1: print(f"[d] Using the relevant ({len(instances)}) v1/2c instances:")
for id, instance in enumerate(instances):
if config.ARGDEBUG >= 1: print(instance)
tasks.append(asyncio.create_task(snmp_v12c_bulkwalk(semaphore, id, instance)))
outputfile = args.output + 'v12c_BulkWalk.csv'
if not exists(outputfile):
write_fieldnames(outputfile, 'Host,Port,Version,CommunityString,OID,Value,Status\n')
# Tell async task to raise an exception
for task in tasks:
task.add_done_callback(handle_task_result)
# Wait for BulkWalk to finish
try:
task_results.extend(await asyncio.gather(*tasks))
except Exception as e:
print(f"[e] A v1/2c BulkWalk error occurred: {e}")
quit()
print("[-] Writing results...\n")
for r in task_results:
if r:
append_csv(outputfile, ['Host', 'Port', 'Version', 'CommunityString', 'OID', 'Value', 'Status'], r)
instances = get_instances_with_attribute(Target_instances, 'Username')
if instances:
tasks = []
task_results = []
print("[i] BulkWalking v3 information...")
if config.ARGDEBUG >= 1: print(f"[d] Using the relevant ({len(instances)}) v3 instances:")
for id, instance in enumerate(instances):
if config.ARGDEBUG >= 1: print(instance)
tasks.append(asyncio.create_task(snmp_v3_bulkwalk(semaphore, id, instance)))
outputfile = args.output + 'v3_BulkWalk.csv'
if not exists(outputfile):
write_fieldnames(outputfile, 'Host,Port,Username,AuthPassword,AuthProtocol,PrivPassword,PrivProtocol,OID,Value,Status\n')
# Tell async task to raise an exception
for task in tasks:
task.add_done_callback(handle_task_result)
# Wait for BulkWalk to finish
try:
task_results.extend(await asyncio.gather(*tasks))
except Exception as e:
print(f"[e] A v3 BulkWalk error occurred: {e}")
quit()
print("[-] Writing results...\n")
for r in task_results:
append_csv(outputfile, ['Host', 'Port', 'Username', 'AuthPassword', 'AuthProtocol', 'PrivPassword', 'PrivProtocol', 'OID', 'Value', 'Status'], r)
if __name__ == '__main__':
try:
if platform == 'win32':
# https://stackoverflow.com/questions/63860576/asyncio-event-loop-is-closed-when-using-asyncio-run
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
asyncio.run(main())
except KeyboardInterrupt:
print('[e] Program termination requested by user')