抓包分析
首先打开charles,记录从打开浏览器到新浪微博登陆成功的全部http请求
打开新浪微博,等待页面加载完成后,输入账号密码点击登陆,charles停止抓包,关闭浏览器。并将抓包结果进行保存。
找到登陆的POST请求https://login.sina.com.cn/sso/login.PHP?clIEnt=ssologin.Js(v1.4.19)
登陆POST请求
进群:548377875 即可获取数十套pdf哦!
理论上我们只需要能完整的提交这个表单就能实现新浪微博的登陆。但是如果进行试验的话,会发现将该表单完整复制之后使用requests进行post提交是无法登陆的,所以可以断定其中某些字段是通过动态获取。
由于新浪微博的首页内容太多太杂
我们将上文中拿到的登陆post请求https://login.sina.com.cn/sso/login.PHP?clIEnt=ssologin.Js(v1.4.19)进行访问发现是一个单独的登陆页面。
登陆页面
打开F12,对登陆按钮进行定位,根据前后台交互的方式可以知道后台应该是通过中某一个内容判断用户点击了登陆按钮,在Source中的Js代码部分检索的type:submit。
按钮定位
Js代码查找登陆 *** 作
初步断定该Js中进行了一些加密 *** 作
username转换
根据命名规则尝试检索用户名username很容易的找到了一段username的转换 *** 作
this.prelogin = function(config,callback) { var url = location.protocol == "https:" ? ssoPreLoginUrl.replace(/^http:/,"https:") : ssoPreLoginUrl; var username = config.username || ""; username = sinaSSOEncoder.base64.encode(urlencode(username)); delete config.username; var arrquery = { entry: me.entry,callback: me.name + ".preloginCallBack",su: username,rsakt: "mod" };
从username = sinaSSOEncoder.base64.encode(urlencode(username));可以看出来,用户名经过了url编码后再进行了base64转码,从键值对可以看出来su提交的就是转码后的账号
使用python实现
def get_username(self): username_quote = urllib.parse.quote_plus(self.user_name) username_base64 = base64.b64encode(username_quote.encode("utf-8")) return username_base64.decode("utf-8")
password转换
下面检索password一下子就发现了关键的一句话,而且明目张胆的写着RSA
password = RSAKey.encrypt([me.servertime,me.nonce].join(" ") + " " + password)
要进行RSA加密需要公钥PublicKey,检索一下Public找到了公钥RSAKey.setPublic(me.rsaPubkey,"10001");这样还不够,再找找看me.rsaPubkey是什么东西me.rsaPubkey = result.pubkey;所以应是返回值中有带咯,在charles里面找一下pubkey
公钥
这个返回值中有很多眼熟的东西servertime,nonce都在这里面了。记下这个有用的url:
https://login.sina.com.cn/sso/prelogin.PHP?entry=weibo&callback=sinaSSOController.preloginCallBack&su=&rsakt=mod&clIEnt=ssologin.Js(v1.4.19)&_=1536460959875
sinaSSOController.preloginCallBack({"retcode":0,"servertime":1536460961,"pcID":"gz-1c8cc52b95dad5397635083e4ddcd33994aa","nonce":"42KG80","pubkey":"EB2A38568661887FA180BDDB5CABD5F21C7BFD59C090CB2D245A87AC253062882729293E5506350508E7F9AA3BB77F4333231490F915F6D63C55FE2F08A49B353F444AD3993CACC02DB784ABBB8E42A9B1BBFFFB38BE18D78E87A0E41B9B8F73A928EE0CCEE1F6739884B9777E4FE9E88A1BBE495927AC4A799B3181D6442443","rsakv":"1330428213","uID":"2239053435","exectime":6})
这个url有点复杂,看到了?,&等内容使用requests里面get的params=传入
def get_Json_data(self,su_value): params = { "entry": "weibo","callback": "sinaSSOController.preloginCallBack","rsakt": "mod","checkpin": "1","clIEnt": "ssologin.Js(v1.4.18)","su": su_value,"_": int(time.time()*1000),} try: response = self.session.get("http://login.sina.com.cn/sso/prelogin.PHP",params=params) Json_data = Json.loads(re.search(r"((?P.*))",response.text).group("data")) except Exception as excep: Json_data = {} logging.error("WeiBologin get_Json_data error: %s",excep) logging.deBUG("WeiBologin get_Json_data: %s",Json_data) return Json_data
根据password = RSAKey.encrypt([me.servertime,me.nonce].join(" ") + " " + password)用python写一下rsa加密
def get_password(self,servertime,nonce,pubkey): string = (str(servertime) + " " + str(nonce) + "" + str(self.pass_word)).encode("utf-8") public_key = rsa.PublicKey(int(pubkey,16),int("10001",16)) password = rsa.encrypt(string,public_key) password = binascii.b2a_hex(password) return password.decode()
文章开头的表单的动态内容都动态获取了接下来就是post表单提交了,然后拿一下user_uniqueID和user_nick就可以(用你的账户)爬必须登录才能获取的数据了
login_url_1 = "http://login.sina.com.cn/sso/login.PHP?clIEnt=ssologin.Js(v1.4.18)&_=%d" % int(time.time())Json_data_1 = self.session.post(login_url_1,data=post_data).Json()if Json_data_1["retcode"] == "0": params = { "callback": "sinaSSOController.callbackLoginStatus","ticket": Json_data_1["ticket"],"ssosavestate": int(time.time()),} response = self.session.get("https://passport.weibo.com/wbsso/login",params=params) Json_data_2 = Json.loads(re.search(r"((?P.*))",response.text).group("result")) if Json_data_2["result"] is True: self.user_uniqueID = Json_data_2["userinfo"]["uniqueID"] self.user_nick = Json_data_2["userinfo"]["displayname"] logging.warning("WeiBologin succeed: %s",Json_data_2) else: logging.warning("WeiBologin Failed: %s",Json_data_2)else: logging.warning("WeiBologin Failed: %s",Json_data_1)return True if self.user_uniqueID and self.user_nick else False
登录结果
完整代码来自:https://github.com/xianhu/LearnPython/blob/master/python_wechat.py
#Coding=utf-8import reimport rsaimport timeimport Jsonimport base64import loggingimport binasciiimport requestsimport urllib.parseclass WeiBologin(object): """ class of WeiBologin,to login weibo.com """ def __init__(self): """ constructor """ self.user_name = None self.pass_word = None self.user_uniqueID = None self.user_nick = None self.session = requests.Session() self.session.headers.update({"User-Agent": "Mozilla/5.0 (windows NT 6.3; WOW64; rv:41.0) Gecko/20100101 firefox/41.0"}) self.session.get("http://weibo.com/login.PHP") return def login(self,user_name,pass_word): """ login weibo.com,return True or False """ self.user_name = user_name self.pass_word = pass_word self.user_uniqueID = None self.user_nick = None # get Json data s_user_name = self.get_username() Json_data = self.get_Json_data(su_value=s_user_name) if not Json_data: return False s_pass_word = self.get_password(Json_data["servertime"],Json_data["nonce"],Json_data["pubkey"]) # make post_data post_data = { "entry": "weibo","gateway": "1","from": "","savestate": "7","userticket": "1","vsnf": "1","service": "miniblog","enCoding": "UTF-8","pwencode": "rsa2","sr": "1280*800","prelt": "529","url": "http://weibo.com/AJAXlogin.PHP?framelogin=1&callback=parent.sinaSSOController.FeedBackUrlCallBack","rsakv": Json_data["rsakv"],"servertime": Json_data["servertime"],"nonce": Json_data["nonce"],"su": s_user_name,"sp": s_pass_word,"returntype": "TEXT",} # get captcha code if Json_data["showpin"] == 1: url = "http://login.sina.com.cn/cgi/pin.PHP?r=%d&s=0&p=%s" % (int(time.time()),Json_data["pcID"]) with open("captcha.jpeg","wb") as file_out: file_out.write(self.session.get(url).content) code = input("请输入验证码:") post_data["pcID"] = Json_data["pcID"] post_data["door"] = code # login weibo.com login_url_1 = "http://login.sina.com.cn/sso/login.PHP?clIEnt=ssologin.Js(v1.4.18)&_=%d" % int(time.time()) Json_data_1 = self.session.post(login_url_1,data=post_data).Json() if Json_data_1["retcode"] == "0": params = { "callback": "sinaSSOController.callbackLoginStatus",Json_data_1) return True if self.user_uniqueID and self.user_nick else False def get_username(self): """ get legal username """ username_quote = urllib.parse.quote_plus(self.user_name) username_base64 = base64.b64encode(username_quote.encode("utf-8")) return username_base64.decode("utf-8") def get_Json_data(self,su_value): """ get the value of "servertime","nonce","pubkey","rsakv" and "showpin",etc """ params = { "entry": "weibo",Json_data) return Json_data def get_password(self,pubkey): """ get legal password """ string = (str(servertime) + " " + str(nonce) + "" + str(self.pass_word)).encode("utf-8") public_key = rsa.PublicKey(int(pubkey,public_key) password = binascii.b2a_hex(password) return password.decode()if __name__ == "__main__": logging.basicConfig(level=logging.DEBUG,format="%(asctime)s %(levelname)s %(message)s") weibo = WeiBologin() weibo.login("username","password")总结
以上是内存溢出为你收集整理的Python模拟登录新浪微博!自动输入账号密码以及自动识别验证码!全部内容,希望文章能够帮你解决Python模拟登录新浪微博!自动输入账号密码以及自动识别验证码!所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)