Forum Discussion

nathe's avatar
nathe
Icon for Cirrocumulus rankCirrocumulus
Oct 21, 2011

Redirect Based on Content

 

Hello all. Hope you can help. Not new to f5 but new to writing my own iRules.

 

 

Scenario: I want to redirect a user to an error page if a certain line of text is returned in the html body. If a user crafts a certain GET request the returned page may include the following error "Microsoft OLE DB Provider for SQL Server error 'xxxxx'" It may also return the SQL table that was queried. I want to stop this information being relayed to the user. I've tried to come up with an iRule below but it's not working - it's not even being triggered from what I can see (or can't see) in the logs. Am I over-complicating things or have I not understood something? I've tried to use the extensive examples on Devcentral and that's why I've come up with the below code.

 

 

Any help greatly appreciated.

 

 

Rgds

 

Nathan

 

 

 

when HTTP_RESPONSE {

 

 

Default amount of request payload to collect (in bytes)

 

set collect_length 2048

 

 

Check for a non-existent Content-Length header

 

if {[HTTP::header Content-Length] eq ""}{

 

 

Use default collect length of 2k for POSTs without a Content-Length header

 

set collect_length $collect_length

 

 

} elseif {[HTTP::header Content-Length] == 0}{

 

 

Don't try collect a payload if there isn't one

 

unset collect_length

 

 

} elseif {[HTTP::header Content-Length] > $collect_length}{

 

 

Use default collect length

 

set collect_length $collect_length

 

 

} else {

 

 

Collect the actual payload length

 

set collect_length [HTTP::header Content-Length]

 

 

}

 

 

If the POST Content-Length isn't 0, collect (a portion of) the payload

 

if {[info exists collect_length]}{

 

 

Trigger collection of the request payload

 

HTTP::collect $collect_length

 

}

 

}

 

 

when HTTP_RESPONSE_DATA {

 

look for SQL Server Provider Error msgs

 

if { [HTTP::payload] contains "Microsoft OLE DB Provider for SQL Server error"} {

 

log local0. "found OLE DB Error in response"

 

HTTP::redirect http://www.domain.com/error.asp

 

 

}

 

 

}

 

  • is response encoded or does it span multiple packet?

    [root@iris:Active] config  b virtual bar list
    virtual bar {
       snat automap
       pool foo
       destination 172.28.17.33:http
       ip protocol tcp
       rules myrule
       profiles {
          http {}
          tcp {}
       }
    }
    
    [root@iris:Active] config  b rule myrule list
    rule myrule {
       when HTTP_RESPONSE {
            set collect_length 2048
    
            if {[HTTP::header Content-Length] eq ""}{
                    set collect_length $collect_length
            } elseif {[HTTP::header Content-Length] == 0}{
                    unset collect_length
            } elseif {[HTTP::header Content-Length] > $collect_length}{
                    set collect_length $collect_length
            } else {
                    set collect_length [HTTP::header Content-Length]
            }
    
            if {[info exists collect_length]}{
                    HTTP::collect $collect_length
            }
    }
    
    when HTTP_RESPONSE_DATA {
            if { [HTTP::payload] contains "Microsoft OLE DB Provider for SQL Server error"}{
                    log local0. "found OLE DB Error in response"
                    HTTP::redirect http://www.domain.com/error.asp
            }
    }
    }
    
    [root@iris:Active] config  curl -i http://10.10.70.110/foo.html
    HTTP/1.1 200 OK
    Date: Fri, 21 Oct 2011 06:27:51 GMT
    Server: Apache/2.0.59 (rPath)
    Last-Modified: Fri, 21 Oct 2011 06:24:47 GMT
    ETag: "66a9-80-213129c0"
    Accept-Ranges: bytes
    Content-Length: 128
    Vary: Accept-Encoding
    Set-Cookie: testcookie=helloworld
    Content-Type: text/html; charset=UTF-8
    
    
    
    WELCOME!!!!
    This's 10.10.70.110.
    Microsoft OLE DB Provider for SQL Server error
    
    
    
    [root@iris:Active] config  curl -i http://172.28.17.33/foo.html
    HTTP/1.0 302 Found
    Location: http://www.domain.com/error.asp
    Server: BigIP
    Connection: Keep-Alive
    Content-Length: 0
    
    
  • nathe's avatar
    nathe
    Icon for Cirrocumulus rankCirrocumulus
    Nitass

     

     

    Thanks for prompt response. Transfer-encoding: chunked.

     

     

    Is this what you meant? When I do a curl and test the url I don't get any body back - just a ">" prompt.

     

     

    Rgds

     

    N
  • yes, can you try to force server to http 1.0?

     

     

    HTTP Payload Collection by Deb

     

    http://devcentral.f5.com/wiki/iRules.HTTPPayloadCollection.ashx
  • nathe's avatar
    nathe
    Icon for Cirrocumulus rankCirrocumulus
    Thanks Nitass

     

     

    This works:

     

     

    when HTTP_REQUEST {

     

    Don't allow data to be chunked

     

    if { [HTTP::version] eq "1.1" } {

     

    if { [HTTP::header is_keepalive] } {

     

    HTTP::header replace "Connection" "Keep-Alive"

     

    }

     

    HTTP::version "1.0"

     

    }

     

    }

     

    when HTTP_RESPONSE {

     

    Get the content length so we can request the data to be

     

    processed in the HTTP_RESPONSE_DATA event.

     

    if { [HTTP::header exists "Content-Length"] && [HTTP::header "Content-Length"] < 1048577 } {

     

    set content_length [HTTP::header "Content-Length"]

     

    } else {

     

    set content_length 1048576

     

    }

     

    log local0.info "Content Length: $content_length"

     

    if { $content_length > 0 } {

     

    HTTP::collect $content_length

     

    }

     

    }

     

    when HTTP_RESPONSE_DATA {

     

    if { [HTTP::payload] contains "Microsoft OLE DB Provider for SQL Server error"}{

     

    log local0. "found OLE DB Error in response"

     

    HTTP::redirect http://www.domain.com/error.asp

     

    }

     

    HTTP::release

     

    }

     

     

    Your guidance has been much appreciated.

     

     

    N