admin 发表于 2020-1-6 15:09:12

Python爬虫进阶-最详细的微博登陆加密分析js加密

0x00 抓包分析
https://img-blog.csdnimg.cn/2020010411482491.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mjg5NTcyOA==,size_16,color_FFFFFF,t_70
简单的搜索之后发现,很多参数都是登陆上面这个请求返回的值,这个请求在输入完账号光标到达密码框时就会生成
https://img-blog.csdnimg.cn/20200104115152499.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mjg5NTcyOA==,size_16,color_FFFFFF,t_70
参数来源su未知servertime请求返回nonce请求返回rsakv请求返回sp未知prelt未知其余参数固定值0x01 加密逻辑分析
https://img-blog.csdnimg.cn/2020010416451190.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mjg5NTcyOA==,size_16,color_FFFFFF,t_70
搜索su=可以很快找到加密的位置,上图看到e.su和e.sp都是由sinaSSOEncoder这个函数生成的,搜索sinaSSOEncoder发现就是这个js,也就是说把当前js全部拷贝下来就可用

https://img-blog.csdnimg.cn/20200104172827708.png
这里可以得知su是由账号加密得到
function getSu(user) {    return sinaSSOEncoder.base64.encode((user))}
https://img-blog.csdnimg.cn/2020010417303947.png
me.rsaPubkey,me.servertime,me.noce这三个值都是由服务器返回的值,不多做解释
https://img-blog.csdnimg.cn/20200104174205679.png
最后这个b则是我们的密码,e.sp也就是处理完后的b值
function getSp(me,pwd) {    var f = new sinaSSOEncoder.RSAKey;
    f.setPublic(me.pubkey, "10001");
    b = f.encrypt(.join("\t") + "\n" + pwd);    return b}
https://img-blog.csdnimg.cn/20200104165543503.png
在当前js文件中搜索prelt得知是由preloginTime生成
https://img-blog.csdnimg.cn/202001041716123.png
preloginTime就是一个时间戳减一个停顿后的时间戳,其实可以设置为一个随机数
function getPrelt() {
    exectime = Math.floor(Math.random() * (20 - 300 + 1) + 300)    return exectime}0x02 代码编写1.获取nonce、rsakv等参数import jsonimport reimport requestsimport execjs

session = requests.session()prelogin_url = 'https://login.sina.com.cn/sso/prelogin.php'login_url = 'https://login.sina.com.cn/sso/login.php'username = 'username'password = 'password'with open('login.js','r',encoding='utf-8') as f:
            login_js = execjs.compile(f.read())
            su = login_js.call('getSu',username)params = {            'entry': 'weibo',            'callback': 'sinaSSOController.preloginCallBack',            'su': su,            'rsakt': 'mod',            'checkpin': '1',            'client': 'ssologin.js(v1.4.19)',            '_': '1578127327125',        }prelogin_resp = requests.get(prelogin_url,params=params)result = re.search(r'sinaSSOController.preloginCallBack\((.*?)\)',prelogin_resp.text).group(1)result_json = json.loads(result)print(result_json)
https://img-blog.csdnimg.cn/20200104180150628.png
2.获取跳转链接data = {            'entry': 'weibo',            'gateway': '1',            'from':'',            'savestate': '7',            'qrcode_flag': 'false',            'useticket': '1',            'pagerefer': '',            'vsnf': '1',            'su': su,            'service': 'miniblog',            'servertime': result_json['servertime'],            'nonce': result_json['nonce'],            'pwencode': 'rsa2',            'rsakv': result_json['rsakv'],            'sp': login_js.call('getSp',result_json,self.password),            'sr': '1920*1080',            'encoding': 'UTF-8',            'prelt': login_js.call('getPrelt'),            'url': 'https://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack',            'returntype': 'META',        }resp = session.post(login_url,data=data)print(resp.text)
https://img-blog.csdnimg.cn/20200104235402988.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mjg5NTcyOA==,size_16,color_FFFFFF,t_70
3.获取跳转链接location = re.search(r'replace\("(.*?)"\);',resp.text).group(1)resp = session.get(location)print(resp.text)
https://img-blog.csdnimg.cn/20200104235544144.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mjg5NTcyOA==,size_16,color_FFFFFF,t_70
4.最后请求location = re.search(r'replace\(\'(.*?)\'\);', resp.text).group(1)resp = self.session.get(location)print(resp.text)
https://img-blog.csdnimg.cn/20200104235707695.png
5.登陆验证
到此为止已经是成功登陆了,最后再请求一下主页,如果返回的源代码中有自己的用户名,那么说明我们已经登陆成功了
resp = self.session.get('https://weibo.com/')print(resp.text)
https://img-blog.csdnimg.cn/20200105000428337.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mjg5NTcyOA==,size_16,color_FFFFFF,t_70
0x03 结尾
整体并不难,主要麻烦在需要多次跳转
完整代码:微博登陆
顺便求关注,求Star,φ(>ω<*)

loco 发表于 2020-1-6 15:43:40

你好骚啊。已star。

admin 发表于 2020-1-6 16:06:30

谢谢loco鸽

sherry798 发表于 2020-1-20 15:27:34

页: [1]
查看完整版本: Python爬虫进阶-最详细的微博登陆加密分析js加密