Skip to content

Commit 2d56396

Browse files
committed
improved the guide
1 parent f84d939 commit 2d56396

File tree

1 file changed

+47
-17
lines changed

1 file changed

+47
-17
lines changed

crowdsec-docs/docs/appsec/rules_deploy.md

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,56 @@
11
---
22
id: rules_deploy
3-
title: Custom Rules deployment
3+
title: Rules deployment
44
sidebar_position: 81
55
---
66

77
# WAF Rules Deployment
88

9-
This guide starts once you have already authored and validated a custom AppSec (WAF) rule locally. The steps below focus on packaging that rule so CrowdSec can load it, wiring the configuration into the acquisition pipeline, deploying the files, and finally smoke-testing the result.
9+
This walkthrough assumes you already wrote and validated a custom AppSec (WAF) rule. We will deploy a concrete example so you can mirror the exact commands on your host.
10+
11+
## Example Rule We Will Deploy
12+
13+
The example blocks any `GET` request whose `user_id` query argument contains non-numeric characters. While you iterate locally, keep it in a working directory as `./block-nonnumeric-user-id.yaml`:
14+
15+
```yaml title="./block-nonnumeric-user-id.yaml"
16+
name: custom/block-nonnumeric-user-id
17+
description: Block GET requests with a non-numeric user_id parameter.
18+
rules:
19+
- and:
20+
- zones:
21+
- METHOD
22+
match:
23+
type: equals
24+
value: GET
25+
- zones:
26+
- ARGS
27+
variables:
28+
- user_id
29+
match:
30+
type: regex
31+
value: "[^0-9]"
32+
labels:
33+
type: exploit
34+
service: http
35+
confidence: 2
36+
spoofable: 0
37+
behavior: "http:exploit"
38+
label: "Non numeric user id"
39+
```
40+
41+
Once the rule behaves as expected, the remaining steps package it for CrowdSec, wire it into the acquisition pipeline, and test it end to end.
1042
1143
## Step 1 — Stage the Rule File
1244
1345
CrowdSec loads AppSec rules from `/etc/crowdsec/appsec-rules/`. Copy your YAML rule into that directory (create a `custom/` subfolder to keep things tidy if you manage several rules):
1446

1547
```bash
1648
sudo install -d -m 750 /etc/crowdsec/appsec-rules/custom
17-
sudo install -m 640 ./my-virtual-patch.yaml \
18-
/etc/crowdsec/appsec-rules/custom/my-virtual-patch.yaml
49+
sudo install -m 640 ./block-nonnumeric-user-id.yaml \
50+
/etc/crowdsec/appsec-rules/custom/block-nonnumeric-user-id.yaml
1951
```
2052

21-
Make sure the `name` inside the rule file matches the file name convention you plan to reference (for example `custom/my-virtual-patch`).
53+
Make sure the `name` inside the rule file matches the file name convention you plan to reference (in our example `custom/block-nonnumeric-user-id`).
2254

2355
:::tip
2456
If you run CrowdSec in a container, copy the file into the volume that is mounted at `/etc/crowdsec/appsec-rules/` inside the container.
@@ -28,17 +60,17 @@ If you run CrowdSec in a container, copy the file into the volume that is mounte
2860

2961
An AppSec configuration lists which rules to load and how to handle matches. Create a new file under `/etc/crowdsec/appsec-configs/` that targets your custom rule:
3062

31-
```yaml title="/etc/crowdsec/appsec-configs/custom-virtual-patching.yaml"
32-
name: custom/virtual-patching
63+
```yaml title="/etc/crowdsec/appsec-configs/custom-block-nonnumeric-user-id.yaml"
64+
name: custom/block-nonnumeric-user-id
3365
default_remediation: ban
3466
inband_rules:
35-
- custom/my-virtual-patch
67+
- custom/block-nonnumeric-user-id
3668
# Add outofband_rules or hooks here if needed
3769
```
3870

3971
Key points:
4072
- `name` is how you will reference this configuration from the acquisition file and in logs.
41-
- `inband_rules` (and/or `outofband_rules`) accept glob patterns, so you can load multiple rules with a single entry such as `custom/my-virtual-patch-*`.
73+
- `inband_rules` (and/or `outofband_rules`) accept glob patterns, so you can load multiple rules with a single entry such as `custom/block-*`.
4274
- Keep file permissions restrictive (`640`) so only the `crowdsec` user can read the file.
4375
- During the reload step CrowdSec validates the syntax; if anything is off, the reload fails and the service logs the parsing error.
4476

@@ -49,7 +81,7 @@ The AppSec acquisition file (`/etc/crowdsec/acquis.d/appsec.yaml`) controls whic
4981
```yaml title="/etc/crowdsec/acquis.d/appsec.yaml"
5082
appsec_configs:
5183
- crowdsecurity/appsec-default
52-
- custom/virtual-patching
84+
- custom/block-nonnumeric-user-id
5385
labels:
5486
type: appsec
5587
listen_addr: 127.0.0.1:7422
@@ -69,27 +101,25 @@ sudo systemctl reload crowdsec
69101
If your init system does not support reload, perform a restart instead. Then verify the rule and configuration are active:
70102

71103
```bash
72-
sudo cscli appsec-rules list | grep my-virtual-patch
73-
sudo cscli appsec-configs list | grep virtual-patching
104+
sudo cscli appsec-rules list | grep block-nonnumeric-user-id
105+
sudo cscli appsec-configs list | grep block-nonnumeric-user-id
74106
sudo journalctl -u crowdsec --since "5 minutes ago" | grep appsec
75107
```
76108

77109
The rule should appear as `enabled`, and the configuration should show up in the list. CrowdSec logs confirm the configuration was loaded without errors.
78110

79111
## Step 5 — Functional Test with `curl`
80112

81-
Trigger the behaviour your rule is meant to catch to ensure it blocks as expected. For example, if the rule protects `/admin` against a malicious header, you can test from the WAF host:
113+
Trigger the behaviour your rule is meant to catch to ensure it blocks as expected. For the example rule, send a request with a non-numeric `user_id` value:
82114

83115
```bash
84-
curl -i -H 'X-Foobar-Bypass: 1' \
85-
-d 'user_id=123;cat /etc/passwd&do=yes' \
86-
http://127.0.0.1/admin
116+
curl -i 'http://127.0.0.1/profile?user_id=abc123'
87117
```
88118

89119
A successful block returns an HTTP status such as `403 Forbidden`, and CrowdSec logs a matching alert:
90120

91121
```bash
92-
sudo journalctl -u crowdsec -n 20 | grep "my-virtual-patch"
122+
sudo journalctl -u crowdsec -n 20 | grep "block-nonnumeric-user-id"
93123
```
94124

95125
If the request is not blocked, double-check that the rule `name` matches the pattern in your AppSec configuration, that the acquisition file lists your configuration, and that the CrowdSec service picked up the changes.

0 commit comments

Comments
 (0)