某音x-gorgon算法python版本

安装好环境可以直接运行测试。
抖音方面有需要交流的可以加我q/v 779226574

# coding:utf-8

import random
import urllib.parse
from time import time
from hashlib import md5
from copy import deepcopy
from random import choice

def check_log(temp_list):
    tmp = []
    for item in temp_list:
        tmp.append(hex(item))
    print('长度:{}  内容:{}\n'.format(len(tmp), tmp))

def hex_string(num):
    tmp_string = hex(num)[2:]
    if len(tmp_string) < 2:
        tmp_string = '0' + tmp_string
    return tmp_string

def reverse(num):
    tmp_string = hex(num)[2:]
    if len(tmp_string) < 2:

        tmp_string = '0' + tmp_string
    return int(tmp_string[1:] + tmp_string[:1], 16)

def RBIT(num):
    result = ''
    tmp_string = bin(num)[2:]
    while len(tmp_string) < 8:
        tmp_string = '0' + tmp_string
    for i in range(0, 8):
        result = result + tmp_string[7 - i]
    return int(result, 2)

class XG:
    def __init__(self, debug):
        self.length = 0x14
        self.debug = debug
        self.hex_510 = [0x55, 0x00, 0x50, choice(range(0, 0xFF)), 0x32, 0xFA, 0x00, 8 * choice(range(0, 0x1F))]

    def addr_3F4(self):
        tmp = ''
        hex_3F4 = []
        for i in range(0x0, 0x100):
            hex_3F4.append(i)
        for i in range(0, 0x100):
            if i == 0:
                A = 0
            elif tmp:
                A = tmp
            else:
                A = hex_3F4[i - 1]
            B = self.hex_510[i % 0x8]
            if A == 0x55:
                if i != 1:
                    if tmp != 0x55:
                        A = 0
            C = A + i + B
            while C >= 0x100:
                C = C - 0x100
            if C < i:
                tmp = C
            else:
                tmp = ''
            D = hex_3F4[C]
            hex_3F4[i] = D
        return hex_3F4

    def initial(self, debug, hex_3F4):
        tmp_add = []
        tmp_hex = deepcopy(hex_3F4)
        for i in range(self.length):
            A = debug[i]
            if not tmp_add:
                B = 0
            else:
                B = tmp_add[-1]
            C = hex_3F4[i + 1] + B
            while C >= 0x100:
                C = C - 0x100
            tmp_add.append(C)
            D = tmp_hex[C]
            tmp_hex[i + 1] = D
            E = D + D
            while E >= 0x100:
                E = E - 0x100
            F = tmp_hex[E]
            G = A ^ F
            debug[i] = G
        return debug

    def calculate(self, debug):
        for i in range(self.length):
            A = debug[i]
            B = reverse(A)
            C = debug[(i + 1) % self.length]
            D = B ^ C
            E = RBIT(D)
            F = E ^ self.length
            G = ~F
            while G < 0:
                G += 0x100000000
            H = int(hex(G)[-2:], 16)
            debug[i] = H
        return debug

    def main(self):
        result = ''
        for item in self.calculate(self.initial(self.debug, self.addr_3F4())):
            result = result + hex_string(item)
        return '0300{}{}0000{}'.format(hex_string(self.hex_510[7]), hex_string(self.hex_510[3]), result)

def X_Gorgon(url, data, cookie, model='utf-8'):
    gorgon = []
    Khronos = hex(int(time()))[2:]
    url_md5 = md5(bytearray(url, 'utf-8')).hexdigest()
    for i in range(0, 4):
        gorgon.append(int(url_md5[2 * i: 2 * i + 2], 16))
    if data:
        if model == 'utf-8':
            data_md5 = md5(bytearray(data, 'utf-8')).hexdigest()
            for i in range(0, 4):
                gorgon.append(int(data_md5[2 * i: 2 * i + 2], 16))
        elif model == 'octet':
            data_md5 = md5(data).hexdigest()
            for i in range(0, 4):
                gorgon.append(int(data_md5[2 * i: 2 * i + 2], 16))
    else:
        for i in range(0, 4):
            gorgon.append(0x0)
    if cookie:
        cookie_md5 = md5(bytearray(cookie, 'utf-8')).hexdigest()
        for i in range(0, 4):
            gorgon.append(int(cookie_md5[2 * i: 2 * i + 2], 16))
    else:
        for i in range(0, 4):
            gorgon.append(0x0)
    for i in range(0, 4):
        gorgon.append(0x0)
    for i in range(0, 4):
        gorgon.append(int(Khronos[2 * i: 2 * i + 2], 16))
    return {'X-Gorgon': XG(gorgon).main(), 'X-Khronos': str(int(Khronos, 16))}

def get_post_xg(url,data,cookie=''):
    index = url.find('?') + 1
    params = url[index:]
    data_encode = urllib.parse.urlencode(data)
    x =  X_Gorgon(params, data_encode,cookie)
    ck = cookie if cookie else f"install_id={int(random.randrange(10 ** 11, 10 ** 12))}"
    headers = {
        "Cookie":ck ,
        "X-Khronos": x['X-Khronos'],
        "sdk-version": "1",
        "X-SS-REQ-TICKET": f"{x['X-Khronos']}233",
        "X-Gorgon": x['X-Gorgon'],
        "User-Agent": "com.ss.android.ugc.aweme/750 (Linux; U; Android 8.0.0; zh_CN; MIX 2; Build/OPR1.170623.027; Cronet/58.0.2991.0)",
        # "User-Agent": "ttnet okhttp/3.10.0.2",
        "Connection": "Keep-Alive",
    }
    return {
        'url':url,
        'data':data,
        'headers':headers
    }

def get_get_xg(url,data='',cookie=''):
    index = url.find('?') + 1
    params = url[index:]
    x =  X_Gorgon(params, data,cookie)
    ck = cookie if cookie else f"install_id={int(random.randrange(10 ** 11, 10 ** 12))}"
    headers = {
        "Cookie": ck,
        "X-Khronos": x['X-Khronos'],
        "sdk-version": "1",
        "X-SS-REQ-TICKET": f"{x['X-Khronos']}322",
        "X-Gorgon": x['X-Gorgon'],
        "User-Agent": "com.ss.android.ugc.aweme/690 (Linux; U; Android 5.1.1; zh_CN; HUAWEI MLA-AL10; Build/HUAWEIMLA-AL10; Cronet/58.0.2991.0)",
        # "User-Agent": "ttnet okhttp/3.10.0.2",
        # "Connection": "Keep-Alive",
    }
    return {
        'url':url,
        'data':data,
        'headers':headers
    }
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值