Oauth的学习以及开发自助上课签到脚本

附上源码:                                 https://github.com/taka250/auto_checkin_skl_hdu

 

 

 

首先了解学习oauth的知识点

https://www.cnblogs.com/blowing00/p/4521135.html

http://cncc.bingj.com/cache.aspx?q=oauth2.0&d=4769308101185638&mkt=zh-CN&setlang=zh-CN&w=7GpK0TUY9vh41MBeGv6mo3zlrgdPnO7v     推荐这位老师写的文章

其中的授权码模式

(A)用户访问客户端,后者将前者导向认证服务器。

(B)用户选择是否给予客户端授权。

(C)假设用户给予授权,认证服务器将用户导向客户端事先指定的"重定向URI"(redirection URI),同时附上一个授权码。

(D)客户端收到授权码,附上早先的"重定向URI",向认证服务器申请令牌。这一步是在客户端的后台的服务器上完成的,对用户不可见。

(E)认证服务器核对了授权码和重定向URI,确认无误后,向客户端发送访问令牌(access token)和更新令牌(refresh token)。

A.用bp抓包第一个请求时

将我们导向了客户端指定的uri(保存在get参数中),响应头也将我们302到了实现指定的uri(ticket就是授权码)。前提是用户给予了授权,在此情况下是用rsa对用户名密码进行验证。

我还发现了请求中包含了state参数。研究了一下是防止csrf的

然后就是拿到token了

要注意你电进行了两次拿token两次设置cookie,第一个token无用(。。。)

 

登录加签到

首先学校的login.js里面

然后我看了下strEnc函数

/** 
* DES加密解密 
* @Copyright Copyright (c) 2006 
* @author Guapo 
* @see DESCore 
*/  
  
/* 
* encrypt the string to string made up of hex 
* return the encrypted string 
*/  
function strEnc(data,firstKey,secondKey,thirdKey){  
  
 var leng = data.length;  
 var encData = "";  

四个参数,第一个是用户名加密码加登录浏览器时的第一个页面中的某个元素的value(lt) 所以说还需要爬虫。

  明明是des加密说是rsa(-v-。。。。跟rsa半毛钱关系都没有。。。。之前还有人分析说这是rsa和des双重加密。。笑)本来想在python里解析js代码或者将js翻译为python后来发现一个配置很麻烦一个用esecjs库翻译的结果运行效率很慢显然来不及签到。那我就直接去写python的des加密脚本。等我学完des准备写的之后才想起来可以去github上找代码。。。(就当学习一次des)然后strEnc的三个参数等我看完原函数后了解了这个其实就是对结果进行三次加密des而已。附上我在github上找到的python代码https://github.com/twhiteman/pyDes  (网上的都是只能对一个64位进行加密需要我改写)巨折磨,这个2006年的js代码将四个字符扩充64位明文,而其他库都是八位进行一次加密,太烦了,我还是在本地启动一个node服务器来跑代码试试。

 

所以我挂了三个进程 qbot 和node服务器加密和python进行checkin

附上python server的代码

from flask import Flask, request
import Skl
import account


app = Flask(__name__)


@app.route('/', methods=["POST"])
def post_data():
    if request.get_json().get('message_type') == 'group' and request.get_json().get('group_id') == int(account.group):
        print(account.group)
        gid = request.get_json().get('group_id')
        uid = request.get_json().get('sender').get('user_id')
        message = request.get_json().get('raw_message')
        skl = Skl.Skl()
        if skl.check_num(message, uid, gid):
            skl.autocheck(message, account.user_1,
                          account.passwd_1, account.group)
            skl.autocheck(message, account.user_2,
                          account.passwd_2, account.group)
    return 'ok'


if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=8001)

和封装的Skl类

import requests
import re
import uuid
import time


