On my site, only few query query parameters are allowed but, some scanners or hackers trying to access url with unique parameters which my php application doesn't support, I can block them in php application level, by validating $_GET
parameters, but my server is getting loaded, so I want to show 403 if parameters are not valid
Query parameters can be in any order
So far what I tried is as follows
# IF there is query string
RewriteCond %{QUERY_STRING} ^.+$
# Then parameters can be only query|debug|lang
# block any extra parameter
RewriteCond %{QUERY_STRING} !(^|$)(query|debug|lang)=[^&]+(&|$) [NC]
RewriteRule ^(.*)$ - [F,L]
But problem here is
http://example.com/search?query=test&debug=on&lang=en&foo=bar
its passing even if hacker pass foo=bar
, I want to show 404, strict parameter checking before reaching php application.
Here is : Rewrite Tester
It's not showing 404
Example of Valid url with query parameter
http://example.com/search?query=test&debug=on&lang=en
Example of INVALID url with query parameter
(Check Is there any query parameter other than allowed one ??? )
http://example.com/search?query=test&debug=on&lang=en&foo=bar
http://example.com/search?a=1
http://example.com/search?a=2
http://example.com/search?query=test&a=1
Same I can do in php, But I want to block request before reaching my php application.
$allowed = array('query', 'lang', 'debug');
foreach($_GET as $key => $value)
{
if(!in_array($key, $allowed))
{
http_response_code(403)
die('Forbidden');
}
}
Also on my website, request uri allowed chars are [A-Za-z0-9_-]
How can I block if request uri containing anything extra
Also want to know,
- is it possible in rewrite to check POST variables too ?
- I see many suspicious agents string how can I block them
- Also I see in referral url hackers trying to inject xss and sqlinjection string how can I block them.