python运行execjs解密js
[转]http://www.knowsky.com/1041161.html
python 记一次计算qzonetoken经历
之前用python写了个发表说说的爬虫,但最近发现在post数据时返回不对,不能用了,抓包后发现需要添加一个qzonetoken的字符串,这个token并不在cookie里。联想到以前爬贴吧时也有个类似的token,贴吧的token是放在html里的,并且是明文。查看html,果然有个window.g_qzonetoken的值,同样是明文,于是愉快的提取出来,然后post,成功。 过几天后,代码又不能用了,还是qzonetoken不对,抓包后发现这个token不再是明文了,变成了这个样子
<script type="text/javascript"> window.g_qzonetoken = (function(){ try{return (+[]+[])+(!+[]+!![]+!![]+!![]+!![]+[])+(!+[]+!![]+!![]+!![]+!![]+!![]+[])+([]+{})[!+[]+!![]]+(!+[]+!![]+!![]+!![]+!![]+[])+([]+{})[!+[]+!![]]+(![]+[])[+[]]+([][[]]+[])[!+[]+!![]]+([][[]]+[])[!+[]+!![]]+(+!![]+[])+(+!![]+[])+(+!![]+[])+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(!+[]+!![]+[])+(![]+[])[+[]]+(!+[]+!![]+!![]+!![]+!![]+!![]+!![]+!![]+[])+(!+[]+!![]+!![]+!![]+!![]+[])+(!+[]+!![]+!![]+!![]+!![]+!![]+[])+(!+[]+!![]+!![]+!![]+!![]+[])+(!+[]+!![]+!![]+!![]+!![]+!![]+!![]+!![]+!![]+[])+([]+{})[!+[]+!![]]+([][[]]+[])[!+[]+!![]]+(![]+[])[+[]]+(![]+[])[+[]]+(+{}+[])[+!![]]+(![]+[])[+[]]+(![]+[])[+[]]+(!+[]+!![]+!![]+!![]+[])+(+!![]+[])+(!+[]+!![]+!![]+!![]+!![]+!![]+!![]+!![]+[])+(!+[]+!![]+!![]+!![]+!![]+!![]+!![]+[])+(!+[]+!![]+!![]+!![]+[])+(+!![]+[])+(!+[]+!![]+[])+(!+[]+!![]+!![]+!![]+!![]+!![]+!![]+!![]+[])+(!+[]+!![]+!![]+!![]+[])+(!+[]+!![]+!![]+!![]+[])+(!+[]+!![]+!![]+!![]+!![]+!![]+!![]+[])+(![]+[])[+[]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(![]+[])[+[]]+(!+[]+!![]+!![]+!![]+[])+([]+[][(![]+[])[!+[]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][[]]+[])[+!![]]+(![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+[]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+([][[]]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+(![]+[])[!+[]+!![]]+([]+{})[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(+{}+[])[+!![]]+(!![]+[])[+[]]+([][[]]+[])[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][[]]+[])[+!![]])())[+[]]+([][[]]+[])[!+[]+!![]]+(+!![]+[])+(!+[]+!![]+!![]+!![]+!![]+[])+(!+[]+!![]+[])+(!+[]+!![]+!![]+!![]+!![]+!![]+!![]+[])+(!+[]+!![]+!![]+[])+(!+[]+!![]+[])+(+!![]+[])+([][[]]+[])[!+[]+!![]+!![]]+([][[]]+[])[!+[]+!![]+!![]]+([]+{})[!+[]+!![]]+([]+{})[!+[]+!![]]+(!+[]+!![]+!![]+!![]+!![]+!![]+!![]+!![]+[])+(+!![]+[])+(+{}+[])[+!![]]+(!+[]+!![]+!![]+!![]+!![]+!![]+!![]+!![]+!![]+[])+(!+[]+!![]+!![]+!![]+[])+(!+[]+!![]+!![]+!![]+!![]+!![]+!![]+!![]+[])+(+[]+[])+(!+[]+!![]+!![]+[])+(!+[]+!![]+!![]+!![]+[])+(!+[]+!![]+!![]+!![]+!![]+!![]+!![]+[])+([][[]]+[])[!+[]+!![]+!![]]+(!+[]+!![]+!![]+!![]+!![]+!![]+!![]+[])+(+[]+[])+(!+[]+!![]+!![]+!![]+!![]+[])+(!+[]+!![]+!![]+!![]+!![]+!![]+[])+(+{}+[])[+!![]]+(!+[]+!![]+!![]+!![]+!![]+[])+([][[]]+[])[!+[]+!![]+!![]]+(![]+[])[+[]]+(+{}+[])[+!![]]+([][[]]+[])[!+[]+!![]]+(!+[]+!![]+!![]+!![]+!![]+!![]+!![]+!![]+[])+(+!![]+[])+(!+[]+!![]+[])+(!+[]+!![]+!![]+!![]+!![]+!![]+!![]+!![]+[])+(+[]+[])+(+{}+[])[+!![]]+(!+[]+!![]+[])+(!+[]+!![]+!![]+[])+(+!![]+[])+(+!![]+[])+([]+{})[!+[]+!![]+!![]+!![]+!![]]+([][[]]+[])[!+[]+!![]+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(+{}+[])[+!![]]+(![]+[])[+[]]+(!+[]+!![]+!![]+[])+(!+[]+!![]+!![]+!![]+!![]+!![]+!![]+!![]+!![]+[])+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(!+[]+!![]+!![]+!![]+!![]+!![]+!![]+[])+(+{}+[])[+!![]]+(!+[]+!![]+!![]+!![]+!![]+!![]+!![]+!![]+[])+(!+[]+!![]+!![]+!![]+!![]+!![]+[])+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(![]+[])[+[]]+([][[]]+[])[!+[]+!![]]+(!+[]+!![]+!![]+!![]+!![]+!![]+!![]+!![]+!![]+[])+([]+{})[!+[]+!![]]+(!+[]+!![]+!![]+!![]+!![]+[])+(!+[]+!![]+!![]+!![]+!![]+!![]+!![]+!![]+!![]+[]);} catch(e) {var xhr = new xmlHttPRequest();xhr.withCredentials = true;xhr.open('post', '//h5.qzone.QQ.com/log/post/error/qzonetoken', true);xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');xhr.send(e);}})(); </script>
`“这什么鬼!” 我第一反应。立马联想到以前看过的一道js题,就是这个样子的,但是不知道这叫什么!还好百度是强大的,了解到这叫jother编码,原来还能这么玩! 然后搜索如何在python中执行js,安装execjs模块,代码是这样的
ctx = execjs.compile('''function qzonetoken(){return %s}'''% jother) print ctx.call("qzonetoken")
将js脚本中的那一串jother编码带入,高兴得跑起来!然后报错了!
raise exceptions.ProgramError(e) execjs._exceptions.ProgramError: ReferenceError: location is not defined ( @ 2 : 7 ) -> return location
提示说location没有定义,我觉得奇怪,我将这段js放到浏览器consle中都能正常运行,为什么python中就不行呢!然后怀疑这个execjs模块有问题,还费劲地安装了pyv8,还是得到同样结果,报错:location无定义。一番搜索后,解释是:execjs只是纯粹的js运行环境,没有dom结构,而这个location就是dom中的一个对象。又碰巧在jother编码工具 这个网站中发现了这样一段话
[]['sort']['constructor']('return location')();可以返回当前的url地址
location没有定义 ,不会就是这个吧? 既然没有解析dom,那我就声明一个location对象,赋值为url地址,试试看。代码改为这样
ctx = execjs.compile('''function qzonetoken(){ location = 'http://user.qzone.qq.com/%s'; return %s}'''% (qqnum,jother)) return ctx.call("qzonetoken")
成功运行,得到了类似这样的token:
82dafbd6f5d1f1606458c68ee54c14aa222ecc81351491a0104e43831c1cfb2e7cc82dbfcd3ffd1b33a031d97de9f8804b8f0c489
将这个与浏览器consle中得到的结果比对,是相同的,再带入post测试,成功! 总结:查这个问题,就吃亏在不懂js,不明白location是什么!计多不压身呀,多学点总没坏处!最后,写这段js代码的程序员赞,是一位有情怀的工程师!