Skip to content

Latest commit

 

History

History
313 lines (254 loc) · 9.75 KB

File metadata and controls

313 lines (254 loc) · 9.75 KB

ModSecurity Google Benchmark Implementation Summary

This document summarizes the new benchmarking framework added to ModSecurity.

Files Created

1. benchmark_rules.cc - Main Benchmark Suite

Purpose: Production-ready benchmark suite comparing different rule targets and operators

Features:

  • 13 pre-built benchmarks covering common scenarios
  • Tests single vs. collection targets (REQUEST_HEADERS:User-Agent vs REQUEST_HEADERS)
  • Compares operators (@contains vs @rx)
  • Tests transformations (none vs single vs multiple)
  • Measures baseline overhead
  • Uses realistic HTTP headers (15 headers with typical values)

Benchmarks Included:

  1. BM_NoRules - Baseline transaction overhead
  2. BM_SingleHeader - Target one header
  3. BM_AllHeaders - Target all headers
  4. BM_HeadersRegexKey - Regex key matching
  5. BM_HeaderNames - REQUEST_HEADERS_NAMES
  6. BM_MultipleHeaderRules - 5 different rules
  7. BM_RegexSingleHeader - @rx on one header
  8. BM_RegexAllHeaders - @rx on all headers
  9. BM_TransformationSingleHeader - With t:lowercase
  10. BM_TransformationAllHeaders - Collection with transformation
  11. BM_MultipleTransformations - Multiple t: actions
  12. BM_Args - ARGS collection
  13. BM_SingleArg - ARGS:name

2. benchmark_template.cc - Customizable Template

Purpose: Template for creating custom benchmarks quickly

Examples Included:

  1. Custom header testing
  2. Operator comparison (@contains vs @rx)
  3. Parameterized tests (varying header count)
  4. JSON body processing
  5. Multiple rule generation
  6. Transformation comparison

Key Features:

  • Clearly marked customization sections
  • Copy-paste ready examples
  • Comments explaining each benchmark type
  • Demonstrates Google Benchmark features (parameterization, labels, ranges)

3. README.md - Comprehensive Documentation

Sections:

  • Installation instructions for all platforms
  • Building methods (helper script, manual, integrated)
  • Running benchmarks with all options
  • Interpreting results
  • Adding custom benchmarks with examples
  • Performance tips
  • Troubleshooting guide

4. QUICKSTART.md - Quick Reference

Contents:

  • TL;DR commands to get started in 30 seconds
  • Installation one-liners
  • Common use cases
  • Quick examples
  • Troubleshooting

5. build_with_benchmark.sh - Build Helper Script

Purpose: Automated build script that handles dependencies

Features:

  • Checks for Google Benchmark installation
  • Verifies libmodsecurity is built
  • Auto-detects PCRE/PCRE2 and YAJL
  • Provides clear error messages
  • Shows exact build command used

Usage:

./build_with_benchmark.sh

6. configure_snippet.m4 - Autotools Integration

Purpose: Autoconf macro for detecting Google Benchmark

Features:

  • --with-benchmark configure option
  • Tries pkg-config first
  • Falls back to library check
  • Sets HAVE_GOOGLE_BENCHMARK conditional
  • Provides BENCHMARK_LDADD and BENCHMARK_CFLAGS

To Use: Copy content to configure.ac and run ./build.sh

7. Makefile.am - Updated Build Configuration

Changes:

  • Added conditional build for benchmark_rules
  • Only builds if HAVE_GOOGLE_BENCHMARK is set
  • Proper LDADD/LDFLAGS for Google Benchmark
  • Maintains backward compatibility (original benchmark still builds)

Architecture

ModSecFixture Helper Class

Manages ModSecurity lifecycle for benchmarks:

  • Creates ModSecurity and RulesSet instances
  • Handles rule loading and error checking
  • Creates Transaction objects
  • Cleans up resources

Transaction Helper Function

runTransaction() executes a complete HTTP transaction:

  1. Connection phase
  2. URI phase
  3. Request headers phase (adds all test headers)
  4. Request body phase
  5. Checks for interventions at each phase
  6. Proper cleanup

Test Data

Static test data includes:

  • 15 realistic HTTP headers (User-Agent, Host, Cookie, etc.)
  • URI with query parameters
  • Client/Server IPs

Usage Workflow

For Quick Testing

# Install Google Benchmark
brew install google-benchmark  # or apt-get install libbenchmark-dev

# Build
cd test/benchmark
./build_with_benchmark.sh

# Run
./benchmark_rules

# Filter specific tests
./benchmark_rules --benchmark_filter="SingleHeader"

For Custom Benchmarks

# Copy template
cp benchmark_template.cc my_test.cc

# Edit my_test.cc:
# - Customize MY_HEADERS
# - Modify rules in benchmark functions
# - Add new benchmark functions

