@anonympins/fingerprint 0.6.7 → 0.7.1

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.
Files changed (63) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/README.md +3 -7
  3. package/composer.json +8 -1
  4. package/package.json +1 -1
  5. package/public/fingerprint-wordpress.zip +0 -0
  6. package/public/fp.wasm +0 -0
  7. package/src/js/build-client.js +9 -11
  8. package/src/js/dynamic-wasm.js +7 -7
  9. package/src/js/fingerprint.builder.js +37 -38
  10. package/src/js/fingerprint.client.js +236 -152
  11. package/src/js/fingerprint.client.obfuscated.js +1 -1
  12. package/src/js/fingerprint.js +517 -101
  13. package/src/js/fingerprint.utils.js +213 -213
  14. package/src/js/library.js +355 -384
  15. package/src/js/mongodb-store.js +1 -1
  16. package/src/js/optimization.worker.js +27 -27
  17. package/src/js/pow.solver.inline.js +206 -62
  18. package/src/js/pow.solver.js +235 -101
  19. package/src/js/pow.worker.js +48 -26
  20. package/src/js/redis-store.js +1 -1
  21. package/src/js/tests/fingerprint.client.init.test.js +146 -143
  22. package/src/js/tests/fingerprint.test.js +101 -40
  23. package/src/js/tests/ip-reputation.test.js +1 -0
  24. package/src/js/upow-model-task.js +14 -15
  25. package/src/php/AutoTuner.php +34 -34
  26. package/src/php/Challenge/ChallengeUtils.php +204 -32
  27. package/src/php/Config/SecurityProfiles.php +21 -0
  28. package/src/php/DirectFingerprint.php +190 -145
  29. package/src/php/FingerprintBuilder.php +29 -30
  30. package/src/php/FingerprintClient.php +24 -25
  31. package/src/php/FingerprintEngine.php +1836 -1880
  32. package/src/php/Ja3AnomalyDetector.php +258 -227
  33. package/src/php/Optimization/FunctionRegistry.php +7 -7
  34. package/src/php/Optimization/Optimization.php +24 -24
  35. package/src/php/Optimization/OptimizationOperators.php +28 -27
  36. package/src/php/Optimization/ProblemInitializers.php +52 -52
  37. package/src/php/ProblemManager.php +31 -34
  38. package/src/php/RequestContext.php +8 -1
  39. package/src/php/Store/IStore.php +7 -8
  40. package/src/php/Store/InMemoryStore.php +66 -66
  41. package/src/php/Store/MongoDbStore.php +5 -5
  42. package/src/php/Store/RedisStore.php +5 -5
  43. package/src/php/Store/StoreManager.php +35 -35
  44. package/src/php/Tests/ChallengeUtilsTest.php +453 -453
  45. package/src/php/Tests/DnsCircuitBreakerTest.php +105 -105
  46. package/src/php/Tests/FingerprintEngineTest.php +662 -693
  47. package/src/php/Tests/IpReputationTest.php +10 -0
  48. package/src/php/Tests/MaliciousPatternsTest.php +103 -103
  49. package/src/php/Tests/MetricsTest.php +97 -97
  50. package/src/php/Tests/ProblemManagerTest.php +376 -376
  51. package/src/php/Tests/QuicFingerprintTest.php +53 -53
  52. package/src/php/Tests/RequestUtilsTest.php +471 -471
  53. package/src/php/Tests/TLSClientHelloParserTest.php +177 -177
  54. package/src/php/Tests/config/ed25519_key.json +3 -3
  55. package/src/php/Utils/Env.php +49 -49
  56. package/src/php/Utils/MaliciousPatterns.php +74 -74
  57. package/src/php/Utils/MetricsManager.php +209 -209
  58. package/src/php/Utils/RequestUtils.php +236 -19
  59. package/src/php/Utils/TLSClientHelloParser.php +369 -212
  60. package/src/php/WordPress/WpDbStore.php +155 -0
  61. package/src/php/WordPress/fingerprint-wordpress.php +217 -0
  62. package/src/php/WordPress/package.php +123 -0
  63. package/src/php/bin/auto-tune.php +118 -118
