Forum Discussion

Edgar_Palamarch's avatar
Edgar_Palamarch
Icon for Nimbostratus rankNimbostratus
Jul 05, 2012

Referer Based Redirect

Hello,

 

 

A former client is leaching images from our website. So, I need to redirect/block requests coming from them based on the referer header. Found a couple sample codes, but couldn't make them to work. Please help.

 

 

Here is what I use:

 

 

when HTTP_REQUEST {

 

set referrer_host [URI::host [HTTP::header value Referer]]

 

if { $referrer_host eq "http://BadReferer.com" } {

 

HTTP::redirect http://BadReferer.com/image.jpg

 

}

 

else { pool www.MyDomain.com

 

}

 

}

 

 

Appreciate your time and help!

3 Replies

  • can you try to change from "http://BadReferer.com" to "BadReferer.com"?

    e.g.

    [root@ve10:Active] config  b virtual bar list
    virtual bar {
       snat automap
       pool foo
       destination 172.28.19.79:80
       ip protocol 6
       rules myrule
       profiles {
          http {}
          tcp {}
       }
    }
    [root@ve10:Active] config  b rule myrule list
    rule myrule {
       when HTTP_REQUEST {
       set referrer_host [URI::host [HTTP::header value Referer]]
       log local0. "\[HTTP::uri\]: [HTTP::uri]"
       log local0. "\[HTTP::header value Referer\]: [HTTP::header value Referer]"
       log local0. "\[URI::host [HTTP::header value Referer]\]: [URI::host [HTTP::header value Referer]]"
    
       if { $referrer_host eq "BadReferer.com" } {
          HTTP::redirect http://BadReferer.com/image.jpg
       }
    }
    }
    
    [root@ve10:Active] config  curl -I http://172.28.19.79 -H "Referer: http://BadReferer.com/something"
    HTTP/1.0 302 Found
    Location: http://BadReferer.com/image.jpg
    Server: BigIP
    Connection: Keep-Alive
    Content-Length: 0
    
    [root@ve10:Active] config  tail /var/log/ltm
    Jul  6 07:28:46 local/tmm info tmm[5111]: Rule myrule : [HTTP::uri]: /
    Jul  6 07:28:46 local/tmm info tmm[5111]: Rule myrule : [HTTP::header value Referer]: http://BadReferer.com/something
    Jul  6 07:28:46 local/tmm info tmm[5111]: Rule myrule : [URI::host http://BadReferer.com/something]: BadReferer.com
    
  • Thanks a lot, nitass! Works as expected.

     

     

    Is there a way to have a few bad referres in the same iRule?
  • yes, you can use data group and class command.

    class wiki

    https://devcentral.f5.com/wiki/irules.class.ashx

    e.g.

    [root@ve10:Active] config  b class badreferer_class list
    class badreferer_class {
       {
          "BadReferer.com"
          "badman.com"
       }
    }
    [root@ve10:Active] config  b rule myrule list
    rule myrule {
       when HTTP_REQUEST {
       set referrer_host [URI::host [HTTP::header value Referer]]
       log local0. "\[HTTP::uri\]: [HTTP::uri]"
       log local0. "\[HTTP::header value Referer\]: [HTTP::header value Referer]"
       log local0. "\[URI::host [HTTP::header value Referer]\]: [URI::host [HTTP::header value Referer]]"
    
       if { [class match -- $referrer_host equals badreferer_class] } {
          HTTP::redirect http://BadReferer.com/image.jpg
       }
    }
    }
    
    [root@ve10:Active] config  curl -I http://172.28.19.79 -H "Referer: http://BadReferer.com/something"
    HTTP/1.0 302 Found
    Location: http://BadReferer.com/image.jpg
    Server: BigIP
    Connection: Keep-Alive
    Content-Length: 0
    
    [root@ve10:Active] config  curl -I http://172.28.19.79 -H "Referer: http://badman.com/something"
    HTTP/1.0 302 Found
    Location: http://BadReferer.com/image.jpg
    Server: BigIP
    Connection: Keep-Alive
    Content-Length: 0
    
    [root@ve10:Active] config  tail /var/log/ltm
    Jul  6 09:05:15 local/tmm info tmm[5111]: Rule myrule : [HTTP::uri]: /
    Jul  6 09:05:15 local/tmm info tmm[5111]: Rule myrule : [HTTP::header value Referer]: http://BadReferer.com/something
    Jul  6 09:05:15 local/tmm info tmm[5111]: Rule myrule : [URI::host http://BadReferer.com/something]: BadReferer.com
    Jul  6 09:05:20 local/tmm info tmm[5111]: Rule myrule : [HTTP::uri]: /
    Jul  6 09:05:20 local/tmm info tmm[5111]: Rule myrule : [HTTP::header value Referer]: http://badman.com/something
    Jul  6 09:05:20 local/tmm info tmm[5111]: Rule myrule : [URI::host http://badman.com/something]: badman.com