-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
396 lines (351 loc) · 9.17 KB
/
main.js
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import { parse as parseHTML } from 'node-html-parser';
import { writeFileSync } from 'fs';
import vm from 'vm';
import parser from '@babel/parser';
import _generator from '@babel/generator';
const generator = _generator.default;
import _traverse from '@babel/traverse';
const traverse = _traverse.default;
import sha1 from 'js-sha1';
async function getPage(url) {
console.log('Fetching page.');
const response = await fetch(url);
if (!response.ok) {
console.log('Fetch page error', response.status);
}
const html = await response.text();
return html;
}
async function getCode(html, name) {
console.log('Parsing script tags.');
const scripts = parseHTML(html).querySelector('head').querySelectorAll('script');
for (let script of scripts) {
if (!script.rawAttributes.src) {
continue;
}
const src = script.rawAttributes.src;
if (src.indexOf(name) < 0) {
continue;
}
console.log('Fetching script.');
const response = await fetch(src);
if (!response.ok) {
console.log('Fetch script error', response.status);
throw new Error();
}
const code = await response.text();
return code;
}
console.log('Script not found.');
throw new Error();
}
function getProxy(obj, name) {
return new Proxy(obj, {
get(obj, prop) {
const value = obj[prop];
if (!value) {
console.log(`NO VALUE: ${name} -> ${prop}`);
}
return value;
},
});
}
function runCode(code) {
const context = getProxy(
{
self: getProxy({ webpackChunkof_vue: [] }, 'self'),
window: getProxy(
{
'navigator.userAgent': 'browser',
},
'window'
),
String: String,
parseInt: parseInt,
decodeURIComponent,
decodeURIComponent,
Date: Date,
Math: Math,
Proxy: Proxy,
log: [],
isNaN: isNaN,
},
'context'
);
vm.createContext(context);
vm.runInContext(code, context);
return context;
}
let shamsg = '';
function runFunction(fun) {
function importer(mod) {
if (mod === 89668) {
// sha
return function (msg) {
shamsg = msg;
return sha1(msg); // the real sha this time
};
} else if (mod === 858156) {
// get property
return function (obj, path, def) {
return obj[path] ? obj[path] : def;
};
} else if (mod === 944114) {
// empty object
return getProxy(
{
default: getProxy(
{
'getters.auth/authUserId': 123123,
},
'default'
),
},
'944114'
);
} else if (mod === 441153) {
// empty object
return getProxy(
{
A: getProxy(
{
'getters.auth/authUserId': 123123,
},
'default'
),
},
'441153'
);
} else {
console.log(`Unknown ${mod} module`);
}
}
importer.n = function (obj) {
return () => obj;
};
const param1 = getProxy({}, 'param1');
const param2 = getProxy({}, 'param2');
fun(param1, param2, importer);
return param2.A;
}
function getMath(ast) {
let math = null;
// 3. stop at every indentifier, if it's 'Math' then we've done
const matchIdentifier = {
Identifier(path) {
if (path.node.name === 'Math') {
math = this.current;
path.stop();
}
},
};
// 2. stop at every return statement, traverse subtree
const matchReturn = {
ReturnStatement(path) {
path.scope.traverse(path.node, matchIdentifier, { current: path });
},
};
// 1. traverse full code
traverse(ast, matchReturn);
return math;
}
function getNames(ast) {
const names = new Set();
// 2. stop at every member expression, except Math itself
const matchReturn = {
MemberExpression(path) {
const name = path.node.object.name;
if (!name || name === 'Math') {
return;
}
names.add(name);
// insert prefix
path.node.object.name = '_' + name;
},
};
// 1. traverse given tree
ast.traverse(matchReturn);
return names;
}
function insertProxy(ast, names) {
const lines = [];
lines.push(`
function _add(a, b) {
log.push(['add', a, b, a + b]);
return a + b;
}
function _sub(a, b) {
log.push(['sub', a, b, a - b]);
return a - b;
}
function _mod(a, b) {
// log.push(['mod', a, b, a % b]);
return a % b;
}
`);
for (let name of names.values()) {
lines.push(`
const _${name} = new Proxy({ obj: ${name} }, {
get(obj, prop) {
obj = obj.obj;
const value = obj[prop];
if (typeof(value) === 'function') {
const code = value.toString();
if (code.indexOf('+') > -1) {
return _add;
} else if (code.indexOf('-') > -1) {
return _sub;
} else if (code.indexOf('%') > -1) {
return _mod;
} else {
'--unknown function';
}
} else {
const index = parseInt(prop);
if (!isNaN(index)) {
log.push(['get', '_${name}', index, value, value.charCodeAt(0)]);
}
}
return value;
}
});
`);
}
const insert = parser.parse(lines.join('')).program;
ast.insertBefore(insert);
}
function getSign(code, url) {
const context = runCode(code);
const fun = context.self.webpackChunkof_vue[0][1][802313];
const fun2 = runFunction(fun);
const result = fun2({ url: url });
context.result = result;
return context;
}
function parseLog(log) {
const indexes = [];
let num = 0;
let entry = null;
for (let i = 0; i < log.length; i++) {
entry = log[i];
if (entry[0] === 'get') {
indexes.push(entry[2]);
entry = log[i + 1];
if (entry[0] === 'add') {
num += entry[2];
} else if (entry[0] === 'sub') {
num -= entry[2];
} else {
console.log('Expected add/sub here');
}
}
}
indexes.sort((a, b) => a - b);
return { num: num, indexes: indexes };
}
function parseOperations(ast) {
const operations = {
'+': '_add',
'-': '_sub',
'%': '_mod',
};
ast.traverse({
BinaryExpression(path) {
const name = operations[path.node.operator];
if (!name) {
console.log('Unknown operation', path.node.operator);
}
path.replaceWith({
type: 'CallExpression',
callee: {
type: 'Identifier',
name: name,
},
arguments: [path.node.left, path.node.right],
});
},
});
}
function createRules(log, sign) {
const signParts = sign.split(':');
const result = parseLog(log);
const shamsgParts = shamsg.split('\n');
const rules = {
'app-token': '33d57ade8c02dbc5a333db99ff9ae26a',
static_param: shamsgParts[0],
prefix: signParts[0],
suffix: signParts[3],
checksum_constant: result.num,
checksum_indexes: result.indexes,
};
return rules;
}
function createHeaders(path, rules) {
const time = Date.now().toString();
const hash = sha1([rules['static_param'], time, path, rules['user-id']].join('\n'));
const checksum =
rules['checksum_indexes'].reduce((total, current) => (total += hash[current].charCodeAt(0)), 0) +
rules['checksum_constant'];
const sign = [rules['prefix'], hash, checksum.toString(16), rules['suffix']].join(':');
return {
accept: 'application/json, text/plain, */*',
'app-token': rules['app-token'],
cookie: rules['cookie'],
sign: sign,
time: time,
'user-id': rules['user-id'],
'user-agent': rules['user-agent'],
'x-bc': rules['x-bc'],
};
}
async function testAPI(path, rules) {
rules = {
...rules,
...{
'user-id': '0',
'x-bc': '',
cookie: '',
'user-agent': '',
},
};
const headers = createHeaders(path, rules);
const response = await fetch(`https://onlyfans.com${path}`, { headers: headers });
return await response.json();
}
async function main() {
// get the code
const html = await getPage('https://onlyfans.com');
const code = await getCode(html, '2313.js');
// parse ast
const ast = parser.parse(code);
const math = getMath(ast);
const names = getNames(math);
// change code
insertProxy(math, names);
parseOperations(math);
const newCode = generator(ast).code;
// create rules
const context = getSign(newCode, 'not important for analysis');
const rules = createRules(context.log, context.result.sign);
// dump rules
const json = JSON.stringify(
rules,
function (k, v) {
if (v instanceof Array) {
return JSON.stringify(v);
} else {
return v;
}
},
2
)
.replace(/\"\[/g, '[')
.replace(/\]\"/g, ']');
writeFileSync('rules.json', json);
console.log('Done');
// test if it works
//const path = '/api2/v2/users/me';
//const msg = await testAPI(path, rules);
//console.log(msg);
}
main();