解码类似 eval(function(p,a,c,k,e,d){e=function(c) 的JavaScript代码

朋友有段javascript代码是加密的,拿来看一下,是以 eval(function(p,a,c,k,e,d){e=function(c) 开头的,代码如下:

eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)d[e(c)]=k[c]||e(c);k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1;};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p;}('f b$=["1N","2j","2i","",\'\\o\\y\\A\\1g\\1b\\s\',\'\\1g\\1b\\P\\N\\O\\H\\S\\J\\o\\T\\U\\y\\A\\s\',"",\'\\o\\y\\A\\1a\\1d\\1c\\R\\s\',\'\\1a\\1d\\1c\\R\\P\\N\\O\\H\\S\\J\\o\\T\\U\\y\\A\\s\',"",\'\\o\\2c\\2b\\2g\\1m\\1p\\2f\\s\',"",\'\\o\\y\\A\\2e\\2v\\1p\\2u\\s\',"",\'\\o\\y\\2t-1q\\s\',\'E-1q\\P\\N\\O\\H\\S\\J\\o\\T\\U\\y\\A\\s\',"",\'\\o\\y\\A\\1r\\1k\\R\\s\',\'\\1r\\1k\\R\\P\\N\\O\\H\\S\\J\\o\\T\\U\\y\\A\\s\',"\\H\\1m\\2a\\1K\\J\\o\\1M\\1I >>","Y","1E","1H","D","D","?1W=1X&1Q=1P&1O=29&1T=","","1s","p","p","","1s","1j","1S","1j","1R","c","1Y","c","1Z",\'\',"1U=",";1V=/","1G"];W.1F=q(){l M};q $(1l){l a.m(1l)};q 1n(){$( b$[0]).V.2z=$( b$[1]).2R-2S+ b$[2]};W.2P=q(){1n()};q 2N(){v{h(a.c.F.g== b$[3]){t( b$[4]);a.c.F.u();l r};f F=/^[\\2O-\\2T]{2,6}$/;h(!F.L(a.c.F.g)){t( b$[5]);a.c.F.u();l r}}x(z){};v{h(a.c.K.g== b$[6]){t( b$[7]);a.c.K.u();l r};f c=/^1[3,4,5,7,8]\\d{9}$/;h(!c.L(a.c.K.g)){t( b$[8]);a.c.K.u();l r}}x(z){};v{h(a.c.Y.g== b$[9]){t( b$[10]);a.c.Y.u();l r}}x(z){};v{h(a.c.1h.g== b$[11]){t( b$[12]);a.c.1h.u();l r}}x(z){};v{h(a.c.G.g== b$[13]){t( b$[14]);a.c.G.u();l r};f G=/^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$/;h(!G.L(a.c.G.g)){t( b$[15]);a.c.G.u();l r}}x(z){};v{h(a.c.D.g== b$[16]){t( b$[17]);a.c.D.u();l r};f 1o=/^\\d{1,2}$/;h(!1o.L(a.c.D.g)){t( b$[18]);a.c.D.u();l r}}x(z){};a.c.1e.2I=M;a.c.1e.g= b$[19];l M};v{2F 2B( b$[20], b$[21], b$[22])}x(z){};v{f 1f=a.m( b$[23]).1i;q 2C(){a.m( b$[24]).1i=1f+ b$[25]+2D.2L()}}x(z){};q 2V(){f p=a.c.p.1C;1D(f i=C;i<a.c.p.Q;i++){h(a.c.p[i].2Q==M){f p=a.c.p[i].1C;2E}};h(a.c.n.g== b$[26]||a.c.n.g==C){f n=1y}Z{f n=a.c.n.g};f B=p*n;a.c.B.g=B;a.m( b$[27]).1v=B};q 2K(){f 1x=a.m( b$[28]);f p=1x.1B[a.m( b$[29]).1B.2H].2G;h(a.c.n.g== b$[2J]||a.c.n.g==C){f n=1y}Z{f n=a.c.n.g};f B=p*n;a.c.B.g=B;a.m( b$[2A]).1v=B};q 2U(i){f k=2M;1D(f j=C;j<k;j++){h(j==i){a.m( b$[1L]+j).V.1z= b$[1J]}Z{a.m( b$[2q]+j).V.1z= b$[2r]}}};q 2s(){a.m( b$[2n]).1A= b$[2o]};q 2p(){a.m( b$[2w]).1A= b$[2x]};f I= b$[2y];h(a.X.Q>C){I=a.X};v{h(I.Q==C&&1u.1t.1w.Q>C){I=1u.1t.1w}}x(e){} a.2d= b$[2k]+I+ b$[2l];a.m( b$[2m]).g=W.2h.a.X;',62,182,'||||||||||document|_|wfform|||var|value|if||||return|getElementById|wfmun|u8bf7|wfproduct|function|false|uff01|alert|focus|try||catch|u586b|ex|u5199|wfprice|0x0|wfcode||wfname|wfemail|u6b63|wfllref|uff0c|wfmob|test|true|u5f0f|u4e0d|u683c|length|u7801|u786e|u91cd|u65b0|style|window|referrer|wfprovince|else|||||||||||u624b|u540d|u53f7|u673a|wfsubmit|thissrc|u59d3|wfaddress|src|paydiv|u8bc1|id|u5728|getHeight|regwfcode|u5730|MAIL|u9a8c|showprice|location|opener|innerHTML|href|wfcpxljg|0x1|display|target|options|alt|for|wfcity|onerror|wfddll|wfarea|u7b49|33|u4ea4|32|u7a0d|wffahuo|imgh|70|imgw|none|block|temp|WFLLURL|path|wfaction|codeimg|_parent|_blank|||||||||||u63d0|u62e9|u9009|cookie|u8be6|u533a|u6240|top|px|wfforml|41|42|43|36|37|opay2|34|35|opay|u5199E|u5740|u7ec6|38|39|40|height|31|PCAS|refreshCode|Math|break|new|title|selectedIndex|disabled|30|priceb|random|0x3|postcheck|u4e00|onload|checked|offsetHeight|0x55|u9fa5|changeItem|pricea'.split('|'),0,{}))

