8000 GitHub - furkanevran/caddy-waf: Caddy WAF (OWASP rule-based filtering, IP and DNS filtering, rate limiting, GeoIP)
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

furkanevran/caddy-waf

Β 
Β 

Repository files navigation

πŸ›‘οΈ Caddy WAF Middleware

A simple Web Application Firewall (WAF) middleware for the Caddy server, designed to provide comprehensive protection against web attacks. This middleware integrates seamlessly with Caddy and offers a wide range of security features to safeguard your applications.


πŸ“‘ Table of Contents

  1. 🌟 Key Features
  2. πŸš€ Installation
  3. πŸ› οΈ Configuration
  4. βš™οΈ Configuration Options
  5. πŸ“œ Rules Format (rules.json)
  6. πŸ›‘οΈ Protected Attack Types
  7. 🚫 Blacklist Formats
  8. ⏱️ Rate Limiting
  9. 🌍 Country Blocking
  10. πŸ”„ Dynamic Updates
  11. πŸ§ͺ Testing
  1. 🐳 Docker Support
  2. 🐍 Rule/Blacklist Population Scripts
  3. πŸ“œ License
  4. πŸ™ Contributing

🌟 Key Features

  • Rule-based request filtering with regex patterns.
  • IP and DNS blacklisting to block malicious traffic.
  • Country-based blocking using MaxMind GeoIP2.
  • Rate limiting per IP address to prevent abuse.
  • Anomaly scoring system for detecting suspicious behavior.
  • Request inspection (URL, args, body, headers, cookies, user-agent).
  • Protection against common attacks (SQL injection, XSS, RCE, Log4j, etc.).
  • Detailed logging and monitoring for security analysis.
  • Dynamic rule reloading without server restart.
  • Severity-based actions (block, log, allow, detect) for fine-grained control.

πŸš€ Installation

# Step 1: Clone the caddy-waf repository from GitHub
git clone https://github.com/fabriziosalmi/caddy-waf.git

# Step 2: Navigate into the caddy-waf directory
cd caddy-waf

# Step 3: Clean up and update the go.mod file
go mod tidy

# Step 4: Fetch and install the required Go modules
go get -v github.com/fabriziosalmi/caddy-waf github.com/caddyserver/caddy/v2 github.com/oschwald/maxminddb-golang

# Step 5: Download the GeoLite2 Country database
wget https://git.io/GeoLite2-Country.mmdb

# Step 6 (optional): Clean up previous build artifacts
# rm -rf buildenv_*

# Step 7: Build Caddy with the caddy-waf module
xcaddy build --with github.com/fabriziosalmi/caddy-waf=./

# Step 8: Run the compiled Caddy server
./caddy run

Final Notes

  • If you encounter any issues, ensure that your Go environment is set up correctly and that you're using a compatible version of Go (as specified in the caddy-waf repository's go.mod file).
  • After building Caddy with xcaddy, the resulting binary will include the WAF middleware. You can verify this by running:
    ./caddy list-modules
    Look for the http.handlers.waf module in the output.

πŸ› οΈ Configuration

Basic Caddyfile Setup

{
    auto_https off
    admin off
    debug
}

:8080 {
    log {
        output stdout
        format console
        level DEBUG
    }

    route {
        waf {
            # Anomaly threshold will block request if the score is => the threshold
            anomaly_threshold 5

            # Rate limiting: 1000 requests per 1 minute
            rate_limit 1000 1m

            # Rules and blacklists
            rule_file rules.json
            ip_blacklist_file ip_blacklist.txt
            dns_blacklist_file dns_blacklist.txt

            # Country blocking (requires MaxMind GeoIP2 database)
            block_countries GeoLite2-Country.mmdb RU CN KP

            # Whitelist countries (requires MaxMind GeoIP2 database)
            # whitelist_countries GeoLite2-Country.mmdb US

             # Define actions based on severity
            severity critical block
            severity high block
            severity medium log
            severity low log

             # Set Log Severity
            log_severity debug

            #Set Log JSON output
            log_json
        }
        respond "Hello, world!" 200
    }
}

