Skip to content

Instantly share code, notes, and snippets.

@fabriziosalmi
Created January 10, 2025 00:21
Show Gist options
  • Save fabriziosalmi/0088e1e480608802f2ea82e1d5322823 to your computer and use it in GitHub Desktop.
Save fabriziosalmi/0088e1e480608802f2ea82e1d5322823 to your computer and use it in GitHub Desktop.

Okay, let's further extend the explanation on tuning options for the Caddy ML WAF (caddy-mlf), digging deeper into the nuances and advanced strategies.

Expanding on the Core Concepts:

To effectively tune caddy-mlf, it's crucial to understand the interplay between the different configuration options. They don't operate in isolation; adjusting one can impact the effectiveness of others. Think of it like a complex instrument where each knob and slider needs careful adjustment to produce the desired sound (in this case, accurate threat detection).

Deeper Dive into the Tuning Goals and Trade-offs:

We touched upon the trade-off between high security (minimizing false negatives) and high availability (minimizing false positives). Let's elaborate:

  • Maximizing Security (Aggressive Tuning):

    • Rationale: In scenarios where the cost of a security breach is extremely high (e.g., financial transactions, sensitive data), you might prioritize catching as many threats as possible, even if it means occasionally blocking legitimate users.
    • Tactics:
      • Lower thresholds: Set anomaly_threshold and blocking_threshold relatively low.
      • Narrower normal ranges: Define normal_*_range options tightly around the expected "good" traffic.
      • Restrictive allow lists: Be very specific with normal_http_methods, normal_user_agents, and normal_referrers.
      • Higher weights for suspicious attributes: Emphasize attributes often associated with attacks (e.g., request_size_weight for buffer overflows, query_param_count_weight for parameter tampering).
      • Longer history window and more entries: Be more sensitive to patterns of behavior over time.
    • Potential Drawbacks: Increased risk of blocking legitimate users (false positives), potentially leading to user frustration and lost business. Requires vigilant monitoring and quick adjustments if false positives occur.
  • Prioritizing Availability (Lenient Tuning):

    • Rationale: For public-facing websites or applications where user experience is paramount, you might tolerate a slightly higher risk of missed attacks to ensure legitimate users are never blocked.
    • Tactics:
      • Higher thresholds: Set anomaly_threshold and blocking_threshold higher.
      • Broader normal ranges: Allow for more variation in request attributes.
      • More permissive allow lists: Include a wider range of acceptable methods, user agents, and referrers.
      • Lower weights for potentially noisy attributes: De-emphasize attributes that might vary significantly in normal traffic.
      • Shorter history window and fewer entries: Focus more on individual request characteristics rather than historical patterns.
    • Potential Drawbacks: Increased risk of missing actual threats (false negatives), potentially leading to security breaches or exploitation. Requires a strong defense-in-depth strategy with other security layers.
  • Finding the Balance (Adaptive Tuning):

    • Rationale: The ideal scenario is a balance between security and availability. This often involves an iterative process of observing, adjusting, and learning from the traffic patterns.
    • Tactics:
      • Start conservatively and gradually tighten: Begin with lenient settings and slowly make them more restrictive as you gain confidence and understanding of your traffic.
      • Monitor and analyze flagged requests: Investigate why requests are being marked as suspicious. Are they genuine threats or legitimate but unusual behavior?
      • Adjust based on real-world observations: Fine-tune thresholds, ranges, and weights based on the analysis of flagged requests.
      • Implement exceptions or whitelisting (if the module supported it directly): For consistently flagged but legitimate traffic, consider adding exceptions or whitelisting rules (though caddy-mlf itself doesn't have explicit whitelisting; you might achieve similar effects by broadening normal ranges or adjusting weights).
      • Regularly review and update configurations: Traffic patterns and attack methods evolve, so your WAF configuration needs to adapt as well.

More Granular Tuning Tactics for Specific Options:

Let's delve deeper into how to tune individual options based on specific scenarios:

  • anomaly_threshold and blocking_threshold:

    • Fine-tuning the gap: The difference between these two thresholds is important. A small gap means requests will quickly escalate from suspicious to blocked. A larger gap provides more time for observation or alternative actions (like logging or triggering alerts).
    • Starting points: A common starting point might be anomaly_threshold around 0.6-0.7 and blocking_threshold around 0.8-0.9, but this is highly application-dependent.
    • Iterative adjustment based on logs: If you see many requests just above the anomaly_threshold that are legitimate, consider raising it. If many clearly malicious requests are just below the blocking_threshold, consider lowering it.
  • normal_request_size_range:

    • Understanding application limits: Know the maximum expected size of legitimate requests (e.g., file uploads, large form submissions).
    • Considering attack vectors: Very small requests could indicate probing or certain types of attacks. Extremely large requests could be attempts at denial-of-service or buffer overflows.
    • Dynamic content: Be mindful of applications that generate dynamically sized responses. The "normal" range might need to be broader.
  • normal_header_count_range:

    • Typical browser behavior: Understand the typical number of headers sent by common browsers.
    • API requests: APIs might have different header counts than standard web page requests.
    • Suspiciously high or low counts: Unusually high header counts could be indicative of attempts to exploit vulnerabilities or bypass security measures. Very low counts might be from automated tools or simplistic attacks.
  • normal_query_param_count_range:

    • Application functionality: Some applications naturally use more query parameters than others.
    • Rest APIs: RESTful APIs often rely heavily on query parameters.
    • Excessive parameters: A large number of query parameters could be an attempt to overwhelm the server or exploit vulnerabilities.
  • normal_path_segment_count_range:

    • Website architecture: The number of path segments often reflects the organization of the website.
    • Deeply nested paths: Legitimate applications might have deeply nested paths.
    • Suspiciously long or short paths: Very long paths could be attempts to access files outside the web root. Very short paths might be probes for common vulnerabilities.
  • normal_http_methods:

    • RESTful principles: For APIs, carefully define the allowed methods (GET, POST, PUT, DELETE, etc.).
    • Disabling unused methods: If your application doesn't use certain HTTP methods, explicitly exclude them.
    • Unexpected methods: Seeing methods like TRACE or TRACK might indicate malicious activity.
  • normal_user_agents:

    • Legitimate browser diversity: Account for the variety of legitimate browsers and their versions.
    • Bots and crawlers: You might need to differentiate between legitimate search engine crawlers and malicious bots.
    • Custom applications: If users access your application through custom clients, include their user agents.
    • Regular expressions (if supported by the module - currently simulated): A more advanced approach would be to use regular expressions to match patterns of legitimate user agents rather than exact strings.
  • normal_referrers:

    • Trusted domains: List domains from which you expect legitimate traffic to originate.
    • Direct access: Consider whether direct access (no referrer) is normal for your application.
    • Empty or suspicious referrers: Be wary of empty referrers or referrers from known malicious sites.
  • Weights:

    • Prioritizing important attributes: Assign higher weights to attributes you believe are strong indicators of malicious activity in your specific context.
    • Experimentation: Start with relatively even weights and then adjust based on observations.
    • The sum doesn't have to be 1: While it's good practice for conceptual clarity, the absolute value isn't as important as the relative weights. Focus on the ratios between weights.
  • history_window and max_history_entries:

    • Types of attacks: A longer window can help detect slower, more persistent attacks. A shorter window is better for identifying rapid bursts of activity.
    • Resource considerations: Storing request history consumes resources. Balance the need for historical data with resource limitations.
    • Session management: Consider the typical session length for your users.

Advanced Tuning Scenarios:

  • Context-Aware Tuning (Hypothetical): Imagine if caddy-mlf could apply different configurations based on the request path or other context. You might have stricter rules for login pages or admin areas compared to public-facing content. While the current simulated version doesn't have this, it's a concept in real-world WAFs.
  • Adaptive Tuning (Manual in this case): Since caddy-mlf is a simulation, "adaptive tuning" means manually adjusting the configuration over time based on observed traffic patterns and incidents. This requires ongoing monitoring and analysis.
  • Utilizing External Data (Hypothetical): Real-world WAFs can integrate with threat intelligence feeds. Imagine if caddy-mlf could incorporate lists of known bad IPs or malicious patterns – this would significantly improve accuracy.

The Crucial Role of Monitoring and Logging:

Tuning without proper monitoring is like flying blind. Key aspects of monitoring include:

  • Detailed Logging: The example configuration includes debug logging, which is essential during tuning.
  • Analyzing Log Output: Look for:
    • Blocked requests: Understand why they were blocked (high anomaly score, exceeding blocking threshold).
    • Suspicious requests: Investigate why they were flagged as suspicious but not blocked.
    • Recurring patterns: Are certain IPs, user agents, or request patterns consistently being flagged?
    • False positives: Identify legitimate requests being incorrectly flagged.
    • False negatives (harder to detect): Look for anomalies in your application logs or other security systems that might indicate missed attacks.
  • Visualizations and Dashboards: For more complex environments, consider using tools to visualize log data and identify trends.

Validating Your Tuning Efforts:

After making changes, it's crucial to validate their effectiveness:

  • Simulated Attacks: Use tools or scripts to simulate various attack scenarios and see if caddy-mlf detects them.
  • Real-World Traffic Observation: Monitor the logs after making changes to see if the behavior is as expected.
  • A/B Testing (if feasible): If you have a way to route a portion of your traffic through a different configuration, you can compare the results.
  • Canary Deployments: Roll out configuration changes to a small subset of your infrastructure first to observe the impact before applying them broadly.

Recognizing the Limitations of a Simulation:

It's essential to remember that caddy-mlf is a simulation. It doesn't have the sophisticated algorithms or real-time learning capabilities of production ML-based WAFs. Its accuracy is entirely dependent on the quality of your configuration. This makes understanding the underlying principles of WAF operation and the characteristics of malicious traffic even more important.

Analogy for Understanding Tuning:

Think of tuning caddy-mlf like adjusting the sensitivity of a motion detector for a security alarm.

  • Too sensitive (low thresholds, narrow ranges): The alarm triggers constantly for minor movements (false positives).
  • Not sensitive enough (high thresholds, broad ranges): Intruders can easily slip by undetected (false negatives).
  • Properly tuned: The alarm only triggers for significant threats, providing effective security without excessive false alarms.

A Structured Tuning Process:

  1. Establish a Baseline: Start with a conservative configuration and log everything.
  2. Analyze Normal Traffic: Understand the typical patterns of legitimate requests.
  3. Identify Tuning Goals: Are you prioritizing security or availability?
  4. Make Incremental Adjustments: Change one or two options at a time.
  5. Monitor and Analyze: Observe the impact of your changes on flagged and blocked requests.
  6. Validate with Testing: Simulate attacks to verify effectiveness.
  7. Iterate and Refine: Continuously adjust the configuration based on your findings.
  8. Document Your Changes: Keep track of what you've changed and why.

By following these extended explanations and implementing a structured tuning process, you can significantly improve the accuracy and effectiveness of your caddy-mlf simulated WAF. Remember that this is an ongoing process that requires continuous monitoring and adaptation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment