JavaScript过滤XSS

souziyuan 2023-7-21 108 7/21
全网资源)一网打尽)搜资源)www.souziyuan.topvar filterXSS=function(oriStr){
    if(!oriStr){
        return oriStr;
    }
    var charCodes=['3c','3e','27','22','28','29','60',{format:'script{}',chr:'3a'}];//要转义字符的16进制ASCII码[1<  2>  3'  4"  5(  6)  7`]
    var xssChars=[],filterChars=[],tmpFormat='{}',tmpChr;
    for(var i=0;i<charCodes.length;i++){
        if('string'==typeof charCodes[i]){
            tmpFormat='{}';
            tmpChr=charCodes[i];
        }else{
            tmpFormat=charCodes[i].format;
            tmpChr=charCodes[i].chr
        }
        xssChars.push(tmpFormat.replace('{}','\\u00'+tmpChr));
        xssChars.push(tmpFormat.replace('{}','%'+tmpChr));//1次encode
        xssChars.push(tmpFormat.replace('{}','%25'+tmpChr));//2次encode
        filterChars.push(tmpFormat.replace('{}','&#x'+tmpChr+';'));
        filterChars.push(tmpFormat.replace('{}','%26%23x'+tmpChr+'%3B'));//1次encode
        filterChars.push(tmpFormat.replace('{}','%2526%2523x' + tmpChr + '%253B'));//2次encode
    }
    for(var i=0;i<xssChars.length;i++){
        oriStr=oriStr.replace(new RegExp(xssChars[i],'gi'),filterChars[i]);
    }
    //预防script:
    oriStr=oriStr.replace(/script[\u000d\u000a\u0020]+\:/,'script:');
    return oriStr;
}
- THE END -
0

本站提供的资源,都来自网络,版权争议与本站无关,所有内容及软件的文章仅限用于学习和研究目的。不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负,我们不保证内容的长久可用性,通过使用本站内容随之而来的风险与本站无关,您必须在下载后的24个小时之内,从您的电脑/手机中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。侵删请致信邮箱souziyuan@outlook.com

共有 0 条评论

您必须 后可评论