@anonympins/fingerprint 0.3.8 → 0.4.0

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 (58) hide show
  1. package/CHANGELOG.md +331 -256
  2. package/README.md +62 -53
  3. package/composer.json +38 -38
  4. package/index.js +4 -4
  5. package/package.json +103 -103
  6. package/phpunit.xml +20 -20
  7. package/public/fp.js +1 -1
  8. package/public/fp.wasm +0 -0
  9. package/src/js/build-client.js +1 -1
  10. package/src/js/fingerprint.client.js +2 -0
  11. package/src/js/fingerprint.js +4429 -4381
  12. package/src/js/mongodb-store.js +79 -79
  13. package/src/js/pow.solver.inline.js +31 -0
  14. package/src/js/pow.solver.js +31 -0
  15. package/src/js/tests/fingerprint.builder.test.js +79 -0
  16. package/src/js/tests/fingerprint.client.init.test.js +120 -0
  17. package/src/js/tests/fingerprint.client.test.js +105 -0
  18. package/src/js/tests/fingerprint.engine.test.js +371 -0
  19. package/src/js/tests/fingerprint.isMalicious.test.js +117 -0
  20. package/src/js/tests/fingerprint.test.js +2319 -0
  21. package/src/js/tests/ip-reputation.test.js +132 -0
  22. package/src/js/tests/ja3AnomalyDetector.test.js +135 -0
  23. package/src/js/tests/library.test.js +96 -0
  24. package/src/js/tests/metrics.test.js +104 -0
  25. package/src/js/tests/pow.solver.test.js +198 -0
  26. package/src/js/tests/problem-manager.test.js +323 -0
  27. package/src/js/tests/stores.test.js +118 -0
  28. package/src/php/Challenge/ChallengeUtils.php +361 -361
  29. package/src/php/Config/SecurityProfiles.php +271 -266
  30. package/src/php/FingerprintBuilder.php +185 -185
  31. package/src/php/FingerprintClient.php +131 -131
  32. package/src/php/FingerprintEngine.php +1006 -1006
  33. package/src/php/Ja3AnomalyDetector.php +227 -227
  34. package/src/php/Optimization/FunctionRegistry.php +62 -62
  35. package/src/php/Optimization/Optimization.php +255 -255
  36. package/src/php/Optimization/OptimizationOperators.php +304 -304
  37. package/src/php/Store/InMemoryStore.php +66 -66
  38. package/src/php/Store/MongoDbStore.php +104 -104
  39. package/src/php/Store/RedisStore.php +53 -53
  40. package/src/php/Tests/ChallengeUtilsTest.php +81 -81
  41. package/src/php/Tests/FingerprintBuilderTest.php +57 -57
  42. package/src/php/Tests/FingerprintClientTest.php +71 -0
  43. package/src/php/Tests/FingerprintEngineTest.php +299 -299
  44. package/src/php/Tests/IpReputationTest.php +156 -156
  45. package/src/php/Tests/Ja3AnomalyDetectorTest.php +179 -179
  46. package/src/php/Tests/MetricsTest.php +45 -45
  47. package/src/php/Tests/PowTest.php +39 -39
  48. package/src/php/Tests/ProblemManagerTest.php +296 -296
  49. package/src/php/Tests/RequestUtilsTest.php +253 -145
  50. package/src/php/Tests/TLSClientHelloParserTest.php +118 -0
  51. package/src/php/Tests/problems.config.json +8 -8
  52. package/src/php/Utils/BigInt.php +144 -144
  53. package/src/php/Utils/Logger.php +29 -29
  54. package/src/php/Utils/MaliciousPatterns.php +58 -58
  55. package/src/php/Utils/MetricsManager.php +166 -166
  56. package/src/php/Utils/RequestUtils.php +1169 -1169
  57. package/src/php/Utils/TLSClientHelloParser.php +117 -0
  58. package/src/php/bin/auto-tune.php +117 -117
