python实现的登录和操作开心网脚本分享

时间:2021-05-22

SNS什么的我是一直无爱的,这次蛋疼写了个登录开心网(kaixin001)并向所有好友发送站内消息的脚本。

开心网在登录的时候做了一些处理,并不传原始密码,从js分析到的结果是:登录时会生成一个随机的key,然后用这个key和原始密码进行xxtea加密,把加密后的结果再进行sha1加密。之后post这个key以及加密后的密码进行登录验证。

以下是很简陋的脚本内容:

#coding: utf-8"""开心网操作脚本Author: piglei2007@gmail.comVersion: 1.0"""import reimport urllibimport urllib2import randomimport hashlibimport binasciiimport cookielibimport simplejsonfrom xxtea import encryptLOGIN_URL = "http:///" }) result = urllib2.urlopen(req).read() # 登录失败 if "errno" in result: raise LoginError("登录失败,请检查用户名或密码") print "用户 %s 登录成功!" % self.username return 'ok' def get_friends_list(self): """ 获得所有好友列表 """ get_friends_params = { 't': str(random.random()), 'type': 'all', } result = urllib2.urlopen(FRIEND_LIST_URL, urllib.urlencode(get_friends_params)).read() friends = simplejson.loads(result) print "你一共有 %s 位好友" % (len(friends) - 1) return friends def send_messages_to_all(self, message=''): """ 给所有好友发消息 """ 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 "消息发送成功" 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 = Kaixin001User( username = 'your_username', password = 'your_password' ) kxu.login() kxu.send_messages_to_all("This message is send by Python.")

这是脚本中需要用到的xxtea算法的python实现(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 _str2long(s, w): n = len(s) m = (4 - (n & 3) & 3) + n s = s.ljust(m, "\0") 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(16, "\0"), False) n = len(v) - 1 z = v[n] y = v[0] sum = 0 q = 6 + 52 // (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 >> 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(16, "\0"), False) n = len(v) - 1 z = v[n] y = v[0] q = 6 + 52 // (n + 1) sum = (q * _DELTA) & 0xffffffff while (sum != 0): e = sum >> 2 & 3 for 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 return _long2str(v, True) if __name__ == "__main__": print decrypt(encrypt('Hello XXTEA!', '16bytelongstring'), '16bytelongstring')

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章