|
|
@@ -208,18 +208,20 @@ class SecurityMiddleware
|
|
208
|
208
|
protected function detectCommandInjection(string $input): bool
|
|
209
|
209
|
{
|
|
210
|
210
|
$patterns = [
|
|
211
|
|
- // 系统命令
|
|
212
|
|
- '/(?:system|exec|shell_exec|passthru|proc_open|popen|pcntl_exec)\s*\(/i',
|
|
|
211
|
+ // 系统命令函数调用(更精确的匹配)
|
|
|
212
|
+ '/(?:system|exec|shell_exec|passthru|proc_open|popen|pcntl_exec)\s*\(\s*[\'\"\$\w]/i',
|
|
213
|
213
|
|
|
214
|
|
- // 管道符
|
|
215
|
|
- '/\|\s*\w+/',
|
|
216
|
|
- '/;\s*\w+/',
|
|
|
214
|
+ // 管道符后跟命令(避免匹配单纯的|字符)
|
|
|
215
|
+ '/\|\s*(?:rm\s|cat\s|ls\s|wget\s|curl\s|nc\s|sh\s|bash\s)/i',
|
|
217
|
216
|
|
|
218
|
|
- // 危险命令
|
|
219
|
|
- '/\b(?:rm\s+-|cat\s+\/|wget\s+|curl\s+|nc\s+)\b/i',
|
|
|
217
|
+ // 分号后跟命令(避免匹配HTML实体如 )
|
|
|
218
|
+ '/;\s*(?:rm\s|cat\s|ls\s|wget\s|curl\s|nc\s|sh\s|bash\s|\$\w+)/i',
|
|
220
|
219
|
|
|
221
|
|
- // 反引号
|
|
222
|
|
- '/`.*?`/',
|
|
|
220
|
+ // 危险命令(更精确的匹配)
|
|
|
221
|
+ '/\b(?:rm\s+-(?:rf?|i)\b|cat\s+\/\w|wget\s+http|curl\s+http|nc\s+-[lv])\b/i',
|
|
|
222
|
+
|
|
|
223
|
+ // 反引号包裹命令(避免匹配代码块中的反引号)
|
|
|
224
|
+ '/`\s*(?:rm\s|cat\s|ls\s|wget\s|curl\s|nc\s|sh\s|bash\s)/i',
|
|
223
|
225
|
];
|
|
224
|
226
|
|
|
225
|
227
|
foreach ($patterns as $pattern) {
|