@@ -1,267 +1,272 @@
1
- <?php
2
-
3
- declare(strict_types=1);
4
-
5
- namespace Anonympins\Fingerprint\Config;
6
-
7
- /**
8
- * Définit les profils de sécurité prédéfinis pour la bibliothèque Fingerprint.
9
- * Ces profils contiennent les poids des scores de suspicion et les seuils de déclenchement.
10
- */
11
- class SecurityProfiles
12
- {
13
- /**
14
- * @var array<string, array<string, mixed>>
15
- */
16
- public const PROFILES = [
17
- /**
18
- * Balanced Profile (Default)
19
- * A general-purpose configuration suitable for most websites, offering a good mix of security and user experience.
20
- * It's sensitive enough to catch common bots without being overly aggressive towards legitimate users.
21
- */
22
- 'balanced' => [
23
- 'summary' => 'Balanced Profile (Default)',
24
- 'description' => 'A general-purpose configuration suitable for most websites, offering a good mix of security and user experience. It\'s sensitive enough to catch common bots without being overly aggressive towards legitimate users.',
25
- 'weights' => [
26
- 'historyScore' => 0.3,
27
- 'rotationScore' => 0.5,
28
- 'headerAnomalyScore' => 0.1,
29
- 'requestPatternScore' => 0.6,
30
- 'inconsistencyScore' => 0.8,
31
- 'behaviorScore' => 0.7,
32
- 'honeypotScore' => 1.0,
33
- 'crossLayerInconsistencyScore' => 0.4,
34
- 'timeInconsistencyScore' => 0.9,
35
- 'tlsSpoofingScore' => 0.8,
36
- 'botScore' => 1.0, // Poids pour le score de bot explicite
37
- 'cookieDroppingScore' => 0.9, // Pénalité élevée pour la suppression de cookies
38
- 'threatIntelScore' => 0.4, // Poids pour le renseignement sur les menaces (ex: IP de proxy connu)
39
- 'clientHintsInconsistencyScore' => 0.7, // Penalizes inconsistency between User-Agent and Client-Hints
40
- 'clickVarianceScore' => 0.6, // Poids pour la variance des clics
41
- 'subnetScore' => 0.5, // Pénalise les sous-réseaux IP avec une activité suspecte agrégée
42
- ],
43
- 'thresholds' => ['low' => 20, 'medium' => 45, 'high' => 75, 'block' => 95],
44
- 'patterns' => [
45
- 'velocityThreshold' => 800,
46
- 'burstThreshold' => 1500,
47
- 'scrapeThreshold' => 1000,
48
- 'historySize' => 10,
49
- 'minSamples' => 5,
50
- 'regularityThreshold' => 50,
51
- 'benfordThreshold' => 0.15,
52
- 'patternWeight' => 80,
53
- 'decayFactor' => 0.9,
54
- 'inactivityReset' => 5000,
55
- ],
56
- ],
57
-
58
- /**
59
- * Strict Profile
60
- * An aggressive configuration for sensitive applications (e.g., financial services, admin panels).
61
- * It uses lower suspicion thresholds and higher penalties for anomalies, prioritizing security over user convenience.
62
- * All new devices are challenged by default.
63
- */
64
- 'strict' => [
65
- 'summary' => 'Strict Profile',
66
- 'description' => 'An aggressive configuration for sensitive applications (e.g., financial services, admin panels). It uses lower suspicion thresholds and higher penalties for anomalies, prioritizing security over user convenience. All new devices are challenged by default.',
67
- 'weights' => [
68
- 'historyScore' => 0.4,
69
- 'rotationScore' => 0.6,
70
- 'headerAnomalyScore' => 0.2,
71
- 'requestPatternScore' => 0.8,
72
- 'inconsistencyScore' => 1.0,
73
- 'behaviorScore' => 0.8,
74
- 'honeypotScore' => 1.0,
75
- 'crossLayerInconsistencyScore' => 0.6,
76
- 'timeInconsistencyScore' => 1.0,
77
- 'tlsSpoofingScore' => 1.0,
78
- 'botScore' => 1.0,
79
- 'cookieDroppingScore' => 1.0, // Pénalité maximale
80
- 'threatIntelScore' => 0.7, // Poids élevé pour les menaces connues (Tor, etc.)
81
- 'clientHintsInconsistencyScore' => 0.9, // Very high penalty in strict mode
82
- 'clickVarianceScore' => 0.7, // High weight for click variance
83
- 'subnetScore' => 0.7, // Poids plus élevé en mode strict
84
- ],
85
- 'thresholds' => ['low' => 10, 'medium' => 35, 'high' => 65, 'block' => 90],
86
- 'patterns' => [
87
- 'velocityThreshold' => 1000,
88
- 'burstThreshold' => 1800,
89
- 'scrapeThreshold' => 1200,
90
- 'historySize' => 15,
91
- 'minSamples' => 4,
92
- 'regularityThreshold' => 40,
93
- 'benfordThreshold' => 0.12,
94
- 'patternWeight' => 90,
95
- 'decayFactor' => 0.85,
96
- 'inactivityReset' => 4000,
97
- ],
98
- 'challengeNewDevices' => true, // Challenge all new devices
99
- ],
100
-
101
- /**
102
- * API Profile
103
- * Optimized for protecting API endpoints. This profile is highly sensitive to request patterns (velocity, bursts)
104
- * and less reliant on browser-specific behavioral metrics. It's designed to quickly identify and throttle scrapers and automated clients.
105
- */
106
- 'api' => [
107
- 'summary' => 'API Profile',
108
- 'description' => 'Optimized for protecting API endpoints. This profile is highly sensitive to request patterns (velocity, bursts) and less reliant on browser-specific behavioral metrics. It\'s designed to quickly identify and throttle scrapers and automated clients.',
109
- 'weights' => [
110
- 'historyScore' => 0.5,
111
- 'rotationScore' => 0.5,
112
- 'headerAnomalyScore' => 0.3,
113
- 'requestPatternScore' => 1.0, // Very high weight for API patterns
114
- 'inconsistencyScore' => 0.7,
115
- 'behaviorScore' => 0.2, // Lower weight, as browser behavior is not applicable
116
- 'honeypotScore' => 1.0,
117
- 'crossLayerInconsistencyScore' => 0.5,
118
- 'timeInconsistencyScore' => 0.8,
119
- 'tlsSpoofingScore' => 0.7,
120
- 'botScore' => 0.5,
121
- 'cookieDroppingScore' => 0.8, // Important pour les clients API qui doivent maintenir un état
122
- 'threatIntelScore' => 0.5, // APIs are often targeted by malicious IPs
123
- 'clientHintsInconsistencyScore' => 0.6, // Relevant signal for APIs
124
- 'clickVarianceScore' => 0.3, // Low weight as not applicable to APIs
125
- 'subnetScore' => 0.8, // Très important pour les API pour détecter les botnets
126
- ],
127
- 'thresholds' => ['low' => 25, 'medium' => 50, 'high' => 80, 'block' => 95],
128
- 'patterns' => [
129
- 'velocityThreshold' => 200, // APIs are expected to be fast
130
- 'burstThreshold' => 500,
131
- 'scrapeThreshold' => 400,
132
- 'historySize' => 20,
133
- 'minSamples' => 8,
134
- 'regularityThreshold' => 20,
135
- 'benfordThreshold' => 0.18,
136
- 'patternWeight' => 85,
137
- 'decayFactor' => 0.9,
138
- 'inactivityReset' => 10000,
139
- ],
140
- // This would be a callable in PHP, but for now, we represent its intent.
141
- 'isApiRequest' => 'req.path.startsWith("/api/") || req.headers.accept?.includes("application/json")',
142
- ],
143
-
144
- /**
145
- * Blog Profile
146
- * Tuned for blogs and content-heavy websites. This profile focuses on detecting content scraping and comment spam
147
- * by placing a high weight on request patterns and honeypot traps, while being more lenient on behavioral metrics
148
- * typical of readers.
149
- */
150
- 'blog' => [
151
- 'summary' => 'Blog Profile',
152
- 'description' => 'Tuned for blogs and content-heavy websites. This profile focuses on detecting content scraping and comment spam by placing a high weight on request patterns and honeypot traps, while being more lenient on behavioral metrics typical of readers.',
153
- 'weights' => [
154
- 'historyScore' => 0.2,
155
- 'rotationScore' => 0.3,
156
- 'headerAnomalyScore' => 0.1,
157
- 'requestPatternScore' => 0.8, // High weight to detect content scraping
158
- 'inconsistencyScore' => 0.7,
159
- 'behaviorScore' => 0.5, // Less emphasis on complex interactions
160
- 'honeypotScore' => 1.0, // Crucial for comment spam
161
- 'crossLayerInconsistencyScore' => 0.4,
162
- 'timeInconsistencyScore' => 0.8,
163
- 'tlsSpoofingScore' => 0.6,
164
- 'botScore' => 0.8,
165
- 'cookieDroppingScore' => 0.7, // Moins critique, mais toujours un signal
166
- 'threatIntelScore' => 0.3, // Lower priority for a blog
167
- 'clientHintsInconsistencyScore' => 0.5,
168
- 'clickVarianceScore' => 0.5, // Moderate weight for click variance
169
- 'subnetScore' => 0.4, // Utile contre le spam de commentaires coordonné
170
- ],
171
- 'thresholds' => ['low' => 25, 'medium' => 55, 'high' => 80, 'block' => 95],
172
- 'patterns' => [
173
- 'velocityThreshold' => 1000, // Readers can be fast
174
- 'burstThreshold' => 2000,
175
- 'scrapeThreshold' => 800, // Very sensitive to scraping patterns
176
- 'historySize' => 12,
177
- 'minSamples' => 5,
178
- 'regularityThreshold' => 60,
179
- 'benfordThreshold' => 0.16,
180
- 'patternWeight' => 85,
181
- 'decayFactor' => 0.92,
182
- 'inactivityReset' => 10000,
183
- ],
184
- ],
185
-
186
- /**
187
- * E-commerce Profile
188
- * A strict profile tailored for e-commerce sites. It's designed to combat inventory scalping,
189
- * price scraping, and account takeover attempts by using high weights for request patterns and fingerprint inconsistency.
190
- * It also challenges all new devices to increase the cost for bots.
191
- */
192
- 'ecommerce' => [
193
- 'summary' => 'E-commerce Profile',
194
- 'description' => 'A strict profile tailored for e-commerce sites. It\'s designed to combat inventory scalping, price scraping, and account takeover attempts by using high weights for request patterns and fingerprint inconsistency. It also challenges all new devices to increase the cost for bots.',
195
- 'weights' => [
196
- 'historyScore' => 0.4,
197
- 'rotationScore' => 0.6,
198
- 'headerAnomalyScore' => 0.2,
199
- 'inconsistencyScore' => 1.0, // Crucial for preventing account takeover
200
- 'behaviorScore' => 0.8, // Important for checkout/login forms
201
- 'honeypotScore' => 1.0,
202
- 'crossLayerInconsistencyScore' => 0.7,
203
- // NOUVEAU: Ajout des scores manquants pour une configuration complète
204
- 'requestPatternScore' => 0.9, // Poids unifié pour les patterns, remplace les scores scindés
205
- 'timeInconsistencyScore' => 0.9,
206
- 'tlsSpoofingScore' => 0.9,
207
- 'botScore' => 1.0,
208
- 'cookieDroppingScore' => 1.0, // Crucial pour la détection de bots e-commerce
209
- 'threatIntelScore' => 0.8, // Very important for e-commerce (scalping proxies)
210
- 'clientHintsInconsistencyScore' => 0.9, // Very important for e-commerce
211
- 'clickVarianceScore' => 0.8, // Very high weight for click variance
212
- 'subnetScore' => 0.9, // Crucial contre les attaques de scalping distribuées
213
- ],
214
- 'thresholds' => ['low' => 15, 'medium' => 40, 'high' => 70, 'block' => 90],
215
- 'patterns' => [
216
- 'velocityThreshold' => 500, // Bots are very fast
217
- 'burstThreshold' => 1000, // Detects rapid retries on the same product/action
218
- 'scrapeThreshold' => 600,
219
- 'historySize' => 15,
220
- 'minSamples' => 6,
221
- 'regularityThreshold' => 30,
222
- 'benfordThreshold' => 0.14,
223
- 'patternWeight' => 95,
224
- 'decayFactor' => 0.88,
225
- 'inactivityReset' => 3000,
226
- ],
227
- 'challengeNewDevices' => true, // New devices are suspicious in e-commerce
228
- // This would be a callable in PHP, but for now, we represent its intent.
229
- 'isApiRequest' => 'req.path.startsWith("/api/cart") || req.path.startsWith("/api/stock") || req.path.startsWith("/api/checkout")',
230
- ],
231
- ];
232
-
233
- /**
234
- * Crée une configuration de sécurité basée sur un profil nommé, avec des surcharges optionnelles.
235
- *
236
- * @param string $profileName Le nom du profil à utiliser ('balanced', 'strict', 'api', etc.).
237
- * @param array<string, mixed> $overrides Un tableau pour fusionner profondément avec le profil, permettant la personnalisation.
238
- * @return array<string, mixed> L'objet de configuration de sécurité final.
239
- */
240
- public static function createSecurityProfile(string $profileName = 'balanced', array $overrides = []): array
241
- {
242
- $baseProfile = self::PROFILES[$profileName] ?? self::PROFILES['balanced'];
243
- return self::deepMerge($baseProfile, $overrides);
244
- }
245
-
246
- /**
247
- * Fusionne profondément deux tableaux. Les propriétés du tableau `$source` écrasent celles du tableau `$target`.
248
- *
249
- * @param array<string, mixed> $target Le tableau cible.
250
- * @param array<string, mixed> $source Le tableau source.
251
- * @return array<string, mixed> Le tableau fusionné.
252
- */
253
- private static function deepMerge(array $target, array $source): array
254
- {
255
- $output = $target;
256
-
257
- foreach ($source as $key => $value) {
258
- if (is_array($value) && isset($output[$key]) && is_array($output[$key])) {
259
- $output[$key] = self::deepMerge($output[$key], $value);
260
- } else {
261
- $output[$key] = $value;
262
- }
263
- }
264
-
265
- return $output;
266
- }
1
+ <?php
2
+
3
+ declare(strict_types=1);
4
+
5
+ namespace Anonympins\Fingerprint\Config;
6
+
7
+ /**
8
+ * Définit les profils de sécurité prédéfinis pour la bibliothèque Fingerprint.
9
+ * Ces profils contiennent les poids des scores de suspicion et les seuils de déclenchement.
10
+ */
11
+ class SecurityProfiles
12
+ {
13
+ /**
14
+ * @var array<string, array<string, mixed>>
15
+ */
16
+ public const PROFILES = [
17
+ /**
18
+ * Balanced Profile (Default)
19
+ * A general-purpose configuration suitable for most websites, offering a good mix of security and user experience.
20
+ * It's sensitive enough to catch common bots without being overly aggressive towards legitimate users.
21
+ */
22
+ 'balanced' => [
23
+ 'summary' => 'Balanced Profile (Default)',
24
+ 'description' => 'A general-purpose configuration suitable for most websites, offering a good mix of security and user experience. It\'s sensitive enough to catch common bots without being overly aggressive towards legitimate users.',
25
+ 'weights' => [
26
+ 'historyScore' => 0.3,
27
+ 'rotationScore' => 0.5,
28
+ 'headerAnomalyScore' => 0.1,
29
+ 'requestPatternScore' => 0.6,
30
+ 'inconsistencyScore' => 0.8,
31
+ 'behaviorScore' => 0.7,
32
+ 'honeypotScore' => 1.0,
33
+ 'crossLayerInconsistencyScore' => 0.4,
34
+ 'timeInconsistencyScore' => 0.9,
35
+ 'tlsSpoofingScore' => 0.8,
36
+ 'botScore' => 1.0, // Poids pour le score de bot explicite
37
+ 'cookieDroppingScore' => 0.9, // Pénalité élevée pour la suppression de cookies
38
+ 'threatIntelScore' => 0.4, // Poids pour le renseignement sur les menaces (ex: IP de proxy connu)
39
+ 'clientHintsInconsistencyScore' => 0.7, // Penalizes inconsistency between User-Agent and Client-Hints
40
+ 'clickVarianceScore' => 0.6, // Poids pour la variance des clics
41
+ 'subnetScore' => 0.5, // Pénalise les sous-réseaux IP avec une activité suspecte agrégée
42
+ ],
43
+ 'thresholds' => ['low' => 20, 'medium' => 45, 'high' => 75, 'block' => 95],
44
+ 'patterns' => [
45
+ 'velocityThreshold' => 800,
46
+ 'burstThreshold' => 1500,
47
+ 'scrapeThreshold' => 1000,
48
+ 'historySize' => 10,
49
+ 'minSamples' => 5,
50
+ 'regularityThreshold' => 50,
51
+ 'benfordThreshold' => 0.15,
52
+ 'patternWeight' => 80,
53
+ 'decayFactor' => 0.9,
54
+ 'inactivityReset' => 5000,
55
+ ],
56
+ 'wasm' => true,
57
+ ],
58
+
59
+ /**
60
+ * Strict Profile
61
+ * An aggressive configuration for sensitive applications (e.g., financial services, admin panels).
62
+ * It uses lower suspicion thresholds and higher penalties for anomalies, prioritizing security over user convenience.
63
+ * All new devices are challenged by default.
64
+ */
65
+ 'strict' => [
66
+ 'summary' => 'Strict Profile',
67
+ 'description' => 'An aggressive configuration for sensitive applications (e.g., financial services, admin panels). It uses lower suspicion thresholds and higher penalties for anomalies, prioritizing security over user convenience. All new devices are challenged by default.',
68
+ 'weights' => [
69
+ 'historyScore' => 0.4,
70
+ 'rotationScore' => 0.6,
71
+ 'headerAnomalyScore' => 0.2,
72
+ 'requestPatternScore' => 0.8,
73
+ 'inconsistencyScore' => 1.0,
74
+ 'behaviorScore' => 0.8,
75
+ 'honeypotScore' => 1.0,
76
+ 'crossLayerInconsistencyScore' => 0.6,
77
+ 'timeInconsistencyScore' => 1.0,
78
+ 'tlsSpoofingScore' => 1.0,
79
+ 'botScore' => 1.0,
80
+ 'cookieDroppingScore' => 1.0, // Pénalité maximale
81
+ 'threatIntelScore' => 0.7, // Poids élevé pour les menaces connues (Tor, etc.)
82
+ 'clientHintsInconsistencyScore' => 0.9, // Very high penalty in strict mode
83
+ 'clickVarianceScore' => 0.7, // High weight for click variance
84
+ 'subnetScore' => 0.7, // Poids plus élevé en mode strict
85
+ ],
86
+ 'thresholds' => ['low' => 10, 'medium' => 35, 'high' => 65, 'block' => 90],
87
+ 'patterns' => [
88
+ 'velocityThreshold' => 1000,
89
+ 'burstThreshold' => 1800,
90
+ 'scrapeThreshold' => 1200,
91
+ 'historySize' => 15,
92
+ 'minSamples' => 4,
93
+ 'regularityThreshold' => 40,
94
+ 'benfordThreshold' => 0.12,
95
+ 'patternWeight' => 90,
96
+ 'decayFactor' => 0.85,
97
+ 'inactivityReset' => 4000,
98
+ ],
99
+ 'challengeNewDevices' => true, // Challenge all new devices
100
+ 'wasm' => true,
101
+ ],
102
+
103
+ /**
104
+ * API Profile
105
+ * Optimized for protecting API endpoints. This profile is highly sensitive to request patterns (velocity, bursts)
106
+ * and less reliant on browser-specific behavioral metrics. It's designed to quickly identify and throttle scrapers and automated clients.
107
+ */
108
+ 'api' => [
109
+ 'summary' => 'API Profile',
110
+ 'description' => 'Optimized for protecting API endpoints. This profile is highly sensitive to request patterns (velocity, bursts) and less reliant on browser-specific behavioral metrics. It\'s designed to quickly identify and throttle scrapers and automated clients.',
111
+ 'weights' => [
112
+ 'historyScore' => 0.5,
113
+ 'rotationScore' => 0.5,
114
+ 'headerAnomalyScore' => 0.3,
115
+ 'requestPatternScore' => 1.0, // Very high weight for API patterns
116
+ 'inconsistencyScore' => 0.7,
117
+ 'behaviorScore' => 0.2, // Lower weight, as browser behavior is not applicable
118
+ 'honeypotScore' => 1.0,
119
+ 'crossLayerInconsistencyScore' => 0.5,
120
+ 'timeInconsistencyScore' => 0.8,
121
+ 'tlsSpoofingScore' => 0.7,
122
+ 'botScore' => 0.5,
123
+ 'cookieDroppingScore' => 0.8, // Important pour les clients API qui doivent maintenir un état
124
+ 'threatIntelScore' => 0.5, // APIs are often targeted by malicious IPs
125
+ 'clientHintsInconsistencyScore' => 0.6, // Relevant signal for APIs
126
+ 'clickVarianceScore' => 0.3, // Low weight as not applicable to APIs
127
+ 'subnetScore' => 0.8, // Très important pour les API pour détecter les botnets
128
+ ],
129
+ 'thresholds' => ['low' => 25, 'medium' => 50, 'high' => 80, 'block' => 95],
130
+ 'patterns' => [
131
+ 'velocityThreshold' => 200, // APIs are expected to be fast
132
+ 'burstThreshold' => 500,
133
+ 'scrapeThreshold' => 400,
134
+ 'historySize' => 20,
135
+ 'minSamples' => 8,
136
+ 'regularityThreshold' => 20,
137
+ 'benfordThreshold' => 0.18,
138
+ 'patternWeight' => 85,
139
+ 'decayFactor' => 0.9,
140
+ 'inactivityReset' => 10000,
141
+ ],
142
+ // This would be a callable in PHP, but for now, we represent its intent.
143
+ 'isApiRequest' => 'req.path.startsWith("/api/") || req.headers.accept?.includes("application/json")',
144
+ 'wasm' => true,
145
+ ],
146
+
147
+ /**
148
+ * Blog Profile
149
+ * Tuned for blogs and content-heavy websites. This profile focuses on detecting content scraping and comment spam
150
+ * by placing a high weight on request patterns and honeypot traps, while being more lenient on behavioral metrics
151
+ * typical of readers.
152
+ */
153
+ 'blog' => [
154
+ 'summary' => 'Blog Profile',
155
+ 'description' => 'Tuned for blogs and content-heavy websites. This profile focuses on detecting content scraping and comment spam by placing a high weight on request patterns and honeypot traps, while being more lenient on behavioral metrics typical of readers.',
156
+ 'weights' => [
157
+ 'historyScore' => 0.2,
158
+ 'rotationScore' => 0.3,
159
+ 'headerAnomalyScore' => 0.1,
160
+ 'requestPatternScore' => 0.8, // High weight to detect content scraping
161
+ 'inconsistencyScore' => 0.7,
162
+ 'behaviorScore' => 0.5, // Less emphasis on complex interactions
163
+ 'honeypotScore' => 1.0, // Crucial for comment spam
164
+ 'crossLayerInconsistencyScore' => 0.4,
165
+ 'timeInconsistencyScore' => 0.8,
166
+ 'tlsSpoofingScore' => 0.6,
167
+ 'botScore' => 0.8,
168
+ 'cookieDroppingScore' => 0.7, // Moins critique, mais toujours un signal
169
+ 'threatIntelScore' => 0.3, // Lower priority for a blog
170
+ 'clientHintsInconsistencyScore' => 0.5,
171
+ 'clickVarianceScore' => 0.5, // Moderate weight for click variance
172
+ 'subnetScore' => 0.4, // Utile contre le spam de commentaires coordonné
173
+ ],
174
+ 'thresholds' => ['low' => 25, 'medium' => 55, 'high' => 80, 'block' => 95],
175
+ 'patterns' => [
176
+ 'velocityThreshold' => 1000, // Readers can be fast
177
+ 'burstThreshold' => 2000,
178
+ 'scrapeThreshold' => 800, // Very sensitive to scraping patterns
179
+ 'historySize' => 12,
180
+ 'minSamples' => 5,
181
+ 'regularityThreshold' => 60,
182
+ 'benfordThreshold' => 0.16,
183
+ 'patternWeight' => 85,
184
+ 'decayFactor' => 0.92,
185
+ 'inactivityReset' => 10000,
186
+ ],
187
+ 'wasm' => true,
188
+ ],
189
+
190
+ /**
191
+ * E-commerce Profile
192
+ * A strict profile tailored for e-commerce sites. It's designed to combat inventory scalping,
193
+ * price scraping, and account takeover attempts by using high weights for request patterns and fingerprint inconsistency.
194
+ * It also challenges all new devices to increase the cost for bots.
195
+ */
196
+ 'ecommerce' => [
197
+ 'summary' => 'E-commerce Profile',
198
+ 'description' => 'A strict profile tailored for e-commerce sites. It\'s designed to combat inventory scalping, price scraping, and account takeover attempts by using high weights for request patterns and fingerprint inconsistency. It also challenges all new devices to increase the cost for bots.',
199
+ 'weights' => [
200
+ 'historyScore' => 0.4,
201
+ 'rotationScore' => 0.6,
202
+ 'headerAnomalyScore' => 0.2,
203
+ 'inconsistencyScore' => 1.0, // Crucial for preventing account takeover
204
+ 'behaviorScore' => 0.8, // Important for checkout/login forms
205
+ 'honeypotScore' => 1.0,
206
+ 'crossLayerInconsistencyScore' => 0.7,
207
+ // NOUVEAU: Ajout des scores manquants pour une configuration complète
208
+ 'requestPatternScore' => 0.9, // Poids unifié pour les patterns, remplace les scores scindés
209
+ 'timeInconsistencyScore' => 0.9,
210
+ 'tlsSpoofingScore' => 0.9,
211
+ 'botScore' => 1.0,
212
+ 'cookieDroppingScore' => 1.0, // Crucial pour la détection de bots e-commerce
213
+ 'threatIntelScore' => 0.8, // Very important for e-commerce (scalping proxies)
214
+ 'clientHintsInconsistencyScore' => 0.9, // Very important for e-commerce
215
+ 'clickVarianceScore' => 0.8, // Very high weight for click variance
216
+ 'subnetScore' => 0.9, // Crucial contre les attaques de scalping distribuées
217
+ ],
218
+ 'thresholds' => ['low' => 15, 'medium' => 40, 'high' => 70, 'block' => 90],
219
+ 'patterns' => [
220
+ 'velocityThreshold' => 500, // Bots are very fast
221
+ 'burstThreshold' => 1000, // Detects rapid retries on the same product/action
222
+ 'scrapeThreshold' => 600,
223
+ 'historySize' => 15,
224
+ 'minSamples' => 6,
225
+ 'regularityThreshold' => 30,
226
+ 'benfordThreshold' => 0.14,
227
+ 'patternWeight' => 95,
228
+ 'decayFactor' => 0.88,
229
+ 'inactivityReset' => 3000,
230
+ ],
231
+ 'challengeNewDevices' => true, // New devices are suspicious in e-commerce
232
+ // This would be a callable in PHP, but for now, we represent its intent.
233
+ 'isApiRequest' => 'req.path.startsWith("/api/cart") || req.path.startsWith("/api/stock") || req.path.startsWith("/api/checkout")',
234
+ 'wasm' => true,
235
+ ],
236
+ ];
237
+
238
+ /**
239
+ * Crée une configuration de sécurité basée sur un profil nommé, avec des surcharges optionnelles.
240
+ *
241
+ * @param string $profileName Le nom du profil à utiliser ('balanced', 'strict', 'api', etc.).
242
+ * @param array<string, mixed> $overrides Un tableau pour fusionner profondément avec le profil, permettant la personnalisation.
243
+ * @return array<string, mixed> L'objet de configuration de sécurité final.
244
+ */
245
+ public static function createSecurityProfile(string $profileName = 'balanced', array $overrides = []): array
246
+ {
247
+ $baseProfile = self::PROFILES[$profileName] ?? self::PROFILES['balanced'];
248
+ return self::deepMerge($baseProfile, $overrides);
249
+ }
250
+
251
+ /**
252
+ * Fusionne profondément deux tableaux. Les propriétés du tableau `$source` écrasent celles du tableau `$target`.
253
+ *
254
+ * @param array<string, mixed> $target Le tableau cible.
255
+ * @param array<string, mixed> $source Le tableau source.
256
+ * @return array<string, mixed> Le tableau fusionné.
257
+ */
258
+ public static function deepMerge(array $target, array $source): array
259
+ {
260
+ $output = $target;
261
+
262
+ foreach ($source as $key => $value) {
263
+ if (is_array($value) && isset($output[$key]) && is_array($output[$key])) {
264
+ $output[$key] = self::deepMerge($output[$key], $value);
265
+ } else {
266
+ $output[$key] = $value;
267
+ }
268
+ }
269
+
270
+ return $output;
271
+ }
267
272
  }