同源策略 & 内容安全策略

为了更好的理解掌握 同源策略(Same origin policy)、内容安全策略(CSP,ContentSecurityPolicy)、跨站脚本攻击(Cross Site Scripting)、跨站请求伪造(Cross-site request forgery)、服务器端请求伪造(Server-Side Request Forgery)做了以下实验

环境配置

  1. 在80端口开放Apache服务器
  2. 在8080端口开放Apache服务器

实验一 CSP验证 & 绕过CSP

首先我们来验证CSP的作用,及内容
首先客户访问localhost:8080/test.php 在header中限制了请求资源

./www2/test.php
<?php
header("Content-Security-Policy: style-src 'self' 'unsafe-inline';");
?>
<link rel="stylesheet" type="text/css" href="http://127.0.0.1/test.php?a=jinlongyu">

接收端代码

www/test.php
<?php
$file = fopen("1.txt","w");
fwrite($file, $_GET['a']);
?>

这里写图片描述
说明资源不能被请求,受到了CSP的限制,他只能请求来自本服务器的资源,而一般有XSS防护的网站基本上都会这么做。如果将限制删除那么就不会出现此情况
如下图所示
这里写图片描述
资源请求成功。
上面主要是内容安全策略的问题,以及我的理解如有不对请指正。

实验二

同源策略问题一直以来困扰着我。直到做这个实验的时候才稍微理解了一点,下面我和大家分享一下。
在HTML资源请求时,只能获取目标服务器资源但不能直接修改该资源。
下面实验在8080:/1.php嵌入80:/1.php并且试图修改里面的html节点2
下面来看一下实验代码及实验效果。

www2/1.php
<meta charset="utf-8">
<iframe src="http://localhost/1.php" id="myframe"></iframe>
</script>
<a href="#" onclick="replaceContent()">点击替换内容</a>
<script type="text/javascript">
content = '我把你的这部分理解成读取一段内容';
            function replaceContent(){
                document.getElementById('myframe').contentWindow.document.body.innerText = content;
            }
document.domain = "localhost";
</script>
www/1.php
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head>
    <title>测试平台1</title>
</head>
<body>
<p>hahaha</p>
<p>fuck me</p>
<p>反弹至服务器</p>
<p></p>
<script type="text/javascript" >

</script>
</body>
</html>

小实验1

我们首先利用上述代码执行,发现被浏览器的同源策略限制了
这里写图片描述

小实验2

修改上述代码 将www/1.php加上document.domain = “localhost”;
那么实验效果如下
这里写图片描述

实验三

<meta charset="utf-8">
<script type="text/javascript" src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>
<iframe src="http://localhost/2.php" id="myframe"></iframe>
</script>
<a href="#" onclick="replaceContent()">点击替换内容</a>
<script type="text/javascript">
content = '我把你的这部分理解成读取一段内容';
            function replaceContent(){
                document.getElementById('myframe').contentWindow.document.body.innerText = content;
            }
document.domain = "localhost";
</script>
<?php 
header('Access-Control-Allow-Origin : http://127.0.0.1:8080/');
echo 222;
?>
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head>
    <title>测试平台1</title>
</head>
<body>
<p>hahaha</p>
<p>fuck me</p>
<p>反弹至服务器</p>
<p></p>
<script type="text/javascript" >
document.domain = "localhost";
</script>
</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值