This package provides optional CrowdSec detection scenarios for crowdsec-http-middleware and compatible middlewares. Node.js >= 24 is required.
install it
npm i crowdsec-client-scenarios
and then read the scenarios guide or the API reference.
This package hosts scenarios used by crowdsec-http-middleware and other middlewares that extend it.
in this part, we will use the variables scenarios and scenariosOptions, to illustrate the use in the middlewares
the defaults scenarios are (defined here) :
The available scenarios are:
This scenario validates the X-Forwarded-For header. Some malicious clients send a fake X-Forwarded-For header to hide their real IP.
This scenario triggers an alert if an untrusted IP tries to pass X-Forwarded-For.
import { XForwardedForChecker } from 'crowdsec-client-scenarios';
const scenarios = [XForwardedForChecker];
const scenariosOptions = {
'x-forwarded-for': {
//list of trusted CIDR, you need to setup here that all the trusted proxies, else, it will trigger alert for your reverse proxies, and extract incorrect ip
trustedProxies: [],
//trigger alert if an untrusted ip set the header (true by default)
alertOnNotTrustedIps: true
}
}
this scenario support the extractIp capability, so, if you enable it, it will extract the IP automatically from the headers, will use set it on req.ip, and will use it to trigger alerts and check if decisions exists
you are using cloudflare (you can get cloudflare ips here, then traefik in subnet 10.0.3.0/24, and finally your webserver
on your server, you will configure :
const scenarios = [XForwardedForChecker];
const scenariosOptions = {
'x-forwarded-for': {
//list of trusted CIDR, you need to setup here that all the trusted proxies, else, it will trigger alert for your reverse proxies, and extract incorrect ip
trustedProxies: [
//cloudflare ips
"173.245.48.0/20",
"103.21.244.0/22",
"103.22.200.0/22",
"103.31.4.0/22",
"141.101.64.0/18",
"108.162.192.0/18",
"190.93.240.0/20",
"188.114.96.0/20",
"197.234.240.0/22",
"198.41.128.0/17",
"162.158.0.0/15",
"104.16.0.0/13",
"104.24.0.0/14",
"172.64.0.0/13",
"131.0.72.0/22",
"2400:cb00::/32",
"2606:4700::/32",
"2803:f800::/32",
"2405:b500::/32",
"2405:8100::/32",
"2a06:98c0::/29",
"2c0f:f248::/32",
//traefik ip
"10.0.3.0/24"
],
//trigger alert if an untrusted ip set the header
alertOnNotTrustedIps: true
}
}
so, if someone with ip 1.2.3.4 it will produce an X-Forwarded-For like 1.2.3.4, 173.245.48.2, and your webserver will detect a remote address like 10.0.3.1 .
This scenario will parse the information, and so set req.ip = "1.2.3.4", so you now know the real ip of your visitor .
I need to warn you, if you miss configured the list of allowed ips, it can ban your reverse proxy .
you can test it before, by setting alertOnNotTrustedIps to false, and log req.ip, if the IP is correct, you can enable the alerts
This scenario allows you to exclude some IPs from alerts.
import { AllowListEnricher } from 'crowdsec-client-scenarios';
const scenarios = [AllowListEnricher];
const scenariosOptions = {
'allow-list': {
//list of CIDR/IP to allow to skip alert checks
allowed: ['127.0.0.1']
}
}
by default, the CIDR allowed are the RFC 1918 one (private network)
const scenariosOptions = {
'allow-list': {
allowed: ['127.0.0.1', '::1', '192.168.0.0/16', '10.0.0.0/8', '172.16.0.0/12']
}
}
This scenario adds context to your alert, linked with the current HTTP request.
import { HTTPEnricher } from 'crowdsec-client-scenarios';
const scenarios = [HTTPEnricher];
// no options
const scenariosOptions = {};
This scenario adds context to your alert with the help of the MaxMind databases.
import { MaxMindEnricher } from 'crowdsec-client-scenarios';
const scenarios = [MaxMindEnricher];
const scenariosOptions = {
maxmind: {
// specify the path to the ASN and/or city databases; set at least one
paths: {
ASN: 'path/to/geoLite2-ASN.mmdb',
city: 'path/to/geoLite2-City.mmdb'
},
// watch for updates: updating the database on the filesystem reloads it automatically
watchForUpdates: true
}
}
To use this scenario, download the free databases from the MaxMind GeoLite2 page. If you need better accuracy you should consider buying commercial subscription.
this library include debug, to debug, you can set the env variable :
DEBUG=crowdsec-client-scenarios:*
MIT