js加密
有的反爬虫策略采用js对需要传输的数据进行加密处理(通常是取md5值)经过加密,传输的就是密文,但是加密函数或者过程一定是在浏览器完成,也就是一定会把代码(js代码)暴露给使用者通过阅读加密算法,就可以模拟出加密过程,从而达到破解过程参看案例1,案例2过程比较啰嗦,笔记比较少,仔细观察案例1:
'''破解有道词典V1'''from urllib import request, parsedef youdao(key):url = "/translate_o?smartresult=dict&smartresult=rule"data = {"i": "boy","from":"AUTO","to": "AUTO","smartresult": "dict","client": "fanyideskweb","salt": "1523100789519","sign": "b8a55a436686cd89873fa46514ccedbe","doctype": "json","version": "2.1","keyfrom": "fanyi.web","action":"FY_BY_REALTIME","typoResult": "false"}# 参数data需要是bytes格式data = parse.urlencode(data).encode()headers = {"Accept": "application/json,text/javascript,*/*;q=0.01",#"Accept-Encoding": "gzip,deflate","Accept-Language": "zh-CN,zh;q=0.9","Connection": "keep-alive","Content-Length": "200","Content-Type": "application/x-www-form-urlencoded;charset=UTF-8","Cookie": "OUTFOX_SEARCH_USER_ID=-1548144101@10.168.8.76;JSESSIONID=aaaTLWzfvp5Hfg9mAhFkw;OUTFOX_SEARCH_USER_ID_NCOO=1999296830.4784973;___rl__test__cookies=1523100789517","Host": "","Origin": "","Referer": "/","User-Agent": "Mozilla/5.0( X11; Linux x86_64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36 X-Requested-With: XMLHttpRequest"}req = request.Request(url=url, data=data, headers=headers)rsp = request.urlopen(req)html = rsp.read().decode()print(html)if __name__ == '__main__':youdao("boy")
案例2:
'''V2处理js加密代码''''''通过查找,能找到js代码中操作代码1. 这个是计算salt的公式 r = "" + ((new Date).getTime() + parseInt(10 * Math.random(), 10));2. sign: n.md5("fanyideskweb" + t + r + "ebSeFb%=XZ%T[KZ)c(sy!");md5一共需要四个参数,第一个和第四个都是固定值的字符串,第三个是所谓的salt,第二个是。。。。。第二个参数就是输入的要查找的单词'''#得到盐def getSalt():'''salt公式是: "" + ((new Date).getTime() + parseInt(10 * Math.random(), 10));把他翻译成python代码:return:'''import time, random#通过对js代码查找,然后把盐的公式中的两项在浏览器的F12状态下console中运行查找结果,然后用Python代码替换(他们的结果相同)salt = int(time.time()*1000) + random.randint(0,10)return salt#得到md5值def getMD5(v):import hashlib#生成md5实例md5 = hashlib.md5()# update需要一共bytes格式的参数md5.update(v.encode("utf-8"))sign = md5.hexdigest()return signdef getSign(key, salt):sign = 'fanyideskweb'+ key + str(salt) + "ebSeFb%=XZ%T[KZ)c(sy!"sign = getMD5(sign)return signfrom urllib import request, parsedef youdao(key):url = "/translate_o?smartresult=dict&smartresult=rule"#要确定多个地方是同一个盐salt = getSalt()data = {"i": key,"from":"AUTO","to": "AUTO","smartresult": "dict","client": "fanyideskweb","salt": str(salt) ,"sign": getSign(key, salt),"doctype": "json","version": "2.1","keyfrom": "fanyi.web","action":"FY_BY_REALTIME","typoResult": "false"}print(data)# 参数data需要是bytes格式data = parse.urlencode(data).encode()headers = {"Accept": "application/json,text/javascript,*/*;q=0.01",#"Accept-Encoding": "gzip,deflate","Accept-Language": "zh-CN,zh;q=0.9","Connection": "keep-alive","Content-Length": len(data),"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8","Cookie": "OUTFOX_SEARCH_USER_ID=-1548144101@10.168.8.76;JSESSIONID=aaaTLWzfvp5Hfg9mAhFkw;OUTFOX_SEARCH_USER_ID_NCOO=1999296830.4784973;___rl__test__cookies=1523100789517","Host": "","Origin": "","Referer": "/","User-Agent": "Mozilla/5.0( X11; Linux x86_64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36 X-Requested-With: XMLHttpRequest"}req = request.Request(url=url, data=data, headers=headers)rsp = request.urlopen(req)html = rsp.read().decode()print(html)if __name__ == '__main__':youdao("boy")
如果觉得《Python之爬虫-- js加密(破解有道词典加密的算法)》对你有帮助,请点赞、收藏,并留下你的观点哦!