DasBrain

DefineClass.java

Mar 16th, 2021
552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.39 KB | None | 0 0
  1. package jni;
  2. import jdk.incubator.foreign.CLinker;
  3. import jdk.incubator.foreign.FunctionDescriptor;
  4. import jdk.incubator.foreign.LibraryLookup;
  5. import jdk.incubator.foreign.MemoryAccess;
  6. import jdk.incubator.foreign.MemoryAddress;
  7. import jdk.incubator.foreign.MemoryLayout;
  8. import jdk.incubator.foreign.MemorySegment;
  9. import jdk.incubator.foreign.NativeScope;
  10.  
  11. import static java.lang.invoke.MethodType.methodType;
  12. import static jdk.incubator.foreign.CLinker.*;
  13. import static jdk.incubator.foreign.MemoryAddress.NULL;
  14.  
  15. import java.nio.charset.StandardCharsets;
  16. import java.util.Base64;
  17.  
  18. public class ClassDefiner {
  19.     public static void main(String[] args) throws Throwable {
  20.         MemoryAddress vm;
  21.         try (var ns = NativeScope.unboundedScope()) {
  22.             var JNI_GetCreatedJavaVMs = CLinker.getInstance().downcallHandle(
  23.                     LibraryLookup.ofDefault().lookup("JNI_GetCreatedJavaVMs").orElseThrow(),
  24.                     methodType(int.class, MemoryAddress.class, int.class, MemoryAddress.class),
  25.                     FunctionDescriptor.of(C_INT, C_POINTER, C_INT, C_POINTER));
  26.             var vmPtr = ns.allocate(C_POINTER);
  27.             int result = (int) JNI_GetCreatedJavaVMs.invokeExact(vmPtr.address(), 1, NULL);
  28.             if (result != 0) throw new Exception("JNI Error: " + result);
  29.             vm = MemoryAccess.getAddress(vmPtr);
  30.         }
  31.         MemoryAddress env;
  32.         try (var ns = NativeScope.unboundedScope()) {
  33.             var vmMethodCount = 8;
  34.             var vmTable = MemoryAccess.getAddress(vm.asSegmentRestricted(C_POINTER.byteSize()));
  35.             var vmTableSeg = vmTable.asSegmentRestricted(MemoryLayout.ofSequence(vmMethodCount, C_POINTER).byteSize());
  36.             var GetEnvIDX = 6;
  37.             var GetEnv = CLinker.getInstance().downcallHandle(MemoryAccess.getAddressAtIndex(vmTableSeg, 6),
  38.                     methodType(int.class, MemoryAddress.class, MemoryAddress.class, int.class),
  39.                     FunctionDescriptor.of(C_INT, C_POINTER, C_POINTER, C_INT));
  40.             var envPtr = ns.allocate(C_POINTER);
  41.             int JNI_VERSION_10 = 0x000a0000; // 10
  42.             int result = (int) GetEnv.invokeExact(vm, envPtr.address(), JNI_VERSION_10);
  43.             if (result != 0) throw new Exception("JNI Error: " + result);
  44.             env = MemoryAccess.getAddress(envPtr);
  45.         }
  46.         try (var ns = NativeScope.unboundedScope()) {
  47.             var envMethodCount = 234;
  48.             var envTable = MemoryAccess.getAddress(env.asSegmentRestricted(C_POINTER.byteSize()));
  49.             var envTableSeg = envTable.asSegmentRestricted(MemoryLayout.ofSequence(envMethodCount, C_POINTER).byteSize());
  50.             var DefineClassIDX = 5;
  51.             var DefineClass = CLinker.getInstance().downcallHandle(MemoryAccess.getAddressAtIndex(envTableSeg, DefineClassIDX),
  52.                     methodType(MemoryAddress.class, MemoryAddress.class, MemoryAddress.class, MemoryAddress.class, MemoryAddress.class, int.class),
  53.                     FunctionDescriptor.of(C_POINTER, C_POINTER, C_POINTER, C_POINTER, C_POINTER, C_INT));
  54.             var name = CLinker.toCString("java/lang/Example", StandardCharsets.UTF_8, ns);
  55.             var bytes = classFile();
  56.             var bytesPtr = ns.allocate(bytes.length);
  57.             bytesPtr.copyFrom(MemorySegment.ofArray(bytes));
  58.             MemoryAddress result = (MemoryAddress) DefineClass.invokeExact(env, name.address(), NULL, bytesPtr.address(), bytes.length);
  59.             if (result.equals(NULL)) throw new Exception("JNI Error: " + result);
  60.         }
  61.         System.out.println(Class.forName("java.lang.Example"));
  62.     }
  63.    
  64.     private static byte[] classFile() {
  65.         return Base64.getDecoder().decode("yv66vgAAADsABQEAEWphdmEvbGFuZy9FeGFtcGxlBwABAQAQamF2YS9sYW5nL09iamVjdAcAAwAhAAIABAAAAAAAAAAA");
  66.     }
  67. }
  68.  
Add Comment
Please, Sign In to add comment