| name | performing-security-headers-audit |
| description | 审计 HTTP 安全头,包括 CSP、HSTS、X-Frame-Options 和 Cookie 属性,以识别缺失或配置错误的浏览器级防护。 |
| domain | cybersecurity |
| subdomain | web-application-security |
| tags | ["penetration-testing","security-headers","csp","hsts","owasp","web-security","hardening"] |
| version | 1.0 |
| author | mahipal |
| license | Apache-2.0 |
执行安全头审计(Performing Security Headers Audit)
适用场景
- 在授权 Web 应用程序安全评估期间作为标准配置审查
- 评估针对 XSS、点击劫持和数据泄漏的浏览器级保护时
- 对要求实施安全头的合规评估(PCI DSS、SOC 2)
- 执行初始侦察以识别易于改进的安全问题时
- 在新部署的 CI/CD 管道安全门禁检查期间
前置条件
- 授权:针对目标应用程序的书面范围(头部审查风险较低)
- curl:从目标端点获取响应头
- SecurityHeaders.com:用于快速头部评估的在线扫描器
- Mozilla Observatory:Mozilla 的 Web 安全测试工具
- Burp Suite:用于跨多个页面的综合头部分析
- Browser DevTools:实时检查头部和 CSP 违规
工作流程
步骤 1:从目标收集安全头
检索并记录所有与安全相关的响应头。
curl -s -I "https://target.example.com/" | grep -iE \
"(strict-transport|content-security|x-frame|x-content-type|x-xss|referrer-policy|permissions-policy|feature-policy|x-permitted|cross-origin|set-cookie|server|x-powered-by|cache-control)"
PAGES=("/" "/login" "/api/health" "/admin" "/account/settings" "/static/app.js")
for page in "${PAGES[@]}"; do
echo "=== $page ==="
curl -s -I "https://target.example.com$page" 2>/dev/null | grep -iE \
"(strict-transport|content-security|x-frame|x-content-type|x-xss|referrer-policy|permissions-policy|set-cookie|server|x-powered)"
echo
done
echo "=== HTTP 响应 ==="
curl -s -I "http://target.example.com/" | head -20
echo "=== HTTPS 响应 ==="
curl -s -I "https://target.example.com/" | head -20
步骤 2:评估传输安全(HSTS)
评估 HTTP 严格传输安全(HSTS)配置。
curl -s -I "https://target.example.com/" | grep -i "strict-transport-security"
curl -s -I "http://target.example.com/" | head -5
curl -s -I "https://target.example.com/login" | grep -i "set-cookie"
curl -s "https://target.example.com/" | grep -oP "http://[^\"']+" | head -20
步骤 3:审计内容安全策略(CSP)
分析 CSP 头的有效性和潜在绕过方式。
CSP=$(curl -s -I "https://target.example.com/" | grep -i "content-security-policy" | cut -d: -f2-)
echo "$CSP"
echo "$CSP" | tr ';' '\n' | while read directive; do
echo " $directive"
if echo "$directive" | grep -q "unsafe-inline"; then
echo " 警告:unsafe-inline 允许内联脚本执行"
fi
if echo "$directive" | grep -q "unsafe-eval"; then
echo " 警告:unsafe-eval 允许 eval() 调用"
fi
if echo "$directive" | grep -q " \* "; then
echo " 警告:通配符允许从任何来源加载"
fi
curl -s -I | grep -i
步骤 4:检查框架保护和点击防御头
验证反点击劫持和 iframe 嵌入控制。
curl -s -I "https://target.example.com/" | grep -i "x-frame-options"
curl -s -I "https://target.example.com/" | grep -i "content-security-policy" | grep -o "frame-ancestors[^;]*"
curl -s -I "https://target.example.com/" | grep -i "x-content-type-options"
curl -s -I "https://target.example.com/" | grep -i "x-xss-protection"
curl -s -I "https://target.example.com/" | grep -i "referrer-policy"
步骤 5:审计 Cookie 安全属性
检查会话和认证 Cookie 的安全标志。
curl -s -I -L "https://target.example.com/login" | grep -i "set-cookie"
curl -s -I "https://target.example.com/login" | grep -i "set-cookie" | while read line; do
echo "Cookie: $(echo "$line" | grep -oP '[^:]+=[^;]+')"
missing=""
echo "$line" | grep -qi "secure" || missing="$missing Secure"
echo "$line" | grep -qi "httponly" || missing="$missing HttpOnly"
echo "$line" | grep -qi "samesite" || missing="$missing SameSite"
if [ -n "$missing" ]; then
echo " 缺少:$missing"
else
echo " 所有标志均存在"
fi
步骤 6:检查 Permissions-Policy 和信息泄露
审查浏览器功能控制和信息泄漏头。
curl -s -I "https://target.example.com/" | grep -i "permissions-policy"
curl -s -I "https://target.example.com/" | grep -iE "(cross-origin-embedder|cross-origin-opener|cross-origin-resource)"
curl -s -I "https://target.example.com/" | grep -iE "(server|x-powered-by|x-aspnet|x-generator)"
curl -s -I "https://target.example.com/account/settings" | grep -i "cache-control"
echo "使用 SecurityHeaders.com 扫描:https://securityheaders.com/?q=target.example.com"
echo "使用 Mozilla Observatory 扫描:https://observatory.mozilla.org/analyze/target.example.com"
核心概念
| 概念 | 定义 |
|---|
| HSTS | 强制浏览器对域名仅使用 HTTPS,防止协议降级攻击 |
| CSP | 限制页面上可以加载的资源(脚本、样式、图片) |
| X-Frame-Options | 控制页面是否可嵌入 iframe(点击劫持防御) |
| X-Content-Type-Options | 防止 MIME 类型嗅探;强制浏览器遵守声明的 Content-Type |
| Referrer-Policy | 控制跨域请求发送多少引用者信息 |
| Permissions-Policy | 限制页面可用的浏览器功能(摄像头、麦克风、地理位置) |
| SameSite Cookie | 控制 Cookie 在跨站上下文中的发送时机(Strict、Lax、None) |
| HSTS 预加载(HSTS Preloading) | 在浏览器源代码中硬编码 HSTS 策略以实现首次访问保护 |
工具与系统
| 工具 | 用途 |
|---|
| SecurityHeaders.com | 提供字母评级的安全头评估在线扫描器 |
| Mozilla Observatory | 带评分和建议的综合 Web 安全扫描器 |
| CSP Evaluator(Google) | 分析内容安全策略的弱点和绕过方式 |
| Burp Suite Professional | 检查所有应用程序页面的响应头 |
| securityheaders(CLI) | 命令行安全头扫描器 |
| Hardenize | TLS 和安全头监控服务 |
常见场景
场景 1:完全缺少头部
旧版应用程序完全不返回安全头。没有 HSTS、CSP、X-Frame-Options 或 Cookie 安全标志。每个页面都容易受到点击劫持,XSS 没有浏览器级缓解,Cookie 通过 HTTP 发送。
场景 2:带 unsafe-inline 的弱 CSP
CSP 头包含 script-src 'self' 'unsafe-inline'。虽然限制了外部脚本加载,但 unsafe-inline 指令允许任何内联脚本执行,使 CSP 对 XSS 失效。
场景 3:没有 Secure 标志的会话 Cookie
会话 Cookie 设置时没有 Secure 标志。在 HTTP/HTTPS 混合站点上,会话令牌可能被网络攻击者通过普通 HTTP 请求拦截。
场景 4:缺少 HSTS 导致 SSL 剥离
没有 HSTS 头。网络上的攻击者可以执行 SSL 剥离攻击,将受害者的 HTTPS 连接降级为 HTTP 并拦截所有流量。
输出格式
## 安全头审计报告
**目标**:target.example.com
**评级**:D(SecurityHeaders.com)
**评估日期**:2024-01-15
### 头部评估
| 头部 | 状态 | 当前值 | 推荐值 |
|--------|--------|---------------|-------------|
| Strict-Transport-Security | 缺失 | - | max-age=31536000; includeSubDomains; preload |
| Content-Security-Policy | 弱 | script-src 'self' 'unsafe-inline' | script-src 'self' 'nonce-{random}' |
| X-Frame-Options | 缺失 | - | DENY |
| X-Content-Type-Options | 存在 | nosniff | nosniff(正确) |
| Referrer-Policy | 缺失 | - | strict-origin-when-cross-origin |
| Permissions-Policy | 缺失 | - | camera=(), microphone=(), geolocation=() |
| X-XSS-Protection | 缺失 | - | 0(配合强 CSP) |
### Cookie 安全
| Cookie | Secure | HttpOnly | SameSite | Path |
|--------|--------|----------|----------|------|
| session | 否 | 是 | 未设置 | / |
| user_pref | 否 | 否 | 未设置 | / |
| csrf_token | 是 | 否 | Strict | / |
### 信息泄露
| 头部 | 值 | 风险 |
|--------|-------|------|
| Server | Apache/2.4.52 | 技术指纹识别 |
| X-Powered-By | PHP/8.1.2 | 针对特定版本的漏洞利用 |
### 修复建议优先级
1. **严重**:向会话 Cookie 添加 Secure 和 SameSite 标志
2. **高**:实施最短 1 年 max-age 的 HSTS
3. **高**:将 CSP 中的 'unsafe-inline' 替换为基于 nonce 的策略
4. **中**:添加 X-Frame-Options: DENY
5. **中**:添加 Referrer-Policy: strict-origin-when-cross-origin
6. **低**:删除 Server 和 X-Powered-By 的版本信息
7. **低**:添加 Permissions-Policy 以限制未使用的浏览器功能