1. Go to this page and download the library: Download kanopi/crs-engine library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
kanopi / crs-engine example snippets
use Kanopi\Crs\CrsConfig;
use Kanopi\Crs\CrsEngine;
use Kanopi\Crs\Request\RequestData;
// Construct once per process — loading the ruleset is the expensive part.
$engine = new CrsEngine(new CrsConfig(
paranoia: 1,
mode: CrsConfig::MODE_BLOCK,
));
$verdict = $engine->evaluate(RequestData::fromGlobals());
if ($verdict->isBlocked()) {
http_response_code(403);
error_log(sprintf(
'CRS blocked request: %s, score %d, %d rule(s) matched',
$verdict->blockingRuleId === null
? 'anomaly threshold reached'
: 'rule ' . $verdict->blockingRuleId,
$verdict->totalScore,
count($verdict->matchedRules),
));
exit;
}
new CrsConfig(
paranoia: 1, // 1 (default) - 4. Higher = more strict, more false positives.
mode: CrsConfig::MODE_BLOCK, // or MODE_MONITOR (records matches, never blocks)
anomalyThresholds: [
'inbound' => 5, // request score >= this blocks the request
'outbound' => 4, // response score >= this blocks the response
],
disabledRules: [920300, 942130], // skip these rule IDs
disabledCategories: ['session_fixation'], // skip whole categories
rulesPath: null, // override location of compiled.php
severityScores: [
'critical' => 5, // what each severity *adds* to the score
'error' => 4,
'warning' => 3,
'notice' => 2,
],
maxRequestBodyBytes: 131072, // request body bytes handed to the ruleset
maxResponseBodyBytes: 524288, // response body bytes — larger, see below
maxArgs: 255, // argument values inspected (counting is uncapped)
maxArgBytes: 131072, // total argument bytes inspected per rule
responseMode: null, // overrides `mode` outbound only
);
new CrsConfig(
mode: CrsConfig::MODE_BLOCK, // reject attacks on the way in
responseMode: CrsConfig::MODE_MONITOR, // only record leakage on the way out
);
new CrsConfig(
// Fields where users legitimately paste code, paths and regexes.
// Prefer this over disabledRules: it keeps the rule working everywhere else.
disabledRules: [
932235, 932260, // unix command injection — trips on shell examples
932280, // shell metacharacters — trips on regexes
],
);