操作开心网发送信息的python脚本

Type: 技术相关 - Posted at: 2010/08/23 14:52
Tags : python

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

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

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

#coding: utf-8
"""
开心网操作脚本

Author: piglei2007@gmail.com
Version: 1.0
"""
import re
import urllib
import urllib2
import random
import hashlib
import binascii
import cookielib
import simplejson

from xxtea import encrypt

LOGIN_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):
    """
    登录失败抛出异常
    """

class Kaixin001User(object):
    """
    操作kaixin001,现有方法:
    
        get_login_key - 获得用户访问登录页面时分配的加密key
        
        get_rpassword - 获得经过xxtea以及sha1加密后的密码
        
        login - 登录
        
        get_friends_list - 获得所有好友,返回字典格式
        
        send_messages_to_all - 给所有好友发消息
    """
    
    def __init__(self, username, password):
        self.username = 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.install_opener(opener)
        
    def get_login_key(self):
        """
        获得登录时候的加密key
        """
        _temp = urllib2.urlopen(LOGIN_KEY_URL).read()
        key = LOGIN_KEY_RE.search(_temp).group(1)
        return key
        
    def login(self):
        """
        登录
        """
        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 = 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):

############################################################  
#                                                          #  
# The implementation of PHPRPC Protocol 3.0                #  
#                                                          #  
# xxtea.py                                                 #  
#                                                          #  
# Release 3.0.0                                            #  
# Copyright (c) 2005-2008 by Team-PHPRPC                   #  
#                                                          #  
# WebSite: http://www.phprpc.org/                         #  
#           http://www.phprpc.net/                         #  
#           http://www.phprpc.com/                         #  
#           http://sourceforge.net/projects/php-rpc/       #  
#                                                          #  
# Authors: Ma Bingyao <andot@ujn.edu.cn>                  #  
#                                                          #  
# This file may be distributed and/or modified under the   #  
# terms of the GNU Lesser General Public License (LGPL)    #  
# version 3.0 as published by the Free Software Foundation #  
# and appearing in the included file LICENSE.              #  
#                                                          #  
############################################################  
#  
# XXTEA encryption arithmetic library.  
#  
# Copyright (C) 2005-2008 Ma Bingyao <andot@ujn.edu.cn>  
# Version: 1.0  
# LastModified: Oct 5, 2008  
# This library is free. You can redistribute it and/or modify it.  
  
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')

<
>
1# args   said this at 2010-08-23 19:26 : [Quote]
用py做位操作,开了眼
2# hef   said this at 2010-08-23 22:34 : [Quote]
我这楼主 renren ID 和 password
要的M
3# alan   said this at 2010-09-15 03:05 : [Quote]
很酷,不过我测试的时候没有成功发送,检查代码发现

121行这里
'uids': ",".join([str(f['uid']) for f in friends])

这里似乎对所有好友的uid进行了遍历然后拼接在一起,是没错,不过经测试不能群发,如果只填写一个uid,是OK的。
4# hef   said this at 2010-09-15 09:29 : [Quote]
Quoted alan:

很酷,不过我测试的时候没有成功发送,检查代码发现

121行这里
'uids': ",".join([str(f['uid']) for f in friends])

这里似乎对所有好友的uid进行了遍历然后拼接在一起,是没错,不过经测试不能群发,如果只填写一个uid,是OK的。


估计 KAIXIN001群发换代码。..
5# Admin   said this at 2010-09-15 17:41 : [Quote]
Quoted alan:

很酷,不过我测试的时候没有成功发送,检查代码发现

121行这里
'uids': ",".join([str(f['uid']) for f in friends])

这里似乎对所有好友的uid进行了遍历然后拼接在一起,是没错,不过经测试不能群发,如果只填写一个uid,是OK的。


可能是因为人数超标的原因,当时测试的时候没有测试这个人数有没有一个最大值。应该是有一个群发的人数限制的,有空的时候可以测试一下。
6# Carl   said this at 2010-09-29 13:39 : [Quote]
请问
FRIEND_LIST_URL = "http://www.kaixin001.com/interface/suggestfriend.php"
是如何获取到的?
我现在想对我的图片进行操作,但是找不到这样的返回图片的JSON格式的URL。
我的邮箱是sd5921054@163.com,谢谢
7# Admin   said this at 2010-09-29 14:01 : [Quote]
Quoted Carl:

请问
FRIEND_LIST_URL = "http://www.kaixin001.com/interface/suggestfriend.php"
是如何获取到的?
我现在想对我的图片进行操作,但是找不到这样的返回图片的JSON格式的URL。
我的邮箱是sd5921054@163.com,谢谢


如果你使用的是firefox浏览器,你可以安装firebug插件来监控每一次点击发送的GET或者POST请求,这样可以很方便的获得你所需要的内容。
8# Carl   said this at 2010-09-29 14:13 : [Quote]
没用过firebug,正在学习中呢
是使用跟踪窗口么?
我还没找到哪里可以监控POST请求
9# Admin   said this at 2010-09-29 14:25 : [Quote]
Quoted Carl:

没用过firebug,正在学习中呢
是使用跟踪窗口么?
我还没找到哪里可以监控POST请求


安装firefox后,点击工具-附加组件,安装firebug,安装后firefox右下角会有一个小虫子,点击之后选择“网络”块的内容就可以看到网络请求了,之后再在页面上进行操作看着网络请求那的变化就可以了。
10# Carl   said this at 2010-09-29 14:40 : [Quote]
以上操作已经做过了,仍然没有在点击“好友”的时候看到http://www.kaixin001.com/interface/suggestfriend.php 的URL信息。
点击“照片”时读到的URL也都不是JSON格式,难道开心的照片没有用JSON么。
11# Admin   said this at 2010-09-29 14:57 : [Quote]
Quoted Carl:

以上操作已经做过了,仍然没有在点击“好友”的时候看到http://www.kaixin001.com/interface/suggestfriend.php 的URL信息。
点击“照片”时读到的URL也都不是JSON格式,难道开心的照片没有用JSON么。


忘了告诉你的,那个获得好友列表的地址是在发送新消息的时候,输入好友字母出现推荐好友时会发送ajax请求,之后返回好友的json数据。
如果是“照片”的话,你找找开心网有没有通过ajax去请求“照片”内容的地方,否则的话只能通过点击“照片”获得返回的页面内容,去解析这个页面内容来获得你要的信息了。
12# Carl   said this at 2010-09-29 15:56 : [Quote]
终于点击照片时看到一条POST请求,POST list.php,URL是http://www.kaixin001.com/photo/list.php,在它的响应窗口里,有我要的JSON格式的图片信息。
但是这个URL放到脚本里读就不好用了,读到的信息都是0。
继续纠结&奋斗中
Name:  *   Remember me
E-mail:  *  Not public, supported by Gravator
Website:      Not required
Question:
4 + 0 = *
Content: