|
| 1 | +## |
| 2 | +# This module requires Metasploit: http://metasploit.com/download |
| 3 | +# Current source: https://github.com/rapid7/metasploit-framework |
| 4 | +## |
| 5 | + |
| 6 | +require 'msf/core' |
| 7 | + |
| 8 | +class MetasploitModule < Msf::Auxiliary |
| 9 | + |
| 10 | + include Msf::Exploit::Remote::MYSQL |
| 11 | + include Msf::Auxiliary::Report |
| 12 | + include Msf::Auxiliary::Scanner |
| 13 | + |
| 14 | + def initialize |
| 15 | + super( |
| 16 | + 'Name' => 'MYSQL Directory Write Test', |
| 17 | + 'Description' => %Q{ |
| 18 | + Enumerate writeable directories using the MySQL SELECT INTO DUMPFILE feature, for more |
| 19 | + information see the URL in the references. ***Note: For every writable directory found, |
| 20 | + a file called test with the text test will be written to the directory.*** |
| 21 | + }, |
| 22 | + 'Author' => [ 'AverageSecurityGuy <stephen[at]averagesecurityguy.info>' ], |
| 23 | + 'References' => [ |
| 24 | + [ 'URL', 'https://dev.mysql.com/doc/refman/5.7/en/select-into.html' ] |
| 25 | + ], |
| 26 | + 'License' => MSF_LICENSE |
| 27 | + ) |
| 28 | + |
| 29 | + register_options([ |
| 30 | + OptPath.new('DIR_LIST', [ true, "List of directories to test", '' ]), |
| 31 | + OptString.new('FILE_NAME', [ true, "Name of file to write", Rex::Text.rand_text_alpha(8) ]), |
| 32 | + OptString.new('TABLE_NAME', [ true, "Name of table to use - Warning, if the table already exists its contents will be corrupted", Rex::Text.rand_text_alpha(8) ]), |
| 33 | + OptString.new('USERNAME', [ true, 'The username to authenticate as', "root" ]) |
| 34 | + ]) |
| 35 | + |
| 36 | + end |
| 37 | + |
| 38 | + # This function does not handle any errors, if you use this |
| 39 | + # make sure you handle the errors yourself |
| 40 | + def mysql_query_no_handle(sql) |
| 41 | + res = @mysql_handle.query(sql) |
| 42 | + res |
| 43 | + end |
| 44 | + |
| 45 | + def run_host(ip) |
| 46 | + print_warning("For every writable directory found, a file called test with the text test will be written to the directory.") |
| 47 | + vprint_status("Login...") |
| 48 | + |
| 49 | + unless mysql_login_datastore |
| 50 | + print_error('Unable to login to the server.') |
| 51 | + return |
| 52 | + end |
| 53 | + |
| 54 | + File.open(datastore['DIR_LIST'], "r") do |f| |
| 55 | + f.each_line do |line| |
| 56 | + check_dir(line.chomp) |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + end |
| 61 | + |
| 62 | + def check_dir(dir) |
| 63 | + begin |
| 64 | + vprint_status("Checking #{dir}...") |
| 65 | + res = mysql_query_no_handle("SELECT _utf8'test' INTO DUMPFILE '#{dir}/" + datastore['FILE_NAME'] + "'") |
| 66 | + rescue ::RbMysql::ServerError => e |
| 67 | + vprint_warning("#{e.to_s}") |
| 68 | + rescue Rex::ConnectionTimeout => e |
| 69 | + vprint_error("Timeout: #{e.message}") |
| 70 | + else |
| 71 | + print_good("#{dir} is writeable") |
| 72 | + report_note( |
| 73 | + :host => rhost, |
| 74 | + :type => "filesystem.file", |
| 75 | + :data => "#{dir} is writeable", |
| 76 | + :port => rport, |
| 77 | + :proto => 'tcp', |
| 78 | + :update => :unique_data |
| 79 | + ) |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | +end |
0 commit comments