I am pretty new to irules so any help here would be appeciated. The irule below is the deafult irule for Access control based on IP from the codeshare area. Is it possible to alter this so that it controls access for a specific URL under the virtual server rather than the whole virtual server? ie. http://www.joeblogs.com/restricted/
when RULE_INIT {
v1.0 - basic ACL.
October, 2007
Tested on BigIP version 9.4.
Purpose:
Bind this rule to a network virtual server to simply allow or disallow traffic based on source IP.
This rule expects a datagroup named trustedAddresses that lists the addresses you wish to allow.
By default, traffic will be dropped.
}
when CLIENT_ACCEPTED {
if { [matchclass [IP::client_addr] equals $::trustedAddresses] }{
Uncomment the line below to turn on logging.
log local0. “Valid client IP: [IP::client_addr] - forwarding traffic”
forward
} else {
Uncomment the line below to turn on logging.
log local0. “Invalid client IP: [IP::client_addr] - discarding”
discard
}
}
You could create another class that contains the URLs that you want to control access to, and then have the IF check the HTTP::uri against that class (similar to the check of the client addr against the trusted addresses) first. If that matches then check the client addr against the trusted list.
Forgive my ignorance here but I am an irule virgin - would it look something like this?
I assume I would need to create datagroups for trustedaddresses and securedvirtualdirectories? What would these look like? I am guessing the trustedaddresses dsatagroup would be an address type group and the securedvirtualdirectories would need to be some sort of string?
}
when CLIENT_ACCEPTED {
if { [matchclass [IP::client_addr] equals $::trustedAddresses] }
{ [matchclass [HTTP::uri] equals $::securedvirtualdirectories]}{
Uncomment the line below to turn on logging.
log local0. “Valid client IP: [IP::client_addr] - forwarding traffic”
forward
} else {
Uncomment the line below to turn on logging.
log local0. “Invalid client IP: [IP::client_addr] - discarding”
discard
}
}
Thanks
I have copied the text into an irule and created an address datagroup called trustedAddresses (and added the allowed IP)
Do I then need to create another datagroup for securedpaths? If so how do I do this with the code you supplied?
Ok - i tried the rule above and created an address datagroup and string datagroup with the securepath info. Seems to scre up my site… are we missing the foward bit of the rule?
I don’t believe the ‘forward’ command is necessary. If you don’t reject it then the F5 should process it normally.
It looks like you have the rule backwards. I think you’re discarding when you want to allow, and allowing when you want to discard.
The IF statement will trigger if someone who is NOT authorized tries to access the secure path. In this case you want to reject them. The ELSE case will hit whenever the user is authorized OR they are trying to access a non-secure part of the site.
The iRule I suggested didn’t have the ELSE part there, so the rule you have is doing the opposite of the one I wrote. Try switching back to the one I had and see if it works.
When I tried the irule that you have written above it seems to break my site (no page displayed) at the default level which is a bit odd.
Can you try using this iRule and do a couple tests, then post the log messages you get? I added some logging and put the ‘forward’ command in, just in case I was wrong and it is needed.
when HTTP_REQUEST {
if { ( [matchclass [HTTP::uri] starts_with $::securePaths] ) and
! ( [matchclass [IP::client_addr] equals $::trustedAddresses] ) }
{
log local0. "Untrusted IP ([IP::client_addr]) attempting to access secure path ([HTTP::uri])"
discard
} else {
log local0. "Allowing connection from [IP::client_addr] to [HTTP::uri]"
forward
}
}
Oh, and a quick suggestion. When posting your iRule code, use a code block to help keep the formatting.
[ code ]
CODE GOES HERE
[ /code ]
(remove the spaces from the tags. I had to put them there to get them to actually appear in the message instead of creating an actual code block)
The following message is in the log:-
TCL error: irule_restrictedbyIP HTTP_REQUEST - cant read ::securePaths: no such variable while executing matchclass [HTTP::uri] starts_with $::securePaths
Sounds like the datagroup/class for the secure paths isn’t defined. Does that error happen when using your own iRule code?
Quite possibly.
Under the data group editor:-
Name - securepaths
Under String I have added:-
class securePaths { “/testing/” }
Is that the datagroup editor in the BIG-IP web interface, or the iRule editor?
In either case, you should have only added /testing/, not what you listed. The editor takes care of the class definition and the quotes and everything.
Also, you might want to name it ‘securePaths’. I’m not sure if the case will matter, but it’s better safe than sorry.
I attached a screenshot of what you should see in the web interface for the datagroup.
Small tweak to the iRule to handle case properly:
when HTTP_REQUEST {
if { ( [matchclass [string tolower [HTTP::uri]] starts_with $::securePaths] ) and
! ( [matchclass [IP::client_addr] equals $::trustedAddresses] ) }
{
log local0. "Untrusted IP ([IP::client_addr]) attempting to access secure path ([HTTP::uri])"
discard
} else {
log local0. "Allowing connection from [IP::client_addr] to [HTTP::uri]"
forward
}
}
I have it converting the path to lowercase. This handles people trying to go to:
http://yourdomain.com/TeStInG
or other variants to try and get around your filtering. As long as you always use lowercase when defining entries in the datagroup you’ll be good.
Thanks
That seems to have sort of fixed it - I can’t do any more testing at present as I have gone past my SLA on the environment. The site still failed but at least I now have :-
Rule irule_restrictedbyIP HTTP_REQUEST: Allowing connection from 192.168.0.109 to /usertransport/userws.asmx
Thanks very much for all of your help on this.
Glad to help.
With that logging in place it should show you any time it processes a connection and what it does with it. Hopefully that will tell you what’s going on when it doesn’t work.
I think that the logic may be a little screwed up in this irule. What I want to happen is for all traffic to be allowed to the site ‘unless’ they are going to one of the paths specified in the securePaths data group. When they they try to go to one of these paths they should only be allowed through if they are in the trustedAddresses data group. The rule we have seems to allow all traffic through to the site if they are not in the trustedAdresses list but if they are then they are not allowed through to any part of the site.
Here’s pseudocode for the iRule:
if ( (path is a secure path) AND (ip address is not trusted) ) {
discard
} else {
forward
}
If the path is not one of the secure paths then the first check will be false, causing the else clause to be executed.
If the IP address is a trusted address then the second part of the IF check will be false, causing the else clause to be executed.
So, anyone who is trusted should always be allowed in. If they aren’t trusted but don’t try to go to a secure path they should be allowed in.
Here’s another way to write the rule that might be a little clearer, though the logic is exactly the same as the previous one.
when HTTP_REQUEST {
if { [matchclass [string tolower [HTTP::uri]] starts_with $::securePaths] } {
if { [matchclass [IP::client_addr] equals $::trustedAddresses] } {
Secure path and trusted IP
log local0. "Allowing connection from [IP::client_addr] to secure path [HTTP::uri]"
forward
} else {
Secure path but not a trusted IP
log local0. "Untrusted IP ([IP::client_addr]) attempting to access secure path ([HTTP::uri])"
discard
}
} else {
Not a secure path
log local0. "Allowing connection from [IP::client_addr] to [HTTP::uri]"
forward
}
}
I tried the rule above and it definately has some adverse effects. With the rule applied users cannot get to the main site regardless of thier IP or path.