βš™οΈ Configuration Options

Option Description Example
anomaly_threshold Sets the anomaly score threshold. anomaly_threshold 20
rule_file JSON file containing WAF rules rule_file rules.json
ip_blacklist_file File with blocked IPs/CIDR ranges ip_blacklist_file blacklist.txt
dns_blacklist_file File with blocked domains dns_blacklist_file domains.txt
rate_limit Rate limiting config rate_limit 100 1m
block_countries Country blocking config block_countries GeoLite2-Country.mmdb RU CN NK
whitelist_countries Country whitelisting config whitelist_countries GeoLite2-Country.mmdb US GB CA
severity Define actions based on severity levels severity critical block
log_severity Sets the minimum logging severity level for this module. log_severity debug
log_json Enables JSON log output log_json

πŸ“œ Rules Format (rules.json)

Rules are defined in a JSON file. Each rule specifies a pattern to match, targets to inspect, and actions to take.

[
    {
        "id": "wordpress-brute-force",
        "phase": 2,
        "pattern": "(?i)(?:wp-login\\.php|xmlrpc\\.php).*?(?:username=|pwd=)",
        "targets": ["URI", "ARGS"],
        "severity": "HIGH",
        "action": "block",
        "score": 8,
        "description": "Block brute force attempts targeting WordPress login and XML-RPC endpoints."
    }
]

Rule Fields

