Log binary HTTP payload in hex

Problem this snippet solves:

This iRule demonstrates how to collect the HTTP request payload and log the output in hex

How to use this snippet:

# Convert $binary to $hex
binary scan $binary H* hex 

# Convert $hex to $ascii
set ascii [binary format H* $hex]

Code :

when HTTP_REQUEST {

   # Log debug? 1=yes, 0=no
   set debug 1

   # Collect up to the first 1MB of POST data
   if {[HTTP::method] eq "POST"}{

      set clength 0

      # Check if there is a content-length header and the value is set to less than 1Mb
      if {[HTTP::header exists "Content-Length"] && [HTTP::header Content-Length] <= 1048576}{
         set clength [HTTP::header Content-Length]
      } else { 
         set clength 1048576
      }
      if {[info exists clength] && $clength > 0} {
         if {$debug}{log local0. "[virtual name]: Collecting $clength bytes"}
         HTTP::collect $clength
      }
   }
}

when HTTP_REQUEST_DATA {

   # Log the payload converted to hex
   binary scan [HTTP::payload] H* payload_hex

   if {$debug}{log local0. "[virtual name]: $payload_hex: $payload_hex"}
}
Published Mar 18, 2015
Version 1.0
No CommentsBe the first to comment