CloFix WAF · Scripting Engine

CloFix Lua Scripting Engine

Embedded Lua 5.4 with clofix_main entry point, threat scoring, and shared dictionaries. Runs custom WAF logic with isolated engines for precise request control.

Scripting Engine #5 25 details Published

Overview

💡 How it works: Lua scripts execute in isolated VM instances with 50ms timeout. Files run in alphabetical order - first non-allow action stops pipeline. The `clofix_main(request)` function receives a request object with all HTTP context, GeoIP, JA3 fingerprint, and threat intelligence fields. Shared dictionaries (`clofix.shared.*`) provide thread-safe cross-request storage with TTL. Payload normalization (`clofix.normalize_payload()`) defeats encoding evasion before pattern matching. The scoring engine allows multi-rule accumulation before final action. Structured attack logging (`clofix.log_attack()`) automatically captures full request context for SIEM integration. All functions are C-bound for performance - Lua scripts typically process 80,000 requests/second on modest hardware.

Feature details

Entry Point

Every script must define `function clofix_main(request)` returning an action table - `{ action = "allow" }`, `{ action = "block", status = 403, message = "..." }`, or `{ action = "rate_limit", rule_name = "RL_001" }`

Request Object

Full read-only access to `request.ip`, `request.method`, `request.path`, `request.uri`, `request.headers`, `request.body`, `request.json_body`, `request.cookies`, `request.query`, `request.form_data`, `request.ja3`, `request.ja4`, `request.is_tor`, `request.is_vpn`, `request.bot_score`, `request.ip_reputation`, `request.country`, `request.asn`

Shared Dictionaries

Persistent state across requests via `clofix.shared.ddos_attack` with `get()`, `set(key, value, ttl?)`, `incr()`, `delete()` - perfect for rate limiting, bot tracking, and DDoS protection

Payload Normalization

`clofix.normalize_payload(s)` - multi-pass URL decode, HTML decode, lowercase, and whitespace collapse for injection detection

Threat Intelligence

`clofix.is_tor()`, `clofix.is_vpn()`, `clofix.ip_reputation()` returns "clean"/"suspicious"/"malicious", `clofix.get_bot_score()` (0-100), `clofix.get_ja3()` fingerprint

Rate Limiting API

`clofix.rate_limit(key, limit, window_secs)` returns (count, exceeded), `clofix.rate_limit_ip(limit, window)`, `clofix.rate_limit_endpoint(limit, window)` for per-IP-path limits

Structured Attack Logging

`clofix.log_attack(rule, type, payload?)` auto-includes IP, country, UA, method, URI - essential for SIEM integration

Action Types

`allow`, `block` (403), `challenge` (429 CAPTCHA), `rate_limit` (429), `log_only`, `redirect` (with redirect_to field), `captcha`

Threat Scoring Engine

Return `{ score = 85 }` instead of action - engine applies thresholds (<20 allow, 20-49 log_only, 50-79 challenge, ≥80 block)

utils.* Helpers

`utils.contains()`, `utils.starts_with()`, `utils.ends_with()`, `utils.split()`, `utils.to_lower()`, `utils.json_encode()`, `utils.json_decode()`, `utils.url_encode()`, `utils.base64_encode()`, `utils.now()`

SQL Injection Detection

Patterns for UNION SELECT, INSERT INTO, DROP TABLE, OR 1=1, SLEEP(), BENCHMARK(), information_schema - with payload normalization

XSS Protection

Script tags, JavaScript protocol, event handlers (onerror, onload), SVG/iframe injection, and DOM-based XSS patterns

Command Injection

Unix command chaining (`;`, `|`, `&&`, `` ` ``), Windows cmd.exe, PowerShell, and dangerous commands (wget, curl, nc, python -c)

Path Traversal/LFI

`../`, `..\`, URL-encoded traversal (`%2e%2e%2f`), sensitive file access (`/etc/passwd`, `boot.ini`, `php://`, `file://`)

SSRF Protection

Internal IP ranges (127.0.0.1, 10.x.x.x, 172.16-31.x.x, 192.168.x.x), cloud metadata (169.254.169.254, metadata.google.internal)

Brute Force Protection

Login path detection with shared dictionary counters, 10 attempts/5min window, 30-minute ban, and configurable thresholds

DDoS Shield

Two-tier protection - burst detection (30 req/2s) and sustained rate (200 req/min) with escalating bans (60s → 600s → 86400s) using shared dictionaries

Bot Detection

`request.is_bot`, `request.is_headless`, `request.bot_score`, UA pattern matching (python, curl, wget, sqlmap, nikto, gobuster), and missing UA blocking

GeoIP Blocking

`request.country` ISO-3166 codes, `request.region`, `request.city`, `request.asn`, `request.org` - block specific countries, ASNs, or regions

JA3/JA4 Fingerprinting

`request.ja3` and `request.ja4` for TLS stack identification - block known attack tools even with IP rotation

IP Reputation

`clofix.ip_reputation()` returns "clean"/"suspicious"/"malicious" - block malicious IPs instantly

Path Whitelisting

Protect admin endpoints (`/admin`, `/wp-admin`) with IP allowlists using shared dictionaries

Execution Limits

50ms wall-clock timeout per script, 32MB memory limit, 200 call stack depth - prevents resource exhaustion

Directory Structure

`./lua_script/ /` with alphabetical execution order - use numeric prefixes (`01_rate_limit.lua`, `02_bot_detection.lua`) for priority

Domain Configuration

Enable Lua with `lua { enabled on; lua_shared_dict ddos_attack 10m; }` in domain config

How CloFix WAF uses it

💡 How it works: Lua scripts execute in isolated VM instances with 50ms timeout. Files run in alphabetical order - first non-allow action stops pipeline. The `clofix_main(request)` function receives a request object with all HTTP context, GeoIP, JA3 fingerprint, and threat intelligence fields. Shared dictionaries (`clofix.shared.*`) provide thread-safe cross-request storage with TTL. Payload normalization (`clofix.normalize_payload()`) defeats encoding evasion before pattern matching. The scoring engine allows multi-rule accumulation before final action. Structured attack logging (`clofix.log_attack()`) automatically captures full request context for SIEM integration. All functions are C-bound for performance - Lua scripts typically process 80,000 requests/second on modest hardware.

Canonical URL: https://clofix.com/features/clofix-waf/lua-engine/