Files
metasploit-gs/modules/auxiliary/cloud/aws/enum_ec2.rb
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

157 lines
5.0 KiB
Ruby
Raw Normal View History

2019-06-11 19:01:22 -05:00
##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
require 'aws-sdk-ec2'
class MetasploitModule < Msf::Auxiliary
2023-03-20 21:21:36 -04:00
include Msf::Auxiliary::Report
2019-06-11 19:01:22 -05:00
def initialize(info = {})
super(
update_info(
info,
2023-04-06 08:17:19 -04:00
'Name' => 'Amazon Web Services EC2 instance enumeration',
'Description' => %q{
Provided AWS credentials, this module will call the authenticated
API of Amazon Web Services to list all EC2 instances associated
with the account
},
'Author' => [
2023-03-20 21:21:36 -04:00
'Aaron Soto <aaron.soto@rapid7.com>',
'RageLtMan <rageltman[at]sempervictus>'
],
2023-04-07 15:35:24 -05:00
'License' => MSF_LICENSE,
'Notes' => {
'SideEffects' => [IOC_IN_LOGS],
'Stability' => [CRASH_SAFE],
'Reliability' => []
}
2019-06-11 19:01:22 -05:00
)
)
register_options(
[
OptInt.new('LIMIT', [false, 'Only return the specified number of results from each region']),
2019-06-11 19:01:22 -05:00
OptString.new('REGION', [false, 'AWS Region (eg. "us-west-2")']),
OptString.new('ACCESS_KEY_ID', [true, 'AWS Access Key ID (eg. "AKIAXXXXXXXXXXXXXXXX")', '']),
OptString.new('SECRET_ACCESS_KEY', [true, 'AWS Secret Access Key (eg. "CA1+XXXXXXXXXXXXXXXXXXXXXX6aYDHHCBuLuV79")', ''])
]
)
end
def enumerate_regions
regions = []
ec2 = Aws::EC2::Resource.new(
region: 'us-west-1',
access_key_id: datastore['ACCESS_KEY_ID'],
secret_access_key: datastore['SECRET_ACCESS_KEY']
)
ec2_regions = ec2.client.describe_regions.data.regions
ec2_regions.each do |r|
regions.append(r.region_name)
end
regions
2019-06-11 19:01:22 -05:00
end
2023-04-06 08:17:19 -04:00
def describe_ec2_instance(inst)
print_good " #{inst.id} (#{inst.state.name})"
print_good " Creation Date: #{inst.launch_time}"
print_good " Public IP: #{inst.public_ip_address} (#{inst.public_dns_name})"
2023-06-10 08:42:40 -04:00
print_good " Private IP: #{inst.private_ip_address} (#{inst.private_dns_name})"
2023-03-20 21:21:36 -04:00
# Report hosts and info
2023-04-06 08:17:19 -04:00
mac_addr = inst.network_interfaces.select do |iface|
iface.private_ip_address == inst.private_ip_address
end.first.mac_address
iname = inst.tags.find { |t| t.key == 'Name' } ? inst.tags.find { |t| t.key == 'Name' }.value : inst.private_dns_name
iinfo = inst.tags.find { |t| t.key == 'Description' } ? inst.tags.find { |t| t.key == 'Description' }.value : nil
2023-03-20 21:21:36 -04:00
report_host(
2023-04-06 08:17:19 -04:00
host: inst.private_ip_address,
2023-03-20 21:21:36 -04:00
mac: mac_addr,
2023-04-06 08:17:19 -04:00
os_name: inst.platform_details,
os_flavor: inst.architecture,
2023-03-20 23:42:12 -04:00
name: iname,
info: iinfo,
2023-06-10 08:42:40 -04:00
comments: "ec2-id: #{inst.id} (#{inst.placement.availability_zone})"
2023-03-20 21:21:36 -04:00
)
2023-04-06 08:17:19 -04:00
if inst.public_ip_address
report_note(
host: inst.private_ip_address,
type: 'ec2.public_ip',
data: inst.public_ip_address
)
end
2023-06-10 08:42:40 -04:00
#eips = inst.network_interfaces.map {|i| i.association && i.association.public_ip}.compact # <-- works in pry, breaks at runtime in AWS SDK
#report_note(
# host: inst.private_ip_address,
# type: 'ec2.public_ips',
# data: eips.join(' ')
#) unless eips.empty?
2023-04-06 08:17:19 -04:00
if inst.public_ip_address && !inst.public_dns_name.empty?
report_note(
host: inst.private_ip_address,
type: 'ec2.public_dns',
2023-06-10 08:42:40 -04:00
data: "#{inst.public_dns_name} #{inst.public_ip_address}"
2023-04-06 08:17:19 -04:00
)
end
if inst.hypervisor
report_note(
host: inst.private_ip_address,
type: 'ec2.hypervisor',
data: inst.hypervisor
)
end
inst.security_groups.each do |s|
2019-06-11 19:01:22 -05:00
print_good " Security Group: #{s.group_id}"
2023-03-20 21:21:36 -04:00
report_note(
2023-04-06 08:17:19 -04:00
host: inst.private_ip_address,
2023-03-20 23:42:12 -04:00
type: "ec2.#{s.group_id}",
2023-03-20 21:21:36 -04:00
data: s.group_name
)
2019-06-11 19:01:22 -05:00
end
2023-04-06 08:17:19 -04:00
inst.tags.each do |t|
2023-03-20 23:42:12 -04:00
print_good " Tag: #{t.key} = #{t.value}"
report_note(
2023-04-06 08:17:19 -04:00
host: inst.private_ip_address,
2023-03-20 23:42:12 -04:00
type: "ec2.tag #{t.key}",
data: t.value
)
end
2019-06-11 19:01:22 -05:00
end
def run
2023-06-26 16:57:08 -04:00
all_regions = enumerate_regions
if datastore['REGION'].blank?
regions = all_regions
elsif !all_regions.include?(datastore['REGION'])
fail_with(Failure::BadConfig, "Invalid AWS region: #{datastore['REGION']}")
else
regions = [datastore['REGION']]
end
2019-06-11 19:01:22 -05:00
2023-03-20 23:42:12 -04:00
regions.uniq.each do |region|
vprint_status "Checking #{region}..."
ec2 = Aws::EC2::Resource.new(
region: region,
access_key_id: datastore['ACCESS_KEY_ID'],
secret_access_key: datastore['SECRET_ACCESS_KEY']
)
2019-06-11 19:01:22 -05:00
2023-06-23 18:48:21 -04:00
instances = datastore['LIMIT'] ? ec2.instances.limit(datastore['LIMIT']) : ec2.instances
print_status "Found #{ec2.instances.count} instances in #{region}"
instances.each do |i|
describe_ec2_instance(i)
2019-06-11 19:01:22 -05:00
end
end
rescue Seahorse::Client::NetworkingError => e
print_error e.message
2019-06-24 12:40:01 -05:00
print_error 'Confirm region name (eg. us-west-2) is valid or blank before retrying'
2023-04-07 15:42:23 -05:00
rescue Aws::EC2::Errors::ServiceError => e
fail_with(Failure::UnexpectedReply, e.message)
2019-06-11 19:01:22 -05:00
end
end