Implement LogCruncher, a blazingly fast log analysis component using Rust. It will parse server logs (Apache/Nginx) to detect attack patterns (SQLi, XSS, Path Traversal) with memory safety and speed that Python cannot match for large files.
Note
Performance: Rust is chosen here because regular expression matching on GB-sized log files is CPU-intensive. Rust's regex crate is O(n), guaranteeing linear time execution.
- Dependencies:
regex: For pattern matching.serde,serde_json: For JSON output.clap: For CLI argument parsing.
- Logic:
- Read log lines from stdin or file.
- Apply regex signatures (e.g.,
(UNION SELECT|OR 1=1)). - Output detected threats as JSON.
- Class:
LogScanner - Method:
analyze_log(filepath)- Spawns
./guard_binsubprocess. - Pipes log content to it.
- Returns findings.
- Spawns
- Build Stage: Add
FROM rust:1.75 AS rust-builder. Compileguard/. - Runtime Stage:
COPY --from=rust-builder /app/target/release/guard /app/bin/guard_bin
- Unit Test: Create a sample
access.logwith a known SQL injection attack. - Manual Run:
cat sample.log | ./guard_binand verify JSON alert. - UI: Upload a log file (if UI supports it) or trigger a dummy log scan.