forked from Ankermgmt/ankermake-m5-protocol
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmegajank.py
294 lines (200 loc) · 8.65 KB
/
megajank.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
293
294
import Cryptodome.Util.Padding
import Cryptodome.Cipher.AES
import tinyec.registry
import tinyec.ec
from libflagship.util import b64e
# mqtt aes handling
def aes_cbc_encrypt(msg, key, iv):
aes = Cryptodome.Cipher.AES.new(key=key, iv=iv, mode=Cryptodome.Cipher.AES.MODE_CBC)
pmsg = Cryptodome.Util.Padding.pad(msg, block_size=16)
cmsg = aes.encrypt(pmsg)
return cmsg
def aes_cbc_decrypt(cmsg, key, iv):
aes = Cryptodome.Cipher.AES.new(key=key, iv=iv, mode=Cryptodome.Cipher.AES.MODE_CBC)
pmsg = aes.decrypt(cmsg)
msg = Cryptodome.Util.Padding.unpad(pmsg, block_size=16)
return msg
def mqtt_aes_encrypt(msg, key, iv=b"3DPrintAnkerMake"):
return aes_cbc_encrypt(msg, key, iv)
def mqtt_aes_decrypt(cmsg, key, iv=b"3DPrintAnkerMake"):
return aes_cbc_decrypt(cmsg, key, iv)
# mqtt checksum handling
def mqtt_checksum_remove(payload):
if xor_bytes(payload) != 0:
# raise ...
print(f"MALFORMED MESSAGE: {payload}")
return payload[:-1]
def mqtt_checksum_add(msg):
return msg + bytes([xor_bytes(msg)])
def xor_bytes(data):
s = 0
for x in data:
s ^= x
return s
# Elliptic-Curve Diffie-Hellman (ECDH) for password exchange in login api
anker_ec_v1_curve = tinyec.registry.get_curve("secp256r1")
anker_ec_v1_public_key = tinyec.ec.Keypair(anker_ec_v1_curve, pub=tinyec.ec.Point(
anker_ec_v1_curve,
0xC5C00C4F8D1197CC7C3167C52BF7ACB054D722F0EF08DCD7E0883236E0D72A38,
0x68D9750CB47FA4619248F3D83F0F662671DADC6E2D31C2F41DB0161651C7C076
))
def ec_pubkey_export(key):
return f"04{key.x:064x}{key.y:064x}"
def ecdh_encrypt_login_password(password, partner=anker_ec_v1_public_key):
# make fresh EC key
eckey = tinyec.ec.make_keypair(anker_ec_v1_curve)
# Create ECDH instance for new key
ecdh = tinyec.ec.ECDH(eckey)
# Perform ECDH with partner key
secret = ecdh.get_secret(partner)
# Extract result as hex, use as AES key
key = bytes.fromhex(hex(secret.x)[2:].zfill(64))
# AES IV is just the first half of the key
iv = key[:16]
# Encrypt password with AES, return base64 encoding
return ec_pubkey_export(eckey.pub), b64e(aes_cbc_encrypt(password, key, iv))
# pppp init string decoder
def pppp_decode_initstring_raw(input):
shuffle = [0x49, 0x59, 0x43, 0x3d, 0xb5, 0xbf, 0x6d, 0xa3, 0x47, 0x53,
0x4f, 0x61, 0x65, 0xe3, 0x71, 0xe9, 0x67, 0x7f, 0x02, 0x03,
0x0b, 0xad, 0xb3, 0x89, 0x2b, 0x2f, 0x35, 0xc1, 0x6b, 0x8b,
0x95, 0x97, 0x11, 0xe5, 0xa7, 0x0d, 0xef, 0xf1, 0x05, 0x07,
0x83, 0xfb, 0x9d, 0x3b, 0xc5, 0xc7, 0x13, 0x17, 0x1d, 0x1f,
0x25, 0x29, 0xd3, 0xdf]
olen = len(input) >> 1
q = 0
output = [0] * olen
for q in range(olen):
xor = 0x39 ^ shuffle[q % 0x36]
for p in range(q+1):
xor ^= output[p]
l = input[q*2+1] - 0x41
h = input[q*2+0] - 0x41
output[q] = xor ^ (l + (h << 4))
return bytes(output)
def pppp_decode_initstring(input):
res = pppp_decode_initstring_raw(input.encode())
return res.decode().rstrip(",").split(",")
# pppp crypto curses
PPPP_SEED = "EUPRAKM"
PPPP_SHUFFLE = [
[0x95, 0xe5, 0x61, 0x97, 0x83, 0x0d, 0xa7, 0xf1],
[0xd3, 0x05, 0x95, 0x8b, 0xdf, 0x13, 0x6d, 0xef],
[0x07, 0x61, 0x0d, 0x6d, 0x7f, 0x67, 0x17, 0x2b],
[0xc1, 0xb5, 0x13, 0x0b, 0xdf, 0x8b, 0x49, 0x3b],
[0x7f, 0x07, 0xd3, 0x02, 0x6d, 0x2f, 0x13, 0xc5],
[0x6d, 0x3d, 0xfb, 0x0d, 0x0b, 0x29, 0xe9, 0x4f],
[0x89, 0x2f, 0xe3, 0xe9, 0x0d, 0x83, 0x6d, 0xe5],
[0x07, 0x53, 0x8b, 0x25, 0x95, 0x47, 0x1f, 0x29],
]
def crypto_decurse(input, key, shuffle):
a, b, c, d = (1, 3, 5, 7)
for q in key:
q = ord(q)
a, b, c, d = [
shuffle[b + (q % a) & 7][q + (c % d) & 7],
shuffle[c + (q % b) & 7][q + (d % a) & 7],
shuffle[d + (q % c) & 7][q + (a % b) & 7],
shuffle[a + (q % d) & 7][q + (b % c) & 7],
]
output = [0] * len(input)
for p, x in enumerate(input):
output[p] = x ^ (a ^ b ^ c ^ d)
a, b, c, d = [
shuffle[b + (x % a) & 7][x + (c % d) & 7],
shuffle[c + (x % b) & 7][x + (d % a) & 7],
shuffle[d + (x % c) & 7][x + (a % b) & 7],
shuffle[a + (x % d) & 7][x + (b % c) & 7],
]
return output
def crypto_curse(input, key, shuffle):
a, b, c, d = (1, 3, 5, 7)
for q in key:
q = ord(q)
a, b, c, d = [
shuffle[b + (q % a) & 7][q + (c % d) & 7],
shuffle[c + (q % b) & 7][q + (d % a) & 7],
shuffle[d + (q % c) & 7][q + (a % b) & 7],
shuffle[a + (q % d) & 7][q + (b % c) & 7],
]
output = [0] * (len(input) + 4)
for p, x in enumerate(input):
x = output[p] = x ^ (a ^ b ^ c ^ d)
a, b, c, d = [
shuffle[b + (x % a) & 7][x + (c % d) & 7],
shuffle[c + (x % b) & 7][x + (d % a) & 7],
shuffle[d + (x % c) & 7][x + (a % b) & 7],
shuffle[a + (x % d) & 7][x + (b % c) & 7],
]
for p in range(len(input), len(input)+4):
x = output[p] = a ^ b ^ c ^ d ^ 0x43
a, b, c, d = [
shuffle[b + (x % a) & 7][x + (c % d) & 7],
shuffle[c + (x % b) & 7][x + (d % a) & 7],
shuffle[d + (x % c) & 7][x + (a % b) & 7],
shuffle[a + (x % d) & 7][x + (b % c) & 7],
]
return output
def crypto_decurse_string(input):
output = crypto_decurse(input, key=PPPP_SEED, shuffle=PPPP_SHUFFLE)
if output[-4:] != [0x43, 0x43, 0x43, 0x43]:
raise ValueError("Invalid decode")
return bytes(output[:-4])
def crypto_curse_string(input):
output = crypto_curse(input, key=PPPP_SEED, shuffle=PPPP_SHUFFLE)
return bytes(output)
# pppp crypto curse, older(?) version
#
# the simple_* functions have been adapted from https://github.com/fbertone/lib32100/issues/7
#
PPPP_SIMPLE_SEED = b"SSD@cs2-network."
PPPP_SIMPLE_SHUFFLE = [
0x7C, 0x9C, 0xE8, 0x4A, 0x13, 0xDE, 0xDC, 0xB2, 0x2F, 0x21, 0x23, 0xE4, 0x30, 0x7B, 0x3D, 0x8C,
0xBC, 0x0B, 0x27, 0x0C, 0x3C, 0xF7, 0x9A, 0xE7, 0x08, 0x71, 0x96, 0x00, 0x97,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, 0xE2, 0xBF, 0x10, 0xD7, 0x75, 0x64, 0x57, 0x76, 0xF3, 0x55, 0xCD, 0xD0, 0xC8,
0x18, 0xE6, 0x36, 0x41, 0x62, 0xCF, 0x99, 0xF2, 0x32, 0x4C, 0x67, 0x60, 0x61, 0x92, 0xCA, 0xD3,
0xEA, 0x63, 0x7D, 0x16, 0xB6, 0x8E, 0xD4, 0x68, 0x35, 0xC3, 0x52, 0x9D, 0x46, 0x44, 0x1E, 0x17,
]
def simple_hash(seed):
hash = [0] * 4
for i in range(len(seed)):
hash[0] = hash[0] ^ seed[i]
hash[1] = hash[1] + seed[i] // 3
hash[2] = hash[2] - seed[i]
hash[3] = hash[3] + seed[i]
return hash[::-1]
def _lookup(hash, b):
index = hash[b & 0x3] + b
return PPPP_SIMPLE_SHUFFLE[index % len(PPPP_SIMPLE_SHUFFLE)]
def simple_decrypt(seed, input):
hash = simple_hash(seed)
output = [0] * len(input)
output[0] = input[0] ^ _lookup(hash, 0)
for i in range(1, len(input)):
output[i] = input[i] ^ _lookup(hash, input[i-1])
return bytes(output)
def simple_encrypt(seed, input):
hash = simple_hash(seed)
output = [0] * len(input)
output[0] = input[0] ^ _lookup(hash, 0)
for i in range(1, len(input)):
output[i] = input[i] ^ _lookup(hash, output[i-1])
return bytes(output)
def simple_decrypt_string(input):
return simple_decrypt(PPPP_SIMPLE_SEED, input)
def simple_encrypt_string(input):
return simple_encrypt(PPPP_SIMPLE_SEED, input)
if __name__ == "__main__":
print(simple_encrypt_string(b"foo"))
print(simple_decrypt_string(simple_encrypt_string(b"foo")))