Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/auxiliary/cloud/aws/enum_ec2.rb
19500 views
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
##
5
6
require 'aws-sdk-ec2'
7
8
class MetasploitModule < Msf::Auxiliary
9
include Msf::Auxiliary::Report
10
def initialize(info = {})
11
super(
12
update_info(
13
info,
14
'Name' => 'Amazon Web Services EC2 instance enumeration',
15
'Description' => %q{
16
Provided AWS credentials, this module will call the authenticated
17
API of Amazon Web Services to list all EC2 instances associated
18
with the account
19
},
20
'Author' => [
21
'Aaron Soto <[email protected]>',
22
'RageLtMan <rageltman[at]sempervictus>'
23
],
24
'License' => MSF_LICENSE,
25
'Notes' => {
26
'SideEffects' => [IOC_IN_LOGS],
27
'Stability' => [CRASH_SAFE],
28
'Reliability' => []
29
}
30
)
31
)
32
33
register_options(
34
[
35
OptInt.new('LIMIT', [false, 'Only return the specified number of results from each region']),
36
OptString.new('REGION', [false, 'AWS Region (eg. "us-west-2")']),
37
OptString.new('ACCESS_KEY_ID', [true, 'AWS Access Key ID (eg. "AKIAXXXXXXXXXXXXXXXX")', '']),
38
OptString.new('SECRET_ACCESS_KEY', [true, 'AWS Secret Access Key (eg. "CA1+XXXXXXXXXXXXXXXXXXXXXX6aYDHHCBuLuV79")', ''])
39
]
40
)
41
end
42
43
def enumerate_regions
44
regions = []
45
46
ec2 = Aws::EC2::Resource.new(
47
region: 'us-west-1',
48
access_key_id: datastore['ACCESS_KEY_ID'],
49
secret_access_key: datastore['SECRET_ACCESS_KEY']
50
)
51
52
ec2_regions = ec2.client.describe_regions.data.regions
53
ec2_regions.each do |r|
54
regions.append(r.region_name)
55
end
56
57
regions
58
end
59
60
def describe_ec2_instance(inst)
61
print_good " #{inst.id} (#{inst.state.name})"
62
print_good " Creation Date: #{inst.launch_time}"
63
print_good " Public IP: #{inst.public_ip_address} (#{inst.public_dns_name})"
64
print_good " Private IP: #{inst.private_ip_address} (#{inst.private_dns_name})"
65
# Report hosts and info
66
mac_addr = inst.network_interfaces.select do |iface|
67
iface.private_ip_address == inst.private_ip_address
68
end.first.mac_address
69
iname = inst.tags.find { |t| t.key == 'Name' } ? inst.tags.find { |t| t.key == 'Name' }.value : inst.private_dns_name
70
iinfo = inst.tags.find { |t| t.key == 'Description' } ? inst.tags.find { |t| t.key == 'Description' }.value : nil
71
report_host(
72
host: inst.private_ip_address,
73
mac: mac_addr,
74
os_name: inst.platform_details,
75
os_flavor: inst.architecture,
76
name: iname,
77
info: iinfo,
78
comments: "ec2-id: #{inst.id} (#{inst.placement.availability_zone})"
79
)
80
if inst.public_ip_address
81
report_note(
82
host: inst.private_ip_address,
83
type: 'ec2.public_ip',
84
data: { :public_ip_address => inst.public_ip_address }
85
)
86
end
87
# eips = inst.network_interfaces.map {|i| i.association && i.association.public_ip}.compact # <-- works in pry, breaks at runtime in AWS SDK
88
# report_note(
89
# host: inst.private_ip_address,
90
# type: 'ec2.public_ips',
91
# data: { :eips => eips.join(' ') }
92
# ) unless eips.empty?
93
if inst.public_ip_address && !inst.public_dns_name.empty?
94
report_note(
95
host: inst.private_ip_address,
96
type: 'ec2.public_dns',
97
data: {
98
:public_dns_name => inst.public_dns_name,
99
:public_ip_address => inst.public_ip_address
100
}
101
)
102
end
103
if inst.hypervisor
104
report_note(
105
host: inst.private_ip_address,
106
type: 'ec2.hypervisor',
107
data: { :hypervisor => inst.hypervisor }
108
)
109
end
110
inst.security_groups.each do |s|
111
print_good " Security Group: #{s.group_id}"
112
report_note(
113
host: inst.private_ip_address,
114
type: "ec2.#{s.group_id}",
115
data: { :group_name => s.group_name }
116
)
117
end
118
inst.tags.each do |t|
119
print_good " Tag: #{t.key} = #{t.value}"
120
report_note(
121
host: inst.private_ip_address,
122
type: "ec2.tag #{t.key}",
123
data: { :tag => t.value }
124
)
125
end
126
end
127
128
def run
129
all_regions = enumerate_regions
130
if datastore['REGION'].blank?
131
regions = all_regions
132
elsif !all_regions.include?(datastore['REGION'])
133
fail_with(Failure::BadConfig, "Invalid AWS region: #{datastore['REGION']}")
134
else
135
regions = [datastore['REGION']]
136
end
137
138
regions.uniq.each do |region|
139
vprint_status "Checking #{region}..."
140
ec2 = Aws::EC2::Resource.new(
141
region: region,
142
access_key_id: datastore['ACCESS_KEY_ID'],
143
secret_access_key: datastore['SECRET_ACCESS_KEY']
144
)
145
146
instances = datastore['LIMIT'] ? ec2.instances.limit(datastore['LIMIT']) : ec2.instances
147
print_status "Found #{ec2.instances.count} instances in #{region}"
148
149
instances.each do |i|
150
describe_ec2_instance(i)
151
end
152
end
153
rescue Seahorse::Client::NetworkingError => e
154
print_error e.message
155
print_error 'Confirm region name (eg. us-west-2) is valid or blank before retrying'
156
rescue Aws::EC2::Errors::ServiceError => e
157
fail_with(Failure::UnexpectedReply, e.message)
158
end
159
end
160
161