CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/auxiliary/cloud/aws/enum_ec2.rb
Views: 11655
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: 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.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: "#{inst.public_dns_name} #{inst.public_ip_address}"
98
)
99
end
100
if inst.hypervisor
101
report_note(
102
host: inst.private_ip_address,
103
type: 'ec2.hypervisor',
104
data: inst.hypervisor
105
)
106
end
107
inst.security_groups.each do |s|
108
print_good " Security Group: #{s.group_id}"
109
report_note(
110
host: inst.private_ip_address,
111
type: "ec2.#{s.group_id}",
112
data: s.group_name
113
)
114
end
115
inst.tags.each do |t|
116
print_good " Tag: #{t.key} = #{t.value}"
117
report_note(
118
host: inst.private_ip_address,
119
type: "ec2.tag #{t.key}",
120
data: t.value
121
)
122
end
123
end
124
125
def run
126
all_regions = enumerate_regions
127
if datastore['REGION'].blank?
128
regions = all_regions
129
elsif !all_regions.include?(datastore['REGION'])
130
fail_with(Failure::BadConfig, "Invalid AWS region: #{datastore['REGION']}")
131
else
132
regions = [datastore['REGION']]
133
end
134
135
regions.uniq.each do |region|
136
vprint_status "Checking #{region}..."
137
ec2 = Aws::EC2::Resource.new(
138
region: region,
139
access_key_id: datastore['ACCESS_KEY_ID'],
140
secret_access_key: datastore['SECRET_ACCESS_KEY']
141
)
142
143
instances = datastore['LIMIT'] ? ec2.instances.limit(datastore['LIMIT']) : ec2.instances
144
print_status "Found #{ec2.instances.count} instances in #{region}"
145
146
instances.each do |i|
147
describe_ec2_instance(i)
148
end
149
end
150
rescue Seahorse::Client::NetworkingError => e
151
print_error e.message
152
print_error 'Confirm region name (eg. us-west-2) is valid or blank before retrying'
153
rescue Aws::EC2::Errors::ServiceError => e
154
fail_with(Failure::UnexpectedReply, e.message)
155
end
156
end
157
158