-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
292 lines (243 loc) · 7.64 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# 2023/12/04 nzg
# try to login automatically because of the brilliant login system
import requests
import time
from logging import *
import json
import re
import hmac
import hashlib
import urllib.parse
import tomllib
class User:
def __init__(self, name, password):
self._name = name
self._password = password
@property
def name(self):
return self._name
@property
def password(self):
return self._password
class College:
def __init__(self, host, alpha, mystery_number):
self.host = host
self.alpha = alpha
self.mystery_number = mystery_number
def read_config(path: str = './config.toml') -> tuple[User, College]:
with open(path, 'rb') as f:
config = tomllib.load(f)
user = User(**config['authentication'])
coll = College(**config['general'])
return user, coll
# steps:
# 1. get the challenge
# 2. meet the challenge
# 3. send the login request
def get_challenge(name: str, pswd: str, college: College) -> dict[str, str]:
timestamp = int(time.time() * 1000)
mystery = college.mystery_number
url = (
f"{college.host}/cgi-bin/get_challenge?"
f"callback=jQuery{mystery}_{timestamp}&username={name}&_={timestamp}"
)
try:
info(f'Req url: {url}')
resp = requests.get(url)
info(f'Res: {resp.text}')
except requests.RequestException as e:
error(f"HTTP request failed: {e}")
exit(1)
response_format = re.compile(r"(jQuery\d+_\d+)\((.*)\)")
match = response_format.match(resp.text)
if not match:
error("Regex match failed")
exit(1)
prefix = match.group(1)
response_content = match.group(2)
info(response_content)
try:
response_json = json.loads(response_content)
except json.decoder.JSONDecodeError:
error("JSON decode error: bad format")
exit(1)
if not isinstance(response_json, dict):
error("JSON decode error: not a dict")
exit(1)
# Add more info to response
response_json["username"] = name
response_json["password"] = pswd
response_json["prefix"] = prefix
response_json["timestamp"] = timestamp
return response_json
# write by good GPT ♥
def calculate_hmac_md5(key, message):
# Ensure key and message are bytes
byte_key = key.encode() if isinstance(key, str) else key
byte_message = message.encode() if isinstance(message, str) else message
# Create a new HMAC object using MD5 hash function
hmac_md5 = hmac.new(byte_key, byte_message, hashlib.md5)
# Return the HMAC in hexadecimal format
return hmac_md5.hexdigest()
def calc_chcksum(context: dict[str, str], alphabet) -> None:
"""
Calculate the checksum for authentication.
This function updates the context dictionary with calculated info, checksum,
and hashed password.
"""
# Extract necessary information from context
token = context["challenge"]
username = context["username"]
passwd_plain = context["password"]
client_ip = context["client_ip"]
ac_id = "1"
magic_n = "200"
magic_type = "1"
# Calculate hashed password
passwd_md5 = calculate_hmac_md5(token, passwd_plain)
info(f"Password MD5: {passwd_md5}")
# Calculate additional info
_info = calc_info(
{
"username": username,
"password": passwd_plain,
"ip": client_ip,
"acid": ac_id,
"enc_ver": "srun_bx1",
},
token,
alphabet,
)
info(f"Info: {_info}")
# Compose and calculate the final checksum
contents = ["", username, passwd_md5, ac_id, client_ip, magic_n, magic_type, _info]
chkstr = token.join(contents)
checksum = hashlib.sha1(chkstr.encode()).hexdigest()
info(f"Checksum: {checksum}")
# Update context with calculated values
context["info"] = _info
context["chksum"] = checksum
context["password"] = f"{{MD5}}{passwd_md5}"
def calc_info(msg: dict, token: str, alpha: str = ''):
str_msg = json.dumps(msg, separators=(",", ":"))
info(f"str_msg: {str_msg}")
raw_str = xEncode(str_msg, token)
info(f"raw_str: {raw_str}")
encoded_string = jq_b64(raw_str, alpha)
return f"{{SRBX1}}{encoded_string}"
def byte2arr(msg: str, flag: bool) -> list[int]:
"""Corresponds to `s()` in source code"""
mb = bytearray(msg, "utf-8")
origin_len = len(mb)
result = [int.from_bytes(mb[i : i + 4], "little") for i in range(0, len(mb), 4)]
if flag:
result.append(origin_len)
return result
def arr2byte(a: list[int], b: bool) -> bytearray:
"""Corresponds to `l()` in source code"""
result = bytearray()
origin_len = a[-1] if b else len(a) * 4
a = a[:-1] if b else a
for i in range(len(a)):
result.extend(a[i].to_bytes(min(4, origin_len - i * 4), "little"))
return result
def xEncode(msg: str, key: str) -> bytearray:
"""
🧐 honestly, I don't see what it is
"""
if not len(msg):
return ""
v = byte2arr(msg, True)
k = byte2arr(key, False)
if len(k) < 4:
k = k + [0] * (4 - len(k))
n = len(v) - 1
z = v[n]
y = v[0]
ff = 0xFFFFFFFF
c = 0x9E3779B9
m = 0
e = 0
p = 0
q = 6 + 52 // (n + 1)
d = 0
while 0 < q:
d = d + c & ff
e = d >> 2 & 3
p = 0
while p < n:
y = v[p + 1]
m = z >> 5 ^ y << 2
m += (y >> 3 ^ z << 4) ^ (d ^ y)
m += k[(p & 3) ^ e] ^ z
v[p] = v[p] + m & ff
z = v[p]
p += 1
y = v[0]
m = z >> 5 ^ y << 2
m += (y >> 3 ^ z << 4) ^ (d ^ y)
m += k[(p & 3) ^ e] ^ z
v[n] = v[n] + m & ff
z = v[n]
q -= 1
return arr2byte(v, False)
def meet_challenge(context: dict[str, str], coll: College) -> str:
calc_chcksum(context, coll.alpha)
# Prepare URL parameters
params = {
"callback": context["prefix"],
"action": "login",
"username": context["username"],
"password": context["password"],
"ac_id": "1",
"ip": context["client_ip"],
"chksum": context["chksum"],
"info": context["info"],
"n": "200",
"type": "1",
"os": "Linux",
"name": "Linux",
"double_stack": "0",
"_": int(time.time() * 1000),
}
# Construct URL
base_url = f"{coll.host}/cgi-bin/srun_portal"
url = f"{base_url}?{urllib.parse.urlencode(params)}"
info(f"final url: {url}")
return url
def login(url: str):
response = requests.get(url)
info(response.text)
def jq_b64(msg: bytearray, alpha: str=''):
"""
The algorithm of jQuery failed to consider unicode
leading to a wrong result, so we have to calculate this
bad result
"""
stupid_alphabet = (
alpha
if alpha
else "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
)
result = ""
for i in range(0, len(msg), 3):
a = msg[i]
b = msg[i + 1] if i + 1 < len(msg) else 0
c = msg[i + 2] if i + 2 < len(msg) else 0
result += stupid_alphabet[(a >> 2) & 0x3F]
result += stupid_alphabet[(((a & 3) << 4) | (b >> 4)) & 0x3F]
result += stupid_alphabet[(((b & 15) << 2) | (c >> 6)) & 0x3F]
result += stupid_alphabet[c & 63]
padding = len(msg) % 3
if padding == 1:
result = result[:-2] + "=="
elif padding == 2:
result = result[:-1] + "="
return result
if __name__ == "__main__":
# decomment to trace
# basicConfig(level=INFO)
user, coll = read_config()
ctx = get_challenge(user.name, user.password, coll)
url = meet_challenge(ctx, coll)
login(url)