| name | detecting-s3-data-exfiltration-attempts |
| description | 通过分析 CloudTrail S3 数据事件、VPC Flow Logs、GuardDuty 发现、Amazon Macie 告警和 S3 访问模式,检测 AWS S3 存储桶的数据泄露企图,识别未授权的批量下载和跨账户数据传输。
|
| domain | cybersecurity |
| subdomain | cloud-security |
| tags | ["cloud-security","aws","s3","data-exfiltration","guardduty","macie","threat-detection"] |
| version | 1.0 |
| author | mahipal |
| license | Apache-2.0 |
检测 S3 数据泄露企图
适用场景
- GuardDuty 检测到异常的 S3 访问模式(如来自不寻常 IP 的批量下载)时
- 调查涉及 S3 中存储的敏感数据的疑似数据泄露时
- 为 S3 数据丢失防护监控构建检测规则时
- 响应 Macie 关于敏感数据被访问或移动的告警时
- 合规要求需要监控和记录所有对分类数据存储的访问时
不适用于:防止数据泄露(使用 S3 存储桶策略、VPC 端点和 SCP)、数据分类(使用 Amazon Macie 发现作业),或网络层面的泄露检测(使用 VPC Flow Logs 配合网络分析工具)。
前置条件
- 配置 CloudTrail 并启用 S3 数据事件日志记录(
GetObject、PutObject、CopyObject)
- 启用 GuardDuty 并激活 S3 Protection 功能
- 对目标存储桶启用 Amazon Macie 进行敏感数据发现
- 配置 CloudWatch Logs 或 Athena 用于大规模查询 CloudTrail 日志
- 配置 VPC 端点策略用于 S3 访问监控
工作流程
步骤 1:在 CloudTrail 中启用 S3 数据事件日志记录
配置 CloudTrail 捕获所有 S3 对象级别操作,用于取证分析。
aws cloudtrail put-event-selectors \
--trail-name management-trail \
--event-selectors '[{
"ReadWriteType": "All",
"IncludeManagementEvents": true,
"DataResources": [{
"Type": "AWS::S3::Object",
"Values": ["arn:aws:s3:::sensitive-data-bucket/", "arn:aws:s3:::customer-records/"]
}]
}]'
aws cloudtrail get-event-selectors --trail-name management-trail \
--query 'EventSelectors[*].DataResources' --output json
aws guardduty update-detector \
--detector-id $(aws guardduty list-detectors --query 'DetectorIds[0]' --output text) \
--data-sources '{"S3Logs":{"Enable":true}}'
步骤 2:查询 CloudTrail 中的异常 S3 访问模式
分析 CloudTrail 日志,识别批量下载活动、异常访问时间和不熟悉的来源 IP。
cat << 'EOF'
SELECT
useridentity.arn as principal,
sourceipaddress,
COUNT(*) as request_count,
SUM(CAST(json_extract_scalar(requestparameters, '$.bytesTransferredOut') AS bigint)) as bytes_downloaded
FROM cloudtrail_logs
WHERE eventname = 'GetObject'
AND eventsource = 's3.amazonaws.com'
AND eventtime > date_add('hour', -24, now())
GROUP BY useridentity.arn, sourceipaddress
ORDER BY request_count DESC
LIMIT 50
EOF
aws logs start-query \
--log-group-name cloudtrail-logs \
--start-time $(date -d "24 hours ago" +%s) \
--end-time $(date +%s) \
--query-string '
fields @timestamp, userIdentity.arn, sourceIPAddress, requestParameters.bucketName, requestParameters.key
| filter eventName = "GetObject"
| stats count() as requestCount by sourceIPAddress, userIdentity.arn
| sort requestCount desc
| limit 25
'
aws logs start-query \
--log-group-name cloudtrail-logs \
--start-time $(date -d "7 days ago" +%s) \
--end-time $(date +%s) \
--query-string '
fields @timestamp, userIdentity.arn, sourceIPAddress, requestParameters.bucketName
| filter eventName in ["CopyObject", "ReplicateObject", "UploadPart"]
| filter userIdentity.accountId != "OUR_ACCOUNT_ID"
| sort @timestamp desc
| limit 100
'
步骤 3:审查 GuardDuty S3 发现
检查表示泄露活动的 GuardDuty S3 专项发现类型。
aws guardduty list-findings \
--detector-id $(aws guardduty list-detectors --query 'DetectorIds[0]' --output text) \
--finding-criteria '{
"Criterion": {
"type": {
"Eq": [
"Exfiltration:S3/MaliciousIPCaller",
"Exfiltration:S3/ObjectRead.Unusual",
"Discovery:S3/MaliciousIPCaller.Custom",
"Discovery:S3/BucketEnumeration.Unusual",
"UnauthorizedAccess:S3/MaliciousIPCaller.Custom",
"UnauthorizedAccess:S3/TorIPCaller",
"Impact:S3/AnomalousBehavior.Delete"
]
}
}
}' --output json
aws guardduty get-findings \
--detector-id $(aws guardduty list-detectors --query 'DetectorIds[0]' --output text) \
--finding-ids FINDING_IDS \
--query 'Findings[*].{Type:Type,Severity:Severity,Resource:Resource.S3BucketDetails[0].Name,Action:Service.Action}' \
--output table
步骤 4:分析 Macie 发现以确定敏感数据访问情况
审查 Macie 发现,将数据敏感性与访问异常进行关联。
aws macie2 list-findings \
--finding-criteria '{
"criterion": {
"category": {"eq": ["CLASSIFICATION"]},
"severity.description": {"eq": ["High", "Critical"]}
}
}' \
--sort-criteria '{"attributeName": "updatedAt", "orderBy": "DESC"}' \
--max-results 25
aws macie2 get-findings \
--finding-ids FINDING_IDS \
--query 'findings[*].{Type:type,Severity:severity.description,Bucket:resourcesAffected.s3Bucket.name,SensitiveDataTypes:classificationDetails.result.sensitiveData[*].category}' \
--output table
aws macie2 create-classification-job \
--job-type ONE_TIME \
--name "exfiltration-investigation" \
--s3-job-definition '{
"bucketDefinitions": [{
"accountId": "ACCOUNT_ID",
"buckets": ["sensitive-data-bucket"]
}]
}'
步骤 5:构建自动化检测规则
创建 CloudWatch 告警和 EventBridge 规则用于实时泄露检测。
aws logs put-metric-filter \
--log-group-name cloudtrail-logs \
--filter-name s3-bulk-download \
--filter-pattern '{$.eventName = "GetObject" && $.eventSource = "s3.amazonaws.com"}' \
--metric-transformations '[{
"metricName": "S3GetObjectCount",
"metricNamespace": "SecurityMetrics",
"metricValue": "1",
"defaultValue": 0
}]'
aws cloudwatch put-metric-alarm \
--alarm-name s3-exfiltration-alert \
--metric-name S3GetObjectCount \
--namespace SecurityMetrics \
--statistic Sum \
--period 3600 \
--threshold 1000 \
--comparison-operator GreaterThanThreshold \
--evaluation-periods 1 \
--alarm-actions arn:aws:sns:us-east-1:ACCOUNT:security-alerts
aws events put-rule \
--name guardduty-s3-exfiltration \
--event-pattern '{
"source": ["aws.guardduty"],
"detail-type": ["GuardDuty Finding"],
"detail": {
"type": [{"prefix": "Exfiltration:S3/"}]
}
}'
步骤 6:实施预防性控制
部署存储桶策略和 VPC 端点策略,限制数据移动路径。
aws ec2 modify-vpc-endpoint \
--vpc-endpoint-id vpce-ENDPOINT_ID \
--policy-document '{
"Statement": [{
"Sid": "RestrictToOwnBuckets",
"Effect": "Allow",
"Principal": "*",
"Action": ["s3:GetObject", "s3:PutObject"],
"Resource": ["arn:aws:s3:::approved-bucket-1/*", "arn:aws:s3:::approved-bucket-2/*"]
}]
}'
aws s3api put-bucket-policy --bucket sensitive-data-bucket --policy '{
"Version": "2012-10-17",
"Statement": [{
"Sid": "DenyNonVpcAccess",
"Effect": "Deny",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::sensitive-data-bucket/*",
"Condition": {
"StringNotEquals": {
"aws:sourceVpce": "vpce-ENDPOINT_ID"
}
}
}]
}'
核心概念
| 术语 | 定义 |
|---|
| S3 数据事件(S3 Data Events) | CloudTrail 对象级别日志记录,捕获 GetObject、PutObject、DeleteObject 和 CopyObject API 调用及请求详情 |
| GuardDuty S3 Protection | 威胁检测功能,分析 CloudTrail S3 数据事件以识别异常访问模式和泄露企图 |
| Amazon Macie | 数据安全服务,在 S3 中发现和分类敏感数据,并为数据暴露风险生成发现 |
| VPC 端点策略(VPC Endpoint Policy) | S3 VPC 端点上的访问控制策略,限制可通过端点访问哪些存储桶和操作 |
| 数据泄露(Data Exfiltration) | 未授权地将数据从组织的 S3 存储传输到攻击者控制的外部位置 |
| 异常行为检测(Anomalous Behavior Detection) | 基于机器学习识别主体 S3 访问模式中偏离既定基线的异常 |
工具与系统
- AWS CloudTrail:S3 对象级别操作的审计日志,用于取证分析和异常检测
- Amazon GuardDuty:基于 ML 的威胁检测,包含用于泄露和未授权访问的 S3 专项发现类型
- Amazon Macie:敏感数据发现和分类,用于将访问异常与数据敏感性关联
- Amazon Athena:SQL 查询引擎,用于大规模分析 CloudTrail 日志以识别批量下载模式
- CloudWatch Logs Insights:实时日志分析,用于针对 CloudTrail 数据构建检测查询
常见场景
场景:被入侵的 IAM 凭据用于 S3 批量数据下载
场景背景:GuardDuty 报告 Exfiltration:S3/ObjectRead.Unusual 发现,显示一名开发者的访问密钥在凌晨 3 点从境外 IP 地址下载了敏感数据存储桶中的数千个对象。
方法:
- 立即停用被入侵的访问密钥
- 查询 CloudTrail 中被入侵主体在过去 72 小时内的所有 S3 操作
- 使用 Athena 查询识别访问了哪些存储桶和对象
- 将访问的对象与 Macie 分类结果交叉比对,评估数据敏感性
- 检查是否有 CopyObject 调用涉及外部账户(跨账户泄露)
- 调查凭据如何被入侵(TruffleHog 扫描、钓鱼调查)
- 实施 VPC 端点策略,将未来的 S3 访问限制在批准的网络路径
常见陷阱:CloudTrail S3 数据事件可能产生海量日志。对于跨越 24 小时以上的查询,应使用分区表的 Athena 而非 CloudWatch Logs Insights。GuardDuty 基线学习需要 7-14 天,因此新账户的正常访问模式可能会产生误报。
输出格式
S3 数据泄露调查报告
============================================
账户: 123456789012
检测来源: GuardDuty Exfiltration:S3/ObjectRead.Unusual
调查日期: 2026-02-23
事件时间线:
2026-02-23 02:47 UTC - 首次来自 185.x.x.x 的异常 GetObject 请求
2026-02-23 02:47-04:12 UTC - 12,847 次 GetObject 请求
2026-02-23 04:15 UTC - GuardDuty 发现生成
2026-02-23 04:20 UTC - PagerDuty 告警发送至 SOC
2026-02-23 04:25 UTC - 访问密钥已停用
被入侵主体:
ARN: arn:aws:iam::123456789012:user/developer-jane
访问密钥: AKIA...WXYZ
来源 IP: 185.x.x.x(Tor 出口节点)
数据影响评估:
访问的存储桶: 3 个
下载的对象: 12,847 个
总数据量: 4.7 GB
敏感数据类型: PII(SSN、电子邮件)、金融数据(信用卡号)
Macie 严重级别: 严重
遏制操作:
[x] 访问密钥已停用
[x] 用户密码已重置,MFA 已重新注册
[x] VPC 端点策略已应用于敏感存储桶
[x] 存储桶策略已限制为仅 VPC 访问
[x] TruffleHog 扫描已对开发者仓库启动