Skip to content

Instantly share code, notes, and snippets.

@chzyer
Created January 11, 2013 06:21
Show Gist options
  • Save chzyer/4508406 to your computer and use it in GitHub Desktop.
Save chzyer/4508406 to your computer and use it in GitHub Desktop.
关于js压缩

关于js压缩 今天无聊, 突然想起来搞一个下载漫画的脚本, 毒手又伸向了imanhua.com 我以前写过一个php版的, 其中他对js的加密对我影响不大, 我只要知道他包含漫画所有文件名list的那个文件名就行了 这次我用python版写, 总体的思路是,抓取到该集漫画的所有图片,然后下载下来, 对于其中js就需要我手动去解析, 然后就碰到了下面这样的js压缩代码

eval(function(p,a,c,k,e,d){e=function(c){return c};if(!''.replace(/^/,String)){while(c--)d[c]=k[c]||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;}('5 11=15;5 12=["/3/2/1/0/14.4","/3/2/1/0/13.4","/3/2/1/0/10.4","/3/2/1/0/7.4","/3/2/1/0/6.4","/3/2/1/0/9.4","/3/2/1/0/8.4","/3/2/1/0/22.4","/3/2/1/0/21.4","/3/2/1/0/24.4","/3/2/1/0/23.4","/3/2/1/0/20.4","/3/2/1/0/17.4","/3/2/1/0/16.4","/3/2/1/0/19.4"];5 18=0;',10,25,'72363|54|Images|Files|png|var|imanhua_005|imanhua_004|imanhua_007|imanhua_006|imanhua_003|len|pic|imanhua_002|imanhua_001||imanhua_014|imanhua_013|sid|imanhua_015|imanhua_012|imanhua_009|imanhua_008|imanhua_011|imanhua_010'.split('|'),0,{}))

人为的格式化的话, 大致结构是这样

eval(function(p,a,c,k,e,d){
	//...
}(arg1, arg2, arg3, arg4, arg5, arg6))

那先来看第一个参数arg1(我精简了list部分)

'5 11=15;5 12=["/3/2/1/0/14.4","/3/2/1/0/13.4"];5 18=0;'

这个实际上就是压缩后的js代码, 按分号分的话就是

5 11=15;
5 12=["/3/2/1/0/14.4","/3/2/1/0/13.4"]
5 18=0;

这个挺像密码的~ 解密需要钥匙, 钥匙是第四个参数

'72363|54|Images|Files|png|var|imanhua_005|imanhua_004|imanhua_007|imanhua_006|imanhua_003|len|pic|imanhua_002|imanhua_001||imanhua_014|imanhua_013|sid|imanhua_015|imanhua_012|imanhua_009|imanhua_008|imanhua_011|imanhua_010'.split('|')

一看就知道这是个大list, split将通过|分成一个list, 按照这里的位置给上面js代码填空。 5 是 var(从0开始),11 是 len, 15是空。 那第一行就是

var len =;

这句肯定是错的。在进行替换的时候没有想到冲突的问题, 如果js代码里面有个值是1, 那是不是也会被替换掉, 那怎么避免?就是例子中15的情况, 当为空是, 不替换, 所以第一行应该是

var len=15;

下面的应该就不难了, 还有一个地方会被忽略的就是不能使用单纯的replace去替换, 因为有可能替换进去的字符串又含有数字,但我们已经不想要再替换了, 所以我暂时的办法是通过字符便利来实现替换

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment