# AWS - IAM, Identity Center & SSO Enum

{{#include ../../../banners/hacktricks-training.md}}

## IAM

Možete pronaći **opis IAM-a** u:

{{#ref}}
../aws-basic-information/
{{#endref}}

### Enumeration

Glavne dozvole potrebne:

- `iam:ListPolicies`, `iam:GetPolicy` i `iam:GetPolicyVersion`
- `iam:ListRoles`
- `iam:ListUsers`
- `iam:ListGroups`
- `iam:ListGroupsForUser`
- `iam:ListAttachedUserPolicies`
- `iam:ListAttachedRolePolicies`
- `iam:ListAttachedGroupPolicies`
- `iam:ListUserPolicies` i `iam:GetUserPolicy`
- `iam:ListGroupPolicies` i `iam:GetGroupPolicy`
- `iam:ListRolePolicies` i `iam:GetRolePolicy`
```bash
# All IAMs
## Retrieves  information about all IAM users, groups, roles, and policies
## in your Amazon Web Services account, including their relationships  to
## one another. Use this operation to obtain a snapshot of the configura-
## tion of IAM permissions (users, groups, roles, and  policies)  in  your
## account.
aws iam get-account-authorization-details

# List users
aws iam get-user #Get current user information
aws iam list-users
aws iam list-ssh-public-keys #User keys for CodeCommit
aws iam get-ssh-public-key --user-name <username> --ssh-public-key-id <id> --encoding SSH #Get public key with metadata
aws iam list-service-specific-credentials #Get special permissions of the IAM user over specific services
aws iam get-user --user-name <username> #Get metadata of user, included permissions boundaries
aws iam list-access-keys #List created access keys
## inline policies
aws iam list-user-policies --user-name <username> #Get inline policies of the user
aws iam get-user-policy --user-name <username> --policy-name <policyname> #Get inline policy details
## attached policies
aws iam list-attached-user-policies --user-name <username> #Get policies of user, it doesn't get inline policies

# List groups
aws iam list-groups #Get groups
aws iam list-groups-for-user --user-name <username> #Get groups of a user
aws iam get-group --group-name <name> #Get group name info
## inline policies
aws iam list-group-policies --group-name <username> #Get inline policies of the group
aws iam get-group-policy --group-name <username> --policy-name <policyname> #Get an inline policy info
## attached policies
aws iam list-attached-group-policies --group-name <name> #Get policies of group, it doesn't get inline policies

# List roles
aws iam list-roles #Get roles
aws iam get-role --role-name <role-name> #Get role
## inline policies
aws iam list-role-policies --role-name <name> #Get inline policies of a role
aws iam get-role-policy --role-name <name> --policy-name <name> #Get inline policy details
## attached policies
aws iam list-attached-role-policies --role-name <role-name> #Get policies of role, it doesn't get inline policies

# List policies
aws iam list-policies [--only-attached] [--scope Local]
aws iam list-policies-granting-service-access --arn <identity> --service-namespaces <svc> # Get list of policies that give access to the user to the service
## Get policy content
aws iam get-policy --policy-arn <policy_arn>
aws iam list-policy-versions --policy-arn <arn>
aws iam get-policy-version --policy-arn <arn:aws:iam::975426262029:policy/list_apigateways> --version-id <VERSION_X>

# Enumerate providers
aws iam list-saml-providers
aws iam get-saml-provider --saml-provider-arn <ARN>
aws iam list-open-id-connect-providers
aws iam get-open-id-connect-provider --open-id-connect-provider-arn <ARN>

# Password Policy
aws iam get-account-password-policy

# MFA
aws iam list-mfa-devices
aws iam list-virtual-mfa-devices
```
### Dozvola Brute Force

Ako ste zainteresovani za svoje dozvole, ali nemate pristup za upit IAM, uvek možete da ih brute-forcujete.

#### bf-aws-permissions

Alat [**bf-aws-permissions**](https://github.com/carlospolop/bf-aws-permissions) je samo bash skripta koja će se pokrenuti koristeći navedeni profil za sve **`list*`, `describe*`, `get*`** akcije koje može pronaći koristeći `aws` cli pomoćne poruke i **vratiće uspešne izvršenja**.
```bash
# Bruteforce permissions
bash bf-aws-permissions.sh -p default > /tmp/bf-permissions-verbose.txt
```
#### bf-aws-perms-simulate

Alat [**bf-aws-perms-simulate**](https://github.com/carlospolop/bf-aws-perms-simulate) može pronaći vaše trenutne dozvole (ili dozvole drugih principala) ako imate dozvolu **`iam:SimulatePrincipalPolicy`**
```bash
# Ask for permissions
python3 aws_permissions_checker.py --profile <AWS_PROFILE> [--arn <USER_ARN>]
```
#### Perms2ManagedPolicies

Ako ste pronašli **neke dozvole koje vaš korisnik ima**, i mislite da su dodeljene od strane **upravljane AWS uloge** (a ne od prilagođene). Možete koristiti alat [**aws-Perms2ManagedRoles**](https://github.com/carlospolop/aws-Perms2ManagedPolicies) da proverite sve **AWS upravljane uloge koje dodeljuju dozvole koje ste otkrili da imate**.
```bash
# Run example with my profile
python3 aws-Perms2ManagedPolicies.py --profile myadmin --permissions-file example-permissions.txt
```
> [!WARNING]
> Moguće je "znati" da li su dozvole koje imate dodeljene od strane AWS upravljane uloge ako vidite da **imate dozvole za usluge koje se ne koriste** na primer.

#### Cloudtrail2IAM

[**CloudTrail2IAM**](https://github.com/carlospolop/Cloudtrail2IAM) je Python alat koji analizira **AWS CloudTrail logove kako bi izvukao i sažeo akcije** koje su izvršili svi ili samo određeni korisnik ili uloga. Alat će **parsirati svaki cloudtrail log iz naznačenog bucket-a**.
```bash
git clone https://github.com/carlospolop/Cloudtrail2IAM
cd Cloudtrail2IAM
pip install -r requirements.txt
python3 cloudtrail2IAM.py --prefix PREFIX --bucket_name BUCKET_NAME --profile PROFILE [--filter-name FILTER_NAME] [--threads THREADS]
```
> [!WARNING]
> Ako pronađete .tfstate (Terraform state files) ili CloudFormation datoteke (ove su obično yaml datoteke smeštene unutar bucket-a sa prefiksom cf-templates), možete ih takođe pročitati da biste pronašli aws konfiguraciju i saznali koje su dozvole dodeljene kome.

#### enumerate-iam

Da biste koristili alat [**https://github.com/andresriancho/enumerate-iam**](https://github.com/andresriancho/enumerate-iam), prvo treba da preuzmete sve API AWS krajnje tačke, od kojih će skripta **`generate_bruteforce_tests.py`** dobiti sve **"list\_", "describe\_", i "get\_" krajnje tačke.** I na kraju, pokušaće da **pristupi njima** sa datim kredencijalima i **naznači da li je uspelo**.

(U mom iskustvu, **alat se u nekom trenutku zamrzava**, [**pogledajte ovo rešenje**](https://github.com/andresriancho/enumerate-iam/pull/15/commits/77ad5b41216e3b5f1511d0c385da8cd5984c2d3c) da biste pokušali da to popravite).

> [!WARNING]
> U mom iskustvu, ovaj alat je kao prethodni, ali radi lošije i proverava manje dozvola.
```bash
# Install tool
git clone git@github.com:andresriancho/enumerate-iam.git
cd enumerate-iam/
pip install -r requirements.txt

# Download API endpoints
cd enumerate_iam/
git clone https://github.com/aws/aws-sdk-js.git
python3 generate_bruteforce_tests.py
rm -rf aws-sdk-js
cd ..

# Enumerate permissions
python3 enumerate-iam.py --access-key ACCESS_KEY --secret-key SECRET_KEY [--session-token SESSION_TOKEN] [--region REGION]
```
#### weirdAAL

Možete takođe koristiti alat [**weirdAAL**](https://github.com/carnal0wnage/weirdAAL/wiki). Ovaj alat će proveriti **nekoliko uobičajenih operacija na nekoliko uobičajenih usluga** (proveriće neka prava za enumeraciju i takođe neka prava za privesc). Ali će proveriti samo kodirane provere (jedini način da se proveri više stvari je kodiranje više testova).
```bash
# Install
git clone https://github.com/carnal0wnage/weirdAAL.git
cd weirdAAL
python3 -m venv weirdAAL
source weirdAAL/bin/activate
pip3 install -r requirements.txt

# Create a .env file with aws credentials such as
[default]
aws_access_key_id = <insert key id>
aws_secret_access_key = <insert secret key>

# Setup DB
python3 create_dbs.py

# Invoke it
python3 weirdAAL.py -m ec2_describe_instances -t ec2test # Just some ec2 tests
python3 weirdAAL.py -m recon_all -t MyTarget # Check all permissions
# You will see output such as:
# [+] elbv2 Actions allowed are [+]
# ['DescribeLoadBalancers', 'DescribeAccountLimits', 'DescribeTargetGroups']
```
#### Alati za učvršćivanje BF dozvola

{{#tabs }}
{{#tab name="CloudSploit" }}
```bash
# Export env variables
./index.js --console=text --config ./config.js --json /tmp/out-cloudsploit.json

# Filter results removing unknown
jq 'map(select(.status | contains("UNKNOWN") | not))' /tmp/out-cloudsploit.json | jq 'map(select(.resource | contains("N/A") | not))' > /tmp/out-cloudsploit-filt.json

# Get services by regions
jq 'group_by(.region) | map({(.[0].region): ([map((.resource | split(":"))[2]) | unique])})' ~/Desktop/pentests/cere/greybox/core-dev-dev-cloudsploit-filtered.json
```
{{#endtab }}

{{#tab name="SteamPipe" }}
```bash
# https://github.com/turbot/steampipe-mod-aws-insights
steampipe check all --export=json

# https://github.com/turbot/steampipe-mod-aws-perimeter
# In this case you cannot output to JSON, so heck it in the dashboard
steampipe dashboard
```
{{#endtab }}
{{#endtabs }}

#### \<YourTool>

Nijedan od prethodnih alata nije sposoban da proveri skoro sve dozvole, pa ako znate bolji alat pošaljite PR!

### Neautentifikovani Pristup

{{#ref}}
../aws-unauthenticated-enum-access/aws-iam-and-sts-unauthenticated-enum.md
{{#endref}}

### Eskalacija Privilegija

Na sledećoj stranici možete proveriti kako da **zloupotrebite IAM dozvole za eskalaciju privilegija**:

{{#ref}}
../aws-privilege-escalation/aws-iam-privesc.md
{{#endref}}

### IAM Post Eksploatacija

{{#ref}}
../aws-post-exploitation/aws-iam-post-exploitation.md
{{#endref}}

### IAM Persistencija

{{#ref}}
../aws-persistence/aws-iam-persistence.md
{{#endref}}

## IAM Identitet Centar

Možete pronaći **opis IAM Identitet Centra** u:

{{#ref}}
../aws-basic-information/
{{#endref}}

### Povezivanje putem SSO sa CLI
```bash
# Connect with sso via CLI aws configure sso
aws configure sso

[profile profile_name]
sso_start_url = https://subdomain.awsapps.com/start/
sso_account_id = <account_numbre>
sso_role_name = AdministratorAccess
sso_region = us-east-1
```
### Enumeracija

Glavni elementi Identity Centra su:

- Korisnici i grupe
- Skupovi dozvola: Imaju pridružene politike
- AWS računi

Zatim, odnosi se kreiraju tako da korisnici/grupe imaju Skupove dozvola nad AWS računom.

> [!NAPOMENA]
> Imajte na umu da postoje 3 načina za pridruživanje politika Skupu dozvola. Pridruživanje AWS upravljanim politikama, politikama koje upravlja korisnik (ove politike treba da budu kreirane u svim računima na koje Skup dozvola utiče) i inline politikama (definisanim tamo).
```bash
# Check if IAM Identity Center is used
aws sso-admin list-instances

# Get Permissions sets. These are the policies that can be assigned
aws sso-admin list-permission-sets --instance-arn <instance-arn>
aws sso-admin describe-permission-set --instance-arn <instance-arn> --permission-set-arn <perm-set-arn>

## Get managed policies of a permission set
aws sso-admin list-managed-policies-in-permission-set --instance-arn <instance-arn> --permission-set-arn <perm-set-arn>
## Get inline policies of a permission set
aws sso-admin get-inline-policy-for-permission-set --instance-arn <instance-arn> --permission-set-arn <perm-set-arn>
## Get customer managed policies of a permission set
aws sso-admin list-customer-managed-policy-references-in-permission-set --instance-arn <instance-arn> --permission-set-arn <perm-set-arn>
## Get boundaries of a permission set
aws sso-admin get-permissions-boundary-for-permission-set --instance-arn <instance-arn> --permission-set-arn <perm-set-arn>

## List accounts a permission set is affecting
aws sso-admin list-accounts-for-provisioned-permission-set --instance-arn <instance-arn> --permission-set-arn <perm-set-arn>
## List principals given a permission set in an account
aws sso-admin list-account-assignments --instance-arn <instance-arn> --permission-set-arn <perm-set-arn> --account-id <account_id>

# Get permissions sets affecting an account
aws sso-admin list-permission-sets-provisioned-to-account --instance-arn <instance-arn> --account-id <account_id>

# List users & groups from the identity store
aws identitystore list-users --identity-store-id <store-id>
aws identitystore list-groups --identity-store-id <store-id>
## Get members of groups
aws identitystore list-group-memberships --identity-store-id <store-id> --group-id <group-id>
## Get memberships or a user or a group
aws identitystore list-group-memberships-for-member --identity-store-id <store-id> --member-id <member-id>
```
### Lokalna Enumeracija

Moguće je kreirati unutar foldera `$HOME/.aws` datoteku config za konfiguraciju profila koji su dostupni putem SSO, na primer:
```ini
[default]
region = us-west-2
output = json

[profile my-sso-profile]
sso_start_url = https://my-sso-portal.awsapps.com/start
sso_region = us-west-2
sso_account_id = 123456789012
sso_role_name = MySSORole
region = us-west-2
output = json

[profile dependent-profile]
role_arn = arn:aws:iam::<acc-id>:role/ReadOnlyRole
source_profile = Hacktricks-Admin
```
Ova konfiguracija se može koristiti sa komandama:
```bash
# Login in ms-sso-profile
aws sso login --profile my-sso-profile
# Use dependent-profile
aws s3 ls --profile dependent-profile
```
Kada se **profil iz SSO koristi** za pristup nekim informacijama, kredencijali se **keširaju** u datoteci unutar fascikle **`$HOME/.aws/sso/cache`**. Stoga se mogu **pročitati i koristiti odatle**.

Pored toga, **dodatni kredencijali** mogu biti pohranjeni u fascikli **`$HOME/.aws/cli/cache`**. Ova keš direktorijum se prvenstveno koristi kada radite sa **AWS CLI profilima** koji koriste IAM korisničke kredencijale ili **pretpostavljaju** uloge putem IAM (bez SSO). Primer konfiguracije:
```ini
[profile crossaccountrole]
role_arn = arn:aws:iam::234567890123:role/SomeRole
source_profile = default
mfa_serial = arn:aws:iam::123456789012:mfa/saanvi
external_id = 123456
```
### Neautentifikovani Pristup

{{#ref}}
../aws-unauthenticated-enum-access/aws-identity-center-and-sso-unauthenticated-enum.md
{{#endref}}

### Eskalacija Privilegija

{{#ref}}
../aws-privilege-escalation/aws-sso-and-identitystore-privesc.md
{{#endref}}

### Post Eksploatacija

{{#ref}}
../aws-post-exploitation/aws-sso-and-identitystore-post-exploitation.md
{{#endref}}

### Postojanost

#### Kreirajte korisnika i dodelite mu dozvole
```bash
# Create user identitystore:CreateUser
aws identitystore create-user --identity-store-id <store-id> --user-name privesc --display-name privesc --emails Value=sdkabflvwsljyclpma@tmmbt.net,Type=Work,Primary=True --name Formatted=privesc,FamilyName=privesc,GivenName=privesc
## After creating it try to login in the console using the selected username, you will receive an email with the code and then you will be able to select a password
```
- Kreirajte grupu i dodelite joj dozvole i postavite kontrolisanog korisnika
- Dajte dodatne dozvole kontrolisanom korisniku ili grupi
- Po defaultu, samo korisnici sa dozvolama iz Management Account-a će moći da pristupaju i kontrolišu IAM Identity Center.

Međutim, moguće je putem Delegate Administrator-a omogućiti korisnicima iz druge računa da njime upravljaju. Neće imati potpuno iste dozvole, ali će moći da obavljaju [**aktivnosti upravljanja**](https://docs.aws.amazon.com/singlesignon/latest/userguide/delegated-admin.html).

{{#include ../../../banners/hacktricks-training.md}}
