# AWS - IAM, Identity Center & SSO Enum {{#include ../../../banners/hacktricks-training.md}} ## IAM Puoi trovare una **descrizione di IAM** in: {{#ref}} ../aws-basic-information/ {{#endref}} ### Enumerazione Permessi principali necessari: - `iam:ListPolicies`, `iam:GetPolicy` e `iam:GetPolicyVersion` - `iam:ListRoles` - `iam:ListUsers` - `iam:ListGroups` - `iam:ListGroupsForUser` - `iam:ListAttachedUserPolicies` - `iam:ListAttachedRolePolicies` - `iam:ListAttachedGroupPolicies` - `iam:ListUserPolicies` e `iam:GetUserPolicy` - `iam:ListGroupPolicies` e `iam:GetGroupPolicy` - `iam:ListRolePolicies` e `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 --ssh-public-key-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 #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 #Get inline policies of the user aws iam get-user-policy --user-name --policy-name #Get inline policy details ## attached policies aws iam list-attached-user-policies --user-name #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 #Get groups of a user aws iam get-group --group-name #Get group name info ## inline policies aws iam list-group-policies --group-name #Get inline policies of the group aws iam get-group-policy --group-name --policy-name #Get an inline policy info ## attached policies aws iam list-attached-group-policies --group-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 #Get role ## inline policies aws iam list-role-policies --role-name #Get inline policies of a role aws iam get-role-policy --role-name --policy-name #Get inline policy details ## attached policies aws iam list-attached-role-policies --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 --service-namespaces # Get list of policies that give access to the user to the service ## Get policy content aws iam get-policy --policy-arn aws iam list-policy-versions --policy-arn aws iam get-policy-version --policy-arn --version-id # Enumerate providers aws iam list-saml-providers aws iam get-saml-provider --saml-provider-arn aws iam list-open-id-connect-providers aws iam get-open-id-connect-provider --open-id-connect-provider-arn # Password Policy aws iam get-account-password-policy # MFA aws iam list-mfa-devices aws iam list-virtual-mfa-devices ``` ### Permessi Brute Force Se sei interessato ai tuoi permessi ma non hai accesso per interrogare IAM, puoi sempre forzarli. #### bf-aws-permissions Lo strumento [**bf-aws-permissions**](https://github.com/carlospolop/bf-aws-permissions) è semplicemente uno script bash che eseguirà utilizzando il profilo indicato tutte le azioni **`list*`, `describe*`, `get*`** che può trovare utilizzando i messaggi di aiuto della CLI di `aws` e **restituirà le esecuzioni riuscite**. ```bash # Bruteforce permissions bash bf-aws-permissions.sh -p default > /tmp/bf-permissions-verbose.txt ``` #### bf-aws-perms-simulate Lo strumento [**bf-aws-perms-simulate**](https://github.com/carlospolop/bf-aws-perms-simulate) può trovare le tue attuali autorizzazioni (o quelle di altri principi) se hai l'autorizzazione **`iam:SimulatePrincipalPolicy`** ```bash # Ask for permissions python3 aws_permissions_checker.py --profile [--arn ] ``` #### Perms2ManagedPolicies Se hai trovato **alcuni permessi che il tuo utente ha**, e pensi che siano concessi da un **ruolo AWS gestito** (e non da uno personalizzato). Puoi utilizzare lo strumento [**aws-Perms2ManagedRoles**](https://github.com/carlospolop/aws-Perms2ManagedPolicies) per controllare tutti i **ruoli gestiti da AWS che concedono i permessi che hai scoperto di avere**. ```bash # Run example with my profile python3 aws-Perms2ManagedPolicies.py --profile myadmin --permissions-file example-permissions.txt ``` > [!WARNING] > È possibile "sapere" se i permessi che hai sono concessi da un ruolo gestito da AWS se vedi che **hai permessi su servizi che non vengono utilizzati** ad esempio. #### Cloudtrail2IAM [**CloudTrail2IAM**](https://github.com/carlospolop/Cloudtrail2IAM) è uno strumento Python che analizza **i log di AWS CloudTrail per estrarre e riassumere le azioni** eseguite da chiunque o solo da un utente o ruolo specifico. Lo strumento **analizzerà ogni log di cloudtrail dal bucket indicato**. ```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] > Se trovi .tfstate (file di stato Terraform) o file CloudFormation (questi sono solitamente file yaml situati all'interno di un bucket con il prefisso cf-templates), puoi anche leggerli per trovare la configurazione aws e scoprire quali permessi sono stati assegnati a chi. #### enumerate-iam Per utilizzare lo strumento [**https://github.com/andresriancho/enumerate-iam**](https://github.com/andresriancho/enumerate-iam) devi prima scaricare tutti gli endpoint API AWS, da questi lo script **`generate_bruteforce_tests.py`** otterrà tutti gli **endpoint "list\_", "describe\_" e "get\_"**. E infine, cercherà di **accedervi** con le credenziali fornite e **indicherà se ha funzionato**. (Nella mia esperienza, **lo strumento si blocca a un certo punto**, [**controlla questa soluzione**](https://github.com/andresriancho/enumerate-iam/pull/15/commits/77ad5b41216e3b5f1511d0c385da8cd5984c2d3c) per provare a risolvere il problema). > [!WARNING] > Nella mia esperienza, questo strumento è simile al precedente ma funziona peggio e controlla meno permessi. ```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 Puoi anche utilizzare lo strumento [**weirdAAL**](https://github.com/carnal0wnage/weirdAAL/wiki). Questo strumento controllerà **diverse operazioni comuni su diversi servizi comuni** (verificherà alcuni permessi di enumerazione e anche alcuni permessi di privesc). Ma controllerà solo i controlli codificati (l'unico modo per controllare più cose è codificare più test). ```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 = aws_secret_access_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'] ``` #### Strumenti di indurimento per BF permessi {{#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 }} #### \ Nessuno degli strumenti precedenti è in grado di controllare tutte le autorizzazioni, quindi se conosci uno strumento migliore invia una PR! ### Accesso Non Autenticato {{#ref}} ../aws-unauthenticated-enum-access/aws-iam-and-sts-unauthenticated-enum.md {{#endref}} ### Escalation dei Privilegi Nella pagina seguente puoi controllare come **abuse IAM permissions to escalate privileges**: {{#ref}} ../aws-privilege-escalation/aws-iam-privesc.md {{#endref}} ### Post Exploitation IAM {{#ref}} ../aws-post-exploitation/aws-iam-post-exploitation.md {{#endref}} ### Persistenza IAM {{#ref}} ../aws-persistence/aws-iam-persistence.md {{#endref}} ## IAM Identity Center Puoi trovare una **description of IAM Identity Center** in: {{#ref}} ../aws-basic-information/ {{#endref}} ### Connetti tramite SSO con 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 = sso_role_name = AdministratorAccess sso_region = us-east-1 ``` ### Enumerazione Gli elementi principali del Centro Identità sono: - Utenti e gruppi - Set di permessi: hanno politiche collegate - Account AWS Poi, vengono create relazioni affinché utenti/gruppi abbiano Set di permessi su Account AWS. > [!NOTE] > Nota che ci sono 3 modi per allegare politiche a un Set di permessi. Allegare politiche gestite da AWS, politiche gestite dal cliente (queste politiche devono essere create in tutti gli account che il Set di permessi sta influenzando) e politiche inline (definite lì). ```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 aws sso-admin describe-permission-set --instance-arn --permission-set-arn ## Get managed policies of a permission set aws sso-admin list-managed-policies-in-permission-set --instance-arn --permission-set-arn ## Get inline policies of a permission set aws sso-admin get-inline-policy-for-permission-set --instance-arn --permission-set-arn ## Get customer managed policies of a permission set aws sso-admin list-customer-managed-policy-references-in-permission-set --instance-arn --permission-set-arn ## Get boundaries of a permission set aws sso-admin get-permissions-boundary-for-permission-set --instance-arn --permission-set-arn ## List accounts a permission set is affecting aws sso-admin list-accounts-for-provisioned-permission-set --instance-arn --permission-set-arn ## List principals given a permission set in an account aws sso-admin list-account-assignments --instance-arn --permission-set-arn --account-id # Get permissions sets affecting an account aws sso-admin list-permission-sets-provisioned-to-account --instance-arn --account-id # List users & groups from the identity store aws identitystore list-users --identity-store-id aws identitystore list-groups --identity-store-id ## Get members of groups aws identitystore list-group-memberships --identity-store-id --group-id ## Get memberships or a user or a group aws identitystore list-group-memberships-for-member --identity-store-id --member-id ``` ### Enumerazione Locale È possibile creare all'interno della cartella `$HOME/.aws` il file config per configurare i profili accessibili tramite SSO, ad esempio: ```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:::role/ReadOnlyRole source_profile = Hacktricks-Admin ``` Questa configurazione può essere utilizzata con i comandi: ```bash # Login in ms-sso-profile aws sso login --profile my-sso-profile # Use dependent-profile aws s3 ls --profile dependent-profile ``` Quando un **profilo da SSO è utilizzato** per accedere a delle informazioni, le credenziali sono **memorizzate** in un file all'interno della cartella **`$HOME/.aws/sso/cache`**. Pertanto possono essere **letto e utilizzato da lì**. Inoltre, **ulteriori credenziali** possono essere memorizzate nella cartella **`$HOME/.aws/cli/cache`**. Questa directory di cache è utilizzata principalmente quando si **lavora con i profili AWS CLI** che utilizzano credenziali di utenti IAM o **assumono** ruoli tramite IAM (senza SSO). Esempio di configurazione: ```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 ``` ### Accesso non autenticato {{#ref}} ../aws-unauthenticated-enum-access/aws-identity-center-and-sso-unauthenticated-enum.md {{#endref}} ### Escalation dei privilegi {{#ref}} ../aws-privilege-escalation/aws-sso-and-identitystore-privesc.md {{#endref}} ### Post Exploitation {{#ref}} ../aws-post-exploitation/aws-sso-and-identitystore-post-exploitation.md {{#endref}} ### Persistenza #### Crea un utente e assegna i permessi ad esso ```bash # Create user identitystore:CreateUser aws identitystore create-user --identity-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 ``` - Crea un gruppo e assegna i permessi e imposta su di esso un utente controllato - Dai permessi extra a un utente o gruppo controllato - Per impostazione predefinita, solo gli utenti con permessi dell'Account di Gestione potranno accedere e controllare l'IAM Identity Center. Tuttavia, è possibile tramite Delegate Administrator consentire agli utenti di un account diverso di gestirlo. Non avranno esattamente gli stessi permessi, ma potranno eseguire [**attività di gestione**](https://docs.aws.amazon.com/singlesignon/latest/userguide/delegated-admin.html). {{#include ../../../banners/hacktricks-training.md}}