@@ -1,118 +1,118 @@
1
- <?php
2
-
3
- declare(strict_types=1);
4
-
5
- /**
6
- * CLI Script d'auto-tuning périodique des configurations de sécurité.
7
- * Ce script lit la configuration courante, récupère les logs, les assainit,
8
- * exécute l'optimiseur génétique, et écrase le fichier JSON d'origine.
9
- */
10
-
11
- $autoloaderPaths = [
12
- __DIR__ . '/../../vendor/autoload.php',
13
- __DIR__ . '/../vendor/autoload.php',
14
- __DIR__ . '/../../../autoload.php',
15
- ];
16
-
17
- $autoloaded = false;
18
- foreach ($autoloaderPaths as $path) {
19
- if (file_exists($path)) {
20
- require_once $path;
21
- $autoloaded = true;
22
- break;
23
- }
24
- }
25
-
26
- if (!$autoloaded) {
27
- fwrite(STDERR, "Erreur : Impossible de charger l'autoloader. Exécutez 'composer install'.\n");
28
- exit(1);
29
- }
30
-
31
- use Anonympins\Fingerprint\Optimization\Optimization;
32
- use Anonympins\Fingerprint\Store\StoreManager;
33
- use Anonympins\Fingerprint\Utils\RequestUtils;
34
-
35
- // 1. Récupération des arguments CLI
36
- $configPath = $argv[1] ?? null;
37
- if (!$configPath) {
38
- echo "Usage: php auto-tune.php [chemin_vers_security-config.json]\n";
39
- exit(1);
40
- }
41
-
42
- if (!file_exists($configPath)) {
43
- fwrite(STDERR, "Erreur : Fichier de configuration introuvable : {$configPath}\n");
44
- exit(1);
45
- }
46
-
47
- $config = json_decode(file_get_contents($configPath), true);
48
- if (json_last_error() !== JSON_ERROR_NONE) {
49
- fwrite(STDERR, "Erreur : Fichier de configuration JSON invalide.\n");
50
- exit(1);
51
- }
52
-
53
- // 2. Connexion au store pour récupérer les logs accumulés
54
- $store = StoreManager::getStore();
55
- if (!$store) {
56
- fwrite(STDERR, "Erreur : Aucun store de persistance actif.\n");
57
- exit(1);
58
- }
59
-
60
- $rawTrafficLogs = $store->get('traffic_logs') ?? [];
61
- if (empty($rawTrafficLogs)) {
62
- echo "[AutoTuning] Aucun log de trafic disponible pour l'optimisation.\n";
63
- exit(0);
64
- }
65
-
66
- // 3. Nettoyage des données pour prévenir l'empoisonnement (Sybil attacks)
67
- $sanitizedLogs = RequestUtils::sanitizeTrafficData($rawTrafficLogs);
68
-
69
- $minDataPoints = $config['autotuning']['minDataPoints'] ?? 200;
70
- if (count($sanitizedLogs) < $minDataPoints) {
71
- echo "[AutoTuning] Reporté : Pas assez de données assainies (" . count($sanitizedLogs) . "/{$minDataPoints}).\n";
72
- exit(0);
73
- }
74
-
75
- echo "[AutoTuning] Lancement de l'optimisation sur " . count($sanitizedLogs) . " données de trafic...\n";
76
-
77
- // 4. Résolution du Front de Pareto
78
- $paretoFront = Optimization::solveFullSecurityTuning(['trafficData' => $sanitizedLogs]);
79
- if (empty($paretoFront)) {
80
- fwrite(STDERR, "[AutoTuning] L'optimisation n'a retourné aucun résultat.\n");
81
- exit(1);
82
- }
83
-
84
- // 5. Sélection de la solution la plus équilibrée
85
- $bestSolution = $paretoFront[0];
86
- $minDistance = sqrt(pow($bestSolution['objectives'][0], 2) + pow($bestSolution['objectives'][1], 2));
87
- foreach ($paretoFront as $candidate) {
88
- $distance = sqrt(pow($candidate['objectives'][0], 2) + pow($candidate['objectives'][1], 2));
89
- if ($distance < $minDistance) {
90
- $minDistance = $distance;
91
- $bestSolution = $candidate;
92
- }
93
- }
94
-
95
- $newConfig = $bestSolution['solution'];
96
- $maxChangeVelocity = 0.15; // Inertie de 15% maximum par cycle
97
-
98
- $applyInertialUpdate = function (array &$current, array $target) use ($maxChangeVelocity) {
99
- $sumCurrent = array_sum($current);
100
- if ($sumCurrent === 0) return;
101
- $sumTarget = 0;
102
- foreach ($current as $k => $v) {
103
- if (isset($target[$k])) $sumTarget += $target[$k];
104
- }
105
- $ratio = ($sumTarget - $sumCurrent) / $sumCurrent;
106
- $factor = 1 + max(-$maxChangeVelocity, min($maxChangeVelocity, $ratio));
107
- foreach ($current as $k => &$v) {
108
- if (isset($target[$k])) $v *= $factor;
109
- }
110
- };
111
-
112
- $applyInertialUpdate($config['thresholds'], $newConfig['thresholds']);
113
- $applyInertialUpdate($config['weights'], $newConfig['weights']);
114
- $applyInertialUpdate($config['patterns'], $newConfig['patterns']);
115
-
116
- // 6. Écrasement propre du fichier de configuration original
117
- file_put_contents($configPath, json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
118
- echo "[AutoTuning] Succès : Fichier {$configPath} mis à jour avec les paramètres optimisés.\n";
1
+ <?php
2
+
3
+ declare(strict_types=1);
4
+
5
+ /**
6
+ * Periodic security configuration auto-tuning CLI script.
7
+ * Reads current configuration, prunes and sanitizes traffic logs,
8
+ * executes the genetic optimizer, and writes back the optimized profile.
9
+ */
10
+
11
+ $autoloaderPaths = [
12
+ __DIR__ . '/../../vendor/autoload.php',
13
+ __DIR__ . '/../vendor/autoload.php',
14
+ __DIR__ . '/../../../autoload.php',
15
+ ];
16
+
17
+ $autoloaded = false;
18
+ foreach ($autoloaderPaths as $path) {
19
+ if (file_exists($path)) {
20
+ require_once $path;
21
+ $autoloaded = true;
22
+ break;
23
+ }
24
+ }
25
+
26
+ if (!$autoloaded) {
27
+ fwrite(STDERR, "Error: Unable to load autoloader. Run 'composer install'.\n");
28
+ exit(1);
29
+ }
30
+
31
+ use Anonympins\Fingerprint\Optimization\Optimization;
32
+ use Anonympins\Fingerprint\Store\StoreManager;
33
+ use Anonympins\Fingerprint\Utils\RequestUtils;
34
+
35
+ // 1. Récupération des arguments CLI
36
+ $configPath = $argv[1] ?? null;
37
+ if (!$configPath) {
38
+ echo "Usage: php auto-tune.php [path_to_security-config.json]\n";
39
+ exit(1);
40
+ }
41
+
42
+ if (!file_exists($configPath)) {
43
+ fwrite(STDERR, "Error: Configuration file not found: {$configPath}\n");
44
+ exit(1);
45
+ }
46
+
47
+ $config = json_decode(file_get_contents($configPath), true);
48
+ if (json_last_error() !== JSON_ERROR_NONE) {
49
+ fwrite(STDERR, "Error: Invalid JSON configuration file.\n");
50
+ exit(1);
51
+ }
52
+
53
+ // 2. Connect to datastore to retrieve accumulated traffic logs
54
+ $store = StoreManager::getStore();
55
+ if (!$store) {
56
+ fwrite(STDERR, "Error: No active persistent datastore available.\n");
57
+ exit(1);
58
+ }
59
+
60
+ $rawTrafficLogs = $store->get('traffic_logs') ?? [];
61
+ if (empty($rawTrafficLogs)) {
62
+ echo "[AutoTuning] No traffic logs available for optimization.\n";
63
+ exit(0);
64
+ }
65
+
66
+ // 3. Sanitize data to prevent poisoning / Sybil attacks
67
+ $sanitizedLogs = RequestUtils::sanitizeTrafficData($rawTrafficLogs);
68
+
69
+ $minDataPoints = $config['autotuning']['minDataPoints'] ?? 200;
70
+ if (count($sanitizedLogs) < $minDataPoints) {
71
+ echo "[AutoTuning] Postponed: Insufficient sanitized logs (" . count($sanitizedLogs) . "/{$minDataPoints}).\n";
72
+ exit(0);
73
+ }
74
+
75
+ echo "[AutoTuning] Starting optimization pass on " . count($sanitizedLogs) . " traffic data points...\n";
76
+
77
+ // 4. Compute Pareto front
78
+ $paretoFront = Optimization::solveFullSecurityTuning(['trafficData' => $sanitizedLogs]);
79
+ if (empty($paretoFront)) {
80
+ fwrite(STDERR, "[AutoTuning] Optimization returned no solutions.\n");
81
+ exit(1);
82
+ }
83
+
84
+ // 5. Select the most balanced solution (closest to origin)
85
+ $bestSolution = $paretoFront[0];
86
+ $minDistance = sqrt(pow($bestSolution['objectives'][0], 2) + pow($bestSolution['objectives'][1], 2));
87
+ foreach ($paretoFront as $candidate) {
88
+ $distance = sqrt(pow($candidate['objectives'][0], 2) + pow($candidate['objectives'][1], 2));
89
+ if ($distance < $minDistance) {
90
+ $minDistance = $distance;
91
+ $bestSolution = $candidate;
92
+ }
93
+ }
94
+
95
+ $newConfig = $bestSolution['solution'];
96
+ $maxChangeVelocity = 0.15; // Max 15% inertial step per cycle
97
+
98
+ $applyInertialUpdate = function (array &$current, array $target) use ($maxChangeVelocity) {
99
+ $sumCurrent = array_sum($current);
100
+ if ($sumCurrent === 0) return;
101
+ $sumTarget = 0;
102
+ foreach ($current as $k => $v) {
103
+ if (isset($target[$k])) $sumTarget += $target[$k];
104
+ }
105
+ $ratio = ($sumTarget - $sumCurrent) / $sumCurrent;
106
+ $factor = 1 + max(-$maxChangeVelocity, min($maxChangeVelocity, $ratio));
107
+ foreach ($current as $k => &$v) {
108
+ if (isset($target[$k])) $v *= $factor;
109
+ }
110
+ };
111
+
112
+ $applyInertialUpdate($config['thresholds'], $newConfig['thresholds']);
113
+ $applyInertialUpdate($config['weights'], $newConfig['weights']);
114
+ $applyInertialUpdate($config['patterns'], $newConfig['patterns']);
115
+
116
+ // 6. Write back optimized parameters to configuration file
117
+ file_put_contents($configPath, json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
118
+ echo "[AutoTuning] Success: File {$configPath} updated with optimized parameters.\n";