Web Application Firewall (WAF) — Security

A WAF (Web Application Firewall) is a filter at layer 7 (the HTTP layer) that reads every HTTP request and blocks the ones that match its rules. Examples are…

Your online shop's server log (the record of every request that comes in) is full of odd requests: SQL injection attempts in the product URL, scripts in the search box, and ../ in a file parameter. Safe code is still the main protection. But you can also put a guard in front of the app that stops common attack patterns before they reach the app.

That guard is a WAF (Web Application Firewall), a filter in front of a web app. A WAF reads every HTTP request, matches it against a list of attack patterns, then blocks the ones that match before they reach your app. Picture a security guard in an office building's lobby holding a list of forbidden items. A bag with an item from the list is held back. But if the list is too broad, the canteen cook carrying a kitchen knife is stopped too, although the knife is for cooking. The guard also doesn't know whether a visitor may enter room 12. That's the job of the room's own door lock, which is your app's code. Here, the guard is the WAF, the list of forbidden items is the WAF's rules, a bag is a request, and the cook who was stopped is a legitimate request that gets blocked too.

Where a WAF stands

This diagram shows the path of a request from a visitor to the app:

Visitor  ->  WAF (read the request, match the rules)  ->  App
               |
               +-> matches a block rule: 403 Forbidden, the request stops here

A WAF works at layer 7 (the application layer, the layer where HTTP lives). A WAF reads the contents of an HTTP request: the path, query, headers and body. The server firewall (UFW) in the Server Hardening lesson only sees the IP address and port. Two forms of WAF are common:

Rules: match, then block or log

A rule has three parts: the part of the request it reads, the pattern it looks for, and the action when it matches. These are three lines of ModSecurity config:

SecRuleEngine On

SecRule ARGS "@contains OR 1=1" "id:1003,phase:2,deny,status:403,log,msg:'SQLi pattern'"
SecRule REQUEST_HEADERS:User-Agent "@contains sqlmap" "id:1007,phase:1,deny,status:403,log,msg:'Scanner'"

DetectionOnly is monitoring mode: a new rule runs first without blocking. So you can see which legitimate requests match it, before a real user is refused.

Testing rules against the request log

The Lab has nine requests from a log. Five classic attacks, four legitimate requests:

Request Kind
GET /products?id=1 OR 1=1 -- attack: SQL injection
GET /search?q=<script>alert(1)</script> attack: XSS
GET /files?path=../../etc/passwd attack: path traversal
GET /ping?host=;cat /etc/passwd attack: command injection
GET /products?id=5 with User-Agent sqlmap attack: automated scanner
GET /search?q=O'Brien legitimate: a person's name
GET /notes?text=buy milk; buy bread legitimate: a shopping note
GET /files?path=docs/guide.pdf legitimate: a document download
GET /ping?host=api.belajarweb.example legitimate: a server check

sqlmap is a program that searches for SQL injection holes automatically, and its name appears in the User-Agent header.

The Lab starts with the two rules people often write first: block arguments containing ', and block arguments containing ;. Guess before you look at the table: how many of the five attacks does the quote rule catch, and which legitimate request gets blocked too?

The result:

Rule Attacks caught Legitimate requests blocked too
argument contains ' 0 O'Brien
argument contains ; command injection buy milk; buy bread

The quote rule catches zero attacks, because none of those five attacks uses a quote. Instead, it blocks someone called O'Brien. That's the answer to the guess. A legitimate request that gets blocked is called a false positive. An attack that gets past the WAF is a false negative. Both matter equally: a WAF that blocks customers hurts as much as a WAF that lets attacks in.

Better rules target the attack's shape, or even name the shape that is allowed:

Rule Catches False positives
argument contains OR 1=1 SQL injection 0
argument contains <script XSS 0
argument contains ../ path traversal 0
on /ping, host isn't a hostname (^[a-z0-9][a-z0-9.-]*$) command injection 0
User-Agent contains sqlmap scanner 0

The /ping rule is an allowlist: it doesn't try to guess every attack shape. It only allows letters, digits, dots and minus signs for a parameter that really holds a host name (hostname). The first character must be a letter or a digit, so -h or -f is refused too, because ping would read it as an option, not a host name. On this Lab log, those five rules catch all five attacks with zero false positives.

Why a WAF doesn't replace secure code

Rules match text patterns. An attack written another way (same meaning, different text) may not match the pattern. That's what WAF bypass means. A WAF also doesn't know your app's logic. Say a request GET /api/orders/124 comes from a user who doesn't own that order. The request looks normal, so the WAF allows it. The check of whether this order belongs to the logged-in user has to be in the code.

So a WAF is an extra layer (defence in depth): it holds back common attacks and gives you time to fix the code. The main protection is still prepared statements, output escaping, input validation and access checks in the app.

⚠️ Rules that block customers, or block nothing

TL;DR: A WAF (Web Application Firewall) is a filter at layer 7 (the HTTP layer) that reads every HTTP request and blocks the ones that match its rules. Examples are Cloudflare WAF, AWS WAF, and ModSecurity (open-source, an Apache/Nginx module) with the OWASP CRS (a ready-made rule set). A rule holds a part of the request, a pattern, and an action (deny with 403, or just log). In ModSecurity, SecRuleEngine DetectionOnly is monitoring mode (it only records) before On. Test rules against the request log. A rule that is too broad causes false positives (legitimate requests blocked too). A precise rule, or an allowlist, catches attacks without bothering users. A WAF can be bypassed (an attack written another way doesn't match the rules) and doesn't know the app's logic, so it's an extra layer, not a replacement for secure code.

⚡ A guard in front of the app

Meaning: A WAF (Web Application Firewall) reads every HTTP request at layer 7 (the HTTP layer), matches it against a list of attack patterns, then blocks the ones that match with 403 Forbidden. Examples are Cloudflare WAF, AWS WAF, and ModSecurity with the OWASP Core Rule Set (a ready-made set of rules).

⚡ Rules: match, then block or log

Write:

SecRule ARGS "@contains OR 1=1" "id:1003,phase:2,deny,status:403,log,msg:'SQLi pattern'"

Meaning: A rule reads one part of the request, looks for a pattern, then acts. This ModSecurity rule reads ARGS (every request argument), looks for the text OR 1=1, then blocks the request with 403. Run a new rule in DetectionOnly mode (it only records) first, so you see which legitimate requests match it before a real user is refused.

⚡ Rules that are too broad

Meaning: Blocking every ' or ; refuses someone called O'Brien and an ordinary shopping note such as buy milk; buy bread, while the real attacks aren't necessarily caught. Good rules target the attack's shape, or name the shape that's allowed (an allowlist).

⚡ An extra layer, not a replacement

Guess first: a WAF matches requests against text patterns of attacks. Why is a WAF only an extra layer, not a replacement for secure code?

A WAF can be bypassed. An attack written another way, same meaning but different text, may not match the pattern. A WAF also doesn't know your app's logic, for example who owns an order.

Meaning: The main protection is still prepared statements, output escaping, and input validation.

What you will learn