javascript过滤危险脚本方法
下面是他们的字符串规则:
1、<(script|link|style|iframe)(.|\n)*<\/\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)*\);?
了解他们的规则后,抓虫行动就水到渠成。
抓虫1
function kickBug(str) {
return str.replace(/\s*/ig,"");
}
a {
height:expression(alert('hei'));
}
抓虫1
function kickBug(str) {
return str.replace(/\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);
}
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
抓虫2
function kickBug(str) {
return str.replace(/]*\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);
}
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
test
抓虫3
function kickBug(str) {
return str.replace(/]*\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);
}
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
expression()
test
抓虫4
function kickBug(str) {
return str.replace(/]*\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);
}
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
这样调用就可以
k1(k2(k3(k4(str))))
这样就是单纯地过滤脚本而已,所谓过滤“危险脚本”应该是能够判断哪些属于“危险"脚本,不危险的就不过滤才对……那可就难办了,相当于防火墙了。