Forum Moderators: phranque
http://www.example.com/myfile.php?;DECLARE%20@S%20CHAR(4000);SET%20@S=CAST(0x4445434C415245204
My question is how can I block all requests that contains the DECLARE in it.
I tried this but does not work.
RewriteCond %{REQUEST_METHOD} DECLARE
RewriteCond %{HTTP_REFERER} !.*example.com.* [OR]
RewriteCond %{HTTP_USER_AGENT} ^$
RewriteRule (.*) ^http://%{REMOTE_ADDR}/$ [R=301,L]
Please help me.
Thanks
[edited by: jdMorgan at 6:12 pm (utc) on Nov. 15, 2008]
[edit reason] example.com [/edit]
RewriteCond %{QUERY_STRING} [^a-z](declare¦char¦set¦cast¦convert¦delete¦drop¦exec¦insert¦meta¦script¦select¦truncate¦update)[^a-z] [NC]
RewriteRule (.*) - [F]
This code returns 403-Forbidden response to requests for any URLs with any of those command or parameter keywords in the appended query string.
[added] Replace all broken pipe "¦" characters with solid pipe characters before use; Posting on this forum modifies the pipe characters. [/added]
Jim
[edited by: jdMorgan at 6:52 pm (utc) on Aug. 26, 2008]
If I include the query string without the long string of letters and numbers in a request to test the block, I get a 403 error. (eg. ;DECLARE%20@S%20CHAR(4000);SET%20@S=CAST)
Not sure I should be concerned or not - it's still serving an error.
You should place the code snippet "early" in your list of mod_rewrite rules; For example, there is no use in redirecting such abusive client requests to canonicalize the domain name, or in rewriting thsee requests to another filepath.
> 406 (Not Acceptable)
This indicates that MultiViews may be enabled on your server. If you are not using content negotiation, try adding
Options -MultiViews ---
I should also add that this code is really only useful if you use scripts which directly access SQL on your server, and want to protect your site from "outside" requests arriving from the Web. If you don't have mySQL installed, then all this code does is "give you the satisfaction" of issuing a 403 response instead of serving the page that was requested with the injected squery string attached. But it's doubtful that the malicious program sending these injection attempts even bothers to check the target server response -- It's more likely a "fire and forget" kind of thing...
Jim
The code above works for page.php?;DECLARE%20@S%20CHAR(4000);SET%20@S=CAST(0x4445434C415245204
but doesn't seem to help when pageid=# is included. Is there a way to include both page types?
Thanks.
Also beware of Alias, Redirect, MultiViews, AcceptPathInfo, and other factors which can pre-empt mod_rewrite rules.
Jim
Also beware of Alias, Redirect, MultiViews, AcceptPathInfo, and other factors which can pre-empt mod_rewrite rules.
Just one question - I have several sites on one dedicated server, so I guess I could add these lines to each separate htaccess file (ie on a per site basis), but can it be placed to work server-wide without having to alter each individual htaccess file? ie at some top level htaccess file? If so, where exactly is that file?
Possible? Yes, of course:
RewriteRule .{150,} - [F]
But note that you asked about the URL, and that is not where the problem is here. The problem is in the query string, which is not part of a URL, but rather, data attached to a URL to be passed to the resource at that URL. So the above code probably won't do what you really want. You'd need something like this:
RewriteCond %{QUERY_STRING} .{150,}
RewriteRule .* - [F]
Jim
RewriteCond %{QUERY_STRING} [^a-z](declare¦char¦set¦cast¦convert¦delete¦drop¦exec¦insert¦meta¦script¦select¦truncate¦update)[^a-z] [NC]
RewriteRule (.*) - [F]
We may also just want to touch base on the fact that this write will directly interfere with some Wordpress functions (changing things like skins and removing posts).
Also, it may provide a challenge (by way of 403) to those who would provide sitewide search using any of the terms contained in the write.