Path: blob/master/modules/post/multi/recon/local_exploit_suggester.rb
27985 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post67include Msf::Auxiliary::Report89def initialize(info = {})10super(11update_info(12info,13'Name' => 'Multi Recon Local Exploit Suggester',14'Description' => %q{15This module suggests local Metasploit exploits that can be used.1617The exploits are suggested based on the architecture and platform18that the user has a shell opened as well as the available exploits19in meterpreter.2021It's important to note that not all local exploits will be fired.22Exploits are chosen based on these conditions: session type,23platform, architecture, and required default options.24},25'License' => MSF_LICENSE,26'Author' => [ 'sinn3r', 'Mo' ],27'Platform' => all_platforms,28'SessionTypes' => [ 'meterpreter', 'shell' ],29'Notes' => {30'Stability' => [SERVICE_RESOURCE_LOSS],31'SideEffects' => [IOC_IN_LOGS, ARTIFACTS_ON_DISK, CONFIG_CHANGES],32'Reliability' => []33}34)35)36register_options([37Msf::OptInt.new('SESSION', [ true, 'The session to run this module on' ]),38Msf::OptBool.new('SHOWDESCRIPTION', [true, 'Displays a detailed description for the available exploits', false])39])4041register_advanced_options([42Msf::OptBool.new('ValidateArch', [true, 'Validate architecture', true]),43Msf::OptBool.new('ValidatePlatform', [true, 'Validate platform', true]),44Msf::OptBool.new('ValidateMeterpreterCommands', [true, 'Validate Meterpreter commands', false]),45Msf::OptString.new('Colors', [false, 'Valid, Invalid and Ignored colors for module checks (unset to disable)', 'grn/red/blu'])46])47end4849def all_platforms50Msf::Module::Platform.subclasses.collect { |c| c.realname.downcase }51end5253def session_arch54# Prefer calling native arch when available, as most LPEs will require this (e.g. x86, x64) as opposed to Java/Python Meterpreter's values (e.g. Java, Python)55session.respond_to?(:native_arch) ? session.native_arch : session.arch56end5758def is_module_arch?(mod)59mod_arch = mod.target.arch || mod.arch60mod_arch.include?(session_arch)61end6263def is_module_wanted?(mod)64mod[:result][:incompatibility_reasons].empty?65end6667def is_session_type?(mod)68# There are some modules that do not define any compatible session types.69# We could assume that means the module can run on all session types,70# Or we could consider that as incorrect module metadata.71mod.session_types.include?(session.type)72end7374def is_module_platform?(mod)75return false if mod.target.nil?7677platform_obj = Msf::Module::Platform.find_platform session.platform7879module_platforms = mod.target.platform ? mod.target.platform.platforms : mod.platform.platforms80module_platforms.include? platform_obj81rescue ArgumentError => e82# When not found, find_platform raises an ArgumentError83elog('Could not find a platform', error: e)84return false85end8687def has_required_module_options?(mod)88get_all_missing_module_options(mod).empty?89end9091def get_all_missing_module_options(mod)92missing_options = []93mod.options.each_pair do |option_name, option|94missing_options << option_name if option.required && option.default.nil? && mod.datastore[option_name].blank?95end96missing_options97end9899def valid_incompatibility_reasons(mod, verify_reasons)100# As we can potentially ignore some `reasons` (e.g. accepting arch values which are, on paper, not compatible),101# this keeps track of valid reasons why we will not consider the module that we are evaluating to be valid.102valid_reasons = []103valid_reasons << "Missing required module options (#{get_all_missing_module_options(mod).join('. ')})" unless verify_reasons[:has_required_module_options]104105incompatible_opts = []106incompatible_opts << 'architecture' unless verify_reasons[:is_module_arch]107incompatible_opts << 'platform' unless verify_reasons[:is_module_platform]108incompatible_opts << 'session type' unless verify_reasons[:is_session_type]109valid_reasons << "Not Compatible (#{incompatible_opts.join(', ')})" if incompatible_opts.any?110111valid_reasons << 'Missing/unloadable Meterpreter commands' if verify_reasons[:missing_meterpreter_commands].any?112valid_reasons113end114115def set_module_options(mod)116ignore_list = ['ACTION', 'TARGET'].freeze117datastore.each_pair do |k, v|118mod.datastore[k] = v unless ignore_list.include?(k.upcase)119end120if !mod.datastore['SESSION'] && session.present?121mod.datastore['SESSION'] = session.sid122end123end124125def set_module_target(mod)126session_platform = Msf::Module::Platform.find_platform(session.platform)127target_index = mod.targets.find_index do |target|128# If the target doesn't define its own compatible platforms or architectures, default to the parent (module) values.129target_platforms = target.platform&.platforms || mod.platform.platforms130target_architectures = target.arch || mod.arch131132target_platforms.include?(session_platform) && target_architectures.include?(session_arch)133end134mod.datastore['Target'] = target_index if target_index135end136137def setup138return unless session139140print_status "Collecting local exploits for #{session.session_type}..."141142setup_validation_options143setup_color_options144145# Collects exploits into an array146@local_exploits = []147exploit_refnames = framework.exploits.module_refnames148exploit_refnames.each_with_index do |name, index|149print "%bld%blu[*]%clr Collecting exploit #{index + 1} / #{exploit_refnames.count}\r"150mod = framework.exploits.create name151next unless mod152153set_module_options mod154set_module_target mod155156verify_result = verify_mod(mod)157@local_exploits << { module: mod, result: verify_result } if verify_result[:has_check]158end159end160161def verify_mod(mod)162return { has_check: false } unless mod.is_a?(Msf::Exploit::Local) && mod.has_check?163164result = {165has_check: true,166is_module_platform: (@validate_platform ? is_module_platform?(mod) : true),167is_module_arch: (@validate_arch ? is_module_arch?(mod) : true),168has_required_module_options: has_required_module_options?(mod),169missing_meterpreter_commands: (@validate_meterpreter_commands && session.type == 'meterpreter') ? meterpreter_session_incompatibility_reasons(session) : [],170is_session_type: is_session_type?(mod)171}172result[:incompatibility_reasons] = valid_incompatibility_reasons(mod, result)173result174end175176def setup_validation_options177@validate_arch = datastore['ValidateArch']178@validate_platform = datastore['ValidatePlatform']179@validate_meterpreter_commands = datastore['ValidateMeterpreterCommands']180end181182def setup_color_options183@valid_color, @invalid_color, @ignored_color =184(datastore['Colors'] || '').split('/')185186@valid_color = "%#{@valid_color}" unless @valid_color.blank?187@invalid_color = "%#{@invalid_color}" unless @invalid_color.blank?188@ignored_color = "%#{@ignored_color}" unless @ignored_color.blank?189end190191def show_found_exploits192unless datastore['VERBOSE']193print_status "#{@local_exploits.length} exploit checks are being tried..."194return195end196197vprint_status "The following #{@local_exploits.length} exploit checks are being tried:"198@local_exploits.each do |x|199vprint_status x[:module].fullname200end201end202203def run204runnable_exploits = @local_exploits.select { |mod| is_module_wanted?(mod) }205if runnable_exploits.empty?206print_error 'No suggestions available.'207vprint_line208vprint_session_info209vprint_status unwanted_modules_table(@local_exploits.reject { |mod| is_module_wanted?(mod) })210return211end212213show_found_exploits214results = runnable_exploits.map.with_index do |mod, index|215print "%bld%blu[*]%clr Running check method for exploit #{index + 1} / #{runnable_exploits.count}\r"216begin217checkcode = mod[:module].check218rescue StandardError => e219elog("#Local Exploit Suggester failed with: #{e.class} when using #{mod[:module].shortname}", error: e)220vprint_error "Check with module #{mod[:module].fullname} failed with error #{e.class}"221next { module: mod[:module], errors: ['The check raised an exception.'] }222end223224if checkcode.nil?225vprint_error "Check failed with #{mod[:module].fullname} for unknown reasons"226next { module: mod[:module], errors: ['The check failed for unknown reasons.'] }227end228229# See def is_check_interesting?230unless is_check_interesting? checkcode231vprint_status "#{mod[:module].fullname}: #{checkcode.message}"232next { module: mod[:module], errors: [checkcode.message] }233end234235# Prints the full name and the checkcode message for the exploit236print_good "#{mod[:module].fullname}: #{checkcode.message}"237238# If the datastore option is true, a detailed description will show239if datastore['SHOWDESCRIPTION']240# Formatting for the description text241Rex::Text.wordwrap(Rex::Text.compress(mod[:module].description), 2, 70).split(/\n/).each do |line|242print_line line243end244end245246next { module: mod[:module], checkcode: checkcode.message }247end248249print_line250print_status valid_modules_table(results)251252vprint_line253vprint_session_info254vprint_status unwanted_modules_table(@local_exploits.reject { |mod| is_module_wanted?(mod) })255256report_data = []257results.each do |result|258report_data << [result[:module].fullname, result[:checkcode]] if result[:checkcode]259end260report_note(261host: session.session_host,262type: 'local.suggested_exploits',263data: { suggested_exploits: report_data }264)265end266267def valid_modules_table(results)268name_styler = ::Msf::Ui::Console::TablePrint::CustomColorStyler.new269check_styler = ::Msf::Ui::Console::TablePrint::CustomColorStyler.new270271# Split all the results by their checkcode.272# We want the modules that returned a checkcode to be at the top.273checkcode_rows, without_checkcode_rows = results.partition { |result| result[:checkcode] }274rows = (checkcode_rows + without_checkcode_rows).map.with_index do |result, index|275color = result[:checkcode] ? @valid_color : @invalid_color276check_res = result.fetch(:checkcode) { result[:errors].join('. ') }277name_styler.merge!({ result[:module].fullname => color })278check_styler.merge!({ check_res => color })279280[281index + 1,282result[:module].fullname,283result[:checkcode] ? 'Yes' : 'No',284check_res285]286end287288Rex::Text::Table.new(289'Header' => "Valid modules for session #{session.sid}:",290'Indent' => 1,291'Columns' => [ '#', 'Name', 'Potentially Vulnerable?', 'Check Result' ],292'SortIndex' => -1,293'WordWrap' => false, # Don't wordwrap as it messes up coloured output when it is broken up into more than one line294'ColProps' => {295'Name' => {296'Stylers' => [name_styler]297},298'Potentially Vulnerable?' => {299'Stylers' => [::Msf::Ui::Console::TablePrint::CustomColorStyler.new({ 'Yes' => @valid_color, 'No' => @invalid_color })]300},301'Check Result' => {302'Stylers' => [check_styler]303}304},305'Rows' => rows306)307end308309def unwanted_modules_table(unwanted_modules)310arch_styler = ::Msf::Ui::Console::TablePrint::CustomColorStyler.new311platform_styler = ::Msf::Ui::Console::TablePrint::CustomColorStyler.new312session_type_styler = ::Msf::Ui::Console::TablePrint::CustomColorStyler.new313314rows = unwanted_modules.map.with_index do |mod, index|315platforms = mod[:module].target.platform&.platforms&.any? ? mod[:module].target.platform.platforms : mod[:module].platform.platforms316platforms ||= []317arch = mod[:module].target.arch&.any? ? mod[:module].target.arch : mod[:module].arch318arch ||= []319320arch.each do |a|321if a != session_arch322if @validate_arch323color = @invalid_color324else325color = @ignored_color326end327else328color = @valid_color329end330331arch_styler.merge!({ a.to_s => color })332end333334platforms.each do |module_platform|335if module_platform != ::Msf::Module::Platform.find_platform(session.platform)336if @validate_platform337color = @invalid_color338else339color = @ignored_color340end341else342color = @valid_color343end344345platform_styler.merge!({ module_platform.realname => color })346end347348mod[:module].session_types.each do |session_type|349color = session_type == session.type ? @valid_color : @invalid_color350session_type_styler.merge!(session_type.to_s => color)351end352353[354index + 1,355mod[:module].fullname,356mod[:result][:incompatibility_reasons].join('. '),357platforms.map(&:realname).sort.join(', '),358arch.any? ? arch.sort.join(', ') : 'No defined architectures',359mod[:module].session_types.any? ? mod[:module].session_types.sort.join(', ') : 'No defined session types'360]361end362363Rex::Text::Table.new(364'Header' => "Incompatible modules for session #{session.sid}:",365'Indent' => 1,366'Columns' => [ '#', 'Name', 'Reasons', 'Platform', 'Architecture', 'Session Type' ],367'WordWrap' => false,368'ColProps' => {369'Architecture' => {370'Stylers' => [arch_styler]371},372'Platform' => {373'Stylers' => [platform_styler]374},375'Session Type' => {376'Stylers' => [session_type_styler]377}378},379'Rows' => rows380)381end382383def vprint_session_info384vprint_status 'Current Session Info:'385vprint_status "Session Type: #{session.type}"386vprint_status "Architecture: #{session_arch}"387vprint_status "Platform: #{session.platform}"388end389390def is_check_interesting?(checkcode)391[392Msf::Exploit::CheckCode::Vulnerable,393Msf::Exploit::CheckCode::Appears,394Msf::Exploit::CheckCode::Detected395].include? checkcode396end397398def print_status(msg = '')399super(session ? "#{session.session_host} - #{msg}" : msg)400end401402def print_good(msg = '')403super(session ? "#{session.session_host} - #{msg}" : msg)404end405406def print_error(msg = '')407super(session ? "#{session.session_host} - #{msg}" : msg)408end409end410411412