psecio/canary
Composer 安装命令:
composer require psecio/canary
包简介
An input detection and response library
README 文档
README
The origin of the term "canary" (as a method of detection) was originally used by those that worked deep in mines and would take a canary (the bird) with them to detect gas or other reasons they needed to leave. If the bird started behaving oddly they knew something was amiss. This same concept is applied in the security world and is similarly called a "canary".
Similarly, the Canary library allows you to define key/value combinations that can be used to detect when certain data is used and notify
you using a variety of methods including the default PHP error log, log handling via Monolog and messages to Slack channels.
For example, you may generate a special username that you want to use as a trigger. This username isn't actually a user in your system
but you do want to be notified if a login attempt is made using it. Canary makes this simple by defining checks with an if method and,
optionally, a handler using a then method. For example, say we generated the username of canary1234@foo.com and we want to detect when
it's used. You can define this in a Canary expression like so:
<?php $_POST = [ 'username' => 'canary1234@foo.com', 'password' => 'sup3rs3cr3t' ]; \Psecio\Canary\Instance::build()->if('username', 'canary1234@foo.com')->execute(); // Or you can set multiple match values to look for with an array $matches = [ 'username' => 'canary1234@foo.com', 'password' => 'sup3rs3cr3t' ]; \Psecio\Canary\Instance::build()->if($matches)->execute(); ?>
In this example we're looking at the current input and checking to see if there's a username value of canary1234@foo.com. In the case
of our current $_POST values, there's a match. By default (if no then handler is defined) the information about the match is output to
the error like (via the Psecio\Canary\Notify\ErrorLog handler). The JSON encoded result looks like this:
{"type":"equals","key":"username","value":"canary1234@foo.com"}
NOTE: Canary automatically pulls in the
$_GETand$_POSTsuperglobal values for evaluation so you don't need to manually pass then in.
Using an external data source
Canary also allows you to use a (static) class method to provide the if portion of the evaluation with data. To use it, just pass in the class
and static method name as a string:
<?php
$classMethod = '\Foo\Bar::criteria';
\Psecio\Canary\Instance::build()->if($classMethod)->execute();
?>
The return from this method must be an array otherwise an exception will be thrown.
Supported Notifier Methods
Currently Canary supports the following notification methods:
| Type | Class | Expected Input |
|---|---|---|
| Error log | \Psecio\Canary\Notify\ErrorLog |
None, uses default location |
| Monolog | \Psecio\Canary\Notify\Monolog |
\Monolog\Logger |
| Callback | \Psecio\Canary\Notify\Callback |
Callable function |
| Slack | \Psecio\Canary\Notify\Slack |
\Maknz\Slack\Client |
| PagerDuty | \Psecio\Canary\Notify\PagerDuty |
\PagerDuty\Event |
Creating a Custom Handler (Callback)
If you don't want your results to go to the error log, you can create your own handler via the then method. Currently the only custom
handler supported is a callable method. So, say we wanted to output a message to the user of our special username and kill the script. We
might use something like this:
<?php $_POST = ['username' => 'canary1234@foo.com']; \Psecio\Canary\Instance::build()->if('username', 'canary1234@foo.com') ->then(function($criteria) { die("You shouldn't have done that!"); }) ->execute(); ?>
In this handler, when it detects that the username value matches our criteria, the callback is executed and the die call kills the script.
Passing in custom data
You can also provide your own data set if you don't want to auto-load the current $_GET and $_POST values. To pass the data in you can use the
data value in the configuration and passing it in:
<?php $config = ['data' => [ 'username' => 'foobar@baz.com' ]]; \Psecio\Canary\Instance::build($config)->if('username', 'canary1234@foo.com')->execute(); ?>
Using a default logger
You can set it as the default logger for all if checks via the notify key in the build() configuration options:
<?php // create a log channel $log = new Logger('name'); $log->pushHandler(new StreamHandler('/tmp/mylog.log', Logger::WARNING)); $config = [ 'notify' => $log ]; \Psecio\Canary\Instance::build($config)->if('username', 'canary1234@foo.com')->execute(); ?>
NOTE: If you provide a default handler via the
notifyconfiguration it will override all other custom notification methods.
Using Monolog
The Canary tool also allows you to use the Monolog logging library to define a bit more customization to the structure of the data and how it's output. Like before, we create the Canary instance but for the input of the then method we provide a Monolog\Logger instance:
<?php use Monolog\Logger; use Monolog\Handler\StreamHandler; require_once 'vendor/autoload.php'; $_GET = ['username' => 'test']; // create a log channel $log = new Logger('name'); $log->pushHandler(new StreamHandler('/tmp/mylog.log', Logger::WARNING)); \Psecio\Canary\Instance::build() ->if('username', 'canary1234@foo.com') ->then($log) ->execute(); ?>
Using Slack
You can also make use of the Maknz\Slack library to send messages to Slack when a canary is triggered:
<?php $settings = [ 'channel' => '#my-channel-name', 'link_names' => true ]; $slack = new Maknz\Slack\Client('https://hooks.slack.com/services/.....', $settings); \Psecio\Canary\Instance::build($config)->if('username', 'canary1234@foo.com')->then($slack); ?>
You'll need to set up an incoming webhook and replace the URL value in the Client
create with the custom URL you're given. The default name for the notifications is Canary Agent and the output includes the same JSON
information as the other notification methods.
Using PagerDuty
Canary also allows you to send notifications to your account on the PagerDuty service using the nmcquay/pagerduty library:
<?php $pager = new \PagerDuty\Event(); $pager->setServiceKey('[.... your service ID ....]'); \Psecio\Canary\Instance::build($config)->if('username', 'canary1234@foo.com')->then($pager); ?>
You can find the service ID by going to your services page (https://[your domain].pagerduty.com/services) and clicking on the service you want to use. The ID is under the "Integrations" tab as the "Integration Key".
psecio/canary 适用场景与选型建议
psecio/canary 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12 次下载、GitHub Stars 达 30, 最近一次更新时间为 2018 年 02 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「response」 「detect」 「input」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 psecio/canary 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 psecio/canary 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 psecio/canary 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Yii2 map input widget. Allows you to select geographcal coordinates via a human-friendly inteface.
Widget to add a remaining character counter to text inputs and textareas
A Laravel 5 Package Provider to Identify/detect a user's browser, device, operating system and Language
Native (browser) color input field for SilverStripe
HTTP request logger middleware for Laravel
Integration bundle for Aura.Input with Aura.Filter
统计信息
- 总下载量: 12
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 30
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-02-15