Pii Leakage in Dynamodb
How PII Leakage Manifests in DynamoDB
PII leakage in DynamoDB typically occurs through misconfigured IAM policies, overly permissive data access patterns, and improper handling of sensitive attributes. The NoSQL nature of DynamoDB creates unique attack vectors that don't exist in traditional RDBMS systems.
One common pattern involves developers using overly broad IAM roles that allow full table scans. When an attacker compromises these credentials, they can enumerate entire datasets. For example:
// Vulnerable: Overly permissive IAM role
const dynamodb = new AWS.DynamoDB.DocumentClient({
region: 'us-east-1'
});
const params = {
TableName: 'Users'
};
dynamodb.scan(params, (err, data) => {
// Returns ALL user records including PII
console.log(data.Items);
});This scan operation exposes every attribute in the table, including sensitive fields like email addresses, phone numbers, and addresses. DynamoDB's eventual consistency model can also lead to stale PII being exposed during replication delays.
Another DynamoDB-specific vulnerability involves Global Secondary Indexes (GSIs) that inadvertently expose PII through their projected attributes. When developers project all attributes into a GSI for query flexibility, they create additional attack surfaces:
# Vulnerable: Projecting all attributes into GSI
dynamodb.create_table(
TableName='Users',
KeySchema=[
{'AttributeName': 'userId', 'KeyType': 'HASH'}
],
AttributeDefinitions=[
{'AttributeName': 'userId', 'AttributeType': 'S'}
],
GlobalSecondaryIndexes=[
{
'IndexName': 'EmailIndex',
'KeySchema': [{'AttributeName': 'email', 'KeyType': 'HASH'}],
'Projection': {'ProjectionType': 'ALL'} // Exposes all attributes!
}
]
)Batch operations present another risk. The BatchGetItem API can return multiple items in a single request, potentially exposing large amounts of PII if not properly filtered:
// Vulnerable: Batch operation without PII filtering
const batchParams = {
RequestItems: {
'Users': {
Keys: [ // All these keys will return full items
{ userId: 'user1' },
{ userId: 'user2' },
{ userId: 'user3' }
]
}
}
};
dynamodb.batchGet(batchParams, (err, data) => {
// Each item contains all attributes, including PII
});Cross-partition queries using Scan operations are particularly dangerous in DynamoDB because they can't be properly rate-limited at the item level, making them attractive targets for attackers seeking to harvest PII.
DynamoDB-Specific Detection
Detecting PII leakage in DynamoDB requires examining both IAM policies and data access patterns. The first step is reviewing IAM policies for overly permissive actions:
// Vulnerable IAM policy
{
'Version': '2012-10-17',
'Statement': [
{
'Effect': 'Allow',
'Action': [
'dynamodb:Scan',
'dynamodb:Query',
'dynamodb:GetItem',
'dynamodb:BatchGetItem'
],
'Resource': 'arn:aws:dynamodb:*:*:table/*' // Too broad!
}
]
}Tools like middleBrick can automatically detect these patterns by scanning your DynamoDB endpoints and analyzing the unauthenticated attack surface. The scanner tests for common PII exposure vectors including:
- Scan operations that return all attributes
- Batch operations without proper filtering
- GSI configurations that project sensitive attributes
- Missing encryption at rest for sensitive data
- Improper VPC endpoint configurations
For runtime detection, implement CloudTrail monitoring to detect suspicious patterns:
import boto3
cloudtrail = boto3.client('cloudtrail')
response = cloudtrail.lookup_events(
LookupAttributes=[
{'AttributeKey': 'EventName', 'AttributeValue': 'Scan'}
],
MaxResults=50
)
for event in response['Events']:
if 'dynamodb:Scan' in event['EventName']:
print(f'Potential PII exposure: {event}')Data classification is critical for DynamoDB PII detection. Use AWS Macie or custom Lambda functions to scan table contents:
const macie = new AWS.Macie();
const response = await macie.getSensitiveDataOverview({
s3JobId: 'your-job-id'
}).promise();
console.log('PII findings:', response.sensitiveData);
// Check for SSN, credit card numbers, etc.middleBrick's DynamoDB-specific scanner can identify these issues in under 15 seconds without requiring credentials, making it ideal for security assessments and CI/CD pipeline integration.
DynamoDB-Specific Remediation
Remediating PII leakage in DynamoDB requires a defense-in-depth approach using native AWS features and proper application design patterns. Start with IAM policy refinement:
// Secure IAM policy
{
'Version': '2012-10-17',
'Statement': [
{
'Effect': 'Allow',
'Action': [
'dynamodb:GetItem',
'dynamodb:Query'
],
'Resource': 'arn:aws:dynamodb:us-east-1:123456789012:table/Users'
},
{
'Effect': 'Allow',
'Action': ['dynamodb:BatchGetItem'],
'Resource': 'arn:aws:dynamodb:us-east-1:123456789012:table/Users',
'Condition': {
'ForAllValues:StringEquals': {
'dynamodb:Attributes': [
'userId', 'username' // Only allow specific attributes
]
}
}
}
]
}Implement attribute-level encryption using AWS KMS:
const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB.DocumentClient();
const kms = new AWS.KMS();
async function encryptPII(data) {
const emailParams = {
KeyId: 'alias/pii-encryption-key',
Plaintext: data.email
};
const encryptedEmail = await kms.encrypt(emailParams).promise();
return {
...data,
email: encryptedEmail.CiphertextBlob.toString('base64')
};
}
async function saveUser(user) {
const encryptedUser = await encryptPII(user);
await dynamodb.put({
TableName: 'Users',
Item: encryptedUser
}).promise();
}Use DynamoDB's fine-grained access control with IAM conditions:
// Secure query with attribute filtering
const params = {
TableName: 'Users',
KeyConditionExpression: 'userId = :userId',
ExpressionAttributeValues: { ':userId': userId },
ProjectionExpression: 'userId, username, #em', // Only specific attributes
ExpressionAttributeNames: { '#em': 'email' }
};
dynamodb.query(params, (err, data) => {
// Only returns specified attributes
});Implement VPC endpoints for DynamoDB to prevent data exfiltration:
Resources:
DynamoDBVPCEndpoint:
Type: AWS::EC2::VPCEndpoint
Properties:
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Deny
Principal: '*'
Action: 'dynamodb:*'
Resource: '*'
Condition:
NotIpAddress: {
'aws:SourceVpc': vpc-id
}
ServiceName: !Sub 'com.amazonaws.${AWS::Region}.dynamodb'
VpcId: !Ref VPCUse middleBrick's continuous monitoring to verify these remediations remain effective over time. The Pro plan can scan your DynamoDB endpoints on a configurable schedule and alert you if new PII exposure risks emerge.
Related CWEs: dataExposure
| CWE ID | Name | Severity |
|---|---|---|
| CWE-200 | Exposure of Sensitive Information | HIGH |
| CWE-209 | Error Information Disclosure | MEDIUM |
| CWE-213 | Exposure of Sensitive Information Due to Incompatible Policies | HIGH |
| CWE-215 | Insertion of Sensitive Information Into Debugging Code | MEDIUM |
| CWE-312 | Cleartext Storage of Sensitive Information | HIGH |
| CWE-359 | Exposure of Private Personal Information (PII) | HIGH |
| CWE-522 | Insufficiently Protected Credentials | CRITICAL |
| CWE-532 | Insertion of Sensitive Information into Log File | MEDIUM |
| CWE-538 | Insertion of Sensitive Information into Externally-Accessible File | HIGH |
| CWE-540 | Inclusion of Sensitive Information in Source Code | HIGH |