Shieldon

适用于PHP的Web应用程序防火墙(WAF)。「Web Application Firewall (WAF) for PHP.」

Github stars Tracking Chart

PHP Web 应用程序防火墙

Shieldon 是适用于 PHP 的 Web 应用程序防火墙(WAF),具有漂亮实用的控制面板,可帮助您轻松管理防火墙规则和安全设置。

安装

2.x 将于8月15日发布

composer require shieldon/shieldon ^2

1.x

composer require shieldon/shieldon ^1

指南:Shieldon 1.x文档

如何使用

此处的示例适用于 Shieldon 2。

您可以选择三种方法在应用程序上使用Shieldon。有三种方法可以选择在应用程序上使用 Shieldon。

  • 将 Shieldon 实施为 PSR-15 中间件。
  • 在应用程序的引导阶段实现 Shieldon。
  • 在由其他控制器扩展的父控制器中实现 Shieldon。

Shieldon 2.x 实现了 PSR-7,因此它可以与现代框架(例如 Laravel,Symfony,Slim,Yii 等)兼容。在这种情况下,将 Shieldon 2.x 用作 PSR-15 中间件是最佳实践。

PSR-15 中间件

示例:Slim 4框架

在此示例中,我将向您提供一些有关如何将 Shieldon 实施为 PSR-15 中间件的提示。

我使用 Slim 4 框架进行演示。 只需稍作修改,该方法也可以用于支持 PSR-15 的任何框架。

(1)创建防火墙中间件。
class FirewallMiddleware
{
    /**
     * Example middleware invokable class
     *
     * @param ServerRequest  $request PSR-7 request
     * @param RequestHandler $handler PSR-15 request handler
     *
     * @return Response
     */
    public function __invoke(Request $request, RequestHandler $handler): Response
    {
        $response = $handler->handle($request);
        $firewall = new \Shieldon\Firewall\Firewall($request, $response);
        // The directory in where Shieldon Firewall will place its files.
        $firewall->configure(__DIR__ . '/../cache/shieldon_firewall');
        // The base url for the control panel.
        $firewall->controlPanel('/firewall/panel/');
        $response = $firewall->run();
        if ($response->getStatusCode() !== 200) {
            $httpResolver = new \Shieldon\Firewall\HttpResolver();
            $httpResolver($response);
        }
        return $response;
    }
}
(2)在您的应用程序中添加防火墙中间件。

例如,如果您使用的是 Slim 4 框架,则代码应如下所示。

$app->add(new FirewallMiddleware());
(3)为控制面板创建路由。

例如,如果您使用的是 Slim 4 框架,则代码应如下所示。 然后,您可以访问 URL https://yourwebsite.com/firewall/panel 登录到控制面板。

$app->any('/firewall/panel[/{params:.*}]', function (Request $request, Response $response, $args) {
$firewall = new \Shieldon\Firewall\Firewall($request, $response);
// The directory in where Shieldon Firewall will place its files.
// Must be the same as firewallMiddleware.
$firewall->configure(__DIR__ . '/../cache/shieldon_firewall');
$panel = new \Shieldon\Firewall\Panel();
$panel->entry();
});

注意:

  • HTTP 方法 POST 和 GET 都应应用于您的网站。
  • 需要 POST 方法来解决被临时阻止的用户解决验证码的问题。

引导阶段

示例:Laravel 6 框架

在应用程序的引导阶段初始化 Shieldon,通常是在包含 composer autoloader 之后立即进行。

在此示例中,我使用 Laravel 6 进行演示。

(1)在初始化 $app 之前

在您的 bootstrap/app.php 中,在 <?php 之后,添加以下代码。

/*
|--------------------------------------------------------------------------
| Run The Shieldon Firewall
|--------------------------------------------------------------------------
|
| Shieldon Firewall will watch all HTTP requests coming to your website.
| Running Shieldon Firewall before initializing Laravel will avoid possible
| conflicts with Laravel's built-in functions.
*/
if (isset($_SERVER['REQUEST_URI'])) {
    // This directory must be writable.
    // We put it in the `storage/shieldon_firewall` directory.
    $storage =  __DIR__ . '/../storage/shieldon_firewall';
    $firewall = new \Shieldon\Firewall\Firewall();
    $firewall->configure($storage);
    // The base url for the control panel.
    $firewall->controlPanel('/firewall/panel/');
    $response = $firewall->run();
    if ($response->getStatusCode() !== 200) {
        $httpResolver = new \Shieldon\Firewall\HttpResolver();
        $httpResolver($response);
    }
}
(2) 为防火墙面板定义路由。
Route::any('/firewall/panel/{path?}', function() {
    $panel = new \Shieldon\Firewall\Panel();
    $panel->csrf(['_token' => csrf_token()]);
    $panel->entry();
})->where('path', '(.*)');