class Skl(object):
    def check_num(self, message, uid, gid):
        if re.match(r'^[0-9]{4}$', message):
            self.code = message
            return 1
        return 0

    def spider(self):
        response = requests.get(
            'https://cas.hdu.edu.cn/cas/login?state=IFgP7G0QAG0UFHop0M4&service=https%3A%2F%2Fskl.hdu.edu.cn%2Fapi%2Fcas%2Flogin%3Fstate%3DIFgP7G0QAG0UFHop0M4%26index%3D')
        response.enconding = 'utf-8'
        setcookie = response.headers['Set-Cookie']
        p = r'JSESSIONID=([0-9A-Za-z]{1,})'
        cookie = re.search(p, setcookie).group(1)
        p = r'<input type="hidden" id="lt" name="lt" value="(LT-\d{1,}-[0-9A-Za-z]{1,}-cas)" />'
        lt = re.search(p, response.text).group(1)
        p = r'<input type="hidden" name="execution" value="([0-9A-Za-z]{1,})" />'
        execu = re.search(p, response.text).group(1)

        return lt, execu, cookie

    def to_node(self, lt, upwd):
        response = requests.get(
            'http://127.0.0.1:2022/?lt={0}&upwd={1}'.format(lt, upwd)).text
        return response

    def checkin(self, rsa, lt, execu, cookie_1, user, code,passwd):
        url_1 = 'https://cas.hdu.edu.cn/cas/login?state=IFgP7G0QAG0UFHop0M4&service=https%3A%2F%2Fskl.hdu.edu.cn%2Fapi%2Fcas%2Flogin%3Fstate%3DIFgP7G0QAG0UFHop0M4%26index%3D'
        data_1 = {
            'rsa': rsa,
            'ul': len(user),
            'pl': len(passwd),
            'lt': lt,
            'execution': execu,
            '_eventId': 'submit'}

        head_1 = {
            'Host': 'cas.hdu.edu.cn',
            'Origin': 'https://cas.hdu.edu.cn',
            'Upgrade-Insecure-Requests': '1',
            'Cookie': 'hdu_cas_un={0}; JSESSIONID={1}; Language=zh_CN'.format(user, cookie_1),
            'Accept-Encoding': 'gzip, deflate',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36',
            'Referer': 'https://cas.hdu.edu.cn/cas/login?state=IFgP7G0QAG0UFHop0M4&service=https%3A%2F%2Fskl.hdu.edu.cn%2Fapi%2Fcas%2Flogin%3Fstate%3DIFgP7G0QAG0UFHop0M4%26index%3D'}

        res = requests.post(url_1, data=data_1,
                            headers=head_1, allow_redirects=False)
        url_2 = res.headers['Location']
        p = r'CASTGC=([^;]+)'
        cookie_3 = re.search(p, res.headers['Set-Cookie']).group(1)

        head_2 = {
            'Host': 'skl.hdu.edu.cn',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36',
            'Upgrade-Insecure-Requests': '1',
            'Referer': 'https://cas.hdu.edu.cn/',
            'Accept-Language': 'zh-CN,zh;q=0.9'}

        url_3 = requests.get(url_2, headers=head_2,
                             allow_redirects=False).headers['Location']

        head_3 = {
            'Host': 'cas.hdu.edu.cn',
            'Upgrade-Insecure-Requests': '1',
            'Cookie': 'CASTGC={0};JSESSIONID={1}; Language=zh_CN'.format(cookie_3, cookie_1),
            'Accept-Encoding': 'gzip, deflate',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36',
            'Referer': 'https://cas.hdu.edu.cn/'}
        url_4 = requests.get(url_3, headers=head_3,
                             allow_redirects=False).headers['Location']

        head_4 = {
            'Host': 'skl.hdu.edu.cn',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36',
            'Upgrade-Insecure-Requests': '1',
            'Referer': 'https://cas.hdu.edu.cn/',
            'Accept-Language': 'zh-CN,zh;q=0.9'}
        token = requests.get(url_4, headers=head_4,
                             allow_redirects=False).headers['X-Auth-Token']

        t = int(time.time()*1000)
        url_5 = 'https://skl.hdu.edu.cn/api/checkIn/code-check-in?userid={0}&code={1}&latitude=30.31958&longitude=120.3391&t={2}'.format(
            user, code, t)
        id = str(uuid.uuid1())
        head_5 = {
            'Host': 'skl.hdu.edu.cn',
            'X-Auth-Token': token,
            'Skl-Ticket': id,
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36',
            'Origin': 'https://skl.hduhelp.com',
            'Referer': 'https://cas.hdu.edu.cn/',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Accept-Encoding': 'gzip, deflate'}
        res = requests.get(url_5, headers=head_5).text
        p = '\"msg\":\"(.+)\"'
        msg = re.search(p, res).group(1)
        return(msg)

    def autocheck(self, message, user, passwd, group):
        secret = []
        secret = self.spider()
        lt = secret[0]
        execu = secret[1]
        cookie = secret[2]
        rsa = self.to_node(lt, user+passwd)
        result = 'hdu账号为'+user+'的返回消息:' + \
            self.checkin(rsa, lt, execu, cookie, user, message,passwd)
        requests.get(
            url='http://127.0.0.1:5700/send_group_msg?group_id={0}&message={1}'.format(group, result))

最后的checkin还涉及到了uuid和时间戳。

个人感觉并不会对uuid进行校验,毕竟只是近似于无法重复的数字

附上源码:                                 https://github.com/taka250/auto_checkin_skl_hdu

posted @ 2022-04-28 20:35  Kabuto_taka  阅读(79)  评论(0编辑  收藏  举报