AWS - S3, Athena & Glacier Enum
Tip
学んで実践する AWS Hacking:
HackTricks Training AWS Red Team Expert (ARTE)
学んで実践する GCP Hacking:HackTricks Training GCP Red Team Expert (GRTE)
学んで実践する Az Hacking:HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks をサポートする
- subscription plans を確認してください!
- 参加する 💬 Discord group または telegram group に参加するか、Twitter 🐦 @hacktricks_live をフォローしてください。
- Share hacking tricks by submitting PRs to the HackTricks and HackTricks Cloud github repos.
S3
Amazon S3 は大量のデータを保存できるサービスです。
Amazon S3 は、REST 状態のデータ保護を実現するための複数のオプションを提供します。これらのオプションには、権限(Policy)、暗号化(クライアント側およびサーバー側)、Bucket Versioning、および MFA による 削除制御 が含まれます。ユーザーは これらのオプションのいずれかを有効にしてデータ保護を実現できます。Data replication は AWS の内部機能で、S3 が各オブジェクトをすべての Availability Zones に自動的に複製するため、組織が有効化する必要がない場合があります。
リソースベースの権限を使用すると、バケットのサブディレクトリごとに権限を個別に定義できます。
Bucket Versioning and MFA based delete
Bucket Versioning が有効な場合、ファイルを変更しようとするあらゆる操作はファイルの新しいバージョンを生成し、以前の内容も保持するため、内容が上書きされることはありません。
さらに、MFA ベースの削除は S3 バケット内のファイルのバージョンが削除されるのを防ぎ、Bucket Versioning を無効化することも防止するため、攻撃者がこれらのファイルを改ざんすることを難しくします。
S3 Access logs
特定のバケットで S3 access login を有効(デフォルトでは無効)にして、ログを別のバケットに保存することで、誰がバケットにアクセスしているかを把握できます(両方のバケットは同じリージョンである必要があります)。
S3 Presigned URLs
バケット内の指定ファイルに アクセスするための presigned URL を生成することが可能です。通常、この presigned URL を使って 指定されたファイルにアクセスする ことができます。presigned URL は次のようになります:
https://<bucket-name>.s3.us-east-1.amazonaws.com/asd.txt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=ASIAUUE8GZC4S5L3TY3P%2F20230227%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20230227T142551Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Security-Token=IQoJb3JpZ2luX2VjELf%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCXVzLWVhc3QtMSJHMEUCIBhQpdETJO3HKKDk2hjNIrPWwBE8gZaQccZFV3kCpPCWAiEAid3ueDtFFU%2FOQfUpvxYTGO%2BHoS4SWDMUrQAE0pIaB40qggMIYBAAGgwzMTgxNDIxMzg1NTMiDJLI5t7gr2EGxG1Y5CrfAioW0foHIQ074y4gvk0c%2B%2Fmqc7cNWb1njQslQkeePHkseJ3owzc%2FCwkgE0EuZTd4mw0aJciA2XIbJRCLPWTb%2FCBKPnIMJ5aBzIiA2ltsiUNQTTUxYmEgXZoJ6rFYgcodnmWW0Et4Xw59UlHnCDB2bLImxPprriyCzDDCD6nLyp3J8pFF1S8h3ZTJE7XguA8joMs4%2B2B1%2FeOZfuxXKyXPYSKQOOSbQiHUQc%2BFnOfwxleRL16prWk1t7TamvHR%2Bt3UgMn5QWzB3p8FgWwpJ6GjHLkYMJZ379tkimL1tJ7o%2BIod%2FMYrS7LDCifP9d%2FuYOhKWGhaakPuJKJh9fl%2B0vGl7kmApXigROxEWon6ms75laXebltsWwKcKuYca%2BUWu4jVJx%2BWUfI4ofoaGiCSaKALTqwu4QNBRT%2BMoK6h%2BQa7gN7JFGg322lkxRY53x27WMbUE4unn5EmI54T4dWt1%2Bg8ljDS%2BvKfBjqmAWRwuqyfwXa5YC3xxttOr3YVvR6%2BaXpzWtvNJQNnb6v0uI3%2BTtTexZkJpLQYqFcgZLQSxsXWSnf988qvASCIUhAzp2UnS1uqy7QjtD5T73zksYN2aesll7rvB80qIuujG6NOdHnRJ2M5%2FKXXNo1Yd15MtzPuSjRoSB9RSMon5jFu31OrQnA9eCUoawxbB0nHqwK8a43CKBZHhA8RoUAJW%2B48EuFsp3U%3D&X-Amz-Signature=3436e4139e84dbcf5e2e6086c0ebc92f4e1e9332b6fda24697bc339acbf2cdfa
presigned URLは、cliからオブジェクトにアクセス権を持つprincipalのcredentialsを使用して作成できます(使用するaccountにアクセス権がない場合、より短いpresigned URLが作成されますが、役に立ちません)
aws s3 presign --region <bucket-region> 's3://<bucket-name>/<file-name>'
Note
presigned URL を生成するために必要な唯一の権限は、付与される権限そのものです。したがって、前のコマンドではプリンシパルが必要とする唯一の権限は
s3:GetObjectです
presigned URL を 他の権限 で作成することも可能です:
import boto3
url = boto3.client('s3').generate_presigned_url(
ClientMethod='put_object',
Params={'Bucket': 'BUCKET_NAME', 'Key': 'OBJECT_KEY'},
ExpiresIn=3600
)
S3 Encryption Mechanisms
DEK means Data Encryption Key and is the key that is always generated and used to encrypt data.
Server-side encryption with S3 managed keys, SSE-S3
このオプションは最小限の設定で済み、使用される暗号化キーの管理はすべてAWSが行います。やるべきことはデータをアップロードするだけで、S3がその他の処理をすべて処理します。各バケットにはバケットキーが割り当てられます。
- Encryption:
- Object Data + created plaintext DEK –> Encrypted data (stored inside S3)
- Created plaintext DEK + S3 Master Key –> Encrypted DEK (stored inside S3) and plain text is deleted from memory
- Decryption:
- Encrypted DEK + S3 Master Key –> Plaintext DEK
- Plaintext DEK + Encrypted data –> Object Data
Please, note that in this case the key is managed by AWS (rotation only every 3 years). If you use your own key you willbe able to rotate, disable and apply access control.
Server-side encryption with KMS managed keys, SSE-KMS
この方法では、S3がKMSを使ってあなたのデータ暗号化キーを生成できます。KMSによりキー管理の柔軟性が大幅に向上します。例えば、CMKを無効化、ローテーション、アクセス制御を適用したり、その利用をAWS Cloud Trailで監査することが可能です。
- Encryption:
- S3 request data keys from KMS CMK
- KMS uses a CMK to generate the pair DEK plaintext and DEK encrypted and send them to S£
- S3 uses the paintext key to encrypt the data, store the encrypted data and the encrypted key and deletes from memory the plain text key
- Decryption:
- S3 ask to KMS to decrypt the encrypted data key of the object
- KMS decrypt the data key with the CMK and send it back to S3
- S3 decrypts the object data
Server-side encryption with customer provided keys, SSE-C
このオプションは、AWS外で既に使用している可能性のある自身のマスターキーを提供する機会を与えます。カスタマー提供キーはデータとともにS3に送信され、S3が暗号化を実行します。
- Encryption:
- The user sends the object data + Customer key to S3
- The customer key is used to encrypt the data and the encrypted data is stored
- a salted HMAC value of the customer key is stored also for future key validation
- the customer key is deleted from memory
- Decryption:
- The user send the customer key
- The key is validated against the HMAC value stored
- The customer provided key is then used to decrypt the data
Client-side encryption with KMS, CSE-KMS
SSE-KMSと同様に、こちらもKMSを使ってデータ暗号化キーを生成します。ただし今回はS3ではなくクライアント側からKMSを呼び出します。暗号化はクライアント側で行われ、暗号化済みデータがS3に送られて保存されます。
- Encryption:
- Client request for a data key to KMS
- KMS returns the plaintext DEK and the encrypted DEK with the CMK
- Both keys are sent back
- The client then encrypts the data with the plaintext DEK and send to S3 the encrypted data + the encrypted DEK (which is saved as metadata of the encrypted data inside S3)
- Decryption:
- The encrypted data with the encrypted DEK is sent to the client
- The client asks KMS to decrypt the encrypted key using the CMK and KMS sends back the plaintext DEK
- The client can now decrypt the encrypted data
Client-side encryption with customer provided keys, CSE-C
この仕組みを使うと、自分で提供したキーを利用し、AWS-SDKクライアントで送信前にデータを暗号化してS3に保存できます。
- Encryption:
- The client generates a DEK and encrypts the plaintext data
- Then, using it’s own custom CMK it encrypts the DEK
- submit the encrypted data + encrypted DEK to S3 where it’s stored
- Decryption:
- S3 sends the encrypted data and DEK
- As the client already has the CMK used to encrypt the DEK, it decrypts the DEK and then uses the plaintext DEK to decrypt the data
Enumeration
One of the traditional main ways of compromising AWS orgs start by compromising buckets publicly accesible. You can find public buckets enumerators in this page.
# Get buckets ACLs
aws s3api get-bucket-acl --bucket <bucket-name>
aws s3api get-object-acl --bucket <bucket-name> --key flag
# Get policy
aws s3api get-bucket-policy --bucket <bucket-name>
aws s3api get-bucket-policy-status --bucket <bucket-name> #if it's public
# list S3 buckets associated with a profile
aws s3 ls
aws s3api list-buckets
# list content of bucket (no creds)
aws s3 ls s3://bucket-name --no-sign-request
aws s3 ls s3://bucket-name --recursive --no-sign-request
# list content of bucket (with creds)
aws s3 ls s3://bucket-name
aws s3api list-objects-v2 --bucket <bucket-name>
aws s3api list-objects --bucket <bucket-name>
aws s3api list-object-versions --bucket <bucket-name>
# copy local folder to S3
aws s3 cp MyFolder s3://bucket-name --recursive
# delete
aws s3 rb s3://bucket-name –-force
# download a whole S3 bucket
aws s3 sync s3://<bucket>/ .
# move S3 bucket to different location
aws s3 sync s3://oldbucket s3://newbucket --source-region us-west-1
# list the sizes of an S3 bucket and its contents
aws s3api list-objects --bucket BUCKETNAME --output json --query "[sum(Contents[].Size), length(Contents[])]"
# Update Bucket policy
aws s3api put-bucket-policy --policy file:///root/policy.json --bucket <bucket-name>
##JSON policy example
{
"Id": "Policy1568185116930",
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1568184932403",
"Action": [
"s3:ListBucket"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::welcome",
"Principal": "*"
},
{
"Sid": "Stmt1568185007451",
"Action": [
"s3:GetObject"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::welcome/*",
"Principal": "*"
}
]
}
# Update bucket ACL
aws s3api get-bucket-acl --bucket <bucket-name> # Way 1 to get the ACL
aws s3api put-bucket-acl --bucket <bucket-name> --access-control-policy file://acl.json
aws s3api get-object-acl --bucket <bucket-name> --key flag #Way 2 to get the ACL
aws s3api put-object-acl --bucket <bucket-name> --key flag --access-control-policy file://objacl.json
##JSON ACL example
## Make sure to modify the Owner’s displayName and ID according to the Object ACL you retrieved.
{
"Owner": {
"DisplayName": "<DisplayName>",
"ID": "<ID>"
},
"Grants": [
{
"Grantee": {
"Type": "Group",
"URI": "http://acs.amazonaws.com/groups/global/AuthenticatedUsers"
},
"Permission": "FULL_CONTROL"
}
]
}
## An ACL should give you the permission WRITE_ACP to be able to put a new ACL
dual-stack
バーチャルホスト方式またはパススタイルのエンドポイント名を使って、dual-stack エンドポイント経由で S3 バケットにアクセスできます。これらは IPv6 経由で S3 にアクセスする際に有用です。
Dual-stack endpoints use the following syntax:
bucketname.s3.dualstack.aws-region.amazonaws.coms3.dualstack.aws-region.amazonaws.com/bucketname
Privesc
次のページで、S3 の権限を悪用して権限を昇格させる方法を確認できます:
Unauthenticated Access
S3 Post Exploitation
Persistence
Other S3 vulns
S3 HTTP Cache Poisoning Issue
According to this research 任意のバケットのレスポンスを別のバケットのもののようにキャッシュすることが可能でした。これを悪用すると、例えば javascript ファイルのレスポンスを書き換え、S3 を利用して静的コードをホストしている任意のページを改ざんすることができました。
Amazon Athena
Amazon Athena は、Amazon Simple Storage Service (Amazon S3) 内のデータを標準の SQL を 使用して 直接 データを分析することを容易にするインタラクティブなクエリサービスです。
監視対象の S3 バケットに出現するコンテンツの形式に合わせて、リレーショナル DB テーブルを準備する必要があります。すると、Amazon Athena はログからそのテーブルにデータを取り込み、クエリを実行できるようになります。
Amazon Athena は既に暗号化されている S3 データをクエリする機能をサポートしており、設定によっては Athena がクエリ結果を暗号化し、それを S3 に保存することもできます。
このクエリ結果の暗号化は、クエリ対象となる基盤の S3 データとは独立しています。つまり S3 データ自体が暗号化されていなくても、クエリ結果は暗号化できます。注意点として、Amazon Athena は 以下の S3 暗号化方式、SSE-S3、SSE-KMS、および CSE-KMS で暗号化されたデータのみをサポートします。
SSE-C と CSE-C はサポートされません。加えて、Amazon Athena は クエリ自体と同じリージョンにある暗号化されたオブジェクトに対してのみクエリを実行する点に注意してください。KMS を使って暗号化された S3 データをクエリする場合、クエリを実行するために Athena ユーザーに特定の権限が必要になります。
Enumeration
# Get catalogs
aws athena list-data-catalogs
# Get databases inside catalog
aws athena list-databases --catalog-name <catalog-name>
aws athena list-table-metadata --catalog-name <catalog-name> --database-name <db-name>
# Get query executions, queries and results
aws athena list-query-executions
aws athena get-query-execution --query-execution-id <id> # Get query and meta of results
aws athena get-query-results --query-execution-id <id> # This will rerun the query and get the results
# Get workgroups & Prepared statements
aws athena list-work-groups
aws athena list-prepared-statements --work-group <wg-name>
aws athena get-prepared-statement --statement-name <name> --work-group <wg-name>
# Run query
aws athena start-query-execution --query-string <query>
参考資料
- https://cloudsecdocs.com/aws/defensive/tooling/cli/#s3
- https://docs.aws.amazon.com/AmazonS3/latest/userguide/dual-stack-endpoints.html
Tip
学んで実践する AWS Hacking:
HackTricks Training AWS Red Team Expert (ARTE)
学んで実践する GCP Hacking:HackTricks Training GCP Red Team Expert (GRTE)
学んで実践する Az Hacking:HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks をサポートする
- subscription plans を確認してください!
- 参加する 💬 Discord group または telegram group に参加するか、Twitter 🐦 @hacktricks_live をフォローしてください。
- Share hacking tricks by submitting PRs to the HackTricks and HackTricks Cloud github repos.
HackTricks Cloud

