Files
metasploit-gs/modules/post/windows/gather/enum_db.rb
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

382 lines
10 KiB
Ruby
Raw Normal View History

2012-09-26 08:40:50 -05:00
##
2017-07-24 06:26:21 -07:00
# This module requires Metasploit: https://metasploit.com/download
2013-10-15 13:50:46 -05:00
# Current source: https://github.com/rapid7/metasploit-framework
2012-09-26 08:40:50 -05:00
##
2016-03-08 14:02:44 +01:00
class MetasploitModule < Msf::Post
2013-09-05 13:41:25 -05:00
include Msf::Post::File
include Msf::Post::Windows::Registry
include Msf::Auxiliary::Report
2021-09-10 12:53:39 +01:00
def initialize(info = {})
super(
update_info(
info,
'Name' => 'Windows Gather Database Instance Enumeration',
'Description' => %q{This module will enumerate a Windows system for installed database instances.},
2021-09-10 12:53:39 +01:00
'License' => MSF_LICENSE,
'Author' => [
'Barry Shteiman <barry[at]sectorix.com>', # Module author
'juan vazquez' # minor help
],
'Platform' => [ 'win' ],
2021-10-06 13:43:31 +01:00
'SessionTypes' => [ 'meterpreter' ],
'Notes' => {
'Stability' => [CRASH_SAFE],
'SideEffects' => [],
'Reliability' => []
},
2021-10-06 13:43:31 +01:00
'Compat' => {
'Meterpreter' => {
'Commands' => %w[
stdapi_fs_search
stdapi_sys_config_getenv
]
}
}
2021-09-10 12:53:39 +01:00
)
)
2013-09-05 13:41:25 -05:00
end
def run
hostname = sysinfo.nil? ? cmd_exec('hostname') : sysinfo['Computer']
print_status("Enumerating databases on #{hostname} (#{session.session_host})")
2013-09-05 13:41:25 -05:00
results = []
2013-09-05 13:41:25 -05:00
if check_mssql
results += enumerate_mssql
end
if check_oracle
results += enumerate_oracle
end
if check_db2
results += enumerate_db2
end
if check_mysql
results += enumerate_mysql
end
if check_sybase
results += enumerate_sybase
end
if results.empty?
print_status('Done, no databases were found')
2013-09-05 13:41:25 -05:00
return
end
print_status("Done, #{results.length} databases found.")
2013-09-05 13:41:25 -05:00
2016-08-10 13:30:09 -05:00
tbl = Rex::Text::Table.new(
2023-02-08 13:47:34 +00:00
'Header' => 'Installed Databases',
2021-09-10 12:53:39 +01:00
'Indent' => 1,
2013-09-05 13:41:25 -05:00
'Columns' =>
[
2023-02-08 13:47:34 +00:00
'Type',
'Instance',
'Database',
'Port'
2021-09-10 12:53:39 +01:00
]
)
2013-09-05 13:41:25 -05:00
2023-02-08 13:47:34 +00:00
results.each do |r|
report_service(host: session.sock.peerhost, port: r[3], name: r[0], info: "#{r[0]}, #{r[1]}")
2013-09-05 13:41:25 -05:00
tbl << r
2023-02-08 13:47:34 +00:00
end
2013-09-05 13:41:25 -05:00
print_line(tbl.to_s)
2023-02-08 13:47:34 +00:00
p = store_loot('host.databases', 'text/plain', session, tbl.to_s, 'databases.txt', 'Running Databases')
2017-07-19 13:02:49 +01:00
print_good("Results stored in: #{p}")
2013-09-05 13:41:25 -05:00
end
##### initial identification methods #####
# Check if MSSQL database instances are installed on host
2013-09-05 13:41:25 -05:00
def check_mssql
if registry_enumkeys('HKLM\\SOFTWARE\\Microsoft').include?('Microsoft SQL Server')
2013-09-05 13:41:25 -05:00
print_status("\tMicrosoft SQL Server found.")
return true
end
2013-09-05 13:41:25 -05:00
return false
2023-02-08 13:47:34 +00:00
rescue StandardError
2013-09-05 13:41:25 -05:00
return false
end
# Check if Oracle database instances are installed on host
2013-09-05 13:41:25 -05:00
def check_oracle
keys = registry_enumkeys('HKLM\\SOFTWARE\\Oracle')
if keys.include?('ALL_HOMES')
2013-09-05 13:41:25 -05:00
print_status("\tOracle Server found.")
return true
end
if keys.include?('SYSMAN')
2013-09-05 13:41:25 -05:00
print_status("\tOracle Server found.")
return true
end
if keys.include?('KEY_XE')
2013-09-05 13:41:25 -05:00
print_status("\tOracle Server found.")
return true
end
2013-09-05 13:41:25 -05:00
return false
2023-02-08 13:47:34 +00:00
rescue StandardError
2013-09-05 13:41:25 -05:00
return false
end
# Check if DB2 database instances are installed on host
2013-09-05 13:41:25 -05:00
def check_db2
if registry_enumkeys('HKLM\\SOFTWARE\\IBM\\DB2').include?('GLOBAL_PROFILE')
2013-09-05 13:41:25 -05:00
print_status("\tDB2 Server found.")
return true
end
2013-09-05 13:41:25 -05:00
return false
2023-02-08 13:47:34 +00:00
rescue StandardError
2013-09-05 13:41:25 -05:00
return false
end
# Check if MySQL database instances are installed on host
2013-09-05 13:41:25 -05:00
def check_mysql
if registry_enumkeys('HKLM\\SOFTWARE').include?('MySQL AB')
2013-09-05 13:41:25 -05:00
print_status("\tMySQL Server found.")
return true
end
return false
2023-02-08 13:47:34 +00:00
rescue StandardError
2013-09-05 13:41:25 -05:00
return false
end
# Check if Sybase database instances are installed on host
2013-09-05 13:41:25 -05:00
def check_sybase
keys = registry_enumkeys('HKLM\\SOFTWARE\\Sybase')
if keys.include?('SQLServer')
2013-09-05 13:41:25 -05:00
print_status("\tSybase Server found.")
return true
end
if keys.include?('Server')
2013-09-05 13:41:25 -05:00
print_status("\tSybase Server found.")
return true
end
2013-09-05 13:41:25 -05:00
return false
2023-02-08 13:47:34 +00:00
rescue StandardError
2013-09-05 13:41:25 -05:00
return false
end
##### deep analysis methods #####
# method to identify MSSQL instances
2013-09-05 13:41:25 -05:00
def enumerate_mssql
results = []
2023-02-08 13:47:34 +00:00
key = 'HKLM\\SOFTWARE\\Microsoft\\Microsoft SQL Server\\Instance Names\\SQL'
2013-09-05 13:41:25 -05:00
instances = registry_enumvals(key)
return results if instances.blank?
instances.each do |i|
tcpkey = "HKLM\\SOFTWARE\\Microsoft\\Microsoft SQL Server\\#{registry_getvaldata(key, i)}\\MSSQLServer\\SuperSocketNetLib\\Tcp\\IPAll"
tcpport = registry_getvaldata(tcpkey, 'TcpPort')
print_good("\t\t+ #{registry_getvaldata(key, i)} (Port:#{tcpport})")
results << ['mssql', "instance:#{registry_getvaldata(key, i)} port:#{tcpport}", 'Microsoft SQL Server', tcpport]
2013-09-05 13:41:25 -05:00
end
results
2023-02-08 13:47:34 +00:00
rescue StandardError
2013-09-05 13:41:25 -05:00
print_error("\t\t! could not identify information")
return results || []
end
# method to identify oracle instances
def enumerate_oracle
results = []
found_key = false
2023-02-08 13:47:34 +00:00
basekey_set = ['HKLM\\SOFTWARE\\Oracle\\SYSMAN', 'HKLM\\SOFTWARE\\ORACLE\\KEY_XE']
2013-09-05 13:41:25 -05:00
basekey_set.each do |basekey|
next if found_key
2021-09-10 12:53:39 +01:00
2013-09-05 13:41:25 -05:00
instances = registry_enumkeys(basekey)
next if instances.blank?
found_key = true
2013-09-05 13:41:25 -05:00
instances.each do |i|
2023-02-08 13:47:34 +00:00
if basekey.include? 'KEY_XE'
oracle_sid = registry_getvaldata(basekey, 'ORACLE_SID')
oracle_home = registry_getvaldata(basekey, 'ORACLE_HOME')
2013-09-05 13:41:25 -05:00
else
key = "#{basekey}\\#{i}"
oracle_sid = registry_getvaldata(key, 'ORACLE_SID')
oracle_home = registry_getvaldata(key, 'ORACLE_HOME')
2013-09-05 13:41:25 -05:00
end
if !exist?(oracle_home + '\\NETWORK\\ADMIN\\tnsnames.ora')
print_error("\t\t! #{oracle_sid} (No Listener Found)")
2013-09-05 13:41:25 -05:00
next
end
data_tnsnames = read_file(oracle_home + '\\NETWORK\\ADMIN\\tnsnames.ora')
if data_tnsnames =~ /PORT\ =\ (\d+)/
2023-02-08 13:47:34 +00:00
port = ::Regexp.last_match(1)
print_good("\t\t+ #{oracle_sid} (Port:#{port})")
results << [ 'oracle', "instance:#{oracle_sid} port:#{port}", 'Oracle Database Server', port ]
2013-09-05 13:41:25 -05:00
else
print_error("\t\t! #{oracle_sid} (No Listener Found)")
2013-09-05 13:41:25 -05:00
end
end
end
2023-02-08 13:47:34 +00:00
if !found_key
2013-09-05 13:41:25 -05:00
print_error("\t\t! Oracle instances not found")
end
results
2023-02-08 13:47:34 +00:00
rescue StandardError
2013-09-05 13:41:25 -05:00
print_error("\t\t! could not identify information")
return results || []
end
# method to identify mysql instances
def enumerate_mysql
results = []
2023-02-08 13:47:34 +00:00
basekey = 'HKLM\\SOFTWARE\\MySQL AB'
2013-09-05 13:41:25 -05:00
instances = registry_enumkeys(basekey)
return results if instances.blank?
2021-09-10 12:53:39 +01:00
2013-09-05 13:41:25 -05:00
instances.each do |i|
key = "#{basekey}\\#{i}"
location = registry_getvaldata(key, 'Location')
2013-09-05 13:41:25 -05:00
data = read_mysql_conf(location)
if data.nil?
data = find_and_read_mysql_conf
end
2013-09-05 13:41:25 -05:00
2023-02-08 13:47:34 +00:00
if data && data =~ (/port=(\d+)/)
port = ::Regexp.last_match(1)
2013-09-05 13:41:25 -05:00
print_good("\t\t+ MYSQL (Port:#{port})")
2023-02-08 13:47:34 +00:00
results << ['mysql', "instance:MYSQL port:#{port}", 'MySQL Server', port]
2013-09-05 13:41:25 -05:00
else
print_error("\t\t! could not identify information")
end
end
results
2023-02-08 13:47:34 +00:00
rescue StandardError
2013-09-05 13:41:25 -05:00
print_error("\t\t! could not identify information")
return results || []
end
# method to identify sybase instances
def enumerate_sybase
2023-02-08 13:47:34 +00:00
basekey = 'HKLM\\SOFTWARE\\Sybase\\SQLServer'
instance = registry_getvaldata(basekey, 'DSLISTEN')
location = registry_getvaldata(basekey, 'RootDir')
2013-09-05 13:41:25 -05:00
results = []
2023-02-08 13:47:34 +00:00
if !exist?(location + '\\ini\\sql.ini')
2013-09-05 13:41:25 -05:00
print_error("\t\t! could not locate configuration file.")
return results
end
2023-02-08 13:47:34 +00:00
data = read_file(location + '\\ini\\sql.ini')
2013-09-05 13:41:25 -05:00
if data =~ /\[#{instance}\]([^\[]*)/
2023-02-08 13:47:34 +00:00
segment = ::Regexp.last_match(1)
2013-09-05 13:41:25 -05:00
else
print_error("\t\t! could not locate information.")
2013-09-05 13:41:25 -05:00
return results
end
2023-02-08 13:47:34 +00:00
if segment =~ /master=\w+,[^,]+,(\d+)/
port = ::Regexp.last_match(1)
2013-09-05 13:41:25 -05:00
else
print_error("\t\t! could not locate information.")
2013-09-05 13:41:25 -05:00
return results
end
print_good("\t\t+ #{instance} (Port:#{port})")
2023-02-08 13:47:34 +00:00
results << [ 'sybase', "instance:#{instance} port:#{port}", 'Sybase SQL Server', port ]
2013-09-05 13:41:25 -05:00
return results
2023-02-08 13:47:34 +00:00
rescue StandardError
print_error("\t\t! could not locate information.")
2013-09-05 13:41:25 -05:00
return results || []
end
# method to identify db2 instances
def enumerate_db2
results = []
2023-02-08 13:47:34 +00:00
cmd_i = cmd_exec('db2cmd', '-i -w /c db2ilist')
cmd_p = cmd_exec('db2cmd', '-i -w /c db2 get dbm cfg')
if cmd_p =~ %r{\ ?TCP/IP\ Service\ name\ +\(SVCENAME\)\ =\ (\w+)}
port = ::Regexp.last_match(1)
2013-09-05 13:41:25 -05:00
else
print_error("\t\t! could not identify instances information")
return results
end
windir = session.sys.config.getenv('windir')
getfile = session.fs.file.search(windir + '\\system32\\drivers\\etc\\', 'services.*', true, -1)
2013-09-05 13:41:25 -05:00
data = nil
getfile.each do |file|
if exist?("#{file['path']}\\#{file['name']}")
data = read_file("#{file['path']}\\#{file['name']}")
2023-02-08 13:47:34 +00:00
break if !data.nil?
2013-09-05 13:41:25 -05:00
end
end
2023-02-08 13:47:34 +00:00
if data && data =~ (/#{port}[\ \t]+(\d+)/)
port_t = ::Regexp.last_match(1)
2013-09-05 13:41:25 -05:00
else
print_error("\t\t! could not identify instances information")
return results
end
cmd_i.split("\n").compact.each do |line|
2021-09-10 12:53:39 +01:00
stripped = line.strip
2013-09-05 13:41:25 -05:00
print_good("\t\t+ #{stripped} (Port:#{port_t})")
2023-02-08 13:47:34 +00:00
results << [ 'db2', "instance:#{stripped} port:#{port_t}", 'DB2 Server', port_t ]
2013-09-05 13:41:25 -05:00
end
results
2023-02-08 13:47:34 +00:00
rescue StandardError
2013-09-05 13:41:25 -05:00
print_error("\t\t! could not identify instances information")
return results || []
end
##### helper methods #####
def read_mysql_conf(location)
return unless location
if exist?(location + '\\my.ini')
return read_file(location + '\\my.ini')
end
if exist?(location + '\\my.cnf')
return read_file(location + '\\my.cnf')
end
nil
end
def find_and_read_mysql_conf
sysdriv = session.sys.config.getenv('SYSTEMDRIVE')
getfile = session.fs.file.search(sysdriv + '\\', 'my.ini', true, -1)
getfile.each do |file|
path = "#{file['path']}\\#{file['name']}"
if exist?(path)
return read_file(path)
2013-09-05 13:41:25 -05:00
end
end
nil
2013-09-05 13:41:25 -05:00
end
2012-09-26 08:40:50 -05:00
end