问题:

写app的时候,需要抓取一个页面的内容,但是这个页面的内容是通过js加载的,这段js是打乱的,直接分析有一定的难度

实现方式:通过eval直接执行拿到运行结果

这段js是这样的

1
eval(function(p,a,c,k,e,d){e=function(c){return c.toString(36)};if(!''.replace(/^/,String)){while(c--){d[c.toString(a)]=k[c]||c.toString(a)}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}('d b=["3://1.0.2/6/5-4/e","3://1.0.2/6/5-4/9","3://1.0.2/6/5-4/7","3://1.0.2/6/5-4/a","3://1.0.2/6/5-4/8","3://1.0.2/6/5-4/c","3://1.0.2/6/5-4/m","3://1.0.2/6/5-4/k","3://1.0.2/6/5-4/l","3://1.0.2/6/5-4/f","3://1.0.2/6/5-4/i","3://1.0.2/6/5-4/g","3://1.0.2/6/5-4/h","3://1.0.2/6/5-4/j"]',23,23,'pstatp|p|com|https|image|pgc|origin|d5891339b36247969d42a7fd7a836f9f|f6c972d4636c491c81b8958fe911288e|556735adf1a24f81841c28c91bfc5cae|df9dac499fee4563a0aefb03bac3fa4c|newImgs|445c5485c19148728b9c04c31096cc04|var|789b3e348ea047ad87c53a6c8c41fa33|4b850a13dc4142199ba9f1d0c8f8b051|634908f1de0d44848be4b1c26c3897d8|25d2e2148e204590a32b5527d5a257b8|5fb7dd3e10ac418097245f80cc5ab349|5f812b16da9946e0b5446fbb00bd125f|ebe5f1137cea4f478160976df7fcf85c|c62a58b50a5740a1ab33b1e1fb9e8771|4cc33ea7b88945d6aaaac004b887750d'.split('|'),0,{}))

方法

抓取到的内容是一段字符串,需要截取获得 function, 然后通过 eval 执行, 直接拿到 js 的运行结果

1
2
3
4
5
6
const str = 'eval(function(p,a,c,k,e,d){e=function(c){return c.toString(36)};if(!''.replace(/^/,String)){while(c--){d[c.toString(a)]=k[c]||c.toString(a)}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}('d b=["3://1.0.2/6/5-4/e","3://1.0.2/6/5-4/9","3://1.0.2/6/5-4/7","3://1.0.2/6/5-4/a","3://1.0.2/6/5-4/8","3://1.0.2/6/5-4/c","3://1.0.2/6/5-4/m","3://1.0.2/6/5-4/k","3://1.0.2/6/5-4/l","3://1.0.2/6/5-4/f","3://1.0.2/6/5-4/i","3://1.0.2/6/5-4/g","3://1.0.2/6/5-4/h","3://1.0.2/6/5-4/j"]',23,23,'pstatp|p|com|https|image|pgc|origin|d5891339b36247969d42a7fd7a836f9f|f6c972d4636c491c81b8958fe911288e|556735adf1a24f81841c28c91bfc5cae|df9dac499fee4563a0aefb03bac3fa4c|newImgs|445c5485c19148728b9c04c31096cc04|var|789b3e348ea047ad87c53a6c8c41fa33|4b850a13dc4142199ba9f1d0c8f8b051|634908f1de0d44848be4b1c26c3897d8|25d2e2148e204590a32b5527d5a257b8|5fb7dd3e10ac418097245f80cc5ab349|5f812b16da9946e0b5446fbb00bd125f|ebe5f1137cea4f478160976df7fcf85c|c62a58b50a5740a1ab33b1e1fb9e8771|4cc33ea7b88945d6aaaac004b887750d'.split('|'),0,{}))'

const sc = str.substring(5, str.length - 1) // 拿到function
const newImgs = eval('(' + sc + ')') // 通过eval 执行 function
const imgs = newImgs.replace('var newImgs=', '') // 拿到结果,处理一下 得到 值
imgList = JSON.parse(imgs) // 将字符串 转为 js 对象