父控制器

示例:CodeIgniter 3 框架

如果使用的是 MVC 框架,则在父控制器中实现 Shieldon 也是一个好主意。 在此示例中,我将使用 CodeIgniter 3 进行演示。

1、创建一个父控制器。

让我们在核心文件夹中创建一个 MY_Controller.php。

class MY_Controller extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }
}
2、初始化防火墙实例

将初始代码放入构造函数中,以便任何扩展MY_Controller的控制器都将初始化Shieldon Firewall,并准备好 $this->firewall() 方法。

class MY_Controller extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        // Composer autoloader
        require_once APPPATH . '../vendor/autoload.php';
        // This directory must be writable.
        $storage = APPPATH . 'cache/shieldon_firewall';
        $firewall = new \Shieldon\Firewall\Firewall();
        $firewall->configure($storage);
        // The base url for the control panel.
        $firewall->controlPanel('/firewall/panel/');
        $response = $firewall->run();
        if ($response->getStatusCode() !== 200) {
            $httpResolver = new \Shieldon\Firewall\HttpResolver();
            $httpResolver($response);
        }
    }
    /**
     * Shieldon Firewall protection.
     */
    public function firewall()
    {
        $firewall = \Shieldon\Container::get('firewall');
        $firewall->run();
    }
}
3、为控制面板定义一个控制器。

我们需要一个控制器进入 Shieldon 防火墙控制面板,在此示例中,我们定义了一个名为 Firewall 的控制器。

class Firewall extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * This is the entry of our Firewall Panel.
     */
    public function panel()
    {
        $panel = new \Shieldon\Firewall\Panel();
        $panel->entry();
    }
}

最后,无论您选择哪种方式,输入https://yoursite.com/firewall/panel/,都将在屏幕上显示登录页面。

默认的用户和密码是 shield user 和 shield pass。首先要做的是在登录到控制面板后更改登录名和密码。

概念

这是有关 Shieldon 工作方式的基本概念。

  • 网络层防火墙,例如CloudFlare。
  • 系统层防火墙,例如iptables模块。
  • 要在Web应用程序层中使用防火墙软件,我们能够在您的APP的早期阶段实施Shieldon,主要是在Composer自动加载器之后。
  • Shieldon会分析您的所有HTTP和HTTPS请求。
  • 一旦Shieldon检测到请求的异常行为,Shieldon就会临时禁止它们,并提示他们验证码以使其解除禁止。
  • 如果某个请求连续多次失败(取决于您的设置),则该请求将在当前数据圈中被永久禁止。
  • 如果请求已被永久禁止,但他们仍然可以访问您的页面,请将其放入系统层防火墙-iptables。

特性

  • SEO友好,不影响SERP。
  • Http型DDOS缓解。
  • 反扒(Anti-scraping)。
  • 限制在线用户数量。
  • 跨站点脚本(XSS)保护。
  • 中断漏洞扫描。
  • 消除暴力攻击。
  • IP管理员。
  • 通过 WWW-Authenticate 保护页面。
  • 详细的统计数据和图表。
  • 向第三方服务发送通知。
  • 用于管理 iptables(系统防火墙)的 Web UI。

实现

以下是与流行的 PHP 框架集成的指南。

防火墙面板

Shieldon 提供了一个防火墙实例,它的可视化界面称为防火墙面板。 通过使用 Shieldon 防火墙,您可以轻松地在 Web 应用程序上实现它。

Firewall Panel

点击 这里 查看演示。

  • user: demo
  • password: demo

屏幕截图

(恕删略。请参见自述文件。)

帮助翻译

非常感谢您考虑为 Shieldon Firewall 做出贡献,但是我们需要您的帮助来翻译Shieldon 库中的 Webiste,文档和 i18n 文件。 这里是链接:

