crowdsec-client-scenarios
This library is a Node.js client to talk with crowdsec rest API .
Start
install it
npm i crowdsec-client-scenarios
and then read the documentation in the wiki
Usage
This package, is planned to host scenarios used by crowdsec-http-middleware and other middleware that extend it
Scenarios
in this part, we will use the variables scenarios
and scenariosOptions
, to illustrate the use in the middlewares
Defaults scenarios
the defaults scenarios are (defined here) :
Available scenarios
The available scenarios are :
XForwardedForChecker
This scenario will validate the XForwardedFor header . Some malicious persons will send you a fake X-Forwarded-For
header, to hide their real IP .
This scenario will trigger an alert, if an untrusted IP try to pass X-Forwarded-For
const scenarios = [XForwardedForChecker];
const scenariosOptions = {
'x-forwarded-for': {
trustedProxies: [],
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
Example
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': {
trustedProxies: [
"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",
"10.0.3.0/24"
],
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
AllowList
This scenario allow you to exclude some ips from alerts
const scenarios = [AllowListEnricher];
const scenariosOptions = {
'allow-list': {
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']
}
}
HTTPEnricher
this scenario will add context to your alert, linked with the current http request
const scenarios = [AllowListEnricher];
const scenariosOptions = {}
MaxMindEnricher
this scenario will add context to your alert with the help of the maxmind databases
const scenarios = [MaxMindEnricher];
const scenariosOptions = {
maxmind: {
paths: {
ASN: 'path/to/geoLite2-ASN.mmdb',
city: 'path/to/geoLite2-City.mmdb'
},
watchForUpdates: true
}
}
to use this scenario, you will need to download free databases available here.
If you need better accuracy you should consider buying commercial subscription.
Debug
this library include debug, to debug, you can set the env variable :
DEBUG=crowdsec-client-scenarios:*