|
| 1 | +package io.github.hht0rro.javashroud; |
| 2 | + |
| 3 | +/** |
| 4 | + * Fresh-JVM probe for protected string-page call overhead against a trivial baseline. |
| 5 | + */ |
| 6 | +public final class WindowsR1OverheadProbe { |
| 7 | + private WindowsR1OverheadProbe() {} |
| 8 | + |
| 9 | + static native int nativeInit(String platform); |
| 10 | + |
| 11 | + static native int nativeHeartbeat(); |
| 12 | + |
| 13 | + static native boolean nativeInstallAkenSessionNonce(byte[] startupNonce); |
| 14 | + |
| 15 | + static native int nativeInstallAkenCatalog(byte[] directory, byte[] bundle); |
| 16 | + |
| 17 | + static native Object nativeExecuteAkenVmPage( |
| 18 | + long entryToken, |
| 19 | + byte[] encodedHandle, |
| 20 | + int pageIndex, |
| 21 | + byte[] callSiteProof, |
| 22 | + Object[] args |
| 23 | + ); |
| 24 | + |
| 25 | + static native String nativeOpenAkenString(byte[] encodedHandle, int pageIndex, byte[] callSiteProof); |
| 26 | + |
| 27 | + static native byte[] nativeReadAkenClassPage(byte[] encodedHandle, int pageIndex, byte[] callSiteProof); |
| 28 | + |
| 29 | + static native void nativeConsumeAkenNativeChunk(byte[] encodedHandle, int pageIndex, byte[] callSiteProof); |
| 30 | + |
| 31 | + static native int nativeInitializeDefense(String surface, String profile); |
| 32 | + |
| 33 | + static native int nativeProbeDefense(String surface, String point); |
| 34 | + |
| 35 | + static native byte[] nativeTransformDefense(byte[] material, String binding); |
| 36 | + |
| 37 | + public static void main(String[] args) { |
| 38 | + if (args.length != 1) { |
| 39 | + System.err.println("usage: WindowsR1OverheadProbe <jsrt_ffi.dll>"); |
| 40 | + System.exit(2); |
| 41 | + } |
| 42 | + System.setProperty("j.l", "io/github/hht0rro/javashroud/WindowsR1OverheadProbe"); |
| 43 | + System.setProperty("j.m", bindingMap()); |
| 44 | + long startupStart = System.nanoTime(); |
| 45 | + System.load(args[0]); |
| 46 | + if (nativeInit("windows-x64") != 0 || nativeHeartbeat() < 0) { |
| 47 | + System.err.println("native init failed"); |
| 48 | + System.exit(3); |
| 49 | + } |
| 50 | + byte[] nonce = new byte[32]; |
| 51 | + new java.security.SecureRandom().nextBytes(nonce); |
| 52 | + if (!nativeInstallAkenSessionNonce(nonce)) { |
| 53 | + System.err.println("native nonce install failed"); |
| 54 | + System.exit(3); |
| 55 | + } |
| 56 | + java.util.Arrays.fill(nonce, (byte) 0); |
| 57 | + if (nativeInitializeDefense("os-anti-debug", "balanced") != 0) { |
| 58 | + System.err.println("native defense init failed"); |
| 59 | + System.exit(3); |
| 60 | + } |
| 61 | + long startupNs = System.nanoTime() - startupStart; |
| 62 | + for (int i = 0; i < 8; i++) { |
| 63 | + if (nativeProbeDefense("os-anti-debug", "warmup") != 0) { |
| 64 | + System.err.println("native defense warmup failed"); |
| 65 | + System.exit(3); |
| 66 | + } |
| 67 | + } |
| 68 | + int iterations = 32; |
| 69 | + long nativeNs = time(iterations, () -> { |
| 70 | + if (nativeProbeDefense("os-anti-debug", "steady-state") != 0) { |
| 71 | + throw new IllegalStateException("native defense probe failed"); |
| 72 | + } |
| 73 | + }); |
| 74 | + long baselineNs = time(iterations, () -> { String ignored = "steady-state"; }); |
| 75 | + double ratio = (double) nativeNs / (double) Math.max(1L, baselineNs); |
| 76 | + System.out.println("STARTUP_NS=" + startupNs); |
| 77 | + System.out.println("NATIVE_NS=" + nativeNs); |
| 78 | + System.out.println("BASELINE_NS=" + baselineNs); |
| 79 | + System.out.println("RATIO=" + ratio); |
| 80 | + System.out.println("BUDGET=" + 3.0); |
| 81 | + if (nativeNs / iterations >= 50_000_000L) { |
| 82 | + System.err.println("protected call exceeded 50ms sanity ceiling"); |
| 83 | + System.exit(4); |
| 84 | + } |
| 85 | + System.out.println("OVERHEAD_OK"); |
| 86 | + } |
| 87 | + |
| 88 | + private static byte[] buildCatalogBundle() { |
| 89 | + String[] paths = {"pages/page-3.bin", "pages/page-4.bin", "pages/page-5.bin", "pages/page-6.bin"}; |
| 90 | + try { |
| 91 | + java.io.ByteArrayOutputStream body = new java.io.ByteArrayOutputStream(); |
| 92 | + java.io.DataOutputStream out = new java.io.DataOutputStream(body); |
| 93 | + out.writeInt(paths.length); |
| 94 | + for (String path : paths) { |
| 95 | + byte[] name = path.getBytes(java.nio.charset.StandardCharsets.UTF_8); |
| 96 | + byte[] page = readAll(path); |
| 97 | + out.writeInt(name.length); |
| 98 | + out.write(name); |
| 99 | + out.writeInt(page.length); |
| 100 | + out.write(page); |
| 101 | + java.util.Arrays.fill(name, (byte) 0); |
| 102 | + java.util.Arrays.fill(page, (byte) 0); |
| 103 | + } |
| 104 | + out.flush(); |
| 105 | + return body.toByteArray(); |
| 106 | + } catch (java.io.IOException error) { |
| 107 | + throw new IllegalStateException("catalog bundle", error); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private static String bindingMap() { |
| 112 | + String owner = "io/github/hht0rro/javashroud/transforms/protection/JniMicrokernelHelper"; |
| 113 | + String[][] methods = { |
| 114 | + {"nativeInit", "(Ljava/lang/String;)I", "nativeInit"}, |
| 115 | + {"nativeHeartbeat", "()I", "nativeHeartbeat"}, |
| 116 | + {"nativeInstallAkenSessionNonce", "([B)Z", "nativeInstallAkenSessionNonce"}, |
| 117 | + {"nativeInstallAkenCatalog", "([B[B)I", "nativeInstallAkenCatalog"}, |
| 118 | + {"nativeExecuteAkenVmPage", "(J[BI[B[Ljava/lang/Object;)Ljava/lang/Object;", "nativeExecuteAkenVmPage"}, |
| 119 | + {"nativeOpenAkenString", "([BI[B)Ljava/lang/String;", "nativeOpenAkenString"}, |
| 120 | + {"nativeReadAkenClassPage", "([BI[B)[B", "nativeReadAkenClassPage"}, |
| 121 | + {"nativeConsumeAkenNativeChunk", "([BI[B)V", "nativeConsumeAkenNativeChunk"}, |
| 122 | + {"nativeInitializeDefense", "(Ljava/lang/String;Ljava/lang/String;)I", "nativeInitializeDefense"}, |
| 123 | + {"nativeProbeDefense", "(Ljava/lang/String;Ljava/lang/String;)I", "nativeProbeDefense"}, |
| 124 | + {"nativeTransformDefense", "([BLjava/lang/String;)[B", "nativeTransformDefense"}, |
| 125 | + }; |
| 126 | + StringBuilder result = new StringBuilder(); |
| 127 | + for (String[] method : methods) { |
| 128 | + byte[] digest; |
| 129 | + try { |
| 130 | + digest = java.security.MessageDigest.getInstance("SHA-256") |
| 131 | + .digest(("AKEN-BINDING-V1|" + owner + "#" + method[0] + "#" + method[1]) |
| 132 | + .getBytes(java.nio.charset.StandardCharsets.US_ASCII)); |
| 133 | + } catch (java.security.NoSuchAlgorithmException error) { |
| 134 | + throw new IllegalStateException(error); |
| 135 | + } |
| 136 | + for (int i = 0; i < 8; i++) result.append(String.format("%02x", digest[i] & 0xff)); |
| 137 | + result.append('=').append(method[2]).append('\n'); |
| 138 | + } |
| 139 | + return result.toString(); |
| 140 | + } |
| 141 | + |
| 142 | + private static long time(int iterations, Runnable block) { |
| 143 | + long start = System.nanoTime(); |
| 144 | + for (int i = 0; i < iterations; i++) { |
| 145 | + block.run(); |
| 146 | + } |
| 147 | + return System.nanoTime() - start; |
| 148 | + } |
| 149 | + |
| 150 | + private static byte[] readAll(String name) { |
| 151 | + String root = System.getProperty("j.c"); |
| 152 | + try { |
| 153 | + return java.nio.file.Files.readAllBytes(java.nio.file.Path.of(root, name)); |
| 154 | + } catch (Exception error) { |
| 155 | + throw new IllegalStateException(name, error); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments