上一篇百度登陸加密協議分析(上)主要講解了codestring,gid,token,rsakey等參數的產生。好了,廢話很少說,我們進入今天的主題,我們接着上一篇的內容往下講解,最後還剩三個字段 callback,password,ppui_logintime。(個人新書《Python爬蟲開發與項目實戰》出版了,你們能夠看一下樣章)javascript
第三部分: html
_eventHandler: function() { var e, t = { focus: function(t) { var n = e.fireEvent('fieldFocus', { ele: this }); n && (this.addClass(e.constant.FOCUS_CLASS), this.removeClass(e.constant.ERROR_CLASS), baidu(e.getElement(t + 'Label')).addClass(e.constant.LABEL_FOCUS_CLASS)) }, blur: function(t) { var n = e.fireEvent('fieldBlur', { ele: this }); n && (this.removeClass(e.constant.FOCUS_CLASS), baidu(e.getElement(t + 'Label')).removeClass(e.constant.LABEL_FOCUS_CLASS)) }, mouseover: function() { var t = e.fireEvent('fieldMouseover', { ele: this }); t && this.addClass(e.constant.HOVER_CLASS) }, mouseout: function() { var t = e.fireEvent('fieldMouseout', { ele: this }); t && this.removeClass(e.constant.HOVER_CLASS) }, keyup: function() { e.fireEvent('fieldKeyup', { ele: this }) } }, n = { focus: { userName: function() { e.config.loginMerge && e.getElement('loginMerge') && (e.getElement('loginMerge').value = 'true', e.getElement('isPhone').value = '') }, password: function() { e._getRSA(function(t) { e.RSA = t.RSA, e.rsakey = t.rsakey }) }, verifyCode: function() {} }, blur: { userName: function() {}, password: function(t) { var n = this.get(0).value; n.length && e.validate(t) }, verifyCode: function(t) { var n = this.get(0).value; n.length && e.validate(t) } }, change: { userName: function() { var t = this.get(0).value; e._loginCheck(t) }, verifyCode: function() {} }, click: { verifyCodeChange: function(t, n) { e.getElement('verifyCode').value = '', e._doFocus('verifyCode'), e.getVerifyCode(), n.preventDefault() } }, keyup: { verifyCode: function() { var t = e.getElement('verifyCode'), n = baidu(t); t.value && 4 == t.value.length ? e._asyncValidate.checkVerifycode.call(e, { error: function(t) { n.addClass(e.constant.ERROR_CLASS), e.setGeneralError(t.msg) }, success: function() { n.removeClass(e.constant.ERROR_CLASS), e.clearGeneralError() } }) : e.$hide('verifyCodeSuccess') } }, submit: function(t) { e.submit(), t.preventDefault() } }; return { entrance: function(i) { e = this; var r = (baidu(i.target), i.target.name); if (!r && i.target.id) { var o = i.target.id.match(/\d+__(.*)$/); o && (r = o[1]) } r && (t.hasOwnProperty(i.type) && t[i.type].apply(baidu(i.target), [ r, i ]), n.hasOwnProperty(i.type) && ('function' == typeof n[i.type] && n[i.type].apply(baidu(i.target), [ i ]), n[i.type].hasOwnProperty(r) && n[i.type][r].apply(baidu(i.target), [ r, i ])), e.initialized || 'focus' != i.type || e._initApi()) } } }(),
callback ='bd__cbs__'+Math.floor(2147483648 *Math.random()).toString(36)
採用的是RSA加密方式: from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_v1_5 password = 'xxxxxxxx' with open('pub.pem') as f: pubkey = f.read() rsakey = RSA.importKey(pubkey) cipher = PKCS1_v1_5.new(rsakey) cipher_text = base64.b64encode(cipher.encrypt(password)) print cipher_text
3.5 因爲以前安裝了pyv8,因此不把gid,callback等js函數翻譯成python了,翻譯過來也很簡單,若是你電腦上沒裝pyv8,就試着翻譯一下。java
function callback(){ return 'bd__cbs__'+Math.floor(2147483648 * Math.random()).toString(36) } function gid(){ return 'xxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (e) { var t = 16 * Math.random() | 0, n = 'x' == e ? t : 3 & t | 8; return n.toString(16) }).toUpperCase() }
3.6 彷佛還有驗證碼沒說,其實就是兩個連接,一個是獲取驗證碼的連接,一個是檢測驗證碼是否正確的連接。驗證碼獲取很簡單,這裏就不詳細說了。下面我會把整個登陸的源代碼,貼在下面有興趣的,能夠去玩一下。python
總結: git
下面我用python模擬了一下登陸,使用了requests和pyv8(其實想偷懶),代碼以下:github
#coding:utf-8 import base64 import json import re from Crypto.Cipher import PKCS1_v1_5 from Crypto.PublicKey import RSA import PyV8 from urllib import quote import requests import time if __name__=='__main__': s = requests.Session() s.get('http://yun.baidu.com') js=''' function callback(){ return 'bd__cbs__'+Math.floor(2147483648 * Math.random()).toString(36) } function gid(){ return 'xxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (e) { var t = 16 * Math.random() | 0, n = 'x' == e ? t : 3 & t | 8; return n.toString(16) }).toUpperCase() } ''' ctxt = PyV8.JSContext() ctxt.enter() ctxt.eval(js) ###########獲取gid#############################3 gid = ctxt.locals.gid() ###########獲取callback#############################3 callback1 = ctxt.locals.callback() ###########獲取token#############################3 tokenUrl="https://passport.baidu.com/v2/api/?getapi&tpl=netdisk&subpro=netdisk_web&apiver=v3" \ "&tt=%d&class=login&gid=%s&logintype=basicLogin&callback=%s"%(time.time()*1000,gid,callback1) token_response = s.get(tokenUrl) pattern = re.compile(r'"token"\s*:\s*"(\w+)"') match = pattern.search(token_response.text) if match: token = match.group(1) else: raise Exception ###########獲取callback#############################3 callback2 = ctxt.locals.callback() ###########獲取rsakey和pubkey#############################3 rsaUrl = "https://passport.baidu.com/v2/getpublickey?token=%s&" \ "tpl=netdisk&subpro=netdisk_web&apiver=v3&tt=%d&gid=%s&callback=%s"%(token,time.time()*1000,gid,callback2) rsaResponse = s.get(rsaUrl) pattern = re.compile("\"key\"\s*:\s*'(\w+)'") match = pattern.search(rsaResponse.text) if match: key = match.group(1) print key else: raise Exception pattern = re.compile("\"pubkey\":'(.+?)'") match = pattern.search(rsaResponse.text) if match: pubkey = match.group(1) print pubkey else: raise Exception ################加密password########################3 password = 'xxxxxxx'#填上本身的密碼 pubkey = pubkey.replace('\\n','\n').replace('\\','') rsakey = RSA.importKey(pubkey) cipher = PKCS1_v1_5.new(rsakey) password = base64.b64encode(cipher.encrypt(password)) print password ###########獲取callback#############################3 callback3 = ctxt.locals.callback() data={ 'apiver':'v3', 'charset':'utf-8', 'countrycode':'', 'crypttype':12, 'detect':1, 'foreignusername':'', 'idc':'', 'isPhone':'', 'logLoginType':'pc_loginBasic', 'loginmerge':True, 'logintype':'basicLogin', 'mem_pass':'on', 'quick_user':0, 'safeflg':0, 'staticpage':'http://yun.baidu.com/res/static/thirdparty/pass_v3_jump.html', 'subpro':'netdisk_web', 'tpl':'netdisk', 'u':'http://yun.baidu.com/', 'username':'xxxxxxxxx',#填上本身的用戶名 'callback':'parent.'+callback3, 'gid':gid,'ppui_logintime':71755, 'rsakey':key, 'token':token, 'password':password, 'tt':'%d'%(time.time()*1000), } ###########第一次post#############################3 post1_response = s.post('https://passport.baidu.com/v2/api/?login',data=data) pattern = re.compile("codeString=(\w+)&") match = pattern.search(post1_response.text) if match: ###########獲取codeString#############################3 codeString = match.group(1) print codeString else: raise Exception data['codestring']= codeString #############獲取驗證碼################################### verifyFail = True while verifyFail: genimage_param = '' if len(genimage_param)==0: genimage_param = codeString verifycodeUrl="https://passport.baidu.com/cgi-bin/genimage?%s"%genimage_param verifycode = s.get(verifycodeUrl) #############下載驗證碼################################### with open('verifycode.png','wb') as codeWriter: codeWriter.write(verifycode.content) codeWriter.close() #############輸入驗證碼################################### verifycode = raw_input("Enter your input verifycode: "); callback4 = ctxt.locals.callback() #############檢驗驗證碼################################### checkVerifycodeUrl='https://passport.baidu.com/v2/?' \ 'checkvcode&token=%s' \ '&tpl=netdisk&subpro=netdisk_web&apiver=v3&tt=%d' \ '&verifycode=%s&codestring=%s' \ '&callback=%s'%(token,time.time()*1000,quote(verifycode),codeString,callback4) print checkVerifycodeUrl state = s.get(checkVerifycodeUrl) print state.text if state.text.find(u'驗證碼錯誤')!=-1: print '驗證碼輸入錯誤...已經自動更換...' callback5 = ctxt.locals.callback() changeVerifyCodeUrl = "https://passport.baidu.com/v2/?reggetcodestr" \ "&token=%s" \ "&tpl=netdisk&subpro=netdisk_web&apiver=v3" \ "&tt=%d&fr=login&" \ "vcodetype=de94eTRcVz1GvhJFsiK5G+ni2k2Z78PYRxUaRJLEmxdJO5ftPhviQ3/JiT9vezbFtwCyqdkNWSP29oeOvYE0SYPocOGL+iTafSv8pw" \ "&callback=%s"%(token,time.time()*1000,callback5) print changeVerifyCodeUrl verifyString = s.get(changeVerifyCodeUrl) pattern = re.compile('"verifyStr"\s*:\s*"(\w+)"') match = pattern.search(verifyString.text) if match: ###########獲取verifyString#############################3 verifyString = match.group(1) genimage_param = verifyString print verifyString else: verifyFail = False raise Exception else: verifyFail = False data['verifycode']= verifycode ###########第二次post#############################3 data['ppui_logintime']=81755
####################################################
# 特意說明,你們會發現第二次的post出去的密碼是改變的,爲何我這裏沒有變化呢?
#是由於RSA加密,加密密鑰和密碼原文即便不變,每次加密後的密碼都是改變的,RSA有隨機因子的關係
#因此我這裏不須要在對密碼原文進行第二次加密了,直接使用上次加密後的密碼便可,是沒有問題的。
# ###################################################################################web
post2_response = s.post('https://passport.baidu.com/v2/api/?login',data=data) if post2_response.text.find('err_no=0')!=-1: print '登陸成功' else: print '登陸失敗'
我把整個代碼上傳到git上了:https://github.com/qiyeboy/baidulogin.git,你們能夠star和fork。json
今天的分享就到這裏,若是你們以爲還能夠呀,記得推薦呦。
歡迎你們支持我公衆號:
本文章屬於原創做品,歡迎你們轉載分享,禁止修改文章的內容。尊重原創,轉載請註明來自:七夜的故事 http://www.cnblogs.com/qiyeboy/