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.

353 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 },
'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' ],
'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
# method called when command run is issued
def run
results = []
print_status("Enumerating Databases on #{sysinfo['Computer']}")
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?
2023-02-08 13:47:34 +00:00
print_status('Done, No Databases were found')
2013-09-05 13:41:25 -05:00
return
end
2023-02-08 13:47:34 +00:00
print_status('Done, 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 #####
# method for Checking if database instances are installed on host - mssql
def check_mssql
2023-02-08 13:47:34 +00:00
key = 'HKLM\\SOFTWARE\\Microsoft'
if registry_enumkeys(key).include?('Microsoft SQL Server')
2013-09-05 13:41:25 -05:00
print_status("\tMicrosoft SQL 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
# method for Checking if database instances are installed on host - oracle
def check_oracle
2023-02-08 13:47:34 +00:00
key = 'HKLM\\SOFTWARE\\Oracle'
if registry_enumkeys(key).include?('ALL_HOMES')
2013-09-05 13:41:25 -05:00
print_status("\tOracle Server found.")
return true
2023-02-08 13:47:34 +00:00
elsif registry_enumkeys(key).include?('SYSMAN')
2013-09-05 13:41:25 -05:00
print_status("\tOracle Server found.")
return true
2023-02-08 13:47:34 +00:00
elsif registry_enumkeys(key).include?('KEY_XE')
2013-09-05 13:41:25 -05:00
print_status("\tOracle 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
# method for Checking if database instances are installed on host - db2
def check_db2
2023-02-08 13:47:34 +00:00
key = 'HKLM\\SOFTWARE\\IBM\\DB2'
if registry_enumkeys(key).include?('GLOBAL_PROFILE')
2013-09-05 13:41:25 -05:00
print_status("\tDB2 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
# method for Checking if database instances are installed on host - mysql
def check_mysql
2023-02-08 13:47:34 +00:00
key = 'HKLM\\SOFTWARE'
if registry_enumkeys(key).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
# method for Checking if database instances are installed on host - sybase
def check_sybase
2023-02-08 13:47:34 +00:00
key = 'HKLM\\SOFTWARE\\Sybase'
if registry_enumkeys(key).include?('SQLServer')
2013-09-05 13:41:25 -05:00
print_status("\tSybase Server found.")
return true
2023-02-08 13:47:34 +00:00
elsif registry_enumkeys(key).include?('Server')
2013-09-05 13:41:25 -05:00
print_status("\tSybase 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
##### deep analysis methods #####
# method to identify mssql instances
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)
2023-02-08 13:47:34 +00:00
if !instances.nil? && !instances.empty?
2013-09-05 13:41:25 -05:00
instances.each do |i|
2021-09-10 12:53:39 +01:00
tcpkey = "HKLM\\SOFTWARE\\Microsoft\\Microsoft SQL Server\\#{registry_getvaldata(key, i)}\\MSSQLServer\\SuperSocketNetLib\\Tcp\\IPAll"
2023-02-08 13:47:34 +00:00
tcpport = registry_getvaldata(tcpkey, 'TcpPort')
2021-09-10 12:53:39 +01:00
print_good("\t\t+ #{registry_getvaldata(key, i)} (Port:#{tcpport})")
2023-02-08 13:47:34 +00:00
results << ['mssql', "instance:#{registry_getvaldata(key, i)} port:#{tcpport}", 'Microsoft SQL Server', tcpport]
2013-09-05 13:41:25 -05:00
end
end
return 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)
2023-02-08 13:47:34 +00:00
if instances.nil? || instances.empty?
2013-09-05 13:41:25 -05:00
next
else
found_key = true
end
instances.each do |i|
2023-02-08 13:47:34 +00:00
if basekey.include? 'KEY_XE'
val_ORACLE_SID = registry_getvaldata(basekey, 'ORACLE_SID')
val_ORACLE_HOME = registry_getvaldata(basekey, 'ORACLE_HOME')
2013-09-05 13:41:25 -05:00
else
key = "#{basekey}\\#{i}"
2023-02-08 13:47:34 +00:00
val_ORACLE_SID = registry_getvaldata(key, 'ORACLE_SID')
val_ORACLE_HOME = registry_getvaldata(key, 'ORACLE_HOME')
2013-09-05 13:41:25 -05:00
end
2023-02-08 13:47:34 +00:00
if !exist?(val_ORACLE_HOME + '\\NETWORK\\ADMIN\\tnsnames.ora')
2013-09-05 13:41:25 -05:00
print_error("\t\t! #{val_ORACLE_SID} (No Listener Found)")
next
end
2023-02-08 13:47:34 +00:00
data_TNSNAMES = read_file(val_ORACLE_HOME + '\\NETWORK\\ADMIN\\tnsnames.ora')
if data_TNSNAMES =~ /PORT\ =\ (\d+)/
port = ::Regexp.last_match(1)
2013-09-05 13:41:25 -05:00
print_good("\t\t+ #{val_ORACLE_SID} (Port:#{port})")
2023-02-08 13:47:34 +00:00
results << [ 'oracle', "instance:#{val_ORACLE_SID} port:#{port}", 'Oracle Database Server', port ]
2013-09-05 13:41:25 -05:00
else
print_error("\t\t! #{val_ORACLE_SID} (No Listener Found)")
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
return 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)
2023-02-08 13:47:34 +00:00
if instances.nil? || instances.empty?
2013-09-05 13:41:25 -05:00
return results
end
2021-09-10 12:53:39 +01:00
2013-09-05 13:41:25 -05:00
instances.each do |i|
key = "#{basekey}\\#{i}"
2023-02-08 13:47:34 +00:00
val_location = registry_getvaldata(key, 'Location')
2013-09-05 13:41:25 -05:00
data = find_mysql_conf(val_location)
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
return 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! couldnt locate information.")
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! couldnt locate information.")
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
2013-09-05 13:41:25 -05:00
print_error("\t\t! couldnt locate information.")
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')
2023-02-08 13:47:34 +00:00
getfile = session.fs.file.search(windir + '\\system32\\drivers\\etc\\', 'services.*', recurse = true, timeout = -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
return 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 find_mysql_conf(val_location)
data = nil
2023-02-08 13:47:34 +00:00
if exist?(val_location + '\\my.ini')
data = read_file(val_location + '\\my.ini')
elsif exist?(val_location + '\\my.cnf')
data = read_file(val_location + '\\my.cnf')
2013-09-05 13:41:25 -05:00
else
2021-09-10 12:53:39 +01:00
sysdriv = session.sys.config.getenv('SYSTEMDRIVE')
2023-02-08 13:47:34 +00:00
getfile = session.fs.file.search(sysdriv + '\\', 'my.ini', recurse = true, timeout = -1)
2013-09-05 13:41:25 -05:00
getfile.each do |file|
if exist?("#{file['path']}\\#{file['name']}")
data = read_file("#{file['path']}\\#{file['name']}")
break
end
end
end
return data
end
2012-09-26 08:40:50 -05:00
end