X-Gorgon算法参数获取(python版,附源码)

昨天在搜索X-Gorgon算法的时候看到一个老哥写的比较好,具体网址忘了,怕下次找不到了所以发在这里码住,稍作了点改动

代码仅用于学习探讨

  1 # -*- coding: utf-8 -*-
  2 """
  3 X-Gorgon加密算法python版
  4 1.时间戳转十六进制
  5 2.将时间戳排序俩次,
  6   a1 v3 是排序key
  7   sprintf(byte_102323F30, "%08x", a1);
  8   sprintf(byte_102323F3A, "%08x", v3);
  9 3.将url参数用MD5加密一次或俩次根据时间戳&运算
 10 4.将第一次排序结果写入前16位地址加一写入(从1插入),隔一位插入,前边拼a1
 11 5.将第二次排序结果写入后16位(从0插入)后边拼e1
 12 """
 13 import requests
 14 import hashlib
 15 import time
 16 
 17 byteTable1 = "D6 28 3B 71 70 76 BE 1B A4 FE 19 57 5E 6C BC 21 B2 14 37 7D 8C A2 FA 67 55 6A 95 E3 FA 67 78 ED 8E 55 33 89 A8 CE 36 B3 5C D6 B2 6F 96 C4 34 B9 6A EC 34 95 C4 FA 72 FF B8 42 8D FB EC 70 F0 85 46 D8 B2 A1 E0 CE AE 4B 7D AE A4 87 CE E3 AC 51 55 C4 36 AD FC C4 EA 97 70 6A 85 37 6A C8 68 FA FE B0 33 B9 67 7E CE E3 CC 86 D6 9F 76 74 89 E9 DA 9C 78 C5 95 AA B0 34 B3 F2 7D B2 A2 ED E0 B5 B6 88 95 D1 51 D6 9E 7D D1 C8 F9 B7 70 CC 9C B6 92 C5 FA DD 9F 28 DA C7 E0 CA 95 B2 DA 34 97 CE 74 FA 37 E9 7D C4 A2 37 FB FA F1 CF AA 89 7D 55 AE 87 BC F5 E9 6A C4 68 C7 FA 76 85 14 D0 D0 E5 CE FF 19 D6 E5 D6 CC F1 F4 6C E9 E7 89 B2 B7 AE 28 89 BE 5E DC 87 6C F7 51 F2 67 78 AE B3 4B A2 B3 21 3B 55 F8 B3 76 B2 CF B3 B3 FF B3 5E 71 7D FA FC FF A8 7D FE D8 9C 1B C4 6A F9 88 B5 E5"
 18 
 19 def encryption(url):
 20     """
 21     对参数全部挨个进行md5加密
 22     """
 23     obj = hashlib.md5()  # 先创建一个md5的对象
 24     # 写入要加密的字节
 25     obj.update(url.encode("UTF-8"))
 26     # 获取密文
 27     secret = obj.hexdigest()
 28     return secret.lower()
 29 
 30 def getXGon(url, stub, cookies):
 31     """
 32     通过请求数据获取XGon加密字符串
 33     """
 34     NULL_MD5_STRING = "00000000000000000000000000000000"
 35     sb = ""
 36     if len(url) < 1:
 37         sb = NULL_MD5_STRING
 38     else:
 39         sb = encryption(url)
 40     if len(stub) < 1:
 41         sb += NULL_MD5_STRING
 42     else:
 43         sb += stub
 44     if len(cookies) < 1:
 45         sb += NULL_MD5_STRING
 46     else:
 47         sb += encryption(cookies)
 48     # 这里没看太懂,先直接给-1试试
 49     index = -1
 50     # index = cookies.index("sessionid=")
 51     if index == -1:
 52         sb += NULL_MD5_STRING
 53     else:
 54         sessionid = cookies[index + 10:]
 55         if sessionid.__contains__(';'):
 56             endIndex = sessionid.index(';')
 57             sessionid = sessionid[:endIndex]
 58         sb += encryption(sessionid)
 59     return sb
 60 
 61 def input(timeMillis, inputBytes):
 62     """
 63     对时间戳和XGon加密串进行加工
 64     """
 65     result = []
 66     for i in range(4):
 67         if inputBytes[i] < 0:
 68             temp = hex(inputBytes[i]) + ''
 69             temp = temp[6:]
 70             result.append(temp)
 71         else:
 72             temp = hex(inputBytes[i]) + ''
 73             result.append(temp)
 74     for i in range(4):
 75         result.append("0")
 76     for i in range(4):
 77         if inputBytes[i + 32] < 0:
 78             result.append(hex(inputBytes[i + 32]) + '')[6:]
 79         else:
 80             result.append(hex(inputBytes[i + 32]) + '')
 81     for i in range(4):
 82         result.append("0")
 83     tempByte = hex(int(timeMillis)) + ""
 84     tempByte = tempByte.replace("0x", "")
 85     for i in range(4):
 86         a = tempByte[i * 2:2 * i + 2]
 87         result.append(tempByte[i * 2:2 * i + 2])
 88     for i in range(len(result)):
 89         result[i] = result[i].replace("0x", "")
 90     return result
 91 
 92 def initialize(data):
 93     """
 94     对加工后的数据进行转码,利用秘钥初始化
 95     """
 96     myhex = 0
 97     byteTable2 = byteTable1.split(" ")
 98     for i in range(len(data)):
 99         hex1 = 0
