```
██╗ ██╗███████╗███████╗██████╗ ██████╗ ██████╗ ██████╗ ██████╗ ████████╗███████╗ ██████╗████████╗
╚██╗██╔╝╚══███╔╝██╔════╝██╔══██╗██╔═══██╗██╔══██╗██╔══██╗██╔═══██╗╚══██╔══╝██╔════╝██╔════╝╚══██╔══╝
╚███╔╝ ███╔╝ █████╗ ██████╔╝██║ ██║██████╔╝██████╔╝██║ ██║ ██║ █████╗ ██║ ██║
██╔██╗ ███╔╝ ██╔══╝ ██╔══██╗██║ ██║██╔═══╝ ██╔══██╗██║ ██║ ██║ ██╔══╝ ██║ ██║
██╔╝ ██╗███████╗███████╗██║ ██║╚██████╔╝██║ ██║ ██║╚██████╔╝ ██║ ███████╗╚██████╗ ██║
╚═╝ ╚═╝╚══════╝╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚══════╝ ╚═════╝ ╚═╝
```
**一款面向现代 Web 的轻量级、基于文件的 PHP 防火墙。**
无需数据库。无需外部服务。绝不妥协。
[](https://php.net)
[](https://packagist.org/packages/webrium/xzeroprotect)
[](LICENSE)
[]()
## 为什么选择 xZeroProtect?
每天,都有机器人爬取你的应用程序,试图寻找暴露的 `.env` 文件、WordPress 管理后台、SQL 注入向量以及已知的 CVE 漏洞——即使你根本没有运行 WordPress。xZeroProtect 在 PHP 层拦截这些请求,它没有任何外部依赖,不需要数据库连接,并提供了简洁的 API,让你能在几分钟内完成配置调整。
- **基于文件** —— 所有数据均存储在磁盘上;不需要 MySQL、Redis 或 memcached
- **零依赖** —— 纯 PHP 8.0+ 实现,无任何其他依赖
- **可组合** —— 每个检测模块都可以独立启用、禁用或扩展
- **学习模式** —— 仅记录威胁而不进行拦截,非常适合在正式上线前进行调整
- **感知 Apache** —— 可选将永久封禁的 IP 写入 `.htaccess`,以便 Apache 在 PHP 启动之前就拒绝这些连接
## 安装
```
composer require webrium/xzeroprotect
```
## 快速开始
将这两行代码添加到你的 `index.php` 或引导文件的最顶部:
```
run();
```
就是这样。默认规则会立即生效。
## 配置
每个选项都有合理的默认值。只需覆盖你需要的配置:
```
$firewall = XZeroProtect::init([
// 'production' → block & log | 'learning' → log only | 'off' → disabled
'mode' => 'production',
// Where ban files, rate data, and logs are stored
'storage_path' => __DIR__ . '/storage/firewall',
// --- Rate limiting ---
'rate_limit' => [
'enabled' => true,
'max_requests' => 60, // requests per window
'per_seconds' => 60, // window size in seconds
],
// --- Automatic banning ---
'auto_ban' => [
'enabled' => true,
'violations_threshold' => 10, // violations before a ban is issued
'ban_duration' => 86400, // ban length in seconds (24 h)
'permanent_after_bans' => 3, // escalate to permanent after N bans
],
// --- Apache integration ---
'apache_blocking' => false,
'htaccess_path' => __DIR__ . '/.htaccess',
// --- Reverse proxies / CDNs (e.g. Cloudflare, Nginx, load balancers) ---
// Leave empty to always use REMOTE_ADDR (safest if the server is
// reachable directly). Add proxy IPs/CIDRs to read the real client IP
// from CF-Connecting-IP / True-Client-IP / X-Real-IP / X-Forwarded-For
// when the request comes from one of these proxies. Use ['*'] to trust
// these headers regardless of REMOTE_ADDR (only if the server is NOT
// directly reachable).
'trusted_proxies' => [],
// --- Always-allow list ---
'whitelist' => [
'ips' => ['127.0.0.1', '10.0.0.0/8'],
'paths' => ['/health', '/ping'],
],
// --- Response sent to blocked clients ---
'block_response' => [
'code' => 403,
'message' => 'Access Denied',
],
// --- Toggle individual detection modules ---
'checks' => [
'crawler_check' => true, // exempt verified crawlers from all checks
'rate_limit' => true,
'blocked_path' => true,
'user_agent' => true,
'payload' => true,
'custom_rules' => true,
],
// --- Log settings ---
'log' => [
'enabled' => true,
'max_file_size' => 10, // MB — auto-rotated when exceeded
'keep_days' => 30,
],
]);
$firewall->run();
```
## 检测模块
### 路径检测
拦截针对敏感路径或不存在路径的请求。由于现代基于路由的 PHP 应用在 URL 中不包含 `.php` 文件,你可以添加该模式以立即拒绝大量 `index.php?id=` 扫描器探针。
```
// Add individual patterns
$firewall->patterns->addPath('.php');
$firewall->patterns->addPath('/control-panel');
// Add many at once
$firewall->patterns->addPaths(['.asp', '.jsp', '/backup', '/staging']);
// Remove a default pattern you want to allow
$firewall->patterns->removePath('xmlrpc');
```
查看所有默认拦截的路径
| 类别 | 匹配模式 |
|----------|----------|
| CMS 面板 | `wp-admin`, `wp-login`, `wp-config`, `xmlrpc`, `administrator`, `typo3` |
| 配置文件暴露 | `.env`, `.git`, `.svn`, `.htaccess`, `.htpasswd`, `web.config` |
| 数据库工具 | `phpmyadmin`, `pma`, `adminer`, `dbadmin` |
| 危险文件 | `.sql`, `.bak`, `.backup`, `.old`, `dump.sql` |
| 路径遍历 | `../`, `..%2f`, `%2e%2e` |
| Web Shell | `shell.php`, `c99.php`, `r57.php`, `webshell` |
| 脚本扩展名 | `.asp`, `.aspx`, `.jsp`, `.cfm`, `.cgi` |
| 信息泄露 | `phpinfo`, `server-status`, `server-info` |
| 安装残留 | `setup.php`, `install.php`, `readme.html` |
### User-Agent 检测
识别并拦截已知的扫描器、暴力破解和漏洞利用工具特征。默认情况下,空的 User-Agent 字符串会被视为可疑请求。
```
$firewall->patterns->addAgent('custom-bad-bot');
$firewall->patterns->removeAgent('curl'); // allow curl if your API clients use it
```
查看默认拦截的 Agent
`sqlmap` · `nikto` · `nessus` · `acunetix` · `netsparker` · `masscan` · `nmap` · `zgrab` · `dirbuster` · `gobuster` · `feroxbuster` · `wfuzz` · `ffuf` · `hydra` · `metasploit` · `semrushbot` · `ahrefsbot` · `libwww-perl` · 以及更多
### Payload 检测
使用编译后的正则表达式扫描 GET 参数、POST 主体、原始输入和 Cookie 中的攻击特征。
```
// Add a custom pattern
$firewall->patterns->addPayload('/CUSTOM_EXPLOIT/i', 'my_label');
// Remove a built-in pattern
$firewall->patterns->removePayload('sqli_union');
```
查看默认的 payload 规则
| 标签 | 检测内容 |
|-------|---------|
| `sqli_union` | `UNION [ALL] SELECT` |
| `sqli_select` | `SELECT ... FROM` |
| `sqli_drop` | `DROP TABLE/DATABASE` |
| `sqli_sleep` | `SLEEP(n)` 基于时间的盲注 |
| `sqli_benchmark` | `BENCHMARK(...)` |
| `sqli_comment` | `--`, `#`, `/* */` 注释符注入 |
| `xss_script` | `