
CrowdSec is composed of a behavior detection engine, able to block classical attacks like credential bruteforce, port scans, web scans, etc.
Based on the type and number of blocked attacks, and after curation of those signals to avoid false positives and poisoning, a global IP reputation DB is maintained and shared with all network members.
Get more info on the CrowdSec official website.
This Express middleware is a “bouncer”, which purpose is to block detected attacks with two remediation systems: ban or challenge detected attackers with a Captcha.
Here is the exhaustive list of bouncer features.
Note: You must first have CrowdSec installed on your server. The installation is very simple.
First, install the Crowdsec Bouncer express middleware:
npm install @crowdsec/express-bounceror
yarn add @crowdsec/express-bouncerThen init the Express middleware. Here is a quick usage example.
const express = require("express");
const bodyParser = require("body-parser");
const expressCrowdsecBouncer = require("@crowdsec/express-bouncer");
(async () => {
// Configure CrowdSec Middleware.
const crowdsecMiddleware = await expressCrowdsecBouncer({
url: "http://localhost:8080",
apiKey: "INSERT_YOUR_BOUNCER_API_KEY",
});
// Configure Express server.
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(crowdsecMiddleware);
// Create an example route.
app.all("/", function (req, res) {
res.status(200).send(`The way is clear!`);
});
// Start server.
app.listen(3000);
console.log(
`Express server configured with Crowdsec middleware available here: http://127.0.0.1:3000`
);
})();Note: To get a bouncer API key, just type
cscli bouncers add express-js-bouncer(you can replace the nameexpress-js-bouncerby anything you prefer).
Here is the full list of parameters than can be passed to expressCrowdsecBouncer.
| Name | Description | Type | Default |
|---|---|---|---|
url (required) | The URL of your LAPI instance. Ex: "http://localhost:8080" | string | - |
apiKey (required) | The bouncer key (generated via cscli) | string | - |
fallbackRemediation | The remediation to use when the received one is unknown | Constant * | BAN_REMEDIATION |
maxRemediation | The maximum remediation to use (flex mode) | Constant * | BAN_REMEDIATION |
bypass | To enable or disable the bouncing | boolean | false |
captchaTexts | To change the text displayed on the CAPTCHA wall | object | view below ** |
banTexts | To change the text displayed on the BAN wall | object | view below *** |
colors | To change the colors of the BAN and CAPTCHA walls | object | view below **** |
customCss | CSS code to customize ban and captcha walls | string | "" |
userAgent | To use a custom bouncer user agent when requesting LAPI | string | "CrowdSec Express-NodeJS bouncer/vx.x.x" |
timeout | The timeout when requesting LAPI | number | 2000 |
captchaGenerationCacheDuration | The minimum time between two CAPTCHA generations for a same IP | number | 60 * 1000 |
captchaResolutionCacheDuration | The time we can consider a captcha as resolved (during an active "captcha" decision) | number | 30 * 60 * 1000 |
hideCrowdsecMentions | To display or hide CrowdSec mention on the BAN and CAPTCHA walls | boolean | false |
customLogger | You can use a custom Winston logger | Winston logger | default logger |
bypassConnectionTest | To bypass the connection test at middleware initialization | boolean | false |
trustedRangesForIpForwarding | The list of IPs to trust as proxies | array | [] |
*: All remediation type are constants and they can be found:
express-crowdsec-bouncer/src/nodejs-bouncer/lib/constants.js.
**:
captchaTextsdefault value:
{
"tabTitle": "Oops..",
"title": "Hmm, sorry but...",
"subtitle": "Please complete the security check.",
"refresh_image_link": "refresh image",
"captcha_placeholder": "Type here...",
"send_button": "CONTINUE",
"error_message": "Please try again.",
"footer": ""
}***:
banTextsdefault value:
{
"tabTitle": "Oops..",
"title": "🤭 Oh!",
"subtitle": "This page is protected against cyber attacks and your IP has been banned by our system.",
"footer": ""
}****:
colorsdefault value:
{
"text": {
"primary": "black",
"secondary": "#AAA",
"button": "white",
"error_message": "#b90000"
},
"background": {
"page": "#eee",
"container": "white",
"button": "#626365",
"button_hover": "#333"
}
}