Skip to content

Commit 384b721

Browse files
committed
simplify setup
1 parent 866f738 commit 384b721

1 file changed

Lines changed: 78 additions & 217 deletions

File tree

crowdsec-docs/docs/appsec/quickstart/nginxopenresty.mdx

Lines changed: 78 additions & 217 deletions
Original file line numberDiff line numberDiff line change
@@ -3,257 +3,121 @@ id: nginxopenresty
33
title: QuickStart - Nginx / OpenResty
44
---
55

6-
76
import Tabs from '@theme/Tabs';
87
import TabItem from '@theme/TabItem';
9-
import CodeBlock from '@theme/CodeBlock';
10-
import UnderlineTooltip from '@site/src/components/underline-tooltip';
11-
12-
# CrowdSec WAF QuickStart for Nginx/OpenResty
13-
14-
## Objectives
15-
16-
The goal of this quickstart is to set up the [AppSec Component](/appsec/intro.md#introduction) to safeguard web applications running on [Nginx](https://nginx.com) or [OpenResty](https://openresty.org/en/).
17-
18-
We'll deploy a [set of rules](https://app.crowdsec.net/hub/author/crowdsecurity/collections/appsec-virtual-patching) designed to block [well-known attacks](https://app.crowdsec.net/hub/author/crowdsecurity/collections/appsec-generic-rules) and [currently exploited vulnerabilities](https://app.crowdsec.net/hub/author/crowdsecurity/collections/appsec-virtual-patching).
19-
20-
Additionally, we'll show how to monitor these alerts through the [Console](https://app.crowdsec.net/).
21-
22-
## Prerequisites
23-
24-
1. If you're new to the [AppSec Component](/appsec/intro.md#introduction) or **W**eb **A**pplication **F**irewalls, start with the [Introduction](/appsec/intro.md#introduction) for a better understanding.
25-
26-
2. It's assumed that you have already installed:
27-
- **CrowdSec [Security Engine](/intro.mdx)**: for installation, refer to the [QuickStart guide](/u/getting_started/installation/linux). The AppSec Component, which analyzes HTTP requests, is included within the security engine as a [Acquisition](/log_processor/data_sources/appsec.md).
28-
- One of the supported web servers for this guide:
29-
- Nginx **[Remediation Component](/u/bouncers/intro)**: installation instructions are available in the [QuickStart guide](/u/bouncers/nginx).
30-
- OpenResty **[Remediation Component](/u/bouncers/intro)**: installation instructions are available in the [QuickStart guide](/u/bouncers/openresty).
31-
32-
This component intercepts HTTP requests at the webserver or reverse-proxy level and forwards them to the AppSec Component for analysis and action.
33-
34-
:::info
35-
The reason we provide Nginx and OpenResty in a single guide is that OpenResty is a web server based on Nginx just the configuration paths are different
36-
:::
37-
38-
:::tip Already did the base setup?
39-
If you already completed the [General Setup](general.mdx) (collections + acquisition), skip to [Remediation Component Setup](#remediation-component-setup).
40-
:::
41-
42-
## AppSec Component Setup
43-
44-
### Collection installation
45-
46-
To begin setting up the AppSec Component, the initial step is to install a relevant set of rules.
47-
48-
We will utilize the [`crowdsecurity/appsec-virtual-patching`](https://app.crowdsec.net/hub/author/crowdsecurity/collections/appsec-virtual-patching) collection, which offers a wide range of rules aimed at identifying and preventing the exploitation of known vulnerabilities.
49-
50-
This <UnderlineTooltip tooltip="Collections are bundle of parsers, scenarios, postoverflows that form a coherent package.">collection</UnderlineTooltip> is regularly updated to include protection against newly discovered vulnerabilities. Upon installation, it receives automatic daily updates to ensure your protection is always current.
51-
We also install the [`crowdsecurity/appsec-generic-rules`](https://app.crowdsec.net/hub/author/crowdsecurity/collections/appsec-generic-rules) collection. This collection contains detection scenarios for generic attack vectors. It provides protection in cases where specific scenarios for vulnerabilities do not exist yet.
52-
53-
On the machine where the Security Engine is installed, just execute the following command:
54-
55-
:::info
56-
You can always view the content of a [collection on the Hub](https://app.crowdsec.net/hub/author/crowdsecurity/collections/appsec-virtual-patching).
57-
:::
58-
59-
```
60-
sudo cscli collections install crowdsecurity/appsec-virtual-patching crowdsecurity/appsec-generic-rules
61-
```
62-
63-
Executing this command will install the following items:
64-
65-
- The [*AppSec Rules*](/appsec/rules_syntax.md) contain the definition of malicious requests to be matched and stopped
66-
- The [*AppSec Configuration*](/appsec/configuration.md#appsec-configuration-files) links together a set of rules to provide a coherent set
67-
- The <UnderlineTooltip tooltip="YAML files that extract relevant data from logs, such as IP addresses, timestamps, or request paths.">CrowdSec Parser</UnderlineTooltip> and <UnderlineTooltip tooltip="Behavioral rules written in a domain-specific language that define what malicious activity looks like, such as multiple failed logins in a short time.">CrowdSec Scenario(s)</UnderlineTooltip> bans for a longer duration repeating offenders
68-
69-
### Setup the Acquisition
70-
71-
Having installed the required components, it's time to configure the CrowdSec acquisition datasource for the AppSec Component ([AppSec datasource](/log_processor/data_sources/appsec.md)). This configuration allows Nginx/OpenResty to forward requests to the AppSec Component for evaluation and decision-making.
72-
73-
Steps:
74-
75-
1. Create the acquisition directory (if it doesn't exist on your machine):
76-
```bash
77-
sudo mkdir -p /etc/crowdsec/acquis.d/
78-
```
79-
80-
2. Create `/etc/crowdsec/acquis.d/appsec.yaml`:
81-
```yaml title="/etc/crowdsec/acquis.d/appsec.yaml"
82-
appsec_configs:
83-
- crowdsecurity/appsec-default
84-
labels:
85-
type: appsec
86-
listen_addr: 127.0.0.1:7422
87-
source: appsec
88-
```
89-
90-
The two important directives in this configuration file are:
918

92-
- `appsec_configs` is the list of [*AppSec Configurations*](/appsec/configuration.md#appsec-configuration-files) that was included in the <UnderlineTooltip tooltip="Collections are bundle of parsers, scenarios, postoverflows that form a coherent package.">Collection</UnderlineTooltip> we just installed.
93-
- the `listen_addr` is the IP and port the AppSec Component will listen to.
9+
# CrowdSec WAF QuickStart for Nginx / OpenResty
9410

95-
:::warning
96-
We do not recommend exposing the AppSec Component to the internet. It should only be accessible from the web server or reverse proxy.
97-
:::
11+
Protect an [Nginx](https://nginx.com) or [OpenResty](https://openresty.org/en/) server with CrowdSec's [AppSec (WAF) Component](/appsec/intro.md#introduction). After the prerequisites below, every step is a single copy-paste command — pick your engine once in the tabs and the page will remember the choice.
9812

9913
:::info
100-
You can find more about the [supported options for the acquisition here](/log_processor/data_sources/appsec.md)
14+
Nginx and OpenResty share a single guide because OpenResty is Nginx with Lua built in — only the bouncer package and a few paths differ.
10115
:::
10216

103-
You can now restart CrowdSec:
104-
105-
```bash
106-
sudo systemctl restart crowdsec
107-
```
17+
## Prerequisites
10818

109-
#### Testing the AppSec Component
19+
Make sure the following are already done on the machine running your web server (all are single-page install guides):
11020

111-
##### Verify the AppSec Component is listening
21+
1. **CrowdSec Security Engine** installed and running — see the [Linux quickstart](/u/getting_started/installation/linux).
22+
2. **Nginx or OpenResty bouncer** installed and registered against the CrowdSec LAPI:
23+
- Nginx: [`crowdsec-nginx-bouncer`](/u/bouncers/nginx#installation)
24+
- OpenResty: [`crowdsec-openresty-bouncer`](/u/bouncers/openresty#installation)
11225

113-
To verify that the AppSec Component is running correctly, we can first check that the port `7422` is open and listening:
26+
Only follow the **Installation** section of those pages — the "Enable AppSec" section is exactly what this quickstart covers, come back here for it.
27+
3. Nginx or OpenResty is currently serving traffic on port 80 (used by the verification step at the end).
11428

115-
:::note
116-
If you have changed the port in the configuration file, replace `7422` with the new port number.
29+
:::info Pick your engine
30+
The tabs below remember your choice across the whole page.
11731
:::
11832

119-
<Tabs
120-
defaultValue="netstat"
121-
groupId="listening-ports"
122-
values={[
123-
{label: 'Netstat', value: 'netstat'},
124-
{label: 'SS', value: 'ss'},
125-
]}>
126-
127-
<TabItem value="netstat">
128-
<CodeBlock className="language-bash">sudo netstat -tlpn | grep 7422</CodeBlock>
129-
</TabItem>
130-
131-
<TabItem value="ss">
132-
<CodeBlock className="language-bash">sudo ss -tlpn | grep 7422</CodeBlock>
133-
</TabItem>
33+
<Tabs groupId="engine" defaultValue="nginx" values={[
34+
{label: 'Nginx', value: 'nginx'},
35+
{label: 'OpenResty', value: 'openresty'},
36+
]}>
37+
<TabItem value="nginx">Commands below will target Nginx.</TabItem>
38+
<TabItem value="openresty">Commands below will target OpenResty.</TabItem>
13439
</Tabs>
13540

136-
<details>
41+
## 1. Install the AppSec rule collections
13742

138-
<summary>Output example</summary>
43+
Same command for both engines:
13944

14045
```bash
141-
tcp 0 0 127.0.0.1:7422 0.0.0.0:* LISTEN 12345/crowdsec
46+
sudo cscli collections install \
47+
crowdsecurity/appsec-virtual-patching \
48+
crowdsecurity/appsec-generic-rules
14249
```
14350

144-
:::note
145-
The output may look different depending on the command you used. As long as you see port 7422 and the `crowdsec` process, the AppSec Component is running.
146-
:::
51+
This pulls the [`appsec-virtual-patching`](https://app.crowdsec.net/hub/author/crowdsecurity/collections/appsec-virtual-patching) collection (rules for known CVEs, auto-updated daily) and the [`appsec-generic-rules`](https://app.crowdsec.net/hub/author/crowdsecurity/collections/appsec-generic-rules) collection (common attack patterns), plus the default AppSec configuration.
14752

148-
</details>
53+
## 2. Turn on the AppSec Component
14954

150-
##### (Optional) Manually testing the AppSec Component with `curl`
151-
152-
<details>
153-
<summary>Expand for short guide</summary>
154-
155-
Before we proceed with configuring the Remediation Component, let's verify that all our current setups are functioning correctly.
156-
157-
1. Create a Remediation Component (Bouncer) API Key:
158-
159-
```bash
160-
sudo cscli bouncers add test_waf -k this_is_a_bad_password
161-
API key for 'test_waf':
162-
163-
this_is_a_bad_password
164-
165-
Please keep this key since you will not be able to retrieve it!
166-
```
167-
168-
2. Emit a legitimate request to the AppSec Component:
169-
170-
```bash
171-
curl -X POST localhost:7422/ -i -H 'x-crowdsec-appsec-uri: /test' -H 'x-crowdsec-appsec-ip: 192.168.1.1' -H 'x-crowdsec-appsec-host: foobar.com' -H 'x-crowdsec-appsec-verb: POST' -H 'x-crowdsec-appsec-api-key: this_is_a_bad_password'
172-
```
173-
174-
Which will give us an answer such as:
55+
Create the acquisition file, then restart CrowdSec:
17556

17657
```bash
177-
HTTP/1.1 200 OK
178-
Date: Tue, 30 Jan 2024 15:43:50 GMT
179-
Content-Length: 36
180-
Content-Type: text/plain; charset=utf-8
181-
182-
{"action":"allow","http_status":200}
58+
sudo mkdir -p /etc/crowdsec/acquis.d
59+
sudo tee /etc/crowdsec/acquis.d/appsec.yaml > /dev/null <<'EOF'
60+
appsec_configs:
61+
- crowdsecurity/appsec-default
62+
labels:
63+
type: appsec
64+
listen_addr: 127.0.0.1:7422
65+
source: appsec
66+
EOF
67+
sudo systemctl restart crowdsec
18368
```
18469

185-
3. Emit a malevolent request to the Appsec Component:
186-
187-
:::info
188-
We're trying to access a `.env` file, a [common way to retrieve credentials left by mistake](https://app.crowdsec.net/hub/author/crowdsecurity/appsec-rules/vpatch-env-access).
70+
:::warning
71+
Keep `listen_addr` on `127.0.0.1` — the AppSec Component must not be reachable from the internet. It should only be queried by your local web server / reverse proxy.
18972
:::
19073

191-
```bash
192-
curl -X POST localhost:7422/ -i -H 'x-crowdsec-appsec-uri: /.env' -H 'x-crowdsec-appsec-ip: 192.168.1.1' -H 'x-crowdsec-appsec-host: foobar.com' -H 'x-crowdsec-appsec-verb: POST' -H 'x-crowdsec-appsec-api-key: this_is_a_bad_password'
193-
194-
```
74+
## 3. Point the bouncer at the AppSec Component
19575

196-
Our request is detected and blocked by the AppSec Component:
76+
<Tabs groupId="engine" defaultValue="nginx" values={[
77+
{label: 'Nginx', value: 'nginx'},
78+
{label: 'OpenResty', value: 'openresty'},
79+
]}>
80+
<TabItem value="nginx">
19781

19882
```bash
199-
HTTP/1.1 403 Forbidden
200-
Date: Tue, 30 Jan 2024 15:57:08 GMT
201-
Content-Length: 34
202-
Content-Type: text/plain; charset=utf-8
203-
204-
{"action":"ban","http_status":403}
83+
sudo sed -i 's|^APPSEC_URL=.*|APPSEC_URL=http://127.0.0.1:7422|' \
84+
/etc/crowdsec/bouncers/crowdsec-nginx-bouncer.conf
85+
sudo systemctl restart nginx
20586
```
20687

207-
Let's now delete our test API Key:
88+
</TabItem>
89+
<TabItem value="openresty">
20890

20991
```bash
210-
sudo cscli bouncers delete test_waf
92+
sudo sed -i 's|^APPSEC_URL=.*|APPSEC_URL=http://127.0.0.1:7422|' \
93+
/etc/crowdsec/bouncers/crowdsec-openresty-bouncer.conf
94+
sudo systemctl restart openresty
21195
```
21296

213-
</details>
214-
215-
## Remediation Component Setup
97+
</TabItem>
98+
</Tabs>
21699

217-
Since our AppSec Component is active and listening, we can now configure the Remediation Component to forward requests to it.
100+
The default bouncer config already contains an empty `APPSEC_URL=` line, so `sed -i` replaces it in place — the command is idempotent and safe to re-run.
218101

219-
To setup forwarding of requests in the remediation component, we'll modify its configuration file and append the following line:
102+
## 4. Verify
220103

221-
- `Nginx`: `/etc/crowdsec/bouncers/crowdsec-nginx-bouncer.conf`
222-
- `OpenResty`: `/etc/crowdsec/bouncers/crowdsec-openresty-bouncer.conf`
104+
Send a request that should trip an AppSec rule:
223105

224106
```bash
225-
APPSEC_URL=http://127.0.0.1:7422
107+
curl -I http://localhost/.env
108+
# expect: HTTP/1.1 403 Forbidden
226109
```
227110

228-
This instructs the remediation component to communicate with the AppSec Component at `http://127.0.0.1:7422`.
229-
230-
Once configured, all incoming HTTP requests will be sent there for analysis. The snippet above assumes that the AppSec Component is running on the same machine.
111+
We're hitting a `.env` file, a [common way to retrieve credentials left by mistake](https://app.crowdsec.net/hub/author/crowdsecurity/appsec-rules/vpatch-env-access) — the AppSec Component detects and blocks it.
231112

232-
We can now restart the service:
113+
Check that CrowdSec recorded the block:
233114

234115
```bash
235-
sudo systemctl restart nginx
116+
sudo cscli metrics show appsec
236117
```
237118

238-
## Testing the AppSec Component + Remediation Component
239-
240-
:::note
241-
We're assuming the web server is installed on the same machine and is listening on port 80. Please adjust your testing accordingly if this is not the case.
242-
You can also look at the [General WAF Testing](/docs/appsec/quickstart/general.mdx#testing-waf-component)
243-
:::
244-
245-
246-
247-
if you try to access `http://localhost/.env` from a browser, your request will be blocked, resulting in the display of the following HTML page:
248-
249-
![appsec-denied](/img/appsec_denied.png)
250-
251-
We can also look at the metrics from `cscli metrics show appsec` it will display:
252-
- the number of requests processed by the AppSec Component
253-
- Individual rule matches
254-
255-
<details>
256-
<summary>Example Output</summary>
119+
<details>
120+
<summary>Example metrics output</summary>
257121

258122
```bash title="sudo cscli metrics show appsec"
259123
Appsec Metrics:
@@ -273,31 +137,28 @@ Appsec '127.0.0.1:7422/' Rules Metrics:
273137

274138
</details>
275139

276-
### Explanation
140+
<details>
141+
<summary>What just happened?</summary>
277142

278-
What happened in the test that we just did is:
143+
1. `curl` hit your web server at `/.env`.
144+
2. The bouncer forwarded the request to the AppSec Component on `127.0.0.1:7422`.
145+
3. The request matched the [`vpatch-env-access`](https://app.crowdsec.net/hub/author/crowdsecurity/appsec-rules/vpatch-env-access) rule.
146+
4. The AppSec Component answered `403`, the bouncer enforced it, and the web server returned the CrowdSec ban page.
279147

280-
1. We did a request (`localhost/.env`) to our local webserver
281-
2. Thanks to the Remediation Component configuration, forwarded the request to `http://127.0.0.1:7422`
282-
3. Our AppSec Component, listening on `http://127.0.0.1:7422` analyzed the request
283-
4. The request matches the [AppSec rule to detect .env access](https://app.crowdsec.net/hub/author/crowdsecurity/appsec-rules/vpatch-env-access)
284-
5. The AppSec Component thus answered with [HTTP 403](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/403) to the Remediation Component, indicating that the request must be blocked
285-
6. The web server then presented us with the default "request blocked" page.
148+
</details>
286149

287-
## Integration with the Console
150+
If you'd rather see the block in a browser, visit `http://<your-host>/.env` — you'll get the CrowdSec ban page:
288151

289-
If you haven't yet, follow the guide about [how to enroll your Security Engine in the Console](/u/getting_started/post_installation/console).
152+
![appsec-denied](/img/appsec_denied.png)
290153

291-
Once done, all your alerts, including the ones generated by the AppSec Component, appear in the Console:
154+
## Monitor in the Console
292155

293-
![appsec-console](/img/appsec_console.png)
156+
If you haven't enrolled the Security Engine yet, follow [how to enroll in the Console](/u/getting_started/post_installation/console). Once enrolled, AppSec alerts show up alongside the rest of your alerts:
294157

158+
![appsec-console](/img/appsec_console.png)
295159

296160
## Next steps
297161

298-
You are now running the AppSec Component on your CrowdSec Security Engine.
299-
300-
As the next steps, you can:
301-
- Monitor WAF alerts with `sudo cscli alerts list` and in the [CrowdSec Console](https://app.crowdsec.net).
162+
- Monitor WAF alerts with `sudo cscli alerts list` or in the [CrowdSec Console](https://app.crowdsec.net).
302163
- Review the [AppSec troubleshooting guide](/appsec/troubleshooting.md) if you need to investigate or refine the deployment.
303164
- Explore [WAF deployment strategies](/appsec/advanced_deployments.mdx), [rules syntax](/appsec/rules_syntax.md), [rule creation](/appsec/create_rules.md), and [benchmarks](/appsec/benchmark.md) to go further.

0 commit comments

Comments
 (0)