@anonympins/fingerprint 0.2.2 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +726 -650
- package/fingerprint.builder.js +171 -160
- package/fingerprint.client.js +482 -459
- package/fingerprint.js +446 -87
- package/package.json +88 -79
- package/pow.solver.js +12 -0
- package/problem-manager.js +198 -22
package/fingerprint.client.js
CHANGED
|
@@ -1,460 +1,483 @@
|
|
|
1
|
-
import { cyrb53, FingerprintBuilder } from './fingerprint.builder.js';
|
|
2
|
-
import { solveChallenge } from './pow.solver.js';
|
|
3
|
-
|
|
4
|
-
const ClientLibrary = {
|
|
5
|
-
// Cache pour éviter de recalculer les constantes (Hardware, etc.)
|
|
6
|
-
_cachedBuilder: null,
|
|
7
|
-
/**
|
|
8
|
-
* Génère l'empreinte de l'appareil actuel.
|
|
9
|
-
*/
|
|
10
|
-
getDeviceFingerprint() {
|
|
11
|
-
if (typeof window === "undefined") {
|
|
12
|
-
console.error("getDeviceFingerprint can only be called on the client-side.");
|
|
13
|
-
return "";
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
if (!this._cachedBuilder) {
|
|
17
|
-
const nav = window.navigator;
|
|
18
|
-
const screen = window.screen;
|
|
19
|
-
|
|
20
|
-
this._cachedBuilder = new FingerprintBuilder();
|
|
21
|
-
|
|
22
|
-
// 1. Hardware (Très stable) : Cœurs, RAM, GPU (si dispo via canvas), Touch
|
|
23
|
-
this._cachedBuilder.add(
|
|
24
|
-
"hw",
|
|
25
|
-
`${nav.hardwareConcurrency}_${nav.deviceMemory}_${nav.maxTouchPoints}`,
|
|
26
|
-
);
|
|
27
|
-
|
|
28
|
-
// 2. Geo/Locale (Stable sauf voyage/VPN) : Timezone, Langue
|
|
29
|
-
this._cachedBuilder.add(
|
|
30
|
-
"geo",
|
|
31
|
-
`${Intl.DateTimeFormat().resolvedOptions().timeZone}_${nav.language}_${new Date().getTimezoneOffset()}`,
|
|
32
|
-
);
|
|
33
|
-
|
|
34
|
-
// 3. Screen (Stable sauf changement moniteur/zoom) : Dimensions, ColorDepth
|
|
35
|
-
this._cachedBuilder.add(
|
|
36
|
-
"scr",
|
|
37
|
-
`${screen.width}x${screen.height}_${screen.colorDepth}`,
|
|
38
|
-
);
|
|
39
|
-
|
|
40
|
-
// 4. Platform (Stable) : OS, Engine
|
|
41
|
-
this._cachedBuilder.add("os", nav.platform);
|
|
42
|
-
|
|
43
|
-
// 5. Graphics (WebGL Vendor/Renderer) - Invariant matériel fort
|
|
44
|
-
try {
|
|
45
|
-
const canvas = document.createElement("canvas");
|
|
46
|
-
const gl =
|
|
47
|
-
canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
|
|
48
|
-
if (gl) {
|
|
49
|
-
const debugInfo = gl.getExtension("WEBGL_debug_renderer_info");
|
|
50
|
-
if (debugInfo) {
|
|
51
|
-
const vendor = gl.getParameter(debugInfo.UNMASKED_VENDOR_WEBGL);
|
|
52
|
-
const renderer = gl.getParameter(debugInfo.UNMASKED_RENDERER_WEBGL);
|
|
53
|
-
this._cachedBuilder.add("gpu", `${vendor}_${renderer}`);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
} catch (e) {
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// 6. Canvas Fingerprinting (Rendering quirks)
|
|
60
|
-
try {
|
|
61
|
-
const canvas = document.createElement("canvas");
|
|
62
|
-
const ctx = canvas.getContext("2d");
|
|
63
|
-
if (ctx) {
|
|
64
|
-
canvas.width = 200;
|
|
65
|
-
canvas.height = 50;
|
|
66
|
-
ctx.textBaseline = "alphabetic";
|
|
67
|
-
ctx.font = "14px 'Arial'";
|
|
68
|
-
ctx.fillStyle = "#f60";
|
|
69
|
-
ctx.fillRect(125, 1, 62, 20);
|
|
70
|
-
ctx.fillStyle = "#069";
|
|
71
|
-
ctx.fillText("fingerprint", 2, 15);
|
|
72
|
-
ctx.fillStyle = "rgba(102, 204, 0, 0.7)";
|
|
73
|
-
ctx.fillText("fingerprint", 4, 17);
|
|
74
|
-
this._cachedBuilder.add("cvs", canvas.toDataURL());
|
|
75
|
-
}
|
|
76
|
-
} catch (e) {
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// 7.
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
.
|
|
94
|
-
.
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
return
|
|
98
|
-
},
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Génère une signature
|
|
102
|
-
* @param {object} payload
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
const
|
|
115
|
-
return
|
|
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
|
-
* Démarre le suivi de la
|
|
146
|
-
* À appeler une fois sur la page.
|
|
147
|
-
*/
|
|
148
|
-
|
|
149
|
-
// S'assurer de ne pas attacher l'écouteur plusieurs fois
|
|
150
|
-
if (
|
|
151
|
-
|
|
152
|
-
document.addEventListener('
|
|
153
|
-
const
|
|
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
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
//
|
|
1
|
+
import { cyrb53, FingerprintBuilder } from './fingerprint.builder.js';
|
|
2
|
+
import { solveChallenge } from './pow.solver.js';
|
|
3
|
+
|
|
4
|
+
const ClientLibrary = {
|
|
5
|
+
// Cache pour éviter de recalculer les constantes (Hardware, etc.)
|
|
6
|
+
_cachedBuilder: null,
|
|
7
|
+
/**
|
|
8
|
+
* Génère l'empreinte de l'appareil actuel.
|
|
9
|
+
*/
|
|
10
|
+
getDeviceFingerprint() {
|
|
11
|
+
if (typeof window === "undefined") {
|
|
12
|
+
console.error("getDeviceFingerprint can only be called on the client-side.");
|
|
13
|
+
return "";
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (!this._cachedBuilder) {
|
|
17
|
+
const nav = window.navigator;
|
|
18
|
+
const screen = window.screen;
|
|
19
|
+
|
|
20
|
+
this._cachedBuilder = new FingerprintBuilder();
|
|
21
|
+
|
|
22
|
+
// 1. Hardware (Très stable) : Cœurs, RAM, GPU (si dispo via canvas), Touch
|
|
23
|
+
this._cachedBuilder.add(
|
|
24
|
+
"hw",
|
|
25
|
+
`${nav.hardwareConcurrency}_${nav.deviceMemory}_${nav.maxTouchPoints}`,
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
// 2. Geo/Locale (Stable sauf voyage/VPN) : Timezone, Langue
|
|
29
|
+
this._cachedBuilder.add(
|
|
30
|
+
"geo",
|
|
31
|
+
`${Intl.DateTimeFormat().resolvedOptions().timeZone}_${nav.language}_${new Date().getTimezoneOffset()}`,
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
// 3. Screen (Stable sauf changement moniteur/zoom) : Dimensions, ColorDepth
|
|
35
|
+
this._cachedBuilder.add(
|
|
36
|
+
"scr",
|
|
37
|
+
`${screen.width}x${screen.height}_${screen.colorDepth}`,
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
// 4. Platform (Stable) : OS, Engine
|
|
41
|
+
this._cachedBuilder.add("os", nav.platform);
|
|
42
|
+
|
|
43
|
+
// 5. Graphics (WebGL Vendor/Renderer) - Invariant matériel fort
|
|
44
|
+
try {
|
|
45
|
+
const canvas = document.createElement("canvas");
|
|
46
|
+
const gl =
|
|
47
|
+
canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
|
|
48
|
+
if (gl) {
|
|
49
|
+
const debugInfo = gl.getExtension("WEBGL_debug_renderer_info");
|
|
50
|
+
if (debugInfo) {
|
|
51
|
+
const vendor = gl.getParameter(debugInfo.UNMASKED_VENDOR_WEBGL);
|
|
52
|
+
const renderer = gl.getParameter(debugInfo.UNMASKED_RENDERER_WEBGL);
|
|
53
|
+
this._cachedBuilder.add("gpu", `${vendor}_${renderer}`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
} catch (e) {
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// 6. Canvas Fingerprinting (Rendering quirks)
|
|
60
|
+
try {
|
|
61
|
+
const canvas = document.createElement("canvas");
|
|
62
|
+
const ctx = canvas.getContext("2d");
|
|
63
|
+
if (ctx) {
|
|
64
|
+
canvas.width = 200;
|
|
65
|
+
canvas.height = 50;
|
|
66
|
+
ctx.textBaseline = "alphabetic";
|
|
67
|
+
ctx.font = "14px 'Arial'";
|
|
68
|
+
ctx.fillStyle = "#f60";
|
|
69
|
+
ctx.fillRect(125, 1, 62, 20);
|
|
70
|
+
ctx.fillStyle = "#069";
|
|
71
|
+
ctx.fillText("fingerprint", 2, 15);
|
|
72
|
+
ctx.fillStyle = "rgba(102, 204, 0, 0.7)";
|
|
73
|
+
ctx.fillText("fingerprint", 4, 17);
|
|
74
|
+
this._cachedBuilder.add("cvs", canvas.toDataURL());
|
|
75
|
+
}
|
|
76
|
+
} catch (e) {
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// 7. Détection des artefacts du Chrome DevTools Protocol (CDP)
|
|
80
|
+
// Ces variables sont souvent injectées par les outils d'automatisation.
|
|
81
|
+
const cdpFootprints = [
|
|
82
|
+
'cdc_adoQpoasnfa76pfcZLmcfl_Array',
|
|
83
|
+
'cdc_adoQpoasnfa76pfcZLmcfl_Promise',
|
|
84
|
+
'cdc_adoQpoasnfa76pfcZLmcfl_Symbol',
|
|
85
|
+
'$cdc_asdjflasutopfhvcZLmcfl_',
|
|
86
|
+
'_selenium',
|
|
87
|
+
'_driver'
|
|
88
|
+
];
|
|
89
|
+
if (cdpFootprints.some(fp => window[fp])) {
|
|
90
|
+
this._cachedBuilder.add("cdp", "true");
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// 7. Bot Detection (Indication cachée)
|
|
94
|
+
if (nav.webdriver) this._cachedBuilder.add("bot", "true");
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return this._cachedBuilder.toString();
|
|
98
|
+
},
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Génère une signature de requête incluant le contexte.
|
|
102
|
+
* @param {object} payload
|
|
103
|
+
*/
|
|
104
|
+
/**
|
|
105
|
+
* Génère une signature de requête incluant le contexte.
|
|
106
|
+
* @param {object} payload
|
|
107
|
+
*/
|
|
108
|
+
generateRequestSignature(payload = {}) {
|
|
109
|
+
const deviceFp = this.getDeviceFingerprint();
|
|
110
|
+
const sortedPayload = Object.keys(payload)
|
|
111
|
+
.sort()
|
|
112
|
+
.map((k) => `${k}=${payload[k]}`)
|
|
113
|
+
.join("&");
|
|
114
|
+
const payloadHash = cyrb53(sortedPayload);
|
|
115
|
+
return `${deviceFp}|req:${payloadHash}`;
|
|
116
|
+
},
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Génère une signature HMAC-SHA256 en utilisant l'API Web Crypto.
|
|
120
|
+
* @param {object} payload - Les données à signer.
|
|
121
|
+
* @param {string} secret - La clé secrète partagée.
|
|
122
|
+
* @returns {Promise<string>} La signature hexadécimale.
|
|
123
|
+
*/
|
|
124
|
+
async generateClientSideSignature(payload, secret) {
|
|
125
|
+
const sortedPayload = Object.keys(payload).sort().map((k) => `${k}=${payload[k]}`).join("&");
|
|
126
|
+
const encoder = new TextEncoder();
|
|
127
|
+
const key = await window.crypto.subtle.importKey("raw", encoder.encode(secret), {
|
|
128
|
+
name: "HMAC",
|
|
129
|
+
hash: "SHA-256"
|
|
130
|
+
}, false, ["sign"]);
|
|
131
|
+
const signatureBuffer = await window.crypto.subtle.sign("HMAC", key, encoder.encode(sortedPayload));
|
|
132
|
+
const hashArray = Array.from(new Uint8Array(signatureBuffer));
|
|
133
|
+
return hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
134
|
+
},
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* @internal
|
|
138
|
+
* Resets the cached fingerprint builder. Used for testing purposes.
|
|
139
|
+
*/
|
|
140
|
+
_resetCache() {
|
|
141
|
+
this._cachedBuilder = null;
|
|
142
|
+
},
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Démarre le suivi des mouvements de la souris pour calculer l'entropie.
|
|
146
|
+
* À appeler une fois sur la page.
|
|
147
|
+
*/
|
|
148
|
+
startMouseEntropyTracker() {
|
|
149
|
+
// S'assurer de ne pas attacher l'écouteur plusieurs fois
|
|
150
|
+
if (mouseMovements > 0) return;
|
|
151
|
+
|
|
152
|
+
document.addEventListener('mousemove', (e) => {
|
|
153
|
+
const dx = e.clientX - lastMousePos.x;
|
|
154
|
+
const dy = e.clientY - lastMousePos.y;
|
|
155
|
+
// Une métrique simple : la somme des distances. Un bot aura souvent 0.
|
|
156
|
+
metrics.mouseEntropy += Math.sqrt(dx * dx + dy * dy);
|
|
157
|
+
lastMousePos = {x: e.clientX, y: e.clientY};
|
|
158
|
+
mouseMovements++;
|
|
159
|
+
}, {passive: true});
|
|
160
|
+
},
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Démarre le suivi de la dynamique de frappe pour calculer la latence.
|
|
164
|
+
* À appeler une fois sur la page.
|
|
165
|
+
*/
|
|
166
|
+
startKeystrokeDynamicsTracker() {
|
|
167
|
+
// S'assurer de ne pas attacher l'écouteur plusieurs fois
|
|
168
|
+
if (keystrokeTimestamps.length > 0) return;
|
|
169
|
+
|
|
170
|
+
document.addEventListener('keydown', () => {
|
|
171
|
+
const now = performance.now();
|
|
172
|
+
if (keystrokeTimestamps.length > 0) {
|
|
173
|
+
const lastTimestamp = keystrokeTimestamps[keystrokeTimestamps.length - 1];
|
|
174
|
+
const latency = now - lastTimestamp;
|
|
175
|
+
// On ignore les latences irréalistes (trop longues ou trop courtes)
|
|
176
|
+
if (latency > 10 && latency < 2000) { // Augmenté à 2s
|
|
177
|
+
if (keystrokeLatencies.length >= KEYSTROKE_HISTORY_MAX) {
|
|
178
|
+
keystrokeLatencies.shift(); // Garder la taille de l'historique
|
|
179
|
+
}
|
|
180
|
+
keystrokeLatencies.push(latency);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
keystrokeTimestamps.push(now);
|
|
184
|
+
}, {passive: true});
|
|
185
|
+
},
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Initialise ou réinitialise les honeypots côté client pour une détection immédiate.
|
|
189
|
+
* Les anciens écouteurs sont supprimés avant d'en ajouter de nouveaux.
|
|
190
|
+
* @param {string[]} honeypotFieldNames - Noms des champs de formulaire cachés.
|
|
191
|
+
*/
|
|
192
|
+
initializeHoneypots(honeypotFieldNames) {
|
|
193
|
+
// 1. Nettoyer les anciens écouteurs
|
|
194
|
+
activeHoneypotListeners.forEach((listener, field) => {
|
|
195
|
+
field.removeEventListener('input', listener);
|
|
196
|
+
});
|
|
197
|
+
activeHoneypotListeners.clear();
|
|
198
|
+
|
|
199
|
+
// 2. Ajouter les nouveaux écouteurs
|
|
200
|
+
honeypotFieldNames.forEach(fieldName => {
|
|
201
|
+
const field = document.querySelector(`[name="${fieldName}"]`);
|
|
202
|
+
if (field) {
|
|
203
|
+
// On utilise une fonction nommée (ou une référence) pour pouvoir la supprimer plus tard.
|
|
204
|
+
// L'option { once: true } est excellente, mais pour une réinitialisation complète,
|
|
205
|
+
// il est plus propre de gérer le nettoyage nous-mêmes.
|
|
206
|
+
const listener = () => {
|
|
207
|
+
this.onHoneypotTrigger();
|
|
208
|
+
// Se supprime lui-même après exécution, comme { once: true }
|
|
209
|
+
field.removeEventListener('input', listener);
|
|
210
|
+
};
|
|
211
|
+
field.addEventListener('input', listener);
|
|
212
|
+
activeHoneypotListeners.set(field, listener); // On stocke la référence
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
},
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Récupère les métriques comportementales collectées.
|
|
219
|
+
* À appeler avant d'envoyer une requête sensible.
|
|
220
|
+
* @returns {ClientBehaviorMetrics}
|
|
221
|
+
*/
|
|
222
|
+
getClientBehaviorMetrics() {
|
|
223
|
+
// Add history length as a behavioral signal.
|
|
224
|
+
metrics.historyLength = window.history.length;
|
|
225
|
+
|
|
226
|
+
// Ajoute un timestamp au moment de la collecte pour la détection de rejeu.
|
|
227
|
+
metrics.clientTimestamp = Date.now();
|
|
228
|
+
|
|
229
|
+
// Normalise l'entropie de la souris
|
|
230
|
+
if (mouseMovements > 10) {
|
|
231
|
+
metrics.mouseEntropy /= mouseMovements;
|
|
232
|
+
}
|
|
233
|
+
// Calcule la latence moyenne des frappes
|
|
234
|
+
if (keystrokeLatencies.length > 0) {
|
|
235
|
+
const sum = keystrokeLatencies.reduce((a, b) => a + b, 0);
|
|
236
|
+
metrics.keystrokeLatency = sum / keystrokeLatencies.length;
|
|
237
|
+
} else {
|
|
238
|
+
metrics.keystrokeLatency = 0;
|
|
239
|
+
}
|
|
240
|
+
return metrics;
|
|
241
|
+
},
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Enrichit une requête fetch avec les en-têtes de fingerprinting et de comportement.
|
|
245
|
+
* @param {RequestInfo} resource
|
|
246
|
+
* @param {RequestInit} [options]
|
|
247
|
+
* @returns {Promise<Response>}
|
|
248
|
+
*/
|
|
249
|
+
async protectedFetch(resource, options = {}) {
|
|
250
|
+
const fp = this.getDeviceFingerprint();
|
|
251
|
+
const behavior = this.getClientBehaviorMetrics();
|
|
252
|
+
|
|
253
|
+
const headers = new Headers(options.headers || {});
|
|
254
|
+
headers.set('X-Device-Fingerprint', fp);
|
|
255
|
+
headers.set('X-Behavior-Metrics', JSON.stringify(behavior));
|
|
256
|
+
|
|
257
|
+
options.headers = headers;
|
|
258
|
+
return fetch(resource, options);
|
|
259
|
+
},
|
|
260
|
+
|
|
261
|
+
// --- Système d'interception de Fetch robuste et anti-conflit ---
|
|
262
|
+
|
|
263
|
+
_isFetchPatched: false,
|
|
264
|
+
_interceptorChain: [],
|
|
265
|
+
// On stocke la fonction fetch originale et on la lie à son contexte (window)
|
|
266
|
+
// pour éviter les erreurs "Illegal invocation" si une autre lib la modifie.
|
|
267
|
+
_originalFetch: (typeof window !== 'undefined') ? window.fetch.bind(window) : null,
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Adds an interceptor function to the `fetch` chain.
|
|
271
|
+
* Chaque intercepteur reçoit `resource`, `options`, et une fonction `next`.
|
|
272
|
+
* Il DOIT appeler `next(resource, options)` pour continuer la chaîne.
|
|
273
|
+
* @param {function(RequestInfo, RequestInit, function): Promise<Response>} interceptor
|
|
274
|
+
*/
|
|
275
|
+
addFetchInterceptor(interceptor) {
|
|
276
|
+
if (!this._isFetchPatched) {
|
|
277
|
+
this.patchGlobalFetch();
|
|
278
|
+
}
|
|
279
|
+
this._interceptorChain.push(interceptor);
|
|
280
|
+
},
|
|
281
|
+
|
|
282
|
+
patchGlobalFetch() {
|
|
283
|
+
if (this._isFetchPatched || !this._originalFetch) return;
|
|
284
|
+
|
|
285
|
+
this._isFetchPatched = true;
|
|
286
|
+
window.fetch = (resource, options) => {
|
|
287
|
+
// Le "dispatcher" qui exécute la chaîne.
|
|
288
|
+
const dispatch = (index, res, opts) => {
|
|
289
|
+
if (index >= this._interceptorChain.length) {
|
|
290
|
+
// Fin de la chaîne, on appelle le fetch original.
|
|
291
|
+
return this._originalFetch(res, opts);
|
|
292
|
+
}
|
|
293
|
+
const nextInterceptor = this._interceptorChain[index];
|
|
294
|
+
// Appelle l'intercepteur actuel en lui passant la fonction pour appeler le suivant.
|
|
295
|
+
return nextInterceptor(res, opts, (nextRes, nextOpts) => dispatch(index + 1, nextRes, nextOpts));
|
|
296
|
+
};
|
|
297
|
+
return dispatch(0, resource, options || {});
|
|
298
|
+
};
|
|
299
|
+
},
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* La fonction qui est appelée lorsqu'un honeypot est déclenché.
|
|
303
|
+
* @private
|
|
304
|
+
*/
|
|
305
|
+
onHoneypotTrigger : () => {
|
|
306
|
+
metrics.honeypotInteraction = true;
|
|
307
|
+
// On pourrait même envoyer un signalement au serveur immédiatement.
|
|
308
|
+
},
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Initialise l'intercepteur de fingerprinting.
|
|
312
|
+
* Il s'ajoute à la chaîne d'interception sans écraser les autres.
|
|
313
|
+
* @param {string[]} [targetDomains] - Optionnel. Liste de domaines à protéger.
|
|
314
|
+
* Si non fourni, protège les requêtes de même origine.
|
|
315
|
+
*/
|
|
316
|
+
initializeFetch(targetDomains = []) {
|
|
317
|
+
const fingerprintInterceptor = (resource, options, next) => {
|
|
318
|
+
const requestUrl = (resource instanceof Request) ? resource.url : String(resource);
|
|
319
|
+
let shouldProtect = false;
|
|
320
|
+
|
|
321
|
+
try {
|
|
322
|
+
const url = new URL(requestUrl, window.location.origin);
|
|
323
|
+
// Protéger si la liste de domaines est vide ET que la requête est de même origine,
|
|
324
|
+
// OU si le domaine de la requête est dans la liste fournie.
|
|
325
|
+
shouldProtect = (targetDomains.length === 0 && url.origin === window.location.origin) ||
|
|
326
|
+
(targetDomains.length > 0 && targetDomains.includes(url.hostname));
|
|
327
|
+
} catch (e) {
|
|
328
|
+
// Si l'URL est relative (ex: '/api/data'), new URL() ne lèvera pas d'erreur.
|
|
329
|
+
// Ce bloc est une sécurité pour les cas où l'URL serait malformée.
|
|
330
|
+
// On protège par défaut si aucune liste de domaines n'est spécifiée.
|
|
331
|
+
shouldProtect = targetDomains.length === 0;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
if (shouldProtect) {
|
|
335
|
+
const fp = this.getDeviceFingerprint();
|
|
336
|
+
const behavior = this.getClientBehaviorMetrics();
|
|
337
|
+
const headers = new Headers(options.headers || {});
|
|
338
|
+
headers.set('X-Device-Fingerprint', fp);
|
|
339
|
+
headers.set('X-Behavior-Metrics', JSON.stringify(behavior));
|
|
340
|
+
options.headers = headers;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// Passe la main à l'intercepteur suivant dans la chaîne.
|
|
344
|
+
return next(resource, options);
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
this.addFetchInterceptor(fingerprintInterceptor);
|
|
348
|
+
},
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Intercepte une réponse de challenge JSON, le résout, et réessaie la requête.
|
|
352
|
+
* @param {Response} response - La réponse initiale (potentiellement 429).
|
|
353
|
+
* @param {RequestInfo} resource - La ressource de la requête originale.
|
|
354
|
+
* @param {RequestInit} options - Les options de la requête originale.
|
|
355
|
+
* @returns {Promise<Response>} - La réponse de la requête réessayée.
|
|
356
|
+
* @private
|
|
357
|
+
*/
|
|
358
|
+
async solveChallengeAndRetry(response, resource, options) {
|
|
359
|
+
if (response.status !== 404 || !response.headers.get('content-type')?.includes('application/json') || response.bodyUsed) {
|
|
360
|
+
return response;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
try {
|
|
364
|
+
const challengeData = await response.json();
|
|
365
|
+
if (!challengeData.challenge || !challengeData.challenge.type) {
|
|
366
|
+
return response; // Pas un challenge JSON valide
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
console.log(`[Fingerprint] Received a '${challengeData.challenge.type}' challenge. Solving...`);
|
|
370
|
+
// L'empreinte de l'appareil qui résout le challenge est cruciale.
|
|
371
|
+
const solverFp = this.getDeviceFingerprint();
|
|
372
|
+
const solutionWrapper = await solveChallenge(challengeData.challenge, solverFp);
|
|
373
|
+
console.log('[Fingerprint] Challenge solved. Retrying original request.');
|
|
374
|
+
|
|
375
|
+
// Ajouter la solution aux paramètres de la requête pour le nouvel essai
|
|
376
|
+
const url = new URL((resource instanceof Request) ? resource.url : String(resource), window.location.origin);
|
|
377
|
+
// La logique de formatage est maintenant cachée dans la classe ChallengeSolution.
|
|
378
|
+
solutionWrapper.applyToUrl(url);
|
|
379
|
+
|
|
380
|
+
// On ajoute l'empreinte du solveur à la requête de réessai.
|
|
381
|
+
url.searchParams.set('pow_fp', solverFp);
|
|
382
|
+
|
|
383
|
+
// On utilise la chaîne d'intercepteurs pour la requête réessayée,
|
|
384
|
+
// ce qui garantit que le fetch original est appelé avec le bon contexte.
|
|
385
|
+
// Cela évite de réintroduire l'erreur "Illegal invocation".
|
|
386
|
+
return window.fetch(url.toString(), options);
|
|
387
|
+
} catch (e) {
|
|
388
|
+
console.error('[Fingerprint] Failed to solve or retry challenge:', e);
|
|
389
|
+
return response; // Retourne la réponse 429 originale en cas d'échec
|
|
390
|
+
}
|
|
391
|
+
},
|
|
392
|
+
/**
|
|
393
|
+
* @typedef {object} ClientConfig
|
|
394
|
+
* @property {boolean} [mouse=true] - Activer le suivi de l'entropie de la souris.
|
|
395
|
+
* @property {boolean} [keystrokes=true] - Activer le suivi de la dynamique de frappe.
|
|
396
|
+
* @property {string[]} [honeypots] - Noms des champs de formulaire honeypot à initialiser.
|
|
397
|
+
* @property {object} [fetch] - Configuration pour l'interception de fetch.
|
|
398
|
+
* @property {string[]} [fetch.targetDomains] - Domaines à protéger. Si non fourni, protège les requêtes de même origine.
|
|
399
|
+
*/
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Initialise toutes les protections côté client en une seule fois.
|
|
403
|
+
* C'est la méthode d'initialisation recommandée.
|
|
404
|
+
* @param {ClientConfig} [config={}] - L'objet de configuration.
|
|
405
|
+
*/
|
|
406
|
+
initializeClient(config = {}) {
|
|
407
|
+
const {
|
|
408
|
+
mouse = true,
|
|
409
|
+
keystrokes = true,
|
|
410
|
+
honeypots = [],
|
|
411
|
+
fetch: fetchConfig = {},
|
|
412
|
+
} = config;
|
|
413
|
+
|
|
414
|
+
if (mouse) {
|
|
415
|
+
this.startMouseEntropyTracker();
|
|
416
|
+
}
|
|
417
|
+
if (keystrokes) {
|
|
418
|
+
this.startKeystrokeDynamicsTracker();
|
|
419
|
+
}
|
|
420
|
+
if (honeypots.length > 0) {
|
|
421
|
+
this.initializeHoneypots(honeypots);
|
|
422
|
+
}
|
|
423
|
+
// On active l'interception si `fetch` est configuré, même avec un objet vide.
|
|
424
|
+
if (config.fetch) {
|
|
425
|
+
this.initializeFetch(fetchConfig.targetDomains);
|
|
426
|
+
|
|
427
|
+
// Ajoute l'intercepteur pour la résolution de challenge
|
|
428
|
+
if (fetchConfig.handleChallenges !== false) {
|
|
429
|
+
this.addFetchInterceptor(async (resource, options, next) => {
|
|
430
|
+
const originalResponse = await next(resource, options);
|
|
431
|
+
// On clone la réponse pour que la lecture du corps par solveChallengeAndRetry ne la consomme pas pour l'appelant original.
|
|
432
|
+
return this.solveChallengeAndRetry(originalResponse.clone(), resource, options);
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
};
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* @typedef {object} ClientBehaviorMetrics
|
|
441
|
+
* @property {number} mouseEntropy - Entropie des mouvements de la souris.
|
|
442
|
+
* @property {number} keystrokeLatency - Latence moyenne entre les frappes.
|
|
443
|
+
* @property {boolean} honeypotInteraction - Vrai si un honeypot a été touché.
|
|
444
|
+
* @property {number} historyLength - La longueur de l'historique de session du navigateur (`window.history.length`).
|
|
445
|
+
* @property {number} clientTimestamp - Timestamp (Date.now()) de la collecte des métriques.
|
|
446
|
+
*/
|
|
447
|
+
|
|
448
|
+
/** @type {ClientBehaviorMetrics} */
|
|
449
|
+
const metrics = {
|
|
450
|
+
mouseEntropy: 0,
|
|
451
|
+
keystrokeLatency: 0,
|
|
452
|
+
honeypotInteraction: false,
|
|
453
|
+
historyLength: 0,
|
|
454
|
+
clientTimestamp: 0,
|
|
455
|
+
};
|
|
456
|
+
|
|
457
|
+
let lastMousePos = { x: 0, y: 0 };
|
|
458
|
+
let mouseMovements = 0;
|
|
459
|
+
let activeHoneypotListeners = new Map(); // Garde une trace des écouteurs actifs
|
|
460
|
+
let keystrokeTimestamps = [];
|
|
461
|
+
let keystrokeLatencies = []; // NOUVEAU: Tableau dédié pour les latences
|
|
462
|
+
const KEYSTROKE_HISTORY_MAX = 20; // On garde l'historique des 20 dernières frappes
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
// Exporter les fonctions individuellement pour la compatibilité ascendante
|
|
467
|
+
export const getDeviceFingerprint = ClientLibrary.getDeviceFingerprint.bind(ClientLibrary);
|
|
468
|
+
export const generateRequestSignature = ClientLibrary.generateRequestSignature.bind(ClientLibrary);
|
|
469
|
+
export const generateClientSideSignature = ClientLibrary.generateClientSideSignature.bind(ClientLibrary);
|
|
470
|
+
export const _resetCache = ClientLibrary._resetCache.bind(ClientLibrary);
|
|
471
|
+
export const startMouseEntropyTracker = ClientLibrary.startMouseEntropyTracker.bind(ClientLibrary);
|
|
472
|
+
export const startKeystrokeDynamicsTracker = ClientLibrary.startKeystrokeDynamicsTracker.bind(ClientLibrary);
|
|
473
|
+
export const initializeHoneypots = ClientLibrary.initializeHoneypots.bind(ClientLibrary);
|
|
474
|
+
export const getClientBehaviorMetrics = ClientLibrary.getClientBehaviorMetrics.bind(ClientLibrary);
|
|
475
|
+
export const protectedFetch = ClientLibrary.protectedFetch.bind(ClientLibrary);
|
|
476
|
+
export const addFetchInterceptor = ClientLibrary.addFetchInterceptor.bind(ClientLibrary);
|
|
477
|
+
export const patchGlobalFetch = ClientLibrary.patchGlobalFetch.bind(ClientLibrary);
|
|
478
|
+
export const initializeFetch = ClientLibrary.initializeFetch.bind(ClientLibrary);
|
|
479
|
+
export const initializeClient = ClientLibrary.initializeClient.bind(ClientLibrary);
|
|
480
|
+
export const solveChallengeAndRetry = ClientLibrary.solveChallengeAndRetry.bind(ClientLibrary);
|
|
481
|
+
|
|
482
|
+
// Export the internal object for testing purposes
|
|
460
483
|
export default ClientLibrary;
|