@anonympins/fingerprint 0.3.2 → 0.3.3

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 (50) hide show
  1. package/CHANGELOG.md +172 -0
  2. package/README.md +276 -34
  3. package/composer.json +38 -0
  4. package/index.js +5 -0
  5. package/package.json +23 -18
  6. package/phpunit.xml +20 -0
  7. package/public/fp.js +2 -0
  8. package/src/js/build-client.js +69 -0
  9. package/{fingerprint.builder.js → src/js/fingerprint.builder.js} +168 -175
  10. package/{fingerprint.client.js → src/js/fingerprint.client.js} +634 -538
  11. package/src/js/fingerprint.client.obfuscated.js +1 -0
  12. package/{fingerprint.js → src/js/fingerprint.js} +255 -101
  13. package/{library.js → src/js/library.js} +1729 -1729
  14. package/{problem-manager.js → src/js/problem-manager.js} +539 -522
  15. package/src/php/AutoTuner.php +155 -0
  16. package/src/php/Challenge/ChallengeUtils.php +306 -0
  17. package/src/php/Config/SecurityProfiles.php +257 -0
  18. package/src/php/DirectFingerprint.php +81 -0
  19. package/src/php/FingerprintBuilder.php +185 -0
  20. package/src/php/FingerprintClient.php +118 -0
  21. package/src/php/FingerprintEngine.php +850 -0
  22. package/src/php/Optimization/FunctionRegistry.php +63 -0
  23. package/src/php/Optimization/Optimization.php +256 -0
  24. package/src/php/Optimization/OptimizationOperators.php +305 -0
  25. package/src/php/Optimization/ProblemInitializers.php +53 -0
  26. package/src/php/ProblemManager.php +255 -0
  27. package/src/php/RequestContext.php +87 -0
  28. package/src/php/Store/IStore.php +42 -0
  29. package/src/php/Store/InMemoryStore.php +67 -0
  30. package/src/php/Store/StoreManager.php +26 -0
  31. package/src/php/Tests/ChallengeUtilsTest.php +82 -0
  32. package/src/php/Tests/FingerprintBuilderTest.php +58 -0
  33. package/src/php/Tests/FingerprintEngineTest.php +219 -0
  34. package/src/php/Tests/PowTest.php +40 -0
  35. package/src/php/Tests/ProblemManagerTest.php +295 -0
  36. package/src/php/Tests/RequestUtilsTest.php +81 -0
  37. package/src/php/Tests/problems.config.json +9 -0
  38. package/src/php/Utils/BigInt.php +102 -0
  39. package/src/php/Utils/BlockList.php +100 -0
  40. package/src/php/Utils/Logger.php +30 -0
  41. package/src/php/Utils/MaliciousPatterns.php +59 -0
  42. package/src/php/Utils/RequestUtils.php +673 -0
  43. package/fingerprint.client.obfuscated.js +0 -1
  44. /package/{mongodb-store.js → src/js/mongodb-store.js} +0 -0
  45. /package/{optimization.worker.js → src/js/optimization.worker.js} +0 -0
  46. /package/{pow.solver.inline.js → src/js/pow.solver.inline.js} +0 -0
  47. /package/{pow.solver.js → src/js/pow.solver.js} +0 -0
  48. /package/{pow.worker.js → src/js/pow.worker.js} +0 -0
  49. /package/{redis-store.js → src/js/redis-store.js} +0 -0
  50. /package/{sql-store.js → src/js/sql-store.js} +0 -0