# Build
g++ -std=c++17 -O3 -I../../headers my_test.cc \
    -L../../src/.libs -lmodsecurity -lbenchmark -lpthread \
    -o my_test

# Run
./my_test

For Integration into Build System

# 1. Add configure snippet to configure.ac
cat configure_snippet.m4  # Copy this content

# 2. Rebuild
./build.sh
./configure --with-benchmark
make

# 3. Run
cd test/benchmark
./benchmark_rules

Example Results

What You'll See

Run on (8 X 2400 MHz CPUs)
------------------------------------------------------------------------
Benchmark                              Time             CPU   Iterations
------------------------------------------------------------------------
BM_NoRules                          1234 ns         1230 ns       567890
BM_SingleHeader                     2345 ns         2340 ns       298765
BM_AllHeaders                       4567 ns         4560 ns       153456

How to Interpret

  • BM_NoRules shows baseline overhead (1.23 microseconds)
  • BM_SingleHeader shows processing one rule on one header (2.34 µs)
  • BM_AllHeaders shows processing one rule on all headers (4.56 µs)
  • Insight: Targeting all headers is ~2x slower than single header

Key Features

1. Modular Design

  • ModSecFixture class separates setup from measurement
  • Easy to add new benchmarks
  • Reusable test data

2. Comprehensive Coverage

  • Different variable targets
  • Different operators
  • Transformations
  • Multiple rules
  • Baseline measurements

3. Well Documented

  • Four documentation files
  • Inline code comments
  • Clear examples
  • Troubleshooting guides

4. Flexible Building

  • Helper script for quick testing
  • Autotools integration for production
  • Manual build for customization

5. Best Practices

  • Disables body processing when testing headers
  • Uses nolog to avoid logging overhead
  • Proper intervention cleanup
  • Realistic test data

Comparison with Original Benchmark

Feature Original (benchmark.cc) New (benchmark_rules.cc)
Framework Custom timing Google Benchmark
Granularity Full transactions Per-rule microbenchmarks
Comparison Manual (use time) Built-in comparison
Statistics None Iterations, CPU time, repetitions
Filtering None Command-line filters
Output Basic JSON, CSV, console
Customization Edit code Template + filters
Use Case Throughput testing Performance analysis

Both are useful:

  • Use benchmark for overall throughput with real rule sets
  • Use benchmark_rules for detailed analysis of specific rules

Performance Tips (from Documentation)

  1. Build with optimizations: CFLAGS="-O3 -march=native"
  2. Use release build, not debug
  3. Disable body processing in test rules
  4. Use nolog action
  5. Run with repetitions: --benchmark_repetitions=10
  6. Disable CPU frequency scaling on Linux
  7. Close other applications

Next Steps for Users

  1. Install Google Benchmark: Choose your platform's method
  2. Quick test: Run ./build_with_benchmark.sh && ./benchmark_rules
  3. Explore results: See which operations are slower
  4. Customize: Copy template and test your specific rules
  5. Compare: Make changes, benchmark, compare results
  6. Integrate: Add to configure.ac for permanent integration

Integration Checklist

  • Create benchmark suite (benchmark_rules.cc)
  • Create customizable template (benchmark_template.cc)
  • Write comprehensive docs (README.md)
  • Write quick start guide (QUICKSTART.md)
  • Create build helper script (build_with_benchmark.sh)
  • Provide autotools integration (configure_snippet.m4)
  • Update Makefile.am
  • User adds snippet to configure.ac (optional)
  • User runs benchmarks
  • User customizes for their needs

Files Overview

test/benchmark/
├── benchmark.cc                    # Original simple benchmark (unchanged)
├── benchmark_rules.cc              # NEW: Google Benchmark suite
├── benchmark_template.cc           # NEW: Template for custom benchmarks
├── build_with_benchmark.sh         # NEW: Helper build script
├── configure_snippet.m4            # NEW: Autotools integration code
├── README.md                       # NEW: Full documentation
├── QUICKSTART.md                   # NEW: Quick reference
├── IMPLEMENTATION_SUMMARY.md       # NEW: This file
├── Makefile.am                     # UPDATED: Added benchmark_rules
├── basic_rules.conf                # Existing
└── download-owasp-*.sh            # Existing

Questions & Support

  • How do I install Google Benchmark? See QUICKSTART.md
  • How do I build? Run ./build_with_benchmark.sh
  • How do I customize? Copy benchmark_template.cc
  • What benchmarks exist? See README.md or run --benchmark_list_tests
  • How do I interpret results? See README.md "Interpreting Results" section
  • Build errors? See README.md "Troubleshooting" section

Summary

This implementation provides a modern, professional benchmarking framework for ModSecurity that enables:

  1. Quick performance analysis of different rule patterns
  2. Detailed comparisons between operators and targets
  3. Statistical rigor through Google Benchmark
  4. Easy customization via templates
  5. Clear documentation for all skill levels

The framework is production-ready, well-documented, and follows ModSecurity coding standards.