sns什么的我是一直無(wú)愛(ài)的,這次蛋疼寫(xiě)了個(gè)登錄開(kāi)心網(wǎng)(kaixin001)并向所有好友發(fā)送站內(nèi)消息的腳本。
開(kāi)心網(wǎng)在登錄的時(shí)候做了一些處理,并不傳原始密碼,從js分析到的結(jié)果是:登錄時(shí)會(huì)生成一個(gè)隨機(jī)的key,然后用這個(gè)key和原始密碼進(jìn)行xxtea加密,把加密后的結(jié)果再進(jìn)行sha1加密。之后post這個(gè)key以及加密后的密碼進(jìn)行登錄驗(yàn)證。
以下是很簡(jiǎn)陋的腳本內(nèi)容:
#coding: utf-8
開(kāi)心網(wǎng)操作腳本
author:
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.***.com/login/login_api.php
login_key_url = http://www.***.com/
friend_list_url = http://www.***.com/interface/suggestfriend.php
message_send_url = http://www.***.com/msg/post.php
login_key_re = re.compile(rnew\senlogin\('(.*?)')
class loginerror(exception):
登錄失敗拋出異常
class kaixin001user(object):
操作kaixin001,現(xiàn)有方法:
get_login_key - 獲得用戶(hù)訪(fǎng)問(wèn)登錄頁(yè)面時(shí)分配的加密key
get_rpassword - 獲得經(jīng)過(guò)xxtea以及sha1加密后的密碼
login - 登錄
get_friends_list - 獲得所有好友,返回字典格式
send_messages_to_all - 給所有好友發(fā)消息
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):
獲得登錄時(shí)候的加密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.***.com/
})
result = urllib2.urlopen(req).read()
# 登錄失敗
if errno in result:
raise loginerror(登錄失敗,請(qǐng)檢查用戶(hù)名或密碼)
print 用戶(hù) %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=''):
給所有好友發(fā)消息
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 消息發(fā)送成功
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實(shí)現(xiàn)(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')