An offline Java RASP agent that mitigates Java4Shell by rejecting malformed Base64 padding at the java.util.Base64.Decoder.decode0 entry point before intrinsic decoding runs. Matching inputs throw IllegalArgumentException with the message base64-java4shell.
Important
This agent is a targeted mitigation for specific malformed Base64 inputs, not a substitute for a vendor-patched JDK. Always test in a staging environment before deploying to production.
Comparison of running the same exploit (./exploit.sh -p 18080) against a vulnerable service before and after attaching the RASP agent:
The malformed Base64 payload triggers remote code execution (uid=1000(ubuntu)...) and terminates the target JVM process:
After hot-attaching the agent to the running JVM with jattach (state=ready), the exploit is intercepted at the decoder entry point (RCE did not fire), the service stays online, and the blocked attempt is recorded in stderr (action=reject):
The agent's check directly mirrors the upstream OpenJDK fix semantics at the Java layer, blocking the vulnerable boundary input before it reaches the native intrinsic while preserving full decodeBlock SIMD acceleration for valid inputs:
- Upstream OpenJDK fix (
5177a70b385c16f26ede191fdac7aaba0bed0f8b): Addscmpl(length, 4)andjcc(Assembler::less, L_bruteForce)at theL_finalBitentry point of the Base64 intrinsic stub — when fewer than 4 bytes remain, execution skipsL_finalBitand immediately falls back to the Java layer sodecode0throws an exception. - CloudRASP equivalent check: Hooks the entry of
java.util.Base64.Decoder.decode0(prior to invoking thedecodeBlockintrinsic) and rejects exactly the affected tail shape — the input is at least 65 bytes, the remainder after 64-byte blocks ((end - start) & 63) is 1–3 bytes, and the last byte is'='. MIME decoders are passed through because they never enter the affected intrinsic tail. Matching inputs throwIllegalArgumentException("base64-java4shell")before the intrinsic runs.
Download the standalone agent JAR from GitHub Releases or build it from source. The public repository contains the protection source code and build instructions only. The examples below assume the standalone JAR is installed at /opt/rasp/java4shell-rasp.jar.
Run jattach under the target service's OS account. Set PID to the target JVM process ID and pass the absolute path to the standalone agent JAR as seen by that JVM (use the path inside the container when containerized):
jattach "$PID" load instrument false /opt/rasp/java4shell-rasp.jarVerify that [java4shell-rasp] state=ready mode=predecode hooked_methods=1 appears in the target JVM's stderr. Installation performs a silent self-test with a known malformed input and reports ready only when the guard intercepts it; the probe is not logged as an attack. Protection takes effect immediately for subsequent decode calls and does not persist across JVM restarts. Dynamic attach must be enabled and the JVM must have a writable temporary directory; if attach is disabled, use one of the restart options below.
Check the target Java binary first with java -XX:+PrintFlagsFinal -version. Only if UseBASE64Intrinsics is present, add the flag before -jar in the startup command (the tested JDK 8 build does not have this flag — do not pass it there):
java -XX:-UseBASE64Intrinsics -jar app.jarAfter restart, confirm the flag is disabled:
jcmd "$PID" VM.flags -all | grep UseBASE64IntrinsicsThe value should be false (if jcmd/attach is disabled, add -XX:+PrintFlagsFinal at startup and verify from stdout). Disabling the intrinsic requires no agent JAR and produces no RASP exceptions or logs, though it may reduce Base64 throughput.
To persist RASP protection and rejection logging across restarts, add -javaagent before -jar:
java -javaagent:/opt/rasp/java4shell-rasp.jar -jar app.jarIf hook installation fails during startup, the agent logs state=refused and halts the JVM with exit code 78. A live-attach failure is also logged as state=refused and rethrown, so jattach reports a nonzero response instead of a false success.
The agent writes structured events to the target JVM's stderr:
| Event | Example Output | Description |
|---|---|---|
| Installed | [java4shell-rasp] state=ready mode=predecode hooked_methods=1 |
Hook is active on Base64.Decoder.decode0; the real matched-method count is reported after a silent self-test passes. |
| Blocked | [java4shell-rasp] action=reject reason=malformed_base64 mode=url count=1 |
Java4Shell-shaped padding rejected (mode is basic or url; MIME input is passed through). Logged for the first 8 hits and powers of two (16, 32, 64, ...) to avoid log flooding. |
| Refused | [java4shell-rasp] state=refused <error> |
Hook installation or its self-test failed. Startup (-javaagent) exits with code 78; live attach rethrows the failure to jattach. |
Build with JDK 8 or later (both artifacts target Java 8 bytecode):
mvn clean packagetarget/java4shell-rasp-1.1.0-agent.jar— Standalone shaded agent JAR forjattachor-javaagentdeployment.target/java4shell-rasp.jar— Unshaded library JAR for embedding in an existing host agent viaDecoderHook.install(instrumentation).
Compatibility & Scope:
- Verified for both startup (
-javaagent) and live attach (jattach) on HotSpot 8, 11, 17, 21, and 25, with the full regression suite executed on 17, 21, and 25 (the JDK 25 install used for verification is a JRE, sojavac/javap/jcmdcame from JDK 21). - Requires JVM class retransformation support and a compatible
java.util.Base64.Decoder.decode0implementation. - Runtime compatibility does not imply that a given JDK build is vulnerable.
- Streaming Base64 decoders and third-party Base64 libraries are out of scope.

