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

2019-08-02 20:22栏目:编程学习

SNS什么的自身是一向无爱的,此番蛋疼写了个登入高兴网(kaixin001)并向装有老铁发送站内新闻的台本。

python完结的报到和操作欢畅网脚本分享,python完毕脚本共享

SNS什么的自个儿是一贯无爱的,这一次蛋疼写了个登陆快乐网(kaixin001)并向全体好朋友发送站内新闻的脚本。

喜悦网在签到的时候做了有的拍卖,并不传原来密码,从js剖析到的结果是:登陆时会生成三个随机的key,然后用这一个key和原本密码进行xxtea加密,把加密后的结果再举行sha1加密。之后post这些key以及加密后的密码举办登陆验证。

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

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

Author: [email protected]
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"newsEnLogin('(.*?)'")

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):

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')

欢乐网在登入的时候做了部分拍卖,并不传原来密码,从js深入分析到的结果是:登陆时会生成几个即兴的key,然后用那几个key和原始密码举行xxtea加密,把加密后的结果再开始展览sha1加密。之后post这么些key以及加密后的密码举办登陆验证。

最简便易行的python脚本,文件操作,助

思路应该是开采t2文件,收取个中的正则表明式,再打开t1文件,用正则表明式去相称,相配成功的都放置result里面,最终输出result字段不就行了嘛。  

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

python脚本自动登陆单点登陆系统

单点登陆(Single Sign On)简称为 SSO,是当前比较盛行的商家业务整合的消除方案之一。SSO的定义是在多少个利用系统中,用户只须求登入一次就足以访谈具备相互信任的使用系统。它满含能够将此次主要的登陆映射到另外使用中用于同八个用户的报到的机制。 当用户率先次访谈应用连串1的时候,因为还尚未登陆,会被引导到表明种类中实行登入;依据用户提供的记名消息,认证系统进行身份效验,借使由此功用q应该回到给用户三个验证的证据--ticket;用户再拜见别的应用的时候就能将以此ticket带上os作为友好评释的凭据,应用系统接受到央求之后会把ticket送到表达系列开始展览成效,检查ticket的合法性假如经过作用,用户就足以在毫无再行登入的情形下访谈应用系统2和动用系统3了1796  

SNS什么的笔者是一贯无爱的,此番蛋疼写了个登入快乐网(kaixin001)并向装有好友发...

#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"newsEnLogin('(.*?)'")

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):

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')

版权声明:本文由威尼斯人app发布于编程学习,转载请注明出处:python实现的登录和操作开心网脚本分享