100         if i == 0:
101             hex1 = int(byteTable2[int(byteTable2[0], 16) - 1], 16)
102             byteTable2[i] = hex(hex1)
103             # byteTable2[i] = Integer.toHexString(hex1);
104         elif i == 1:
105             temp = int("D6", 16) + int("28", 16)
106             if temp > 256:
107                 temp -= 256
108             hex1 = int(byteTable2[temp - 1], 16)
109             myhex = temp
110             byteTable2[i] = hex(hex1)
111         else:
112             temp = myhex + int(byteTable2[i], 16)
113             if temp > 256:
114                 temp -= 256
115             hex1 = int(byteTable2[temp - 1], 16)
116             myhex = temp
117             byteTable2[i] = hex(hex1)
118         if hex1 * 2 > 256:
119             hex1 = hex1 * 2 - 256
120         else:
121             hex1 = hex1 * 2
122         hex2 = byteTable2[hex1 - 1]
123         result = int(hex2, 16) ^ int(data[i], 16)
124         data[i] = hex(result)
125     for i in range(len(data)):
126         data[i] = data[i].replace("0x", "")
127     return data
128 
129 def handle(data):
130     """
131     对转码后的数据进行替换
132     """
133     for i in range(len(data)):
134         byte1 = data[i]
135         if len(byte1) < 2:
136             byte1 += '0'
137         else:
138             byte1 = data[i][1] + data[i][0]
139         if i < len(data) - 1:
140             byte1 = hex(int(byte1, 16) ^ int(data[i + 1], 16)).replace("0x", "")
141         else:
142             byte1 = hex(int(byte1, 16) ^ int(data[0], 16)).replace("0x", "")
143         byte1 = byte1.replace("0x", "")
144         a = (int(byte1, 16) & int("AA", 16)) / 2
145         a = int(abs(a))
146         byte2 = ((int(byte1, 16) & int("55", 16)) * 2) | a
147         byte2 = ((byte2 & int("33", 16)) * 4) | (int)((byte2 & int("cc", 16)) / 4)
148         byte3 = hex(byte2).replace("0x", "")
149         if len(byte3) > 1:
150             byte3 = byte3[1] + byte3[0]
151         else:
152             byte3 += "0"
153         byte4 = int(byte3, 16) ^ int("FF", 16);
154         byte4 = byte4 ^ int("14", 16)
155         data[i] = hex(byte4).replace("0x", "")
156     return data
157 
158 def xGorgon(timeMillis, inputBytes):
159     """
160     时间戳和XGon加密字符串处理,生成最终X-Gorgon参数
161     """
162     data1 = []
163     data1.append("3")
164     data1.append("61")
165     data1.append("41")
166     data1.append("10")
167     data1.append("80")
168     data1.append("0")
169     data2 = input(timeMillis, inputBytes)
170     data2 = initialize(data2)
171     data2 = handle(data2)
172     for i in range(len(data2)):
173         data1.append(data2[i])
174 
175     xGorgonStr = ""
176     for i in range(len(data1)):
177         temp = data1[i] + ""
178         if len(temp) > 1:
179             xGorgonStr += temp
180         else:
181             xGorgonStr += "0"
182             xGorgonStr += temp
183     return xGorgonStr
184 
185 def strToByte(str):
186     """
187     对XGon加密字符串进行二次转换
188     """
189     length = len(str)
190     str2 = str
191     bArr = []
192     i = 0
193     while i < length:
194         # bArr[i/2] = b'\xff\xff\xff'+(str2hex(str2[i]) << 4+str2hex(str2[i+1])).to_bytes(1, "big")
195         a = str2[i]
196         b = str2[1 + i]
197         c = ((str2hex(a) << 4) + str2hex(b))
198         bArr.append(c)
199         i += 2
200     return bArr
201 
202 def str2hex(s):
203     """
204     16进制处理
205     """
206     odata = 0
207     su = s.upper()
208     for c in su:
209         tmp = ord(c)
210         if tmp <= ord('9'):
211             odata = odata << 4
212             odata += tmp - ord('0')
213         elif ord('A') <= tmp <= ord('F'):
214             odata = odata << 4
215             odata += tmp - ord('A') + 10
216     return odata
217 
218 if __name__ == "__main__":
219     # 抓包获取请求url
220     url = "https://xxxxx"
221     ts = str(time.time()).split(".")[0]
222     # ts = "1638959449"
223     # _rticket = str(time.time() * 1000).split(".")[0]
224     params = url[url.index('?') + 1:]
225     # 下面两个参数按需填写
226     STUB = ""
227     cookies = ""
228     # 获取gorgon参数
229     s = getXGon(params, STUB, cookies)
230     gorgon = xGorgon(ts, strToByte(s))
231     # 拼请求头
232     headers = {
233         "X-Gorgon": gorgon,
234         # "X-SS-REQ-TICKET": "1585711173953",
235         "X-Khronos": ts,
236         # "sdk-version": "1",
237         # "Accept-Encoding": "gzip",
238         # "X-SS-REQ-TICKET": _rticket,
239         # "User-Agent": "",
240         # "Host": "aweme.snssdk.com",
241         # "Cookie": cookies,
242         # "Connection": "Keep-Alive",
243         # "x-tt-token": "00080ab789c0bf0519740314c59de87d8ace96d49d8ab2afd7a0f09cba0911612f99baf92acae289860e0f84ffd97fc2c344"
244     }
245     resp = requests.get(url, headers=headers)
246     print(resp.text)
247  

 

posted @ 2021-12-09 15:38  汪凡  阅读(1351)  评论(0编辑  收藏  举报