Writing a WAF in Rust
I’m developing a small WAF application for personal servers and small web applications.
https://github.com/hiroeorz/kunato/tree/main
Typically, WAFs are provided as managed cloud services, but this can be costly. I wanted something easier to deploy. Additionally, I wanted a system that could automatically set up blocks for vulnerabilities reported daily.
The project name is Kunato.
Kunato operates as a proxy between browsers and HTTP servers. It has the following features:
Features
Reverse Proxy
Located between browsers and applications and HTTP servers, it inspects all requests while forwarding traffic.
HTTP/2, HTTP/3, and HTTP/1.1
Supports HTTP/2 over TLS (ALPN) and h2c (connection based on prior knowledge), as well as HTTP/3 over QUIC as an option. It also has automatic fallback to HTTP/1.1. Requests to upstream servers are made using HTTP/1.1.
Automatic TLS Certificate Management
Uses the ACME protocol (TLS-ALPN-01 on HTTPS ports) to obtain and renew Let’s Encrypt certificates. No external tools like certbot, cron jobs, or deployment hooks are required. By specifying domains in the tls.acme setting, Kunato automatically handles certificate issuance, renewal, and rotation, caching them locally for use even after restarts.
If you already have your own certificates, you can specify them. Specifying certificates prevents Kunato from automatically obtaining them from Let’s Encrypt.
WebSocket Proxy
Forwards the HTTP/1.1 upgrade handshake to the upstream server, establishing a bidirectional tunnel. The handshake passes through the rule engine and protection policies (protection budgets), and idle tunnels are automatically closed.
Categorized Rule Sets
Rules are grouped by categories such as “SQL Injection” or “XSS.” Each category has a default action, which can be overridden by individual rules.
Daily Rule Updates Based on CVE
An optional update feature automatically updates the managed rule set once daily. Signatures for attacks exploiting the latest CVEs are added. During updates, conditional requests and SHA256 validation ensure validity, followed by an atomic swap.
The process involves GitHub Actions triggering at 3 AM, where OpenCode runs to collect newly registered CVEs and vulnerabilities reported in frameworks like Rails and PHP. It then selects vulnerabilities that can be mitigated by the WAF, simulates attacks, adds rules if they fail to block, and confirms success before committing to the main branch.
Subsequently, Kunato running on each HTTP server fetches the latest rule set from the main branch of GitHub and applies it without downtime (hot swap).
Response Header Rewriting
Removes or replaces headers that reveal server information (e.g., Server, X-Powered-By) in responses from upstream servers or those generated by Kunato itself.
Response Inspection
Inspects responses from upstream servers (status codes, headers, body) based on the category. For bodies, a size limit is set, allowing blocking, logging, or passing through. Data exceeding the limit is streamed without inspection.
Inspection of Encoded Bodies
Bodies compressed with gzip, deflate, or brotli are decompressed for matching. Request bodies are decoded based on content type (e.g., application/x-www-form-urlencoded, JSON escaping). Traffic itself remains unaltered. YAML-formatted rules and hot reloading — rules are written in YAML. Changes to rule files are detected at runtime, allowing immediate reload without restarting the process or disconnecting connections.
High Fault Tolerance
Continues service by retaining the last valid configuration, isolating failures, and avoiding traffic impact during configuration reloads.
Simple Installation
Installs a static binary, systemd service, configuration files, and an initial rule set on a Linux server by executing curl | sudo sh.
Detection and Mitigation of DoS/DDoS Attacks
Detects abnormal request patterns and automatically limits or blocks access from malicious sources.
Implementation in Rust
Achieves memory safety and predictable performance. Delivered as a single static binary without runtime dependencies.
Tools
Various operations are consolidated into the kunato command for simplicity.
Example:
Upgrade to the latest version
$ sudo kunato upgrade
Check logs
$ sudo kunato log
Stream logs in real time
$ sudo kunato log -f
For more details, check the README. Time range specification for logs is also possible.
README.md
Sandbox Testing
The project’s scripts run tools inside Docker containers to simulate attacks and verify defense capabilities through sandbox testing.
Current Status
Personally, I like the feature that automatically updates rules based on CVEs. Also, the ease of installation via curl is a plus.
Now that it’s somewhat functional, I’m using it as a WAF for this blog’s Rails application.
Comments
No comments yet.