首页 > web前端 > js教程 > 正文

javascript过滤危险脚本方法_javascript技巧

PHP中文网
发布: 2016-05-16 19:01:52
原创
1631人浏览过

脚本藏身之处不过有四: 1、<script>标签、<link>标签、<style>标签、iframe标签 2、on开头的标签属性 3、javascript(vbscript)伪协议 4、css的epression</script>

下面是他们的字符串规则: 
1、\s* 
2、\s*on[a-z]+\s*=\s*("[^"]+"|'[^']+'|[^\s]+)\s*(?=>) 
3、\s*(href|src)\s*=\s*("\s*(javascript|vbscript):[^"]+"|'\s*(javascript|vbscript):[^']+'|(javascript|vbscript):[^\s]+)\s*(?=>) 
4、epression\((.|\n)*\);? 
了解他们的规则后,抓虫行动就水到渠成。 

<textarea id="bug" cols="80" rows="16"> 
<button id="kick">抓虫1</button> 
<script> 
function kickBug(str) { 
return str.replace(/<(script|link|style|iframe)(.|\n)*\/\1>\s*/ig,""); 
} 
</script> 
<iframe></iframe> 
<link href='test.css'></link> 
<style> 
a { 
height:expression(alert('hei')); 
} 
</style> 
</textarea> 
<button id="kick">抓虫1</button> 
<script> 
function kickBug(str) { 
return str.replace(/<(script|link|style|iframe)(.|\n)*\/\1>\s*/ig,""); 
} 
if(!/msie/i.test(navigator.userAgent)){ 
HTMLElement.prototype.__defineGetter__("innerText",function(){ 
return this.textContent; 
}); 
HTMLElement.prototype.__defineSetter__("innerText",function(text){ 
this.textContent = text; 
}); 
} 
document.getElementById("kick").onclick = function() { 
var bug = document.getElementById("bug"); 
bug.innerText = kickBug(bug.innerText); 
} 
</script>
登录后复制
<textarea id="bug" cols="80" rows="5"> 
<a onclick="test(); 
test1()" onblur= 
"test3()">test</a> 
</textarea> 
<button id="kick">抓虫2</button> 
<script> 
function kickBug(str) { 
return str.replace(/<[a-z][^>]*\s*on[a-z]+\s*=[^>]+/ig,function($0,$1){ 
return $0.replace(/\s*on[a-z]+\s*=\s*("[^"]+"|'[^']+'|[^\s]+)\s*/ig,""); 
}); 
} 
if(!/msie/i.test(navigator.userAgent)){ 
HTMLElement.prototype.__defineGetter__("innerText",function(){ 
return this.textContent; 
}); 
HTMLElement.prototype.__defineSetter__("innerText",function(text){ 
this.textContent = text; 
}); 
} 
document.getElementById("kick").onclick = function() { 
var bug = document.getElementById("bug"); 
bug.innerText = kickBug(bug.innerText); 
} 
</script>
登录后复制
<textarea id="bug" cols="80" rows="5"> 
<a onclick="test();" href=" 
jAvascript:alert('a')" href="jAvascript:" 
href="vbscript:alert()" 
>test</a> 
</textarea> 
<button id="kick">抓虫3</button> 
<script> 
function kickBug(str) { 
return str.replace(/<[a-z][^>]*\s*(href|src)\s*=[^>]+/ig,function($0,$1){ 
$0 = $0.replace(/&#(6[5-9]|[78][0-9]|9[0789]|1[01][0-9]|12[012]);?/g,function($0,$1){return String.fromCharCode($1);}); 
return $0.replace(/\s*(href|src)\s*=\s*("\s*(javascript|vbscript):[^"]+"|'\s*(javascript|vbscript):[^']+'|(javascript|vbscript):[^\s]+)/ig,""); 
}); 
} 
if(!/msie/i.test(navigator.userAgent)){ 
HTMLElement.prototype.__defineGetter__("innerText",function(){ 
return this.textContent; 
}); 
HTMLElement.prototype.__defineSetter__("innerText",function(text){ 
this.textContent = text; 
}); 
} 
document.getElementById("kick").onclick = function() { 
var bug = document.getElementById("bug"); 
bug.innerText = kickBug(bug.innerText); 
} 
</script>
登录后复制
<textarea id="bug" cols="80" rows="5"> 
expression() 
<a style="color:expression( 
'red' 
)">test</a> 
</textarea> 
<button id="kick">抓虫4</button> 
<script> 
function kickBug(str) { 
return str.replace(/<[a-z][^>]*\s*style\s*=[^>]+/ig,function($0,$1){ 
$0 = $0.replace(/&#(6[5-9]|[78][0-9]|9[0789]|1[01][0-9]|12[012]);?/g,function($0,$1){return String.fromCharCode($1);}); 
return $0.replace(/\s*style\s*=\s*("[^"]+(expression)[^"]+"|'[^']+\2[^']+'|[^\s]+\2[^\s]+)\s*/ig,""); 
}); 
} 
if(!/msie/i.test(navigator.userAgent)){ 
HTMLElement.prototype.__defineGetter__("innerText",function(){ 
return this.textContent; 
}); 
HTMLElement.prototype.__defineSetter__("innerText",function(text){ 
this.textContent = text; 
}); 
} 
document.getElementById("kick").onclick = function() { 
var bug = document.getElementById("bug"); 
bug.innerText = kickBug(bug.innerText); 
} 
</script>
登录后复制

这样调用就可以 
k1(k2(k3(k4(str)))) 

这样就是单纯地过滤脚本而已,所谓过滤“危险脚本”应该是能够判断哪些属于“危险"脚本,不危险的就不过滤才对……那可就难办了,相当于防火墙了。

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号