Field Description Example
id Unique rule identifier sql_injection
phase Processing phase (1-2) 1
pattern Regular expression pattern `(?i)(?:select
targets Areas to inspect ["ARGS", "BODY"]
severity Rule severity (CRITICAL, HIGH, MEDIUM, LOW) CRITICAL
action Action to take (block, log) block
score Score for anomaly detection 10
description Rule description Block SQL injection attempts.

πŸ›‘οΈ Protected Attack Types

  1. SQL Injection
    • Basic SELECT/UNION injections
    • Time-based injection attacks
    • Boolean-based injections
  2. Cross-Site Scripting (XSS)
    • Script tag injection
    • Event handler injection
    • SVG-based XSS
  3. Path Traversal
    • Directory traversal attempts
    • Encoded path traversal
    • Double-encoded traversal
  4. Remote Code Execution (RCE)
    • Command injection
    • Shell command execution
    • System command execution
  5. Log4j Exploits
    • JNDI lookup attempts
    • Nested expressions
  6. Protocol Attacks
    • Git repository access
    • Environment file access
    • Configuration file access
  7. Scanner Detection
    • Common vulnerability scanners
    • Web application scanners
    • Network scanning tools

🚫 Blacklist Formats

IP Blacklist (ip_blacklist.txt)

192.168.1.1
10.0.0.0/8
2001:db8::/32

DNS Blacklist (dns_blacklist.txt)

malicious.com
evil.example.org

⏱️ Rate Limiting

Configure rate limits using requests count and time window:

# 100 requests per minute
rate_limit 100 1m

# 10 requests per second
rate_limit 10 1s

# 1000 requests per hour
rate_limit 1000 1h

🌍 Country Blocking

Block traffic from specific countries using ISO country codes:

# Block requests from Russia, China, and North Korea
block_countries /path/to/GeoLite2-Country.mmdb RU CN KP

πŸ”„ Dynamic Updates

Rules and blacklists can be updated without server restart:

  1. Modify rules.json or blacklist files.
  2. Reload Caddy: caddy reload.

πŸ§ͺ Testing

Basic Testing

# Test rate limiting
for i in {1..10}; do curl -i http://localhost:8080/; done

# Test country blocking
curl -H "X-Real-IP: 1.2.3.4" http://localhost:8080/

# Test SQL injection protection
curl "http://localhost:8080/?id=1+UNION+SELECT+*+FROM+users"

# Test XSS protection
curl "http://localhost:8080/?input=<script>alert(1)</script>"

Load Testing

ab -n 1000 -c 100 http://localhost:8080/

Security Testing Suite

A test.sh script is included in this repository to perform a comprehensive security test suite. This script sends a series of forged curl requests, each designed to simulate a different type of attack.

Below is an example of how to run the script and a sample output:

caddy-waf % ./test.sh
WAF Security Test Suite
Target: http://localhost:8080
Date: Dom  5 Gen 2025 16:00:37 CET
----------------------------------------
[βœ—] SQL Injection - SQL Server Version                           [200]
[βœ—] SQL Injection - SQL Server Time Delay                        [200]
[βœ“] SQL Injection - Oracle Time Delay                            [403]
[βœ—] SQL Injection - Error Based 1                                [200]
[βœ—] SQL Injection - Error Based 2                                [200]
[βœ—] SQL Injection - Error Based 3                                [200]
[βœ—] SQL Injection - MySQL user                                   [200]
[βœ—] SQL Injection - PostgreSQL user                              [200]
[βœ—] SQL Injection - Case Variation                               [200]
[βœ—] SQL Injection - Whitespace Variation                         [200]
[βœ—] SQL Injection - Obfuscation Variation                        [200]
[βœ—] SQL Injection - Unicode Variation                            [200]
[βœ—] SQL Injection - Triple URL Encoded Variation                 [200]
[βœ—] SQL Injection - OOB DNS Lookup                               [200]
[βœ—] SQL Injection - Oracle OOB DNS Lookup                        [200]
[βœ“] SQL Injection - Header - Basic Select                        [403]
[βœ“] SQL Injection - Cookie - Basic Select                        [403]
[βœ—] SQL Injection - Header - Basic Select                        [200]
[βœ—] SQL Injection - JSON body                                    [200]
[βœ“] XSS - Basic Script Tag                                       [403]
[βœ“] XSS - IMG Onerror                                            [403]
[βœ“] XSS - JavaScript Protocol                                    [403]
[βœ“] XSS - SVG Onload                                             [403]
[βœ“] XSS - Anchor Tag JavaScript                                  [403]
[βœ“] XSS - URL Encoded Script                                     [403]
[βœ“] XSS - Double URL Encoded                                     [403]
[βœ“] XSS - URL Encoded IMG                                        [403]
[βœ“] XSS - Body Onload                                            [403]
[βœ“] XSS - Input Onfocus Autofocus                                [403]
[βœ“] XSS - Breaking Out of Attribute                              [403]
[βœ“] XSS - HTML Encoded                                           [403]
[βœ“] XSS - IFRAME srcdoc                                          [403]
[βœ“] XSS - Details Tag                                            [403]
[βœ“] XSS - HTML Comment Breakout                                  [403]
[βœ“] Path Traversal - Basic                                       [403]
[βœ“] Path Traversal - Double Dot                                  [403]
[βœ“] Path Traversal - Triple Dot                                  [403]
[βœ“] Path Traversal - URL Encoded                                 [403]
[βœ—] Path Traversal - Double URL Encoded                          [200]
[βœ“] Path Traversal - Mixed Slashes                               [403]
[βœ—] Path Traversal - UTF-8 Encoded                               [200]
[βœ“] Path Traversal - Encoded and Literal                         [403]
[βœ“] Path Traversal - Mixed Encoding                              [403]
[βœ—] Path Traversal - Multiple Slashes                            [200]
[βœ“] RCE - Basic Command                                          [403]
[βœ“] RCE - Base64 Command                                         [403]
[βœ“] RCE - Backticks                                              [403]
[βœ“] RCE - List Files                                             [403]
[βœ“] RCE - Uname                                                  [403]
[βœ“] RCE - ID                                                     [403]
[βœ“] RCE - whoami Command                                         [403]
[βœ“] RCE - Echo Test                                              [403]
[βœ“] RCE - Hex Encoded Command                                    [403]
[βœ“] RCE - Curl Request                                           [403]
[βœ“] RCE - Wget Request                                           [403]
[βœ“] RCE - Ping                                                   [403]
[βœ“] RCE - PowerShell Command                                     [403]
[βœ—] Log4j - JNDI LDAP                                            [200]
[βœ—] Log4j - Environment                                          [200]
[βœ—] Log4j - JNDI RMI                                             [200]
[βœ—] Log4j - System Property                                      [200]
[βœ—] Log4j - Lowercase                                            [200]
[βœ—] Log4j - Uppercase                                            [200]
[βœ—] Log4j - Date                                                 [200]
[βœ“] Log4j - Base64                                               [403]
[βœ—] Log4j - Partial Lookup                                       [200]
[βœ—] Log4j - URL Encoded                                          [200]
[βœ“] Header - SQL Injection                                       [403]
[βœ“] Header - XSS Cookie                                          [403]
[βœ“] Header - Path Traversal                                      [403]
[βœ“] Header - Custom X-Attack                                     [403]
[βœ—] Header -  X-Forwarded-Host                                   [200]
[βœ“] Header - User-Agent SQL                                      [403]
[βœ—] Header -  Host Spoof                                         [200]
[βœ“] Header -  Accept-Language                                    [403]
[βœ“] Protocol - Git Access                                        [403]
[βœ“] Protocol - Env File                                          [403]
[βœ“] Protocol - htaccess                                          [403]
[βœ—] Protocol - Web.config Access                                 [200]
[βœ“] Protocol - Java Web Descriptor                               [403]
[βœ“] Protocol - SVN Access                                        [403]
[βœ—] Protocol - Robots.txt                                        [200]
[βœ—] Protocol - VS Code Settings                                  [200]
[βœ—] Protocol - config.php Access                                 [200]
[βœ—] Protocol - Apache Server Status                              [200]
[βœ“] Valid - Homepage                                             [200]
[βœ“] Valid - API Endpoint                                         [200]
[βœ“] Scanner - SQLMap                                             [403]
[βœ“] Scanner - Acunetix                                           [403]
[βœ“] Scanner - Nikto                                              [403]
[βœ“] Scanner - Nmap                                               [403]
[βœ“] Scanner - Dirbuster                                          [403]
[βœ“] Valid - Health Check                                         [200]
[βœ“] Valid - Chrome Browser                                       [200]
[βœ—] Scanner -  Burp Suite                                        [200]
[βœ“] Scanner - OWASP ZAP                                          [403]
[βœ“] Scanner - Nessus                                             [403]
[βœ“] Scanner - Qualys                                             [403]
[βœ—] Scanner -  Wfuzz                                             [200]
[βœ“] Scanner -  OpenVAS                                           [403]
----------------------------------------
Results Summary
Total Tests: 100
Passed: 63
Failed: 37
Pass Percentage: 63%

The script performs 100 tests for different attack vectors, and shows the result for each test.

🐳 Docker Support

A Dockerfile is included to simplify building a Docker image with the Caddy server and WAF middleware. Here's how to use it:

# Build the Docker image
docker build -t caddy-waf .

# Run the Docker container
docker run -p 8080:8080 caddy-waf

The Dockerfile sets up the required environment, builds the Caddy server with the WAF middleware, and exposes port 8080.

🐍 Rule/Blacklist Population Scripts

Three Python scripts are provided in this repository to help automate the population of your rules and blacklists:

get_owasp_rules.py

This script fetches the OWASP core rules and converts them into the JSON format required for the WAF rules. You can use this script to automatically download and update your rules with the latest OWASP configurations.

python3 get_owasp_rules.py

get_blacklisted_ip.py

This script downloads the blacklisted IPs from several external sources. It consolidates the IPs into a single ip_blacklist.txt file, which can be used for blocking IPs.

python3 get_blacklisted_ip.py

get_blacklisted_dns.py

This script downloads blacklisted domains from various sources and consolidates them into the dns_blacklist.txt file.

python3 get_blacklisted_dns.py

These scripts provide a quick start and enable users to dynamically update their rules and blacklists using third-party sources.

πŸ“œ License

This project is licensed under the AGPLv3 License.

πŸ™ Contributing

Contributions are welcome! Please open an issue or submit a pull request.

About

Caddy WAF (OWASP rule-based filtering, IP and DNS filtering, rate limiting, GeoIP)

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 48.2%
  • Python 26.9%
  • Shell 22.3%
  • Dockerfile 2.6%
0