Forum Moderators: phranque

Message Too Old, No Replies

Block user agent with rewrite

trying to deny some user agents, not sure if this is correct

         

MickeyRoush

6:14 am on Jun 2, 2011 (gmt 0)

10+ Year Member



I saw this in one of my logs (specific dates and times changed).


81.24.211.33 - - [18/Apr/2011:05:36:26 -0400] "GET / HTTP/1.1" 403 - www.example.com "-" "xpymep.exe" "-"

It was denied via IP, but if I wanted to deny it via user agent, would this work?

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} ^xpymep\.exe [NC]
RewriteRule .* - [F]

Or could xpymep\.exe
be replaced with just xpymep or xpymep.exe


I also saw this on another log:

202.43.74.178 - - [11/Apr/2011:21:01:41 -0400] "GET /index.php HTTP/1.1" 403 - exampl.com "http://example.com/index.php" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)" "109.230.246.6"
212.33.216.225 - - [11/Apr/2011:21:03:13 -0400] "GET / HTTP/1.1" 403 - example.com "http://example.com/index.php" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)" "109.230.246.6"

It was denied, but could I deny it via user agent since the logs show many different IPs associated with it? Strange that the user agent shows as an IP. Could this be because of use of a proxy?
Anyways, would this work?

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} ^109\.230\.246\.6
RewriteRule .* - [F]

Any help would be greatly appreciated.

wilderness

1:11 pm on Jun 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Mickey,
Basic understanding and use of anchors should be a primary requirement of mod_rewrite.

1)begins with
2)ends with
3)contains
4) exactly as
5) any combination of the above

Regards your User-Agent inquiry (xpymep)
You may use the entire UA or any portion of the UA. You may also utilize anchors accordingly.

Your third example of xpymep.exe would either not work or generate a syntax error, as you failed to escape the period (see forum charter).

RewriteCond %{HTTP_USER_AGENT} ^109\.230\.246\.6


Needs to change to REMOTE_ADDR and the ends with anchor need to be added.

RewriteCond %{REMOTE_ADDR} ^109\.230\.246\.6$

Honing in on Class D's (in the above example "6") is a bad practice and the visitor will return almost immediately from another range within that same class.
One solution is to either deny the entire Class D, or the entire provider and/or backbone range.

You may also utilize multiple criteria in you denies.
EX:
# UA "contains and comes from IP
RewriteCond %{HTTP_USER_AGENT} ^xpymep\.exe [NC]
RewriteCond %{REMOTE_ADDR} ^109\.230\.246\.
RewriteRule .* - [F]

You may also include EXCEPTs in your primary data line:
# UA "contains and does NOT come from IP
RewriteCond %{HTTP_USER_AGENT} ^xpymep\.exe [NC]
RewriteCond %{REMOTE_ADDR} !^109\.230\.246\.
RewriteRule .* - [F]

MickeyRoush

7:41 pm on Jun 2, 2011 (gmt 0)

10+ Year Member



Thanks wilderness.

I do still have a question about this:
202.43.74.178 - - [11/Apr/2011:21:01:41 -0400] "GET /index.php HTTP/1.1" 403 - example.com "http://example.com/index.php" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)" "109.230.246.6"
212.33.216.225 - - [11/Apr/2011:21:03:13 -0400] "GET / HTTP/1.1" 403 - example.com "http://example.com/index.php" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)" "109.230.246.6"

I made the issue in question bold. Would this be considered a User Agent or Remote Addr? Isn't the Remote Addr at the beginning of the line?

wilderness

8:47 pm on Jun 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The data fields are separated as follows.

202.43.74.178 - -

[11/Apr/2011:21:01:41 -0400]

"GET /index.php

HTTP/1.1"

403 -

example.com

"http://example.com/index.php"

"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"

"109.230.246.6"

Your closing field (109) is something added by your server or host.
Whether it's a proxy or something else is unknown to me.
In any event it's certainly not the requesting IP (that is 202), and is what would normally focus on.

I recall seeing and an example to do "proxy checks" on "headers", however I've never used it.

Sorry can't be more help.

Here's a very OLD example from Apache on the fields in Combined full logs [httpd.apache.org]