@anonympins/fingerprint 0.4.6 → 0.5.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.
@@ -1,145 +1,203 @@
1
- <?php
2
-
3
- declare(strict_types=1);
4
-
5
- namespace Anonympins\Fingerprint\Utils;
6
-
7
- class BigInt
8
- {
9
- private static bool $useGmp;
10
- /** @var \GMP|string */
11
- private $value;
12
-
13
- /**
14
- * @param \GMP|string|int $number Le nombre initial.
15
- */
16
- public function __construct($number)
17
- {
18
- if (!isset(self::$useGmp)) {
19
- self::$useGmp = extension_loaded('gmp');
20
- }
21
-
22
- if (self::$useGmp) {
23
- if ($number instanceof \GMP) {
24
- $this->value = $number;
25
- } else {
26
- $this->value = gmp_init($number);
27
- }
28
- } else {
29
- if ($number instanceof BigInt) {
30
- $this->value = $number->value;
31
- } else {
32
- $this->value = (string)$number;
33
- }
34
- }
35
- }
36
-
37
- /**
38
- * Crée une instance à partir d'une chaîne hexadécimale.
39
- */
40
- public static function fromHex(string $hex): BigInt
41
- {
42
- if (self::$useGmp) {
43
- return new self(gmp_init($hex, 16));
44
- } else {
45
- $dec = '0';
46
- $len = strlen($hex);
47
- for ($i = 0; $i < $len; $i++) {
48
- $dec = bcadd(bcmul($dec, '16'), (string)hexdec($hex[$i]));
49
- }
50
- return new self($dec);
51
- }
52
- }
53
-
54
- /**
55
- * Compare cette instance avec une autre.
56
- * @return int < 0 si this < other, 0 si this == other, > 0 si this > other.
57
- */
58
- public function compareTo(BigInt $other): int
59
- {
60
- if (self::$useGmp) {
61
- return gmp_cmp($this->value, $other->value);
62
- } else {
63
- return bccomp($this->value, $other->value);
64
- }
65
- }
66
-
67
- /**
68
- * Effectue un décalage de bits vers la gauche (<<).
69
- */
70
- public function shiftLeft(int $bits): BigInt
71
- {
72
- if (self::$useGmp) {
73
- return new self(gmp_mul($this->value, gmp_pow("2", $bits)));
74
- } else {
75
- return new self(bcmul($this->value, bcpow("2", (string)$bits)));
76
- }
77
- }
78
-
79
- /**
80
- * Effectue un décalage de bits vers la droite (>>).
81
- */
82
- public function shiftRight(int $bits): BigInt
83
- {
84
- if (self::$useGmp) {
85
- return new self(gmp_div_q($this->value, gmp_pow("2", $bits)));
86
- } else {
87
- return new self(bcdiv($this->value, bcpow("2", (string)$bits)));
88
- }
89
- }
90
-
91
- /**
92
- * Retourne la représentation en chaîne de caractères.
93
- */
94
- public function __toString(): string
95
- {
96
- if (self::$useGmp) {
97
- return gmp_strval($this->value);
98
- } else {
99
- return $this->value;
100
- }
101
- }
102
-
103
- /**
104
- * Retourne la représentation hexadécimale.
105
- */
106
- public function toHex(): string
107
- {
108
- if (self::$useGmp) {
109
- return gmp_strval($this->value, 16);
110
- } else {
111
- $hex = '';
112
- $dec = $this->value;
113
- while (bccomp($dec, '0') > 0) {
114
- $rem = bcmod($dec, '16');
115
- $hex = dechex((int)$rem) . $hex;
116
- $dec = bcdiv($dec, '16');
117
- }
118
- return $hex ?: '0';
119
- }
120
- }
121
-
122
- /**
123
- * Crée une instance à partir d'une puissance de 2.
124
- */
125
- public static function pow(int $base, int $exp): BigInt
126
- {
127
- if (self::$useGgmp) {
128
- return new self(gmp_pow((string)$base, $exp));
129
- } else {
130
- return new self(bcpow((string)$base, (string)$exp));
131
- }
132
- }
133
-
134
- /**
135
- * Soustrait un autre BigInt.
136
- */
137
- public function sub(BigInt $other): BigInt
138
- {
139
- if (self::$useGmp) {
140
- return new self(gmp_sub($this->value, $other->value));
141
- } else {
142
- return new self(bcsub($this->value, $other->value));
143
- }
144
- }
1
+ <?php
2
+
3
+ declare(strict_types=1);
4
+
5
+ namespace Anonympins\Fingerprint\Utils;
6
+
7
+ class BigInt
8
+ {
9
+ private static ?bool $useGmp = null;
10
+ /** @var \GMP|string */
11
+ private $value;
12
+
13
+ private static function useGmp(): bool
14
+ {
15
+ if (self::$useGmp === null) {
16
+ self::$useGmp = extension_loaded('gmp');
17
+ }
18
+ return self::$useGmp;
19
+ }
20
+
21
+ /**
22
+ * @param \GMP|string|int $number Le nombre initial.
23
+ */
24
+ public function __construct($number)
25
+ {
26
+ if (self::useGmp()) {
27
+ if ($number instanceof \GMP) {
28
+ $this->value = $number;
29
+ } else {
30
+ $str = (string)$number;
31
+ if (($dot = strpos($str, '.')) !== false) {
32
+ $str = substr($str, 0, $dot);
33
+ }
34
+ $this->value = gmp_init($str);
35
+ }
36
+ } else {
37
+ if ($number instanceof BigInt) {
38
+ $this->value = $number->value;
39
+ } else {
40
+ $str = (string)$number;
41
+ if (($dot = strpos($str, '.')) !== false) {
42
+ $str = substr($str, 0, $dot);
43
+ }
44
+ $this->value = $str;
45
+ }
46
+ }
47
+ }
48
+
49
+ /**
50
+ * Crée une instance à partir d'une chaîne hexadécimale.
51
+ */
52
+ public static function fromHex(string $hex): BigInt
53
+ {
54
+ $hex = trim(strtolower($hex));
55
+ if (substr($hex, 0, 2) === '0x') {
56
+ $hex = substr($hex, 2);
57
+ }
58
+ if (self::useGmp()) {
59
+ return new self(gmp_init($hex, 16));
60
+ } else {
61
+ $dec = '0';
62
+ $len = strlen($hex);
63
+ for ($i = 0; $i < $len; $i++) {
64
+ $dec = bcadd(bcmul($dec, '16', 0), (string)hexdec($hex[$i]), 0);
65
+ }
66
+ return new self($dec);
67
+ }
68
+ }
69
+
70
+ /**
71
+ * Compare cette instance avec une autre.
72
+ * @return int < 0 si this < other, 0 si this == other, > 0 si this > other.
73
+ */
74
+ public function compareTo(BigInt $other): int
75
+ {
76
+ if (self::useGmp()) {
77
+ return gmp_cmp($this->value, $other->value);
78
+ } else {
79
+ return bccomp($this->value, $other->value, 0);
80
+ }
81
+ }
82
+
83
+ /**
84
+ * Effectue un décalage de bits vers la gauche (<<).
85
+ */
86
+ public function shiftLeft(int $bits): BigInt
87
+ {
88
+ if (self::useGmp()) {
89
+ return new self(gmp_mul($this->value, gmp_pow("2", $bits)));
90
+ } else {
91
+ return new self(bcmul($this->value, bcpow("2", (string)$bits, 0), 0));
92
+ }
93
+ }
94
+
95
+ /**
96
+ * Effectue un décalage de bits vers la droite (>>).
97
+ */
98
+ public function shiftRight(int $bits): BigInt
99
+ {
100
+ if (self::useGmp()) {
101
+ return new self(gmp_div_q($this->value, gmp_pow("2", $bits)));
102
+ } else {
103
+ return new self(bcdiv($this->value, bcpow("2", (string)$bits, 0), 0));
104
+ }
105
+ }
106
+
107
+ /**
108
+ * Retourne la représentation en chaîne de caractères.
109
+ */
110
+ public function __toString(): string
111
+ {
112
+ if (self::useGmp()) {
113
+ return gmp_strval($this->value);
114
+ } else {
115
+ return $this->value;
116
+ }
117
+ }
118
+
119
+ /**
120
+ * Retourne la représentation hexadécimale.
121
+ */
122
+ public function toHex(): string
123
+ {
124
+ if (self::useGmp()) {
125
+ return gmp_strval($this->value, 16);
126
+ } else {
127
+ $hex = '';
128
+ $dec = $this->value;
129
+ while (bccomp($dec, '0', 0) > 0) {
130
+ $rem = bcmod($dec, '16', 0);
131
+ $hex = dechex((int)$rem) . $hex;
132
+ $dec = bcdiv($dec, '16', 0);
133
+ }
134
+ return $hex ?: '0';
135
+ }
136
+ }
137
+
138
+ /**
139
+ * Crée une instance à partir d'une puissance de 2.
140
+ */
141
+ public static function pow(int $base, int $exp): BigInt
142
+ {
143
+ if (self::useGmp()) {
144
+ return new self(gmp_pow((string)$base, $exp));
145
+ } else {
146
+ return new self(bcpow((string)$base, (string)$exp, 0));
147
+ }
148
+ }
149
+
150
+ /**
151
+ * Soustrait un autre BigInt.
152
+ */
153
+ public function sub(BigInt $other): BigInt
154
+ {
155
+ if (self::useGmp()) {
156
+ return new self(gmp_sub($this->value, $other->value));
157
+ } else {
158
+ return new self(bcsub($this->value, $other->value, 0));
159
+ }
160
+ }
161
+
162
+ /**
163
+ * Computes ($this ^ $exponent) % $modulus
164
+ */
165
+ public function modPow(BigInt $exponent, BigInt $modulus): BigInt
166
+ {
167
+ if (self::useGmp()) {
168
+ return new self(gmp_powm($this->value, $exponent->value, $modulus->value));
169
+ } else {
170
+ return new self(bcpowmod((string)$this->value, (string)$exponent->value, (string)$modulus->value, 0));
171
+ }
172
+ }
173
+
174
+ /**
175
+ * Computes $this % $modulus
176
+ */
177
+ public function mod(BigInt $modulus): BigInt
178
+ {
179
+ if (self::useGmp()) {
180
+ return new self(gmp_mod($this->value, $modulus->value));
181
+ } else {
182
+ return new self(bcmod((string)$this->value, (string)$modulus->value, 0));
183
+ }
184
+ }
185
+
186
+ public function add(BigInt $other): BigInt
187
+ {
188
+ if (self::useGmp()) {
189
+ return new self(gmp_add($this->value, $other->value));
190
+ } else {
191
+ return new self(bcadd((string)$this->value, (string)$other->value, 0));
192
+ }
193
+ }
194
+
195
+ public function mul(BigInt $other): BigInt
196
+ {
197
+ if (self::useGmp()) {
198
+ return new self(gmp_mul($this->value, $other->value));
199
+ } else {
200
+ return new self(bcmul((string)$this->value, (string)$other->value, 0));
201
+ }
202
+ }
145
203
  }
@@ -1,59 +1,75 @@
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
- }
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
+ // Server-Side Request Forgery (SSRF)
31
+ 'ssrf' => '/(?:https?:\/\/)?(?:127\.\d+\.\d+\.\d+|169\.254\.169\.254|10\.\d+\.\d+\.\d+|172\.(?:1[6-9]|2\d|3[01])\.\d+\.\d+|192\.168\.\d+\.\d+|localhost|0\.0\.0\.0|\[[0:]+1\])\b/i',
32
+ // Carriage Return Line Feed (CRLF) Injection
33
+ 'crlf' => '/[\r\n]|%0[ad]/i',
34
+ // Cross-Site Scripting (XSS)
35
+ 'xss' => '/(<script|javascript:|on\w+\s*=|alert\s*\(|confirm\s*\(|prompt\s*\(|<img\s+src[^>]+onerror|<iframe)/i',
36
+ // Open Redirect
37
+ 'openRedirect' => '/^(https?:)?\/\/(?![^\/]*?(localhost|127\.0\.0\.1))[^\s\/]+/i',
38
+ // Local/Remote File Inclusion (LFI/RFI)
39
+ 'lfi' => '/(?:etc\/passwd|win\.ini|boot\.ini|php:\/\/filter|data:\/\/|zip:\/\/)/i',
40
+ // Shellshock (CVE-2014-6271)
41
+ 'shellshock' => '/\(\)\s*\{\s*:\s*;\s*\}\s*/i',
42
+ // NoSQL Injection (MongoDB query operators)
43
+ 'nosql' => '/\$(?:eq|ne|gt|gte|lt|lte|in|nin|and|or|nor|not|expr|jsonSchema|mod|regex|text|where|elemMatch)/i',
44
+ ];
45
+
46
+ /**
47
+ * Vérifie si une chaîne de caractères contient des patterns d'injection connus.
48
+ * @param string $str La chaîne à vérifier.
49
+ * @param array<string> $typesToDetect Les types d'injections à détecter (par défaut, tous).
50
+ * @return bool True si un pattern malveillant est détecté, false sinon.
51
+ */
52
+ public static function isMalicious(string $str, array $typesToDetect = []): bool
53
+ {
54
+ if (empty($typesToDetect)) {
55
+ $typesToDetect = array_filter(array_keys(self::INJECTION_PATTERNS), function ($key) {
56
+ return $key !== 'openRedirect';
57
+ });
58
+ }
59
+
60
+ foreach ($typesToDetect as $type) {
61
+ if (isset(self::INJECTION_PATTERNS[$type])) {
62
+ try {
63
+ if (preg_match(self::INJECTION_PATTERNS[$type], $str)) {
64
+ return true;
65
+ }
66
+ }catch (\Exception $e){
67
+ error_log($type);
68
+ error_log($e->getMessage());
69
+ }
70
+ }
71
+ }
72
+
73
+ return false;
74
+ }
59
75
  }