This article mainly refers to: http://www.cnblogs.com/mouse-coder/archive/2013/03/03/2941265.html
Author: The mouse who knocks the code
Original SHA1 encryption method can refer to my previous blog: http://blog.csdn.net/monsion/article/details/7981366
Knock code of the mouse classmate has introduced and detailed, no longer say, with the Python source code, attack reference. The code is very bad, do not spray ah.
The first is the weibologin.py file, which implements a class.
#! /usr/bin/env python #-*-coding:utf-8-*-import sys import urllib import urllib2 import cookielib import base64 Import Re Import JSON import hashlib import RSA import binascii class WEIBOLOGIN:CJ = Cookielib. Lwpcookiejar () Cookie_support = Urllib2. Httpcookieprocessor (CJ) opener = Urllib2.build_opener (Cookie_support, Urllib2. HttpHandler) Urllib2.install_opener (opener) PostData = {' entry ': ' Weibo ', ' Gateway ': ' 1 ', ' from ': ', ' Savestat
E ': ' 7 ', ' Userticket ': ' 1 ', ' ssosimplelogin ': ' 1 ', ' VSNF ': ' 1 ', ' vsnval ': ', ' su ': ', ' service ': ' Miniblog ', ' Servertime ': ', ' nonce ': ', ' pwencode ': ' rsa2 ', ' sp ': ', ' Encoding ': ' UTF-8 ', ' prelt ': ' The ' is ', ' rsakv ': ', ' url ': ' http://weibo.com/ajaxlogin.php?framelogin=1&callback= Parent.sinaSSOController.feedBackUrlCallBack ', ' returntype ': ' META '} def get_servertime (self,username): url = ' htt P://login.sina.com.cn/sso/prelogin.php?entry=sso&callback=sinassocontroller.prelogincallback&su=%s&rsakt=mod&client=ssologin.js (v1.4.4) '% username data = urllib2.urlopen (URL). Read () p = re.compile (' \ (. *)
\) ' Try:json_data = P.search (data). Group (1) data = Json.loads (json_data) servertime = str (data[' servertime '])
nonce = data[' nonce '] PubKey = data[' PubKey '] rsakv = data[' rsakv '] return servertime, Nonce, PubKey, RSAKV
Except:print ' Get Severtime error! ' Return None def get_pwd (self, password, servertime, nonce, PubKey): rsapublickey = Int (PubKey,) key = RSA.
PublicKey (Rsapublickey, 65537) #创建公钥 message = str (servertime) + ' \ t ' + str (nonce) + ' \ n ' + str (password) #拼接明文js加密文件中得到
passwd = rsa.encrypt (message, key) #加密 passwd = Binascii.b2a_hex (passwd) #将加密信息转换为16进制. return passwd def get_user (self, username): Username_ = urllib.quote (username) Username = base64.encodestring (Userna ME_) [: -1] return username def get_account (self,filename): f=file (filename) flag = 0 for line in f:if flag = = 0:usernamE = Line.strip () flag +=1 else:pwd = Line.strip () f.close () return username,pwd def login (self,filename):
USERNAME,PWD = self.get_account (filename) url = ' Http://login.sina.com.cn/sso/login.php?client=ssologin.js (v1.4.4) ' Try:servertime, Nonce, pubkey, rsakv = self.get_servertime (username) print servertime print nonce print Pu
Bkey print rsakv except:print ' Get servertime error! ' Return weibologin.postdata[' servertime '] = servertime weibologin.postdata[' nonce '] = nonce weibologin.postdata[' Rsak V '] = rsakv weibologin.postdata[' su '] = Self.get_user (username) weibologin.postdata[' sp '] = self.get_pwd (pwd, Serverti Me, Nonce, pubkey) Weibologin.postdata = Urllib.urlencode (weibologin.postdata) headers = {' user-agent ': ' mozilla/5.0 (X 11; Linux i686; rv:8.0) gecko/20100101 firefox/8.0 chrome/20.0.1132.57 safari/536.11 '} req = Urllib2. Request (url = URL, data = weibologin.postdata, headers = headers) result = Urllib2.uRlopen (req) Text = Result.read () print text p = re.compile (' location\.replace\ (\) ') #此处和之前略有区别, be careful.
Try:login_url = P.search (text). Group (1) #print Login_url urllib2.urlopen (login_url) print "Login success!"
Return 1 except:print ' Login error! ' return 0
And then the main.py file.
#-*-coding:utf-8-*-import weibologin import urllib import urllib2 import time
filename = '. Config/account ' #保存微博账号的用户名和密码, first act user name, second behavior password
wblogin = Weibologin.weibologin ()
if wblogin.login (filename) ==1:
print ' Login success! '
else:
print ' Login error! '
Exit ()