Python implementation of Login and operation Happy Net script sharing

Source: Internet
Author: User
Tags decrypt sha1 sha1 encryption urlencode
SNS What I have been no love, this time the egg has written a login happy (kaixin001) and send all friends in the station message script.

NET in the time of the login to do 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 encryption and then SHA1 encryption. Post this key and the encrypted password for login verification.

Here's a rundown of the script content:

#coding: Utf-8 "" "Happy Net Operation script author:piglei2007@gmail.comversion:1.0" "" Import reimport urllibimport Urllib2import Randomimport hashlibimport binasciiimport cookielibimport simplejsonfrom xxtea Import encryptlogin_url = "/http/ www.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): "" "Failed to throw exception" "" Class Kaixin001user (object): "" Operation kaixin001, existing method: Get_login_key-         Encrypt key Get_rpassword assigned when user accesses login page-get password after Xxtea and SHA1 login-Login get_friends_list-Get all friends, return dictionary format Send_messages_to_all-Send message to all Friends "" "Def __init__ (self, username, password): Self.username = Username sel F.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.install _opener (opener) def Get_login_key (self): "" "gets login Time encryption Key" "" _temp = Urllib2.urlopen (login_key_url). Read () key = Login_key_re.search (_temp). Group (1) Return key def login (self): "" "Login" "" Login_key = Sel F.get_login_key () Rpassword = Self.get_rpassword (Self.password, login_key) login_params = {' Email ': self.usern Ame, ' encypt ': Login_key, ' rpasswd ': rpassword, ' url ': '/home/', ' ver ': ' 1 '} req = Urllib 2.Request (Login_url, Urllib.urlencode (login_params), {"Referer": "http://www.kaixin001.com/"}) result = Urlli B2.urlopen (req). Read () # Login failed if "errno" in Result:raise LoginError ("Login failed, check user name or password") print "user%s" Login Successful! "% self.username return ' OK ' def get_friends_list (self):" "    Get all Friends List "" "Get_friends_params = {' t ': Str (random.random ()), ' type ': ' All ',} result = URL Lib2.urlopen (Friend_list_url, Urllib.urlencode (Get_friends_params)). Read () Friends = simplejson.loads (result) PR     int "You have a total of%s friends"% (Len (Friends)-1) Return Friends def send_messages_to_all (self, message= "):" "Send messages to all friends "" "Friends = self.get_friends_list () send_params = {' Attachment_cancel ': ', ' attachment_forwarding '       : ', ' attachment_random ': ', ' Code ': ', ' content ': message, ' forward_thread ': ', ' rcode ': ',  ' Service ': ' 0 ', ' texttype ': ' html ', ' UIDs ': ', '. Join ([Str (f[' UID ')] for F in Friends])} result = Urllib2.urlopen (Message_send_url, Urllib.urlencode (send_params)) print result.geturl () print "message sent successfully" return "OK" ' Def get_rpassword (self, password, key): "" Get Encrypted Password "" "XXTEA_PW = Binascii.b2a_hex (Encrypt (passwor D, key)) R_password= HASHLIB.SHA1 (XXTEA_PW). Hexdigest () return R_password if __name__ = = ' __main__ ': Kxu = kaixin001user (username = ' your_username ', password = ' Your_password ') Kxu.login () Kxu.send_messages_to_all ("This message is the send by Python.") )

This is the python implementation of the Xxtea algorithm that needs to be used in the script (xxtea.py):

import struct _delta = 0x9e3779b9 def _long2str (V, w): n = (len (v)-1) << 2 if w:m = V[-1] if (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 _str 2long (S, w): n = Len (s) m = (4-(N & 3) & 3) + n s = s.ljust (M, "mm") v = list (Struct.unpack (' <%il '%  (M >> 2), s)) if W:v.append (n) return v def encrypt (str, key): if str = = ": return str v = _str2long (str, True) K = _str2long (Key.ljust (+), False) n = Len (v)-1 z = v[n] y = v[0] sum = 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 << 4) ^ (sum ^ y) + (K[p & 3 ^ e] ^ z))) & 0xFFFFFFFF z = v[p] y = v[0] v[n] = (V[n] + ((z >> 5 ^ y << 2) + (y >&gt ; 3 ^ Z << 4) ^ (sUm ^ y) + (K[n & 3 ^ e] ^ z))) & 0xFFFFFFFF z = v[n] Q-= 1 return _long2str (V, False) def decrypt (str,    Key): if str = = ": return str v = _str2long (str, false) K = _str2long (Key.ljust (+"), false) n = Len (v)-1 z = v[n] y = v[0] Q = 6 +//(n + 1) sum = (Q * _delta) & 0xFFFFFFFF while (sum! = 0): E = Sum >& Gt 2 & 3 for P in xrange (n, 0,-1): Z = v[p-1] v[p] = (V[p]-((z >> 5 ^ y << 2) + (y >& Gt 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 return _long2str (V, True) if __name__ = = "__main__": Print Decrypt (Encrypt (' Hello xxtea! ', ' 16bytelongstring '), ' 16bytelongstring ')
  • 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.