Python implementation of Login and operation Happy Net script sharing _python

Source: Internet
Author: User
Tags decrypt sha1 sha1 encryption throw exception urlencode

SNS What I have been no love, this time the egg pain wrote a login net (kaixin001) and to all the friends send messages in the station script.

Happy in the log in when doing some processing, do not pass the original password, from the JS analysis to the result is: Login will generate a random key, and then use this key and the original password for Xxtea encryption, the results of the encryption after the SHA1 encryption. Post this key and the encrypted password for logon verification.

Here's a rundown of the scripting stuff:

#coding: Utf-8 "" "" Happy Net Operation script author:piglei2007@gmail.com version:1.0 "" "Import re import urllib import urllib2 import R Andom Import hashlib Import binascii import cookielib import Simplejson from Xxtea import encrypt login_url = "Http://ww w.kaixin001.com/login/login_api.php "Login_key_url =" http://www.kaixin001.com/"Friend_list_url =" http:// www.kaixin001.com/interface/suggestfriend.php "Message_send_url =" http://www.kaixin001.com/msg/post.php "Login_

Key_re = Re.compile (r) new\senlogin\ (' (. *?) ') Class Loginerror (Exception): "" Login failed Throw exception "" Class Kaixin001user (object): "" "Action kaixin001, existing method: Get_log In_key-Gets the encrypted key Get_rpassword assigned when the user accesses the login page-Gets the password after Xxtea and SHA1 encrypted login-Login Get_friends_list -Get all friends, return to dictionary format send_messages_to_all-send messages to all Friends "" "Def __init__ (self, username, password): Self.usern Ame = Username Self.password = Password SELF.CJ = cookielib. Cookiejar () opener = Urllib2.build_opener (urllib2. HTtpcookieprocessor (SELF.CJ)) opener.addheaders = [("User-agent", "mozilla/5.0" (X11; U FreeBSD i386; En-us; rv:1.9.1) gecko/20090704 firefox/3.5 "), (" Accept "," */* "), (" Host "," www.kaixin001.com ")] Urllib2.ins Tall_opener (opener) def Get_login_key (self): "" "" "" "" "" _temp = Urllib2.urlopen (login_key
    _url). Read () key = Login_key_re.search (_temp). Group (1) Return key def login (self): "" Login "" "" "
      Login_key = Self.get_login_key () Rpassword = Self.get_rpassword (Self.password, login_key) Login_params = {      
    ' Email ': self.username, ' encypt ': Login_key, ' rpasswd ': rpassword, ' url ': '/home/', ' ver ': ' 1 ' } req = Urllib2. Request (Login_url, Urllib.urlencode (login_params), {"Referer": "http://www.kaixin001.com/"}) result = Urll Ib2.urlopen (req). Read () # Login failed if ' errno ' in result:raise loginerror ("Login failed, check user name or password") prinT "user%s login succeeded!" "% self.username return ' OK ' def get_friends_list (self):" "Get all Buddy list" "Get_friends_param s = {' t ': Str (random.random ()), ' type ': ' All ',} result = Urllib2.urlopen (Friend_list_url, Urllib  . UrlEncode (Get_friends_params)). Read () Friends = simplejson.loads (result) print "You have a total of%s bit friends" (Len (Friends) -1) Return Friends def send_messages_to_all (self, message= "):" "Send Message to all friends" "Friends = self. Get_friends_list () Send_params = {' Attachment_cancel ': ', ' attachment_forwarding ': ', ' attachmen  T_random ': ', ' Code ': ', ' content ': message, ' forward_thread ': ', ' rcode ': ', ' Service ': ' 0 ', ' texttype ': ' html ', ' UIDs ': ', '. Join ([Str (f[' UID ') for F in Friends])} result = Urllib2.url
    
  
  Open (Message_send_url, Urllib.urlencode (send_params)) print result.geturl () print "message sent successfully" return ' OK ' def get_rpassword (self, password, key): "" "" "" "" "XXTEA_PW = Binascii.b2a_hex (Encrypt (password, key)) R_password = HASHLIB.SHA1 (XXTEA_PW). Hexdigest () return R_password if __name__ = ' __main__ ': Kxu = Kaixin001use R (username = ' your_username ', password = ' Your_password ') Kxu.login () Kxu.send_messages_to_all ("This Messa GE are send by Python. ")

This is the Python implementation of the Xxtea algorithm that is needed in the script (xxtea.py):

import struct _delta = 0x9e3779b9 def _long2str (V, w): n = (len (v)-1) << 2 if w:m = V[-1] I  F (M < n-3) or (M > N): Return "' n = m s = struct.pack (' <%il '% len (v), *v) return s[0:n] if w else S def _str2long (S, w): n = Len (s) m = (4-(N & 3) & 3) + n s = s.ljust (M, "C") v = list (struct.u Npack (' <%il '% (M >> 2), s) if W:v.append (n) return v def encrypt (str, key): if str = = ': return s TR v = _str2long (str, True) k = _str2long (Key.ljust, ""), False) n = Len (v)-1 z = v[n] y = v[0] s Um = 0 Q = 6 +//(n + 1) while q > 0:sum = (sum + _delta) & 0xFFFFFFFF e = Sum >> 2 ; 3 for P in Xrange (n): y = v[p + 1] v[p] = (V[p] + ((z >> 5 ^ y << 2) + (y >> 3 ^ z &l t;< 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z))) & 0xFFFFFFFF z = v[p] y = v[0] v[n] = (v[n) + (Z &G T;> 5 ^ y <<2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (K[n & 3 ^ e] ^ z)) & 0xFFFFFFFF z = v[n] Q-= 1 re Turn _long2str (V, FALSE) def decrypt (str, key): if str = = ': return str v = _str2long (str, false) K = _str2lo Ng (Key.ljust, ""), False) n = Len (v)-1 z = v[n] y = v[0] Q = 6 +//(n + 1) sum = (Q * _delta) &A mp 
      0xFFFFFFFF while (sum!= 0): E = Sum >> 2 & 3 to P in xrange (n, 0,-1): Z = v[p-1] V[P] = (V[p]-((z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z)) ; 0xFFFFFFFF y = v[p] z = v[n] v[0] = (V[0]-((z >> 5 ^ y << 2) + (y >> 3 ^ z << 4 ) ^ (sum ^ y) + (k[0 & 3 ^ e] ^ z))) & 0xFFFFFFFF y = v[0] sum = (Sum-_delta) & 0xFFFFFFFF Retu RN _long2str (V, True) if __name__ = = "__main__": Print Decrypt (Encrypt (' Hello xxtea! ', ' 16bytelongstring '), ' 16bytel Ongstring ')

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.