# Copyright 2026 Tailwind Resource Group # SPDX-License-Identifier: Apache-2.0 # Provided as is, with no warranty. License: https://everythingsfine.io/license/ # # java4shell_b64_guard.tcl: find Base64 with padding that cannot be correct. # # Interim protection for CVE-2026-114938 (Java4Shell). Attach this iRule to # an HTTP virtual server that is in front of a Java application. The virtual # server must have an HTTP profile and must decrypt TLS. # # The iRule examines the URI, each request header and the start of the # request body. It looks for a run of Base64 characters that has "=" padding # after it. Correct padding has only two forms: a run with a length of # 4n+2 and "==", or a run with a length of 4n+3 and "=". All other padding # is malformed. The iRule writes a log line for a malformed run. It also # refuses the request if you set static::j4s_block to 1. # # This iRule does not replace the fixed JDK or -XX:-UseBASE64Intrinsics. when RULE_INIT { # 0 = write a log line only. 1 = write a log line and refuse the request. set static::j4s_block 0 # Ignore runs that are shorter than this number of characters. set static::j4s_min_run 60 # Examine no more than this number of bytes of each request body. set static::j4s_max_body 1048576 # The search patterns find 4 x (j4s_min_run / 4) or more Base64 # characters with padding after them that is not correct for that # length. j4s_any finds only a full run, and is fast when nothing # matches. j4s_re finds the position fast, but it can also match the # end part of a correct run. Keep j4s_min_run at 1020 or less. set j4s_map [list @K@ [expr { $static::j4s_min_run / 4 }] @C@ {[A-Za-z0-9+/_-]}] set static::j4s_any [string map $j4s_map {(?:\A|[^A-Za-z0-9+/_-])(?:@C@{4}){@K@,}(?:=+|@C@=+|@C@{2}(?:=(?!=)|={3,})|@C@{3}={2,})}] set static::j4s_re [string map $j4s_map {(?:@C@{4}){@K@,}(?:=+|@C@=+|@C@{2}(?:=(?:[^=]|$)|={3,})|@C@{3}={2,})}] } # j4s_scan: return "" if the text has no malformed padded run. If it has # one, return a description of the first. The text is examined in parts # of about 16384 characters. A part never ends in a run or in its padding. # Each -start index is less than the text length: some Tcl versions read # past the end of the text if it is not. proc j4s_scan { text } { if { [string first "=" $text] < 0 } { return "" } set len [string length $text] set from 0 while { $from < $len } { set to $len set next [expr { $from + 16384 }] if { $next < $len && [regexp -indices -start $next {[^A-Za-z0-9+/_-][^=]} $text cut] } { set to [expr { [lindex $cut 0] + 1 }] } set part [string range $text $from [expr { $to - 1 }]] if { [regexp $static::j4s_any $part] } { set detail [call j4s_find $part] if { $detail ne "" } { return $detail } } set from $to } return "" } # j4s_find: the same as j4s_scan, for one part. A match of j4s_re is a # malformed run only if it starts where the run starts. proc j4s_find { text } { set len [string length $text] set start 0 while { $start < $len && [regexp -indices -start $start $static::j4s_re $text match] } { set first [lindex $match 0] set eq [string first "=" $text $first] regexp -indices -start $eq {\A=+} $text pads set start [expr { [lindex $pads 1] + 1 }] set run [expr { $eq - $first }] if { $run < $static::j4s_min_run } { continue } if { $first > 0 && [regexp {[A-Za-z0-9+/_-]} [string index $text [expr { $first - 1 }]]] } { continue } return "run=$run pad=[expr { [lindex $pads 1] - $eq + 1 }]" } return "" } # j4s_check: do j4s_scan on the text. If the text has percent-encoding, do # j4s_scan on the decoded text also. proc j4s_check { text } { set detail [call j4s_scan $text] if { $detail eq "" && [string first "%" $text] >= 0 } { set detail [call j4s_scan [URI::decode [string map {+ %2B} $text]]] } return $detail } when HTTP_REQUEST { set j4s_where "" set j4s_detail [call j4s_check [HTTP::uri]] if { $j4s_detail ne "" } { set j4s_where "uri" } else { foreach j4s_name [HTTP::header names] { foreach j4s_value [HTTP::header values $j4s_name] { set j4s_detail [call j4s_check $j4s_value] if { $j4s_detail ne "" } { set j4s_where "header $j4s_name" break } } if { $j4s_detail ne "" } { break } } } if { $j4s_detail ne "" } { if { $static::j4s_block } { log local0.warning "java4shell_guard: malformed Base64 in $j4s_where ($j4s_detail), client [IP::client_addr], virtual [virtual name], host [HTTP::host], action=block" HTTP::respond 400 content "Bad Request" "Connection" "close" return } log local0.warning "java4shell_guard: malformed Base64 in $j4s_where ($j4s_detail), client [IP::client_addr], virtual [virtual name], host [HTTP::host], action=log" } # Collect the start of the body. A body with no Content-Length header # (chunked encoding) is not examined. if { [HTTP::header exists "Content-Length"] } { set j4s_len [string trimleft [HTTP::header value "Content-Length"] "0"] if { $j4s_len ne "" && [string is digit -strict $j4s_len] } { if { [string length $j4s_len] > 9 || $j4s_len > $static::j4s_max_body } { set j4s_len $static::j4s_max_body } HTTP::collect $j4s_len } } } when HTTP_REQUEST_DATA { # The BIG-IP can collect more bytes than requested. Examine only the # first static::j4s_max_body bytes. set j4s_body [HTTP::payload $static::j4s_max_body] set j4s_detail [call j4s_scan $j4s_body] if { $j4s_detail eq "" } { set j4s_type [string tolower [HTTP::header value "Content-Type"]] if { [string match "application/x-www-form-urlencoded*" $j4s_type] } { set j4s_detail [call j4s_scan [URI::decode [string map {+ %2B} $j4s_body]]] } } unset j4s_body if { $j4s_detail ne "" } { if { $static::j4s_block } { log local0.warning "java4shell_guard: malformed Base64 in body ($j4s_detail), client [IP::client_addr], virtual [virtual name], host [HTTP::host], action=block" HTTP::respond 400 content "Bad Request" "Connection" "close" return } log local0.warning "java4shell_guard: malformed Base64 in body ($j4s_detail), client [IP::client_addr], virtual [virtual name], host [HTTP::host], action=log" } HTTP::release }