tiny_cloud_ninj
Jan 24, 2018Historic F5 Account
Side-Band HTTP Payload concern
Hello experts! I am having an issue sending a HTTP Payload to another server. My goal is to intercept a File upload and send it to another Virtual Server (which has a ASM Policy applied). The main app base64 encodes the file upload and transmits the file within a URL Form encoded post. I have an iRule which intercepts the file decodes the base64 into a variable. The issue is when I attempt to append the needed Multi Part headers and footers to the capture decoded file, the resulting file becomes unreadable.
Code
when RULE_INIT {
set static::debug 1
set static::content [ifile get ]
}
when HTTP_REQUEST {
set VirusDetected 0
set output 0
if { ([HTTP::uri] contains "") && ( [HTTP::method] eq "POST") } {
set clen [HTTP::header Content-Length]
if {$static::debug} {log local0. "Request Length $clen" }
HTTP::collect $clen
}
}
when HTTP_REQUEST_DATA {
set payload [HTTP::payload]
if {[URI::query "?$payload" ] eq "SaveAttachment" } {
set IsAdaptEnabled 1
set parametervalue [URI::query "?$payload" ]
set parametervalue [URI::decode $parametervalue]
set parametervalue [URI::decode $parametervalue]
set contenttype1 [expr { [string first "data:" $parametervalue] + 5 } ]
set contenttype2 [expr { [string first ";" $parametervalue $contenttype1] - 1 } ]
set contenttypesnip [string range $parametervalue $contenttype1 $contenttype2];
set filename1 [expr { [string first "FileName" $parametervalue] + 9 } ]
set filename2 [expr { [string first "FileSize" $parametervalue $filename1] - 2 } ]
set filenamesnip [string range $parametervalue $filename1 $filename2];
set len1 [expr { [string first "base64," $parametervalue] + 7 } ]
set len2 [expr { [string first "`" $parametervalue $len1] - 1 } ]
set filesnip [string range $parametervalue $len1 $len2];
if {[catch {b64decode $filesnip} decodedfilesfile ] == 0 and $decodedfilesfile ne ""} {
base64 decoding succeeded
} else {
base64 decoding failed
}
set decodedfilesfilelength [string length $decodedfilesfile]
if {$decodedfilesfilelength <= 20971520} {
set firstboundary "----WebKitFormBoundaryPMvLP4u7CcWWqOv7 \r\n"
set lastboundary " \r\n\r\n----WebKitFormBoundaryPMvLP4u7CcWWqOv7\r\n"
set contentdisposition "Content-Disposition: form-data; name=$filenamesnip; filename=$filenamesnip; size=$decodedfilesfilelength\r\nContent-Type: $contenttypesnip\r\n\r\n"
set payload $firstboundary$contentdisposition$decodedfilesfile$lastboundary
set s1 [call /Common/HSSR::http_req -state hstate -virt ASM_Internal_Virtual_Server -uri "http:///VirusCheck-for-filename/$filenamesnip" -method POST -body $payload -type "multipart/form-data ----WebKitFormBoundaryPMvLP4u7CcWWqOv7" -debug 1 -wait 60]
if {$static::debug} {log local0. "ASM Virtual Server Responce $s1"}
if {($s1 == 403) || ($s1 == 555)} {
set VirusDetected 1
HTTP::payload replace 0 $clen null
HTTP::release
} else {
HTTP::release
}
call /Common/HSSR::http_close hstate
} else {
HTTP::respond 200 content "$static::content" noserver
}
}
}