Forum Moderators: phranque

Message Too Old, No Replies

Poke a hole in a "Deny From" range?

         

TorontoBoy

2:59 pm on Dec 24, 2018 (gmt 0)

5+ Year Member Top Contributors Of The Month



If you deny a range using a "deny from" statement, but then want to poke a hole, is there an easy way?

For example, Hetzner is usually annoying so I "deny from 136.243.0.0/16". But a Mastodon server at 136.243.88.169 is useful, so I want to poke a hole.

Is there a comparable statement where I can say "not deny from 136.243.88.169"?

I could do a
SetEnvIf Remote_Addr ^136\.243\. bad_range
SetEnvIf Remote_Addr ^136\.243\.88\.169 !bad_range
deny from env=bad_range


But my SetEnvIf statements are before my "deny from" statements, so the specific IP gets banned. Is this logic correct?

not2easy

3:50 pm on Dec 24, 2018 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



You can add "allow from 136.243.88.169" to your "deny from" list. It can depend on how the deny from list is set up. If it starts with "order deny,allow" then you can tack on the allow from line at the end.

You can also use "allow from env=" if it is blocked with "SetEnvIf".

lucy24

5:43 pm on Dec 24, 2018 (gmt 0)

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



Unfortunately, mod_authzthingy doesn't work as a toggle: "Deny all of A, but allow A.B, except don't allow A.B.C. unless it is specifically A.B.C.D.” In the vast majority of cases, Allow/Deny has one of two patterns:

Allow from all
Deny from {bad ranges here}
OR
Deny from all
Allow from {good ranges here}

but it's important to understand that, unlike many apache mods, the individual rules aren't handled in sequential order, one by one as you meet them. Instead, the “Order” directive means to read all the Allow lines first, and then read all the Deny lines--or, of course, vice versa. If any given request meets a condition on both sides (like, by definition, "All" and then some exact number) or on neither side (usually only if you don't have an "all" line) the second element applies.

In 2.4 you have more options
:: pause to direct dirty look hostwards ::
but, yeah, narrower allowing-and-denying has to be done in mod_setenvif. I've done a little bit of this myself:

SetEnvIf Remote_Addr ^52 bad_range
SetEnvIf Remote_Addr ^52\.5[56] !bad_range
SetEnvIf Remote_Addr ^52\.162 !bad_range
but also
BrowserMatch nice-robot-name !bad_range
winding up with
Deny from env=bad_range

Edit:
my SetEnvIf statements are before my "deny from" statements
Well, they'd have to be, otherwise you wouldn't be able to use mod_authwhatsit with environmental variables. (“Deny from env=blahblah”). Strictly speaking it isn't that they are “before” or “after”, but that mod_setenvif executes before mod_auththingummy. (“Reverse alphabetical order” is a good rough-and-ready rule, with exceptions that are out of your control.)

Recently, while experimenting, I was reminded that you can set an environmental variable in a subsidiary directory--or in a single site within a multi-site userspace--and it will still result in a lockout. This can be useful if you want to set or unset additional access-control criteria for only selected parts of your site.

Each module's 403s are independent; module A can't override a lockout issued by module B.

TorontoBoy

7:38 pm on Dec 24, 2018 (gmt 0)

5+ Year Member Top Contributors Of The Month



I removed Hetzner from my "deny from" range and then added the SetEnvIf for the same range:
SetEnvIf Remote_Addr ^136\.243\. bad_range
SetEnvIf Remote_Addr ^136\.243\.88\.169 !bad_range
deny from env=bad_range

It seemed much easier for me to remember in the future.

As usual, thanks all for the advice.