@@ -0,0 +1,102 @@
1
+ <?php
2
+
3
+ declare(strict_types=1);
4
+
5
+ namespace Anonympins\Fingerprint\Utils;
6
+
7
+ /**
8
+ * Classe d'émulation pour les opérations sur de grands entiers (BigInt),
9
+ * similaire au BigInt de JavaScript. Utilise l'extension GMP.
10
+ * @internal
11
+ */
12
+ class BigInt
13
+ {
14
+ /** @var \GMP */
15
+ private \GMP $gmp;
16
+
17
+ /**
18
+ * @param \GMP|string|int $number
19
+ */
20
+ public function __construct($number)
21
+ {
22
+ if ($number instanceof \GMP) {
23
+ $this->gmp = $number;
24
+ } else {
25
+ $this->gmp = gmp_init($number);
26
+ }
27
+ }
28
+
29
+ /**
30
+ * Crée une instance à partir d'une chaîne hexadécimale.
31
+ */
32
+ public static function fromHex(string $hex): self
33
+ {
34
+ return new self(gmp_init($hex, 16));
35
+ }
36
+
37
+ /**
38
+ * Compare cette instance avec une autre.
39
+ * @return int < 0 si this < other, 0 si this == other, > 0 si this > other.
40
+ */
41
+ public function compareTo(self $other): int
42
+ {
43
+ return gmp_cmp($this->gmp, $other->gmp);
44
+ }
45
+
46
+ /**
47
+ * Effectue un décalage de bits vers la gauche (<<).
48
+ */
49
+ public function shiftLeft(int $bits): self
50
+ {
51
+ return new self(gmp_mul($this->gmp, gmp_pow(2, $bits)));
52
+ }
53
+
54
+ /**
55
+ * Effectue un décalage de bits vers la droite (>>).
56
+ */
57
+ public function shiftRight(int $bits): self
58
+ {
59
+ return new self(gmp_div_q($this->gmp, gmp_pow(2, $bits)));
60
+ }
61
+
62
+ /**
63
+ * Retourne la représentation en chaîne de caractères.
64
+ */
65
+ public function __toString(): string
66
+ {
67
+ return gmp_strval($this->gmp);
68
+ }
69
+
70
+ /**
71
+ * Retourne la représentation hexadécimale.
72
+ */
73
+ public function toHex(): string
74
+ {
75
+ return gmp_strval($this->gmp, 16);
76
+ }
77
+
78
+ /**
79
+ * Retourne la valeur GMP sous-jacente.
80
+ * @return \GMP
81
+ */
82
+ public function getGmpValue(): \GMP
83
+ {
84
+ return $this->gmp;
85
+ }
86
+
87
+ /**
88
+ * Crée une instance à partir d'une puissance de 2.
89
+ */
90
+ public static function pow(int $base, int $exp): self
91
+ {
92
+ return new self(gmp_pow($base, $exp));
93
+ }
94
+
95
+ /**
96
+ * Soustrait un autre BigInt.
97
+ */
98
+ public function sub(self $other): self
99
+ {
100
+ return new self(gmp_sub($this->gmp, $other->gmp));
101
+ }
102
+ }
@@ -0,0 +1,100 @@
1
+ <?php
2
+
3
+ declare(strict_types=1);
4
+
5
+ namespace Anonympins\Fingerprint\Utils;
6
+
7
+ /**
8
+ * Implémentation PHP d'une liste de blocage IP/CIDR, similaire à Node.js `net.BlockList`.
9
+ */
10
+ class BlockList
11
+ {
12
+ /**
13
+ * @var array<string> Liste des IPs ou CIDR à bloquer.
14
+ */
15
+ private array $entries = [];
16
+
17
+ /**
18
+ * Ajoute une adresse IP ou une plage CIDR à la liste.
19
+ * @param string $entry Une adresse IP (ex: '192.168.1.1') ou une plage CIDR (ex: '192.168.1.0/24').
20
+ * @return void
21
+ */
22
+ public function add(string $entry): void
23
+ {
24
+ $this->entries[] = $entry;
25
+ }
26
+
27
+ /**
28
+ * Vérifie si une adresse IP est présente dans la liste de blocage.
29
+ * @param string $ip L'adresse IP à vérifier.
30
+ * @return bool True si l'IP est bloquée, false sinon.
31
+ */
32
+ public function check(string $ip): bool
33
+ {
34
+ foreach ($this->entries as $entry) {
35
+ if (str_contains($entry, '/')) {
36
+ // C'est une plage CIDR
37
+ if ($this->ipInCidr($ip, $entry)) {
38
+ return true;
39
+ }
40
+ } else {
41
+ // C'est une IP directe
42
+ if ($ip === $entry) {
43
+ return true;
44
+ }
45
+ }
46
+ }
47
+ return false;
48
+ }
49
+
50
+ /**
51
+ * Vérifie si une adresse IP se trouve dans une plage CIDR donnée.
52
+ * @param string $ip L'adresse IP à vérifier.
53
+ * @param string $cidr La plage CIDR (ex: '192.168.1.0/24').
54
+ * @return bool True si l'IP est dans la plage, false sinon.
55
+ */
56
+ private function ipInCidr(string $ip, string $cidr): bool
57
+ {
58
+ [$network, $mask] = explode('/', $cidr);
59
+ $mask = (int)$mask;
60
+
61
+ if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) && filter_var($network, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
62
+ // IPv6
63
+ $ipBinary = inet_pton($ip);
64
+ $networkBinary = inet_pton($network);
65
+
66
+ if ($ipBinary === false || $networkBinary === false) {
67
+ return false;
68
+ }
69
+
70
+ $ipHex = bin2hex($ipBinary);
71
+ $networkHex = bin2hex($networkBinary);
72
+
73
+ $numBytes = (int)ceil($mask / 8);
74
+ $compareLength = $numBytes * 2;
75
+
76
+ if (substr($ipHex, 0, $compareLength) !== substr($networkHex, 0, $compareLength)) {
77
+ return false;
78
+ }
79
+
80
+ if ($mask % 8 !== 0) {
81
+ $bitMask = (0xFF << (8 - ($mask % 8))) & 0xFF;
82
+ $ipByte = hexdec(substr($ipHex, $numBytes * 2 - 2, 2));
83
+ $networkByte = hexdec(substr($networkHex, $numBytes * 2 - 2, 2));
84
+ return ($ipByte & $bitMask) === ($networkByte & $bitMask);
85
+ }
86
+
87
+ return true;
88
+
89
+ } elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) && filter_var($network, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
90
+ // IPv4
91
+ $ipLong = ip2long($ip);
92
+ $networkLong = ip2long($network);
93
+ $wildcard = pow(2, (32 - $mask)) - 1;
94
+ $netmask = ~$wildcard;
95
+ return (($ipLong & $netmask) === ($networkLong & $netmask));
96
+ }
97
+
98
+ return false;
99
+ }
100
+ }
@@ -0,0 +1,30 @@
1
+ <?php
2
+
3
+ declare(strict_types=1);
4
+
5
+ namespace Anonympins\Fingerprint\Utils;
6
+
7
+ /**
8
+ * Un simple wrapper de logger pour passer les données à une fonction de rappel.
9
+ */
10
+ class Logger
11
+ {
12
+ /**
13
+ * @var callable
14
+ */
15
+ private $callback;
16
+
17
+ public function __construct(callable $callback)
18
+ {
19
+ $this->callback = $callback;
20
+ }
21
+
22
+ public function log(string $level, string $type, array $data): void
23
+ {
24
+ $logEntry = array_merge($data, [
25
+ 'type' => $type,
26
+ 'timestamp' => (int)floor(microtime(true) * 1000),
27
+ ]);
28
+ ($this->callback)($logEntry);
29
+ }
30
+ }
@@ -0,0 +1,59 @@
1
+ <?php
2
+
3
+ declare(strict_types=1);
4
+
5
+ namespace Anonympins\Fingerprint\Utils;
6
+
7
+ /**
8
+ * Fournit des fonctions pour détecter les patterns d'injection malveillants.
9
+ */
10
+ class MaliciousPatterns
11
+ {
12
+ /**
13
+ * @var array<string, string> Map des patterns malveillants regroupés par type.
14
+ */
15
+ private const INJECTION_PATTERNS = [
16
+ // SQL/NoSQL injections, including time-based attacks
17
+ 'sql' => '/(\$ne|\' *OR *\'1\'=\'1|[\'";]\s*--|; ?(DROP|TRUNCATE|DELETE)|UNION SELECT|(?:SLEEP|BENCHMARK)\s*\(|WAITFOR DELAY)/i',
18
+ // Log4Shell (JNDI injection)
19
+ 'log4shell' => '/\$\{jndi:(ldap|rmi|dns):/i',
20
+ // Server-Side Template Injection (SSTI) for engines like Jinja2, Twig, etc.
21
+ 'ssti' => '/\{\{.*\}\}|\{%.*%\}/',
22
+ // XML External Entity (XXE) injection
23
+ 'xxe' => '/<!ENTITY\s+.*SYSTEM/i',
24
+ // Path Traversal
25
+ 'traversal' => '/(\.\.\/|\.\.)/',
26
+ // Remote Command Execution (RCE)
27
+ // The original regex had an issue with `|cmd` being interpreted as a modifier in some PCRE versions.
28
+ // Using a non-capturing group (?:...) makes it more robust and fixes the compilation error.
29
+ 'rce' => '/`.*`|(?:^|[\n;&|]\s*)(?:ping|ls|whoami|cat|rm|ncat|nc|bash|sh|powershell|cmd)\b/i',
30
+ ];
31
+
32
+ /**
33
+ * Vérifie si une chaîne de caractères contient des patterns d'injection connus.
34
+ * @param string $str La chaîne à vérifier.
35
+ * @param array<string> $typesToDetect Les types d'injections à détecter (par défaut, tous).
36
+ * @return bool True si un pattern malveillant est détecté, false sinon.
37
+ */
38
+ public static function isMalicious(string $str, array $typesToDetect = []): bool
39
+ {
40
+ if (empty($typesToDetect)) {
41
+ $typesToDetect = array_keys(self::INJECTION_PATTERNS);
42
+ }
43
+
44
+ foreach ($typesToDetect as $type) {
45
+ if (isset(self::INJECTION_PATTERNS[$type])) {
46
+ try {
47
+ if (preg_match(self::INJECTION_PATTERNS[$type], $str)) {
48
+ return true;
49
+ }
50
+ }catch (\Exception $e){
51
+ error_log($type);
52
+ error_log($e->getMessage());
53
+ }
54
+ }
55
+ }
56
+
57
+ return false;
58
+ }
59
+ }