Python uses RSA encryption algorithm module to simulate Sina Weibo login _python

Source: Internet
Author: User
Tags base64 sha1 sha1 encryption

PC Login Sina Weibo, the client with JS in advance username, password are encrypted, and before the post will get a set of parameters, which will also be part of the post_data. This way, you can't simulate post logins (such as Renren) in the usual simple way.

Through the crawler to obtain Sina Weibo data, analog login is essential.

1, before submitting a POST request, need to get four parameters (Servertime,nonce,pubkey and RSAKV), not previously mentioned just to get the simple servertime,nonce, here is mainly due to JS on the user name, The password encryption method has changed.

1.1 Due to the changes in encryption, we will use the RSA module, the introduction of the RSA public key encryption algorithm can refer to the relevant content in the network. Download and install the RSA module:

Download: https://pypi.python.org/pypi/rsa/3.1.1

RSA Module Document Address: http://stuvel.eu/files/python-rsa-doc/index.html

Select the RSA installation package (. Egg) that is appropriate for your own Python version, and the installation requires a command line to use Easy_ Install.exe (Win on the installation Setuptool download from here: Setuptools-0.6c11.win32-py2.6.exe installation file) for installation, for example: Easy_install Rsa-3.1.1-py2.6.egg, the final command line tests import RSA, and the installation succeeds without an error.

1.2 Access to and view Sina Weibo login js file

Check the source code of the Sina pass URL (http://login.sina.com.cn/signup/signin.php), where you can find the address of the JS http://login.sina.com.cn/js/sso/ Ssologin.js, but after the open content is encrypted, you can find an online decryption site to decrypt, view the end user name and password encryption method.

1.3 Login

Login first step, add your own user name (username), request Prelogin_url link address:

Prelogin_url = ' http://login.sina.com.cn/sso/prelogin.php?entry=sso&callback= Sinassocontroller.prelogincallback&su=%s&rsakt=mod&client=ssologin.js (v1.4.4) '% username

Use the Get method to obtain the following similar content:

Sinassocontroller.prelogincallback ({"Retcode": 0, "servertime": 1362041092, "pcid": " gz-6664c3dea2bfdaa3c94e8734c9ec2c9e6a1f "," nonce ":" iryp4n "," PubKey ":" Eb2a38568661887fa180bddb5cabd5f21c7bfd59c090cb2d245a87ac253062882729293e5506350508e7f9aa3bb77f4333231490f915f6d63c55fe2f0 8a49b353f444ad3993cacc02db784abbb8e42a9b1bbfffb38be18d78e87a0e41b9b8f73a928ee0ccee1f6739884b9777e4fe9e88a1bbe495927ac4a79 9b3181d6442443 "," rsakv ":" 1330428213 "," Exectime ": 1})

And then extract the Servertime,nonce,pubkey and rsakv that we want. Of course, the values of PubKey and RSAKV can be written dead in code, they are fixed values.

2, before username after BASE64 calculation:

Copy Code code as follows:

Username_ = Urllib.quote (username)
Username = base64.encodestring (username) [:-1]

The password is SHA1 encrypted after three times, and the values of servertime and nonce are added to interfere with it. namely: two times SHA1 encryption, the result plus servertime and nonce value, and then SHA1 calculate once.

In the latest RSA encryption method, username is still the same as the previous processing;

The password encryption method differs from the original:

2.1 First create an RSA public key, the public key of the two parameters Sina Weibo has given a fixed value, but give a 16 string, the first is to log in the first step of the PubKey, the second is JS encrypted file in the ' 10001 '.

These two values need to be converted from 16 to 10, but they can also be written in code. Here, write 10001 directly to 65537. The code is as follows:


Copy Code code as follows:

Rsapublickey = Int (PubKey, 16)
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进制.

2.2 Request Pass Url:login_url = ' http://login.sina.com.cn/sso/login.php?client=ssologin.js (v1.4.4) '

Header information that needs to be sent

Copy Code code as follows:

Postpara = {
' Entry ': ' Weibo ',
' Gateway ': ' 1 ',
' From ': ',
' SaveState ': ' 7 ',
' Userticket ': ' 1 ',
' Ssosimplelogin ': ' 1 ',
' VSNF ': ' 1 ',
' Vsnval ': ',
' su ': encodedusername,
' Service ': ' Miniblog ',
' Servertime ': Servertime,
' Nonce ': nonce,
' Pwencode ': ' RSA2 ',
' SP ': Encodedpassword,
' Encoding ': ' UTF-8 ',
' Prelt ': ' 115 ',
' RSAKV ': rsakv,
' URL ': ' Http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack ',
' ReturnType ': ' META '
}

RSAKV is added to the requested content, the Pwencode value is modified to RSA2, and the other is consistent with the previous.

Organize the parameters and post requests. Check whether the login is successful, you can refer to the post after the content of a sentence location.replace ("http://weibo.com/ajaxlogin.php?framelogin=1&callback= parent.sinassocontroller.feedbackurlcallback&retcode=101&reason=%b5%c7%c2%bc%c3%fb%bb%f2%c3%dc%c2%eb% B4%ed%ce%f3 ");

If retcode=101 indicates that the login failed. The result is similar to the success of the login, but the Retcode value is 0.

3, after the successful login, in the body of the Replace information in the URL is the next we want to use the URL. Then use the Get method on the URL above to send a request to the server to save the cookie information for this request, which is the login cookie we need.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.