-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecode.js
More file actions
306 lines (278 loc) · 9.35 KB
/
Copy pathdecode.js
File metadata and controls
306 lines (278 loc) · 9.35 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
/**
* ⚠️ PRIVACY RESEARCH USE ONLY
* For authorized testing environments that comply with all applicable laws.
* See: https://github.com/botswin/qrator-Privacy-Research/blob/main/DISCLAIMER.md
*/
// @ts-check
const assert = require('assert');
const crypto = require('crypto');
/**
* hpFrom: Same as in encode – encode a JS string into a Buffer via UTF-8.
*
* @param {string} s - Input string.
* @returns {Buffer} - Buffer of UTF-8 bytes.
*/
function hpFrom(s) {
const encoder = new TextEncoder();
const uint8 = encoder.encode(s);
return Buffer.from(uint8);
}
/**
* Kt: Same as in encode – percent-encode non-ASCII, then XOR with r (key).
*
* @param {string} t - Input string (should be percent-encoded already).
* @param {string} r - Key string used for XOR.
* @returns {string} - XORed result.
*/
function Kt(t, r) {
const c = r.length;
let W = '';
t.split('').map((ch) => {
const n = ch.charCodeAt(0);
if (n > 127) {
let hex = n.toString(16);
const i = hex.length;
for (let u = 0; u < 4 - i; u++) {
hex = '0' + hex;
}
W += '%' + hex;
} else {
W += ch;
}
});
return W.split('')
.map((ch, idx) => {
return String.fromCharCode(ch.charCodeAt(0) ^ r[idx % c].charCodeAt(0));
})
.join('');
}
/**
* Ot: Same as in encode – convert JS string into array of 32-bit words.
*
* @param {string} t - Input string.
* @returns {number[]} - Array of 32-bit words.
*/
function Ot(t) {
const nArray = [];
// Initialize array length: (t.length >> 2)
nArray[(t.length >> 2) - 1] = undefined;
for (let i = 0; i < nArray.length; i++) {
nArray[i] = 0;
}
const e = 8 * t.length;
for (let r = 0; r < e; r += 8) {
nArray[r >> 5] |= (255 & t.charCodeAt(r / 8)) << r % 32;
}
return nArray;
}
/**
* yt: Same as in encode – take array of 32-bit words (t) and bit-length (r),
* compute MD5-based mixing, and return new array of 32-bit words [A,B,C,D].
*
* @param {number[]} t - Array of 32-bit words.
* @param {number} r - Bit-length (e.g. 8 * original string length, or 512 + ...).
* @returns {number[]} - Array of four 32-bit unsigned integers.
*/
function yt(t, r) {
const byteCount = Math.floor(r / 8);
const fullBytes = Buffer.alloc(t.length * 4);
for (let i = 0; i < t.length; i++) {
const word = t[i] >>> 0;
fullBytes[i * 4 + 0] = word & 0xff;
fullBytes[i * 4 + 1] = (word >>> 8) & 0xff;
fullBytes[i * 4 + 2] = (word >>> 16) & 0xff;
fullBytes[i * 4 + 3] = (word >>> 24) & 0xff;
}
const originalBytes = fullBytes.slice(0, byteCount);
const md5Digest = crypto.createHash('md5').update(originalBytes).digest();
const A = md5Digest.readUInt32LE(0);
const B = md5Digest.readUInt32LE(4);
const C = md5Digest.readUInt32LE(8);
const D = md5Digest.readUInt32LE(12);
return [A, B, C, D];
}
/**
* gt: Same as in encode – implements HMAC-MD5-like routine.
*
* @param {string} t - Input text.
* @param {string} r - Input key.
* @returns {string} - Result as raw binary string (not hex).
*/
function gt(t, r) {
const o = Ot(t);
let oCut = o;
// If o longer than 16 words, reduce
if (o.length > 16) {
oCut = yt(o, 8 * t.length);
}
const cArr = [];
const WArr = [];
// Prepare inner and outer pads
for (let n = 0; n < 16; n++) {
cArr[n] = 909522486 ^ oCut[n];
WArr[n] = 1549556828 ^ oCut[n];
}
// Compute inner hash
const eArr = yt(cArr.concat(Ot(r)), 512 + 8 * r.length);
// Compute outer hash, then pass to Ct
return Ct(yt(WArr.concat(eArr), 640));
}
/**
* Ct: Convert array of 32-bit words back into raw JS string (characters per byte).
*
* @param {number[]} t - Array of 32-bit words.
* @returns {string} - Raw string.
*/
function Ct(t) {
let n = '';
const e = 32 * t.length;
for (let r = 0; r < e; r += 8) {
n += String.fromCharCode((t[r >> 5] >>> r % 32) & 255);
}
return n;
}
/**
* bt: Compute MD5 of input text, return raw string via Ct(yt(...))
*
* @param {string} t - Input text.
* @returns {string} - Raw MD5 result as string.
*/
function bt(t) {
return Ct(yt(Ot(t), 8 * t.length));
}
/**
* Rt: Convert raw binary string into hex representation.
*
* @param {string} t - Input raw string.
* @returns {string} - Hex string (lowercase).
*/
function Rt(t) {
let o = '';
for (let n = 0; n < t.length; n++) {
const r = t.charCodeAt(n);
o += '0123456789abcdef'.charAt((r >>> 4) & 15) + '0123456789abcdef'.charAt(15 & r);
}
return o;
}
/**
* xt: Same as encode's key derivation:
* If r is provided: if n true, return raw gt(r, t); else return hex of gt(r, t).
* If r not provided: if n true, return raw bt(t); else return hex of bt(t).
*
* @param {string} t - Input text t.
* @param {string} [r] - Input key r (optional).
* @param {boolean} [n] - Flag for raw vs hex (optional).
* @returns {string} - Derived value (hex or raw).
*/
function xt(t, r, n) {
return r
? n
? gt(r, t)
: (function (t, r) {
return Rt(gt(t, r));
})(r, t)
: n
? bt(t)
: (function (t) {
return Rt(bt(t));
})(t);
}
/**
* parseCookie: Same as in encode – brute-force MD5 until prefix matches.
*
* @param {string} cookie - Cookie string containing e + '-' + nonce-hash fragment.
* @returns {{cookie: string, nonce: number}}
*/
function parseCookie(cookie) {
const [e, o, cFragment] = cookie.split('-');
let found = 0;
for (let W = 1; W < 1e7; W++) {
const h = crypto
.createHash('md5')
.update(e + W)
.digest('hex');
if (h.startsWith(cFragment)) {
found = W;
break;
}
}
return { cookie: e, nonce: found };
}
/**
* decryptValue: Reverse of encodeValue.
*
* @param {string} encodedValue - e.g. 'v1$67d9$TBNOVgoX...'
* @param {string} qrator_jsr_cookie - Same cookie that was used in encode.
* @returns {Object} - The original JS object that was JSON-stringified.
*/
function decryptValue(encodedValue, qrator_jsr_cookie) {
// 1. Split encodedValue into parts: ['v1', randomValue, base64Data]
const parts = encodedValue.split('$');
if (parts.length !== 3 || parts[0] !== 'v1') {
return encodedValue; // Invalid format, return as is
}
const randomValue = parts[1];
const base64Data = parts[2];
// 2. Recompute parseCookie to get e and nonce c
const { cookie: e, nonce: c } = parseCookie(qrator_jsr_cookie);
// 3. Derive the same hexKey that was used in encode:
// t = randomValue + e + c
const t = '' + randomValue + e + c;
// xt(t) without third arg → hex string
const hexKey = xt(t);
// 4. Base64→Buffer→UTF8 string to recover XORed result (XORedString)
const xorBuf = Buffer.from(base64Data, 'base64');
const xorString = xorBuf.toString('utf8');
// 5. Reverse the XOR step: produce W (percent-encoded string)
// W = for each char in xorString, XOR with hexKey char
let W = '';
for (let i = 0; i < xorString.length; i++) {
const chCode = xorString.charCodeAt(i);
const keyCode = hexKey.charCodeAt(i % hexKey.length);
W += String.fromCharCode(chCode ^ keyCode);
}
// 6. W now contains ASCII + "%XXXX" sequences for non-ASCII.
// We must convert every "%XXXX" into the corresponding Unicode char.
// For example, "%00e9" → codepoint 0x00e9 → "é".
const decoded = W.replace(/%([0-9a-fA-F]{4})/g, (_, hex) => {
return String.fromCharCode(parseInt(hex, 16));
});
// 7. decoded is the original JSON string of the value
let originalObj;
try {
originalObj = JSON.parse(decoded);
} catch (eParse) {
throw new Error('Failed to JSON.parse decrypted string: ' + eParse.message);
}
return originalObj;
}
// ==============================
// Example usage / self-test
// ==============================
if (require.main === module) {
// Provided qrator_jsr_cookie and randomValue + value from your example
const qrator_jsr_cookie =
'v2.0.1748810466.926.18e18377qIgMovMv|Fs09gz2IRGhcowVM|4CrqtgLCFQAhcZPdtEF4UmcG5qvqM0NDWEgx9sWCJJbUosO7K3tHBJ6YP+almohGILePl+OQ31T1OAKvDJjAyw==-zY1Wuybi/bcIc9dp3TTp+5CBfWI=-00';
const randomValue = '67d9';
const value = {
value: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36',
time: 1748810468.811,
};
function encodeValue(randomValue, qrator_jsr_cookie, value) {
const i = JSON.stringify(value);
const { cookie: o, nonce: c } = parseCookie(qrator_jsr_cookie);
const keyText = '' + randomValue + o + c;
const base64Encoded = hpFrom(Kt(i, xt(keyText))).toString('base64');
return 'v1$' + randomValue + '$' + base64Encoded;
}
const encodedValue = encodeValue(randomValue, qrator_jsr_cookie, value);
console.log('Encoded Value (for test):', encodedValue);
const decrypted = decryptValue(encodedValue, qrator_jsr_cookie);
console.log('Decrypted Object:', decrypted);
assert.deepStrictEqual(decrypted, value);
console.log('✅ Decryption matches original value!');
}
module.exports = {
parseCookie,
decryptValue,
};