方法一:最简单的解密方法是将 eval() 直接替换成 document.write(),然后将代码输出到页面即可。

在输出前,我们先输出一个<xmp>标记,具体代码构造如下:

<script type="text/javascript">
 document.write("<xmp>");
 /*eval*/document.write(" 这里是除 eval() 之外的代码部分 ");
 document.write("</xmp>");
</script>

将上述代码保存为 .html 文件,打开即可得到你要的代码,顺便对代码格式化一下更方便阅读。

方法二:也不复杂,通过这个方法可以了解一下更多的信息。

大家看原代码中的蓝色部分: while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p;

其实这个 eval(function(p,a,c,k,e,d){})) 中自带解码函数e(),while循环产生的每个 p 就是解码后的函数代码,我们将蓝色代码中的 return p 替换为:

document.getElementById("textarea").innerText=p

这样就能将解码后的代码直接输出在一个文本区域内,方便复制出来,构造如下:

<textarea id="textarea" rows="50" cols="100"></textarea>
<script type="text/javascript">
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)d[e(c)]=k[c]||e(c);k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1;};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);document.getElementById('textarea').innerText=p;}('f b$=["1N","2j","2i","",\'\\o\\y\\A\\1g\\1b\\s\',\'\\1g\\1b\\P\\N\\O\\H\\S\\J\\o\\T\\U\\y\\A\\s\',"",\'\\o\\y\\A\\1a\\1d\\1c\\R\\s\',\'\\1a\\1d\\1c\\R\\P\\N\\O\\H\\S\\J\\o\\T\\U\\y\\A\\s\',"",\'\\o\\2c\\2b\\2g\\1m\\1p\\2f\\s\',"",\'\\o\\y\\A\\2e\\2v\\1p\\2u\\s\',"",\'\\o\\y\\2t-1q\\s\',\'E-1q\\P\\N\\O\\H\\S\\J\\o\\T\\U\\y\\A\\s\',"",\'\\o\\y\\A\\1r\\1k\\R\\s\',\'\\1r\\1k\\R\\P\\N\\O\\H\\S\\J\\o\\T\\U\\y\\A\\s\',"\\H\\1m\\2a\\1K\\J\\o\\1M\\1I >>","Y","1E","1H","D","D","?1W=1X&1Q=1P&1O=29&1T=","","1s","p","p","","1s","1j","1S","1j","1R","c","1Y","c","1Z",\'\',"1U=",";1V=/","1G"];W.1F=q(){l M};q $(1l){l a.m(1l)};q 1n(){$( b$[0]).V.2z=$( b$[1]).2R-2S+ b$[2]};W.2P=q(){1n()};q 2N(){v{h(a.c.F.g== b$[3]){t( b$[4]);a.c.F.u();l r};f F=/^[\\2O-\\2T]{2,6}$/;h(!F.L(a.c.F.g)){t( b$[5]);a.c.F.u();l r}}x(z){};v{h(a.c.K.g== b$[6]){t( b$[7]);a.c.K.u();l r};f c=/^1[3,4,5,7,8]\\d{9}$/;h(!c.L(a.c.K.g)){t( b$[8]);a.c.K.u();l r}}x(z){};v{h(a.c.Y.g== b$[9]){t( b$[10]);a.c.Y.u();l r}}x(z){};v{h(a.c.1h.g== b$[11]){t( b$[12]);a.c.1h.u();l r}}x(z){};v{h(a.c.G.g== b$[13]){t( b$[14]);a.c.G.u();l r};f G=/^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$/;h(!G.L(a.c.G.g)){t( b$[15]);a.c.G.u();l r}}x(z){};v{h(a.c.D.g== b$[16]){t( b$[17]);a.c.D.u();l r};f 1o=/^\\d{1,2}$/;h(!1o.L(a.c.D.g)){t( b$[18]);a.c.D.u();l r}}x(z){};a.c.1e.2I=M;a.c.1e.g= b$[19];l M};v{2F 2B( b$[20], b$[21], b$[22])}x(z){};v{f 1f=a.m( b$[23]).1i;q 2C(){a.m( b$[24]).1i=1f+ b$[25]+2D.2L()}}x(z){};q 2V(){f p=a.c.p.1C;1D(f i=C;i<a.c.p.Q;i++){h(a.c.p[i].2Q==M){f p=a.c.p[i].1C;2E}};h(a.c.n.g== b$[26]||a.c.n.g==C){f n=1y}Z{f n=a.c.n.g};f B=p*n;a.c.B.g=B;a.m( b$[27]).1v=B};q 2K(){f 1x=a.m( b$[28]);f p=1x.1B[a.m( b$[29]).1B.2H].2G;h(a.c.n.g== b$[2J]||a.c.n.g==C){f n=1y}Z{f n=a.c.n.g};f B=p*n;a.c.B.g=B;a.m( b$[2A]).1v=B};q 2U(i){f k=2M;1D(f j=C;j<k;j++){h(j==i){a.m( b$[1L]+j).V.1z= b$[1J]}Z{a.m( b$[2q]+j).V.1z= b$[2r]}}};q 2s(){a.m( b$[2n]).1A= b$[2o]};q 2p(){a.m( b$[2w]).1A= b$[2x]};f I= b$[2y];h(a.X.Q>C){I=a.X};v{h(I.Q==C&&1u.1t.1w.Q>C){I=1u.1t.1w}}x(e){} a.2d= b$[2k]+I+ b$[2l];a.m( b$[2m]).g=W.2h.a.X;',62,182,'||||||||||document|_|wfform|||var|value|if||||return|getElementById|wfmun|u8bf7|wfproduct|function|false|uff01|alert|focus|try||catch|u586b|ex|u5199|wfprice|0x0|wfcode||wfname|wfemail|u6b63|wfllref|uff0c|wfmob|test|true|u5f0f|u4e0d|u683c|length|u7801|u786e|u91cd|u65b0|style|window|referrer|wfprovince|else|||||||||||u624b|u540d|u53f7|u673a|wfsubmit|thissrc|u59d3|wfaddress|src|paydiv|u8bc1|id|u5728|getHeight|regwfcode|u5730|MAIL|u9a8c|showprice|location|opener|innerHTML|href|wfcpxljg|0x1|display|target|options|alt|for|wfcity|onerror|wfddll|wfarea|u7b49|33|u4ea4|32|u7a0d|wffahuo|imgh|70|imgw|none|block|temp|WFLLURL|path|wfaction|codeimg|_parent|_blank|||||||||||u63d0|u62e9|u9009|cookie|u8be6|u533a|u6240|top|px|wfforml|41|42|43|36|37|opay2|34|35|opay|u5199E|u5740|u7ec6|38|39|40|height|31|PCAS|refreshCode|Math|break|new|title|selectedIndex|disabled|30|priceb|random|0x3|postcheck|u4e00|onload|checked|offsetHeight|0x55|u9fa5|changeItem|pricea'.split('|'),0,{}));
</script>

同样将以上代码保存为 .html 文件并打开运行,即可得到解码后的代码了。

PS. 这里就不贴出解码后的代码了,想知道结果是什么,自己试试呗 -_-!!!

另:解码后的代码里如出现类似 \u8bf7\u586b\u5199\u59d3\u540d\uff01 的代码,这是中文的Unicode编码形式,使用 Unicode编码转换工具即可,例如

http://tool.chinaz.com/Tools/Unicode.aspx