Labs / Network Security / Proxies

Lab 2 of 2 · Proxies

WAF filtering: a firewall that reads the request

A web application firewall: what its rules catch, miss, and refuse by mistake.

Start this lab 5 steps

The idea

Every attack on a website arrives on the site's own port, 80 or 443, in an ordinary-looking request, so a Firewall that reads addresses and ports lets it in. Put a firewall in front of the site that reads the request itself, and find out what it catches, what slips past, and whom it refuses by mistake.

The WAF answers for the site
The WAF answers for the site
Two spellings, one path
Two spellings, one path
What the rules read
What the rules read
What may sit between the two words
What may sit between the two words
One on schedule, five early, then 429
One on schedule, five early, then 429

Steps

  1. A firewall that reads the request

    Pedal Point rents bikes from stations around town, and Inés runs its booking site. The site, rentals, sits on the shop's own network. The name customers type, pedalpoint.lab, points somewhere else: at shopWaf.

    A packet firewall in front of a website has to allow port 80, or 443 for HTTPS, or nobody can book a bike. Every attack on the site arrives on that port too, inside an ordinary HTTP request: a strange path, a strange search, a few thousand requests a minute. Addresses and ports can't tell those apart from customers.

    A web application firewall, a WAF, reads the request. shopWaf is a reverse proxy: customers connect to it, it reads each request whole, and it passes on only the ones its rules allow, over its own connection to rentals. A forward proxy speaks for one building's PCs to the whole web; a reverse proxy speaks for one site to everyone.

    WAFs grew up with web shops. ModSecurity, the best-known open-source one, appeared in 2002, and from 2008 the card industry's rules, PCI DSS, told sites taking cards to run a WAF or have their code reviewed.

    rentals has an admin page at /admin, which Inés wants kept from the public. Fetch the home page from customerPc, then the admin page.

    customerPc:~# curl -sS -m 3 http://pedalpoint.lab/
    customerPc:~# curl -sS -i -m 3 http://pedalpoint.lab/admin
  2. Two disguises for one path

    The rules live in their own file on shopWaf, /etc/nginx/waf.rules. Each line is an if: a part of the request, ~* meaning "matches this pattern, in any case", the pattern, and the name of the rule to log.

    $uri is the path. ^/admin is a regular expression: ^ means "at the start", so the rule matches any path that starts with /admin.

    Nobody probing a site stops at the polite spelling. A URL can write any byte as % and two hex digits (0 to 9 and a to f), so %61 is the letter a and /%61dmin is /admin in disguise.

    A path can also wander: /book/../admin goes into /book and back out, and lands on /admin. curl tidies .. away before sending, so --path-as-is makes it send the path exactly as typed.

    Read the rules, try both disguises, then read the WAF's own log, /var/log/nginx/waf.log, one line per refusal.

    shopWaf:~# cat /etc/nginx/waf.rules
    customerPc:~# curl -sS -I -m 3 http://pedalpoint.lab/%61dmin
    customerPc:~# curl -sS -I -m 3 --path-as-is http://pedalpoint.lab/book/../admin
    shopWaf:~# tail -n 2 /var/log/nginx/waf.log
  3. An attack in the query string

    Everything after the ? is the query string, where a search form's fields travel: /search?q=harbour. It is also where a classic attack on database-backed sites, SQL injection, travels.

    The attacker types database commands into a field, hoping the site pastes them into its own database query. union select is two words from that attack: it makes a query return rows from another table, such as the passwords.

    The argument rule reads $args, the query string, with the pattern union.+select: union, then at least one of anything, then select.

    Send the attack twice: plainly, then with the U written %55, which the application decodes back into U when it reads the field. Then read rentals' own log, /var/log/httpd.log, which notes each request it was handed.

    customerPc:~# curl -sS -I -m 3 "http://pedalpoint.lab/search?q=1+union+select+password+from+users"
    customerPc:~# curl -sS -m 3 "http://pedalpoint.lab/search?q=1+%55NION+SELECT+password+from+users"
    rentals:~# tail -n 2 /var/log/httpd.log
  4. Refusing a customer

    A WAF can fail the other way: refuse an honest request. That is a false positive, and it is worse than it sounds. The customer gets a 403 and leaves, rentals never hears of it, and only the WAF's log knows.

    Pedal Point's booking form searches by station and bike. Someone picking up a Select Cruiser at Union Square sends station=union-square&bike=select-cruiser.

    The fix is a tighter pattern, closer to the attack as it was sent: union, then spaces, then select.

    In a query string a space is sent as + or %20, so the new pattern is union(%20|[+])+select. (%20|[+]) means %20 or +, since | means "or", and the + after it means one or more of them. Brackets make the inner + a plain character.

    Try the booking. Then write the new rules file on shopWaf and tell nginx to read it again with nginx -s reload, which swaps the rules without dropping a single connection. Then try the booking and the attack again.

    customerPc:~# curl -sS -I -m 3 "http://pedalpoint.lab/search?station=union-square&bike=select-cruiser"
    shopWaf:~# nginx -s reload
    customerPc:~# curl -sS -I -m 3 "http://pedalpoint.lab/search?station=union-square&bike=select-cruiser"
    customerPc:~# curl -sS -I -m 3 "http://pedalpoint.lab/search?q=1+union+select+password+from+users"
  5. Too many, too fast

    Some trouble looks innocent one request at a time. Someone guessing passwords, a bot copying every price, a flood meant to knock the site over: each request is ordinary, and there are thousands of them.

    shopWaf counts requests from each address with nginx's limit_req: one request every 200 ms, 5 a second. The first request is on schedule, and up to 5 more may come early, each taking a place in a bucket that empties at the same pace. A request that finds the bucket full gets 429 Too Many Requests, and rentals never hears of it.

    Two more lines of the rules read the User-Agent header, the name a client sends for itself. nikto tests web sites for holes, and masscan sweeps networks for open ports. Both send their names by default.

    Ask for the home page calling yourself nikto, then calling yourself a browser. Then send 12 requests at once: [1-12] in a URL makes curl send twelve, one after another, and -w prints each one's status code. Then read the WAF's log.

    customerPc:~# curl -sS -I -m 3 -A "Nikto/2.5.0" http://pedalpoint.lab/
    customerPc:~# curl -sS -I -m 3 -A "Mozilla/5.0" http://pedalpoint.lab/
    customerPc:~# curl -sS -m 3 -o /dev/null -w "%{http_code} " "http://pedalpoint.lab/?try=[1-12]"
    shopWaf:~# tail -n 3 /var/log/nginx/waf.log

Run these steps on real machines

Keep reading

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab