Python simulated Login - Conquest verification code 9 weibo.com

Keywords: PHP Session Firefox encoding

Login interface

Http Analyzer and folders can be used for packet capturing analysis, but it seems to be very complex. It's better to use Firefox (chrome is far from Firefox).

First of all, after the user name is entered, the pre login will take place at: http://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinaSSOController.preloginCallBack&su=ZW5nbGFuZHNldSU0MDE2My5jb20%3D&rsakt=mod&checkpin=1&client=ssologin.js(v1.4.18)&_=1443156845536 49b353f444ad3993cacc02db784abbb8e42a9b1bbfffb38be18d78e87a0e 41b9b8f73a928ee0ccee1f6739884b9777e4fe9e88a1bbe495927ac4a799b3181d6442443 "," rsakv ":" 1330428213 "," showpin ": 0," expectime ": 16}). We can get four useful variables, servertime, nonce, pubkey and rsakv.

At present, the user name encryption of sina Weibo adopts Base64 encryption algorithm, while the encryption algorithm of sina Weibo login password uses RSA2, which is the key of simulated Login. It is necessary to create an rsa public key first, and both parameters of the public key are given fixed values. The first parameter is pubkey in the first step of login, and the second parameter is' 10001 'in js encrypted file (for netizens The two values need to be converted from hexadecimal to decimal, 10001 to decimal to 65537, and then added with servertime and nonce to encrypt again.

The data to be submitted is:

postdata = {
        'entry': 'weibo',
        'gateway': '1',
        'from': '',
        'savestate': '7',
        'useticket': '1',
        'pagerefer': "http://login.sina.com.cn/sso/logout.php?entry=miniblog&r=http%3A%2F%2Fweibo.com%2Flogout.php%3Fbackurl",
        'vsnf': '1',
        'su': su,
        'service': 'miniblog',
        'servertime': servertime,
        'nonce': nonce,
        'pwencode': 'rsa2',
        'rsakv': rsakv,
        'sp': password_secret,
        'sr': '1366*768',
        'encoding': 'UTF-8',
        'prelt': '115',
        'url': 'http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack',
        'returntype': 'META'
        }

After the submission, because of the redirection, we also need to get the url of the redirection.

Use Python 3.

import time
import base64
import rsa
import binascii
import requests
import re
import random
try:
    from PIL import Image
except:
    pass
try:
    from urllib.parse import quote_plus
except:
    from urllib import quote_plus

'''
//If the login protection is not enabled, you can log in without entering the verification code
//If login protection is turned on, you need to enter the verification code

'''


# Construct Request headers
agent = 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0'
headers = {
    'User-Agent': agent
}

session = requests.session()

# Visit the initial page with cookie s
index_url = "http://weibo.com/login.php"
try:
    session.get(index_url, headers=headers, timeout=2)
except:
    session.get(index_url, headers=headers)
try:
    input = raw_input
except:
    pass


def get_su(username):
    """
    //First encodeuriccomponent in javascript for email address and mobile number
    //Corresponding to urllib.parse.quote ﹣ plus in Python 3
    //Then decode after base64 encryption
    """
    username_quote = quote_plus(username)
    username_base64 = base64.b64encode(username_quote.encode("utf-8"))
    return username_base64.decode("utf-8")


# Pre login to obtain servertime, nonce, pubkey, rsakv
def get_server_data(su):
    pre_url = "http://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinaSSOController.preloginCallBack&su="
    pre_url = pre_url + su + "&rsakt=mod&checkpin=1&client=ssologin.js(v1.4.18)&_="
    pre_url = pre_url + str(int(time.time() * 1000))
    pre_data_res = session.get(pre_url, headers=headers)

    sever_data = eval(pre_data_res.content.decode("utf-8").replace("sinaSSOController.preloginCallBack", ''))

    return sever_data


# print(sever_data)


def get_password(password, servertime, nonce, pubkey):
    rsaPublickey = int(pubkey, 16)
    key = rsa.PublicKey(rsaPublickey, 65537)  # Create public key
    message = str(servertime) + '\t' + str(nonce) + '\n' + str(password)  # Obtained by splicing the plaintext js encrypted file
    message = message.encode("utf-8")
    passwd = rsa.encrypt(message, key)  # encryption
    passwd = binascii.b2a_hex(passwd)  # Converts encrypted information to hexadecimal.
    return passwd


def get_cha(pcid):
    cha_url = "http://login.sina.com.cn/cgi/pin.php?r="
    cha_url = cha_url + str(int(random.random() * 100000000)) + "&s=0&p="
    cha_url = cha_url + pcid
    cha_page = session.get(cha_url, headers=headers)
    with open("cha.jpg", 'wb') as f:
        f.write(cha_page.content)
        f.close()
    try:
        im = Image.open("cha.jpg")
        im.show()
        im.close()
    except:
        print(u"Please go to the current directory, find the verification code and enter it")


def login(username, password):
    # su is the encrypted user name
    su = get_su(username)
    sever_data = get_server_data(su)
    servertime = sever_data["servertime"]
    nonce = sever_data['nonce']
    rsakv = sever_data["rsakv"]
    pubkey = sever_data["pubkey"]
    showpin = sever_data["showpin"]
    password_secret = get_password(password, servertime, nonce, pubkey)

    postdata = {
        'entry': 'weibo',
        'gateway': '1',
        'from': '',
        'savestate': '7',
        'useticket': '1',
        'pagerefer': "http://login.sina.com.cn/sso/logout.php?entry=miniblog&r=http%3A%2F%2Fweibo.com%2Flogout.php%3Fbackurl",
        'vsnf': '1',
        'su': su,
        'service': 'miniblog',
        'servertime': servertime,
        'nonce': nonce,
        'pwencode': 'rsa2',
        'rsakv': rsakv,
        'sp': password_secret,
        'sr': '1366*768',
        'encoding': 'UTF-8',
        'prelt': '115',
        'url': 'http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack',
        'returntype': 'META'
        }
    login_url = 'http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.18)'
    if showpin == 0:
        login_page = session.post(login_url, data=postdata, headers=headers)
    else:
        pcid = sever_data["pcid"]
        get_cha(pcid)
        postdata['door'] = input(u"Please enter the verification code")
        login_page = session.post(login_url, data=postdata, headers=headers)
    login_loop = (login_page.content.decode("GBK"))
    # print(login_loop)
    pa = r'location\.replace\([\'"](.*?)[\'"]\)'
    loop_url = re.findall(pa, login_loop)[0]
    # print(loop_url)
    # This issue can also add a judgment of whether the login is successful, and write it in the next improvement
    login_index = session.get(loop_url, headers=headers)
    uuid = login_index.text
    uuid_pa = r'"uniqueid":"(.*?)"'
    uuid_res = re.findall(uuid_pa, uuid, re.S)[0]
    web_weibo_url = "http://weibo.com/%s/profile?topnav=1&wvr=6&is_all=1" % uuid_res
    weibo_page = session.get(web_weibo_url, headers=headers)
    weibo_pa = r'<title>(.*?)</title>'
    # print(weibo_page.content.decode("utf-8"))
    userID = re.findall(weibo_pa, weibo_page.content.decode("utf-8", 'ignore'), re.S)[0]
    print(u"Welcome %s, Landing successfully" % userID)


if __name__ == "__main__":
    username = input(u'User name:')
    password = input(u'Password:')
    login(username, password)

See also:
http://blog.csdn.net/andrewseu/article/details/48730735
http://www.jianshu.com/p/816594c83c74
https://github.com/ResolveWang

Posted by postalservice14 on Sun, 15 Dec 2019 12:27:39 -0800