关于 remediating-s3-bucket-misconfiguration
面向 AWS 安全团队的操作指南,用于识别并修复 S3 存储桶的公开访问和加密缺失问题。涵盖审计、修复、监控及组织级预防控制。
主要功能
- 01检测公开或配置错误的存储桶
- 02启用账户级 Block Public Access
- 03收紧存储桶策略并停用 ACL
- 04强制默认服务器端加密
- 05配置访问日志与告警
- 06通过 SCP 和 Config 预防问题
适用场景
- 01响应公开访问或缺少加密的告警
- 02调查公开存储桶导致的数据泄露
- 03准备存储安全控制审计
- 04为 AWS Organization 建立预防控制
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ mukul975/ anthropic-cybersecurity-skills --skill remediating-s3-bucket-misconfiguration安装后在 Claude Code 中输入/remediating-s3-bucket-misconfiguration确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
LICENSEreferences/api-reference.mdscripts/agent.py详细介绍
修复 S3 存储桶配置错误
何时使用
- 当 AWS Config 或 Security Hub 报告 S3 存储桶具有公开访问权限或缺少加密时
- 当安全扫描发现 S3 存储桶策略授予 Principal "*"(所有人)访问权限时
- 当准备数据保护审计、需要存储安全控制措施的取证时
- 当响应涉及可公开访问的 S3 对象的数据暴露事件时
- 当为整个 AWS Organization 中新建 S3 存储桶建立预防性控制措施时
不要使用于 Azure Blob Storage 或 GCP Cloud Storage 的配置错误,用于 S3 数据分类(参见 implementing-cloud-dlp-policy),或用于与安全无关的 S3 访问模式分析。
前置条件
- 具有 S3 管理权限(s3:、s3-outposts:)的 AWS 账户
- 已启用 AWS Config 以评估 S3 资源合规性
- AWS CloudTrail 记录 S3 数据事件以进行访问审计
- 已启用 Macie 以发现 S3 存储桶中的敏感数据
工作流
步骤 1:识别所有公开和配置错误的存储桶
使用多种检测方法识别具有公开访问权限的 S3 存储桶。依靠 AWS Config 规则、S3 Access Analyzer 和 Macie,而不是手动检查。
# Enable S3 Access Analyzer for external access detection
aws accessanalyzer create-analyzer \
--analyzer-name s3-analyzer \
--type ACCOUNT
# List all S3 buckets with public access indicators
aws s3api list-buckets --query 'Buckets[*].Name' --output text | while read bucket; do
public_status=$(aws s3api get-public-access-block --bucket "$bucket" 2>/dev/null)
if [ $? -ne 0 ]; then
echo "NO PUBLIC ACCESS BLOCK: $bucket"
fi
done
# Check bucket policies for public access grants
aws s3api list-buckets --query 'Buckets[*].Name' --output text | while read bucket; do
policy=$(aws s3api get-bucket-policy --bucket "$bucket" 2>/dev/null)
if echo "$policy" | grep -q '"Principal":"*"' 2>/dev/null; then
echo "PUBLIC POLICY DETECTED: $bucket"
fi
done
# Use AWS Config to find non-compliant buckets
aws configservice get-compliance-details-by-config-rule \
--config-rule-name s3-bucket-public-read-prohibited \
--compliance-types NON_COMPLIANT \
--query 'EvaluationResults[*].EvaluationResultIdentifier.EvaluationResultQualifier.ResourceId'
步骤 2:在账户级别启用 S3 阻止公开访问
在 AWS 账户级别应用四项"阻止公开访问"设置作为安全网。这可防止账户中的任何存储桶被设为公开,无论单个存储桶策略或 ACL 如何。
# Enable account-level Block Public Access (all four settings)
aws s3control put-public-access-block \
--account-id 123456789012 \
--public-access-block-configuration '{
"BlockPublicAcls": true,
"IgnorePublicAcls": true,
"BlockPublicPolicy": true,
"RestrictPublicBuckets": true
}'
# Verify account-level settings
aws s3control get-public-access-block --account-id 123456789012
# Enable at bucket level for defense in depth
aws s3api put-public-access-block \
--bucket production-data-bucket \
--public-access-block-configuration '{
"BlockPublicAcls": true,
"IgnorePublicAcls": true,
"BlockPublicPolicy": true,
"RestrictPublicBuckets": true
}'
步骤 3:审计并修复存储桶策略和 ACL
审查所有存储桶策略中过于宽松的 Principal 语句,并移除遗留 ACL。强制执行存储桶所有权控制以完全禁用 ACL。
# Remove a public bucket policy
aws s3api delete-bucket-policy --bucket exposed-bucket
# Replace with a restrictive policy
aws s3api put-bucket-policy --bucket exposed-bucket --policy '{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenyUnencryptedTransport",
"Effect": "Deny",
"Principal": "*",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::exposed-bucket",
"arn:aws:s3:::exposed-bucket/*"
],
"Condition": {
"Bool": {"aws:SecureTransport": "false"}
}
},
{
"Sid": "AllowOnlyVPCEndpoint",
"Effect": "Deny",
"Principal": "*",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::exposed-bucket",
"arn:aws:s3:::exposed-bucket/*"
],
"Condition": {
"StringNotEquals": {"aws:SourceVpce": "vpce-0abc123def456"}
}
}
]
}'
# Enforce bucket owner for all objects (disable ACLs)
aws s3api put-bucket-ownership-controls --bucket exposed-bucket \
--ownership-controls '{"Rules": [{"ObjectOwnership": "BucketOwnerEnforced"}]}'
步骤 4:强制执行默认加密
为所有存储桶启用使用 AWS KMS 或 AES-256 的默认服务器端加密。添加拒绝未加密对象上传的存储桶策略。
# Enable default KMS encryption
aws s3api put-bucket-encryption --bucket production-data-bucket \
--server-side-encryption-configuration '{
"Rules": [{
"ApplyServerSideEncryptionByDefault": {
"SSEAlgorithm": "aws:kms",
"KMSMasterKeyID": "arn:aws:kms:us-east-1:123456789012:key/key-id"
},
"BucketKeyEnabled": true
}]
}'
# Deny unencrypted uploads via bucket policy
aws s3api put-bucket-policy --bucket production-data-bucket --policy '{
"Version": "2012-10-17",
"Statement": [{
"Sid": "DenyUnencryptedUploads",
"Effect": "Deny",
"Principal": "*",
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::production-data-bucket/*",
"Condition": {
"StringNotEquals": {"s3:x-amz-server-side-encryption": ["aws:kms", "AES256"]}
}
}]
}'
步骤 5:启用访问日志记录和监控
配置 S3 服务器访问日志记录和 CloudTrail 数据事件,以跟踪所有对象级操作。设置 EventBridge 规则以对可疑访问模式发出告警。
# Enable server access logging
aws s3api put-bucket-logging --bucket production-data-bucket \
--bucket-logging-status '{
"LoggingEnabled": {
"TargetBucket": "s3-access-logs-bucket",
"TargetPrefix": "production-data-bucket/"
}
}'
# Enable CloudTrail S3 data events
aws cloudtrail put-event-selectors --trail-name management-trail \
--event-selectors '[{
"ReadWriteType": "All",
"DataResources": [{
"Type": "AWS::S3::Object",
"Values": ["arn:aws:s3:::production-data-bucket/"]
}]
}]'
步骤 6:使用 SCP 和 Config 部署预防性控制措施
使用服务控制策略防止在整个组织内禁用"阻止公开访问"。部署带自动修复功能的 AWS Config 规则。
# SCP preventing Block Public Access removal
aws organizations create-policy \
--name PreventS3PublicAccess \
--type SERVICE_CONTROL_POLICY \
--content '{
"Version": "2012-10-17",
"Statement": [{
"Sid": "DenyRemovePublicAccessBlock",
"Effect": "Deny",
"Action": [
"s3:PutBucketPublicAccessBlock",
"s3:PutAccountPublicAccessBlock"
],
"Resource": "*",
"Condition": {
"StringNotLike": {"aws:PrincipalArn": "arn:aws:iam::*:role/SecurityAdmin"}
}
}]
}'
关键概念
| 术语 | 定义 |
|---|---|
| S3 阻止公开访问 | 四项账户级和存储桶级设置,可覆盖任何授予对 S3 资源公开访问权限的策略或 ACL |
| 存储桶策略 | 附加到 S3 存储桶的基于 JSON 的资源策略,定义谁可以在什么条件下访问哪些对象 |
| ACL(访问控制列表) | 遗留的 S3 访问机制,在存储桶或对象级别授予权限;应通过 BucketOwnerEnforced 禁用 |
| BucketOwnerEnforced | 所有权控制设置,禁用存储桶上的所有 ACL,使存储桶拥有者成为访问控制的唯一权威 |
| 服务器端加密 | 使用 AES-256(SSE-S3)、AWS KMS(SSE-KMS)或客户提供的密钥(SSE-C)对静态对象进行自动加密 |
| VPC 终端节点 | VPC 与 S3 之间的私有连接,将存储桶访问限制为源自 VPC 内部的流量 |
| S3 Access Analyzer | IAM Access Analyzer 功能,用于识别与账户或组织外部实体共享的 S3 存储桶 |
工具与系统
- AWS Config:根据托管规则评估 S3 存储桶合规性,并对不合规资源触发自动修复
- Amazon Macie:发现并分类 S3 存储桶中的敏感数据,以识别哪些错误配置会导致最高的数据暴露风险
- IAM Access Analyzer:识别策略或 ACL 允许外部主体访问的 S3 存储桶
- S3 Storage Lens:提供组织范围内对 S3 使用模式、访问指标和安全异常的可见性
- Prowler:开源工具,依据 CIS 基准和最佳实践检查 S3 安全性
常见场景
场景:包含 PII 的可公开读取 S3 存储桶导致的数据泄露
背景:一名安全研究人员报告称,一个包含 263,000 份银行转账 PDF 的 S3 存储桶可被公开读取。该存储桶由一名开发人员创建,他需要与外部合作伙伴共享文件,并将 ACL 设置为公开。
处理方法:
- 立即在该特定存储桶上启用 Block Public Access,以停止暴露
- 通过设置 BucketOwnerEnforced 所有权控制撤销所有公开 ACL
- 审计 CloudTrail 和 S3 访问日志,以确定哪些 IP 地址访问了暴露的对象
- 在该存储桶上运行 Macie,以分类暴露的 PII 类型并评估监管通知要求
- 启用账户级 Block Public Access,以防止所有存储桶再次发生此类问题
- 部署 SCP,防止除 SecurityAdmin 之外的任何主体修改 Block Public Access 设置
- 为合法的合作伙伴共享用例创建预签名 URL 机制或 S3 Access Point
陷阱:启用 Block Public Access 而未通知设置公开访问的团队会破坏他们的工作流。在修复之前未运行访问日志分析会丢失谁访问了暴露数据的证据。
输出格式
S3 Bucket Security Remediation Report
=======================================
Account: 123456789012
Assessment Date: 2025-02-23
Buckets Scanned: 156
ACCOUNT-LEVEL CONTROLS:
Block Public Access: ENABLED (all four settings)
SCP Preventing Removal: DEPLOYED
CRITICAL FINDINGS (Remediated):
[S3-001] production-uploads - Public READ via ACL
Status: REMEDIATED - BucketOwnerEnforced applied
Objects Exposed: 273,412
Duration of Exposure: 47 days
Unique External IPs Accessed: 1,247
[S3-002] analytics-export - Public bucket policy (Principal: *)
Status: REMEDIATED - Policy replaced with VPC endpoint restriction
Sensitive Data (Macie): 12,400 objects with PII detected
HIGH FINDINGS:
[S3-003] 14 buckets missing default encryption
Status: REMEDIATED - KMS encryption enabled
[S3-004] 8 buckets without server access logging
Status: REMEDIATED - Logging enabled to centralized log bucket
SUMMARY:
Buckets Remediated: 24/156
Encryption Coverage: 100%
Access Logging Coverage: 100%
Block Public Access: 156/156 buckets