create a nginx config that block sqli in first 50 params
- User
create a nginx config that block sqli in first 50 params
- Assistant
create a nginx config that block sqli in first 50 params
Blocking SQL Injection (SQLi) attacks effectively requires inspecting incoming request parameters for malicious patterns that are commonly used in such attacks. While Nginx alone provides some capabilities for basic filtering using regular expressions, for comprehensive protection—especially when dealing with a large number of parameters (like the first 50)—it's recommended to use a Web Application Firewall (WAF) such as ModSecurity in conjunction with Nginx.
Below, I’ll provide two solutions:
This configuration uses Nginx’s rewrite and if directives to inspect the request’s query string for common SQLi patterns. It also includes a basic mechanism to limit the length of the query string, which indirectly limits the number of parameters.
http {
server {
listen 80;
server_name example.com;
# Limit the size of the request URI to control the number of parameters
# Adjust 'invalid_header' size as needed to handle up to 50 parameters
large_client_header_buffers 4 16k;
# Define a variable to detect SQLi attempts
set $sqli_detected 0;
# Regex to match common SQL injection patterns
if ($args ~* "(union(\s+all)?\s+select|select\s+.*\s+from|insert\s+into|drop\s+table|--|;|/\*|\*/|sleep\s*\()") {
set $sqli_detected 1;
}
# Optionally, limit the number of parameters by restricting query string length
# Example: Allow up to 4096 characters (~50 parameters depending on length)
if ($args ~ "^.{4097,}$") {
set $sqli_detected 1;
}
# Block the request if SQLi is detected
if ($sqli_detected) {
return 403;
}
# Handle requests normally if no SQLi is detected
location / {
proxy_pass http://backend_server;
# Other proxy settings...
}
}
}
large_client_header_buffers: Increases the buffer sizes to handle longer URLs with many parameters. Adjust as necessary.if statements use case-insensitive regex (~*) to check for common SQL keywords and patterns in the query string ($args).403 Forbidden status.if and regex in Nginx can impact performance under high load.For robust SQLi protection, especially when dealing with a large number of parameters, integrating ModSecurity with Nginx is highly recommended. ModSecurity is a powerful open-source WAF that can provide detailed inspections and customizable rules.
Ensure you have ModSecurity installed with Nginx. You can follow ModSecurity's official installation guide.
Create a ModSecurity configuration file (e.g., /etc/nginx/modsec/modsec.conf) with the following content:
# Basic ModSecurity configuration
SecRuleEngine On
SecRequestBodyAccess On
SecResponseBodyAccess Off
# Set the maximum number of request arguments to 50
SecAction "id:900000,phase:1,pass,nolog,setvar:tx.max_args=50"
# Rule to deny requests with more than 50 arguments
SecRule ARGS "@count gt 50" "id:900001,phase:1,deny,status:403,msg:'Too many request arguments'"
# Include the OWASP Core Rule Set
Include /usr/share/modsecurity-crs/base_rules/*.conf
# Customize CRS to inspect only the first 50 parameters
# Note: CRS doesn't natively limit to first 50; custom rules may be needed.
Modify your Nginx server block to include ModSecurity settings:
http {
# Load ModSecurity module
modsecurity on;
modsecurity_rules_file /etc/nginx/modsec/modsec.conf;
server {
listen 80;
server_name example.com;
location / {
# Enable ModSecurity for this location
ModSecurityEnabled on;
ModSecurityConfig /etc/nginx/modsec/modsec.conf;
proxy_pass http://backend_server;
# Other proxy settings...
}
}
}
SecRuleEngine On: Activates ModSecurity.SecRequestBodyAccess On: Enables inspection of request bodies for POST requests.SecRule ARGS "@count gt 50" denies any request exceeding 50 parameters, responding with a 403 Forbidden status.While it's possible to implement basic SQLi protection using Nginx's native configuration capabilities, leveraging a dedicated WAF like ModSecurity provides a more robust and scalable solution, especially when dealing with a large number of parameters. ModSecurity's advanced rule sets and customization options make it better suited for effectively detecting and blocking sophisticated SQL Injection attempts.
If you choose to proceed with the basic Nginx configuration, ensure that you continuously update and refine your regex patterns to keep up with evolving SQLi techniques. However, for production environments where security is paramount, integrating ModSecurity with Nginx is the recommended approach.
Disclaimer: Always test security configurations in a staging environment before deploying them to production to ensure they work as expected and do not inadvertently block legitimate traffic.
Not yet included in a published release.