作者

Shieldon 库是由台湾的 Terry L. 带给您的。

许可证

Shieldon Firewall 是根据 MIT 许可获得许可的开源软件。



(The first version translated by vz on 2020.08.05)

Main metrics

Overview
Name With Ownerterrylinooo/shieldon
Primary LanguagePHP
Program languagePHP (Language Count: 6)
PlatformBSD, Linux, Mac, Solaris, Windows
License:MIT License
所有者活动
Created At2019-05-14 03:38:37
Pushed At2023-06-17 09:05:07
Last Commit At
Release Count11
Last Release Name2.1.1 (Posted on )
First Release Name1.0.0 (Posted on )
用户参与
Stargazers Count865
Watchers Count27
Fork Count101
Commits Count725
Has Issues Enabled
Issues Count50
Issue Open Count1
Pull Requests Count9
Pull Requests Open Count3
Pull Requests Close Count10
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

Shieldon - Web Application Firewall for PHP

Build Status codecov PHP from Packagist License: MIT

Shieldon is a Web Application Firewall (WAF) for PHP. Taking less than 10 minutes only, PHP expert developers will understand how to implement Shiedon Firewall on their Web applications. The goal of this library is to make the PHP community more secure and being extremely use-to-use.

Concepts

This is basic concepts about how Shieldon works.

  • The network-layer firewall such as CloudFlare.
  • The system-layer firewall such as iptables module.
  • To use firewall software in the Web application layer, we are capable of implementing Shieldon in a very early stage of your APP, mostly just after Composer autoloader.
  • Shieldon analyzes all your HTTP and HTTPS requests.
  • Once Shieldon has detected strange behaviors of a request, Shieldon will temporarily ban them and prompt them CAPTCHA for them to unban.
  • If a request fails in a row many times (depends on your setting), they will be permanently banned in current data circle.
  • If a request has been permanently banned, but they still access your page, drop them in System-layer firewall - iptables.

Features

  • SEO friendly
  • Http-type DDOS mitigation.
  • Anti-scraping.
  • Online session control.
  • Cross-site scripting (XSS) protection.
  • Interrupting vulnerability scanning.
  • Eradicating brute force attacks.
  • IP manager.
  • Protecting pages via WWW-Authenticate.
  • Detailed statistics and charts.
  • Send notifications when specific events occurred. Supported modules:
    • Telegram
    • Line Notify
    • SendGrid
  • Web UI for System firewall - iptables and ip6tables.
  • More features will come...

Installation

Use PHP Composer:

composer require shieldon/shieldon

Or, download it and include the Shieldon autoloader.

require 'Shieldon/autoload.php';

Implementing

Here are the guides of integrating with the popular PHP frameworks.

Firewall Panel

Shieldon provides a Firewall Instance, and it's visualization UI called Firewall Panel. By using Shieldon Firewall, you can easily implement it on your Web application.

Firewall Panel

Click here to view demo.

  • user: demo
  • password: demo

Screenshots

Only a few screenshots are listed below.

Firewall Panel

Captcha Stats

Captcha Statistics

Online Session Stats

You can see the real-time data here if Online Session Limit is enabled.

Firewall Panel - Online Session Control

Rule Table

You can temporarily ban a user here.

Firewall Panel - Rule Table

Responsive

Shieldon's Firewall Panel is fully responsive, and you can manage it when you are not in front of your computer, using your mobile phone at any time.

Responsive Firewall Panel

Dialog

Temporarily Ban a User

When the users or robots are trying to view many your web pages in a short period of time, they will temporarily get banned. Get unbanned by solving a Catpcha.

Firewall Dialog 1

Permanently Ban a User

When a user has been permanently banned.

Firewall Dialog 2

Online Session Control

Firewall Dialog 3

When a user has reached the online session limit.

Notification

Provided by Messenger library.

Telegram

Send notification via Telegram API.

Author

Shieldon library is brought to you by Terry L. from Taiwan.

Contributing

Thank you very much for considering contributing to Shieldon Firewall, yet we need your help to translate our webiste, documentation and i18n files in Shieldon library. Here are the links:

License

Shieldon Firewall is an open-sourced software licensed under the MIT license.