In this code, we're using the Flask framework to create a simple web application with a search functionality. The is_sql_injection_attack function checks if a given payload contains any suspicious SQL keywords or patterns. In the before_request decorator, we intercept the incoming request and check if the query parameter (q) contains a potential SQL injection attack. If an attack is detected, we abort the request with a 403 Forbidden status code.
To use the code, you need to have Flask installed (pip install flask). Save the code to a file, run it, and access the search endpoint (/search?q=) in your browser or through an HTTP client. If the query contains suspicious SQL keywords, the request will be rejected with a 403 status code.
Please note that this is a basic example for educational purposes, and real-world WAF implementations involve more sophisticated techniques, including pattern matching, anomaly detection, and integration with security rule sets.