03 · 安全基础¶
官方文档:https://owasp.org/ · https://www.cloudflare.com/learning/security/ iGaming 重点:DDoS 是最常见威胁 · 撞库攻击 · 赔率爬取 · 支付安全(PCI DSS)
1. DDoS 攻击类型 & 防护¶
攻击分层¶
L3/L4(网络/传输层):
- UDP Flood:大量 UDP 包耗尽带宽
- SYN Flood:半开连接耗尽服务器资源
- ICMP Flood:Ping 洪水
L7(应用层,最难防):
- HTTP Flood:大量正常 HTTP 请求
- Slowloris:慢速连接耗尽连接池
- 撞库:使用泄露的账号密码批量登录
- 赔率爬取:爬虫高频抓取赔率数据
Cloudflare DDoS 防护流程¶
1. Cloudflare 边缘节点自动检测异常流量模式
2. 超过阈值触发 CAPTCHA 或 JS Challenge
3. 攻击流量在边缘清洗,不到达 Origin
4. 告警通知运维团队
手动触发 Under Attack Mode(I'm Under Attack):
Cloudflare Dashboard → Security → Settings → Security Level: Under Attack
或通过 API:
curl -X PATCH "https://api.cloudflare.com/client/v4/zones/{zone_id}/settings/security_level" \
-H "Authorization: Bearer {token}" \
-d '{"value":"under_attack"}'
2. WAF 规则(Cloudflare)¶
规则表达式语法¶
# 封锁特定 User-Agent
http.user_agent contains "python-requests"
# 封锁特定 IP 段(竞争对手/攻击者)
ip.src in {1.2.3.0/24 5.6.7.0/24}
# 允许白名单 IP 绕过(内部 IP)
not ip.src in {10.0.0.0/8 172.16.0.0/12}
# 防赔率爬取:限制 /api/odds 频率
http.request.uri.path eq "/api/odds" and rate.requests > 60
# 防撞库:登录接口速率限制
http.request.uri.path eq "/auth/login" and rate.requests > 10
# 地区封锁(合规要求)
ip.geoip.country in {"CN" "KP" "IR"}
# 复杂规则:非正常浏览器 + 高频请求
(not http.user_agent matches "Mozilla|Chrome|Safari|Firefox")
and http.request.method eq "POST"
WAF 规则动作¶
Block → 直接返回 403,记录日志
Challenge → CAPTCHA 验证(Turnstile)
JS Challenge → JavaScript 检测(无感知)
Log → 仅记录不拦截(测试规则时用)
Skip → 跳过后续规则(白名单)
3. IAM 权限最小化¶
AWS IAM 设计原则¶
1. 永远不用 Root 账号做日常操作
2. 每个服务/应用单独一个 IAM Role(不共用)
3. 权限只授予需要的,不授予 *(Admin)
4. 使用 IAM Conditions 进一步限制(MFA/IP/时间)
最小权限 Policy 示例¶
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Resource": "arn:aws:s3:::my-backup-bucket/*",
"Condition": {
"IpAddress": {
"aws:SourceIp": "10.0.0.0/8"
}
}
}
]
}
EC2 Instance Role(推荐,不用 Access Key)¶
# 在 EC2 实例上使用 Role,无需配置 Access Key
aws s3 ls s3://my-bucket/ # 自动使用 Instance Role
# 检查当前 Role
curl http://169.254.169.254/latest/meta-data/iam/info
4. 密钥管理¶
AWS Secrets Manager¶
# 存储数据库密码
aws secretsmanager create-secret \
--name "prod/db/password" \
--secret-string '{"username":"admin","password":"s3cr3t"}'
# 应用读取
aws secretsmanager get-secret-value \
--secret-id "prod/db/password" \
--query SecretString --output text
# Python SDK 读取
import boto3, json
client = boto3.client('secretsmanager', region_name='ap-southeast-1')
secret = json.loads(client.get_secret_value(SecretId='prod/db/password')['SecretString'])
AWS Parameter Store(适合非敏感配置)¶
# 存储参数
aws ssm put-parameter \
--name "/app/prod/db_host" \
--value "mydb.cluster.rds.amazonaws.com" \
--type "SecureString"
# 读取参数
aws ssm get-parameter --name "/app/prod/db_host" --with-decryption
HashiCorp Vault(自托管密钥管理)¶
# 官方文档:https://developer.hashicorp.com/vault/docs
vault kv put secret/myapp password="s3cr3t"
vault kv get secret/myapp
5. SSH 安全 & 零信任¶
SSH 密钥管理¶
# 生成 ED25519 密钥(推荐,比 RSA 更安全更短)
ssh-keygen -t ed25519 -C "deploy@company.com"
# 部署公钥
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server
# SSH 配置(~/.ssh/config)
Host bastion-prod
HostName bastion.company.com
User deploy
IdentityFile ~/.ssh/id_ed25519
ServerAliveInterval 30
Host app-prod-*
User deploy
ProxyJump bastion-prod
IdentityFile ~/.ssh/id_ed25519
Cloudflare Tunnel(替代 VPN)¶
# 官方文档:https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/
# 安装 cloudflared
wget https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb
dpkg -i cloudflared-linux-amd64.deb
# 创建 tunnel
cloudflared tunnel create my-tunnel
cloudflared tunnel route dns my-tunnel internal.company.com
# 配置(~/.cloudflared/config.yml)
tunnel: <tunnel-id>
credentials-file: /etc/cloudflared/<tunnel-id>.json
ingress:
- hostname: internal.company.com
service: http://localhost:8080
- service: http_status:404
# 作为 systemd 服务运行
cloudflared service install
6. 常见攻击 & 防御¶
SQL 注入¶
# 危险:直接拼接
query = f"SELECT * FROM users WHERE username='{username}'"
# 安全:参数化查询
cursor.execute("SELECT * FROM users WHERE username=%s", (username,))
撞库攻击防护¶
检测特征:
- 短时间大量登录失败
- 不同 IP 但相同 User-Agent
- 异常时间段(凌晨高峰)
防护措施:
1. Cloudflare Rate Limiting(登录接口 10次/分钟/IP)
2. CAPTCHA(reCAPTCHA v3 无感知打分)
3. 多因素认证(MFA)
4. 异常 IP 加入黑名单
5. 设备指纹识别
PCI DSS 核心要求(支付安全)¶
要求1:防火墙保护持卡人数据
要求2:不使用默认密码
要求3:保护存储的持卡人数据(加密/脱敏)
要求4:传输中加密(TLS 1.2+)
要求6:应用安全开发
要求8:身份识别与认证
要求10:跟踪监控所有网络访问
要求11:定期安全测试
博彩平台通常:
- 支付数据完全交给第三方支付(PayPal/Stripe/本地支付)
- 不存储 CVV,卡号存 Token
- 相关网段做 VPC 隔离
7. 安全日志 & 审计¶
CloudTrail(AWS 操作审计)¶
# 查找最近的高风险操作
aws cloudtrail lookup-events \
--lookup-attributes AttributeKey=EventName,AttributeValue=DeleteSecurityGroup \
--start-time 2025-01-01 \
--max-results 10
# 查找特定用户操作
aws cloudtrail lookup-events \
--lookup-attributes AttributeKey=Username,AttributeValue=suspicious-user
AWS GuardDuty(威胁检测)¶
# 开启 GuardDuty
aws guardduty create-detector --enable
# 查看发现的威胁
aws guardduty list-findings --detector-id <id>
aws guardduty get-findings --detector-id <id> --finding-ids <finding-id>
官方文档 & 学习资源¶
| 资源 | 链接 |
|---|---|
| OWASP Top 10 | https://owasp.org/www-project-top-ten/ |
| Cloudflare WAF 规则语法 | https://developers.cloudflare.com/ruleset-engine/rules-language/ |
| AWS Security Best Practices | https://docs.aws.amazon.com/wellarchitected/latest/security-pillar/ |
| AWS IAM Policy 文档 | https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html |
| HashiCorp Vault 文档 | https://developer.hashicorp.com/vault/docs |
| PCI DSS 快速参考 | https://www.pcisecuritystandards.org/document_library/ |
| TryHackMe(实战练习) | https://tryhackme.com/ |
常见问题 & 坑¶
Q: WAF 规则误伤正常用户怎么办?
A: 新规则先设置为 Log 模式观察3-7天,确认误报率可接受再切换到 Block
Q: 遭受 DDoS 时 Origin 已经宕机如何快速恢复? A: 1. 开启 Cloudflare Under Attack Mode;2. 检查是否有大量 origin IP 泄露(用 Cloudflare Tunnel 彻底隐藏);3. 临时启用 Always Online(Cloudflare 缓存版本)
Q: Secrets Manager 的密钥轮换? A: 开启自动轮换,设置 Lambda 函数在轮换时通知应用重新加载连接,不要硬编码密码
最后更新:2025-04