Use profiles:
- Set the credentials with
aws configure --profile$profile. - Use in all commands
--profile $profileto use this identity.
See https://github.com/eon01/AWS-CheatSheet for many commands or https://swisskyrepo.github.io/InternalAllTheThings/cloud/aws/aws-access-token/
General
Get Infos of the current used profile
aws [--profile $profile] sts get-caller-identity
Another possibility to gain an ARN (with account id) is to call a non-existing function and to extract data from the error message.
EC2
List all EC2 images = AMI’s:
aws [--profile $profile] ec2 describe-images --owners amazon --executable-users all
To get a list of all AMI’s from the account of our target organization, we need the Account ID. To get that, try various searches for a string the organization would probably use for their images.
aws [--profile $profile] ec2 describe-images --executable-users all --filters "Name=description,Values=$string"
aws [--profile $profile] ec2 describe-images --executable-users all --filters "Name=name,Values=$string"
You can also try this for snapshots:
aws [--profile $profile] ec2 describe-snapshots --filters "Name=description,Values=$string"
aws [--profile $profile] ec2 describe-snapshots --filters "Name=name,Values=$string"
Then, you can query images, snapshots, etc. with the found AccountID:
aws [--profile $profile] ec2 describe-images --owners $accountID --executable-users all
aws [--profile $profile] ec2 describe-snapshots > /tmp/a // afterwards, scan /tmp/a for all AccountIDs to see images from this account.
IAM
Background:
- An identity is a IAM resource which can be authorized to perform actions and access resources.
- An identity includes
- users,
- roles and
- groups.
- An identity can be assigned
- invididual / inline policies (which were given specific/manually to this user) or
- managed policies (shared policies which can be assigned various users/groups).
- Identities can be recursive.
Create a new user
aws [--profile $profile] iam create-user --user-name $newUserName
Create access and secret key so that we can interact with the AWS CLI as this user:
aws [--profile $profile] iam create-access-key --user-name $newUserName
Now, set a new profile with the new keys:
aws configure --profile $newProfileName
Assign permissions to the new user (per default, a new user does not have any permissions.)
Enumerate
See various information (also user count!):
aws --profile $profile iam get-account-summary
Get all users
aws --profile $profile iam list-users
Get all groups
aws --profile $profile iam list-groups
Get all roles
aws --profile $profile iam list-roles
Get all managed policies
- which were defined from this account (–scope Local) and
- which are actually used by at least one identity (–only-attached)
aws --profile $profile iam list-policies --scope Local --only-attached
All of above in pacu:
run iam__enum_users_roles_policies_groups
Enumerate IAM users from other accounts
Prerequisites:
- You have a account id.
- You want to enumerate users of this account.
Create a new S3 bucket:
aws [--profile $profile] s3 mb s3://b-$RANDOM-$RANDOM
Create a policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowUserToListBucket",
"Effect": "Allow",
"Resource": "arn:aws:s3:::$createdBucketName",
"Principal": {
"AWS": ["arn:aws:iam::$accountId:user/cloudadmin"]
},
"Action": "s3:ListBucket"
}
]
}
Apply the policy:
aws [--profile $profile] s3api put-bucket-policy --bucket $bucketName --policy file://policy.json
- If there is no return, the users exists.
- Else: User does not exist.
Enumerate IAM privileges
Scenario:
- You have an AWS access and want to know what is possible with it (which IAM permissions it have).
Get inline policies of the user:
aws --profile $profile iam list-user-policies --user-name $username
Get managed policies of the user:
aws --profile $profile iam list-attached-user-policies --user-name $username
Get groups in which is user is part of:
aws --profile $profile iam list-groups-for-user --user-name $username
- If you found groups: Get for each group infos which
- inline policies they include with
aws --profile $profile iam list-group-policies --group-name $foundGroupName - and managed policies they include with
aws --profile $profileiam list-attached-group-policies --group-name $foundGroupName- If you fand a managed policy, you can get the versions of it with
aws --profile $profile iam list-policy-versions --policy-arn "arn:aws:iam::aws:policy/$foundPolicy" - Now, you can retrieve the policy of this managed policy with all the permissions it has:
aws --profile $profile iam get-policy-version --policy-arn arn:aws:iam::aws:policy/$foundPolicy --version-id $foundVersion
- If you fand a managed policy, you can get the versions of it with
- inline policies they include with
Database
Listing the tables via the API:
aws dynamodb list-tables [--endpoint-url http://s3.bucket.htb/shell/]
Note: If credentials are missing, execute aws configure before.
Get the structure of a table:
aws dynamodb describe-table --table-name users [--endpoint-url http://s3.bucket.htb/shell/]
Query a table with the CLI:
aws dynamodb scan --table-name users [--endpoint-url http://s3.bucket.htb/shell/]
List bucket content:
aws --endpoint-url=http://s3.bucket.htb/ s3 ls adserver/images/
S3 Buckets
List all buckets
aws s3api list-buckets [--endpoint-url http://s3.bucket.htb/]
Sync a whole bucket:
aws s3 sync s3://adserver . [--endpoint-url http://s3.bucket.htb/]
Copy a file:
aws s3 cp t.php s3://adserver/t.php [--endpoint-url http://s3.bucket.htb/]
Enumerate S3 bucket names
Scenario:
- You have AWS CLI access from an account.
- You want to enumerate S3 bucket names.
What to do:
- Create another user B
- Give B permissions to read S3 buckets only when the account ID starts with 0.
- Try accessing it. If there is access denied, the account ID does not start with 0.
- Repeat with 1.
- Repeat…
Create a new user called enumUser and a new profile. See the IAM section above.
Define a policy which allowes to read only for buckets which start with 0:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowResourceAccount",
"Effect": "Allow",
"Action": [
"s3:ListBucket",
"s3:GetObject"
],
"Resource": "*",
"Condition": {
"StringLike": {"s3:ResourceAccount": ["0*"]}
}
}
]
}
Apply this policy to the new user enumUser:
aws --profile $profile iam put-user-policy --user-name enumUser --policy-name s3-read --policy-document file://policy-file.json
Verify that the permissions were granted:
aws --profile $profile iam list-user-policies --user-name enumUse
Check access:
aws --profile enumUser s3 ls $bucket
- If there is access denied: Edit the policy file, increment the number, set the policy file, check access again. Repeat.
- If there is no access denied: You’ve found one digit of the account Id. Repeat with the next digit.
s3recon
Scan for public buckets: (Possible wordlist)
s3recon wordlist.txt -o "results.json" --public
cloud_enum
Tools
- Bucket finder
- Fingerprinting and exploiting AWS infrastructure
- S3 Bucket scanner
- bucketlist
- https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/Methodology%20and%20Resources/Cloud%20-%20AWS%20Pentest.md
- https://six2dez.gitbook.io/pentest-book/enumeration/cloud/aws
Leave a Reply
You must be logged in to post a comment.