-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add fingerprints for JDK classes (#63)
Co-authored-by: Aman Sharma <[email protected]>
- Loading branch information
1 parent
ff1c0be
commit 19191f4
Showing
12 changed files
with
227 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...commons/src/main/java/io/github/algomaster99/terminator/commons/fingerprint/JdkClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package io.github.algomaster99.terminator.commons.fingerprint; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
/** | ||
* A class that represents a JDK class. It contains the name of the class and the bytes of the class as a {@link ByteBuffer}. | ||
*/ | ||
public record JdkClass(String name, ByteBuffer bytes) {} |
87 changes: 87 additions & 0 deletions
87
...mmons/src/main/java/io/github/algomaster99/terminator/commons/fingerprint/JdkIndexer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package io.github.algomaster99.terminator.commons.fingerprint; | ||
|
||
import io.github.classgraph.ClassGraph; | ||
import io.github.classgraph.Resource; | ||
import io.github.classgraph.ScanResult; | ||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.util.ArrayList; | ||
import java.util.Enumeration; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.jar.JarEntry; | ||
import java.util.jar.JarFile; | ||
import nonapi.io.github.classgraph.classpath.SystemJarFinder; | ||
|
||
/** | ||
* The JdkIndexer class provides a utility to list all JDK classes by scanning the JDK used for the execution of the application. | ||
*/ | ||
public class JdkIndexer { | ||
|
||
/** | ||
* Returns a list of all JDK classes. The list is populated by scanning the JDK used for the execution of this application. | ||
* | ||
* @return a list of all JDK classes, never null. | ||
*/ | ||
public static List<JdkClass> listJdkClasses() { | ||
List<JdkClass> jdkClasses = new ArrayList<>(); | ||
try (ScanResult scanResult = new ClassGraph() | ||
.enableSystemJarsAndModules() | ||
.disableDirScanning() | ||
.disableJarScanning() | ||
.acceptLibOrExtJars() | ||
.acceptModules("jdk.*", "java.*") | ||
.ignoreClassVisibility() | ||
.enableMemoryMapping() | ||
.scan(); ) { | ||
scanResult.getAllClasses().forEach(classInfo -> { | ||
Resource resource = classInfo.getResource(); | ||
if (resource != null) { | ||
byte[] byteBuffer; | ||
try { | ||
byteBuffer = resource.load(); | ||
jdkClasses.add( | ||
new JdkClass(classInfo.getName().replaceAll("\\.", "/"), ByteBuffer.wrap(byteBuffer))); | ||
} catch (IOException e) { | ||
System.err.println("Error loading resource " + resource + ": " + e); | ||
} | ||
} | ||
}); | ||
} | ||
jdkClasses.addAll(indexJrt()); | ||
return jdkClasses; | ||
} | ||
|
||
/** | ||
* Creates an index of the external Jrt jar. This jar provides an API for older jvms to access the modules in the JDK. | ||
* The jvm itself does not need this jar. | ||
* @return the list of external jrt jdk classes | ||
*/ | ||
private static List<JdkClass> indexJrt() { | ||
List<JdkClass> jdkClasses = new ArrayList<>(); | ||
Set<String> jreLibOrExtJars = SystemJarFinder.getJreLibOrExtJars(); | ||
for (String path : jreLibOrExtJars) { | ||
try { | ||
jdkClasses.addAll(readJarFile(path)); | ||
} catch (Exception e) { | ||
System.err.println("Error reading jar file " + path + ": " + e); | ||
} | ||
} | ||
return jdkClasses; | ||
} | ||
|
||
private static List<JdkClass> readJarFile(String jarFilePath) throws IOException { | ||
List<JdkClass> jdkClasses = new ArrayList<>(); | ||
try (JarFile jarFile = new JarFile(jarFilePath)) { | ||
Enumeration<JarEntry> entries = jarFile.entries(); | ||
while (entries.hasMoreElements()) { | ||
JarEntry entry = entries.nextElement(); | ||
if (entry.getName().endsWith(".class")) { | ||
byte[] byteBuffer = jarFile.getInputStream(entry).readAllBytes(); | ||
jdkClasses.add(new JdkClass(entry.getName().replace(".class", ""), ByteBuffer.wrap(byteBuffer))); | ||
} | ||
} | ||
} | ||
return jdkClasses; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...s/src/main/java/io/github/algomaster99/terminator/commons/fingerprint/provenance/Jdk.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package io.github.algomaster99.terminator.commons.fingerprint.provenance; | ||
|
||
import io.github.algomaster99.terminator.commons.fingerprint.classfile.ClassFileAttributes; | ||
|
||
public record Jdk(ClassFileAttributes classFileAttributes) implements Provenance { | ||
@Override | ||
public ClassFileAttributes classFileAttributes() { | ||
return classFileAttributes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+1.49 KB
terminator-commons/src/test/resources/classfile/GeneratedConstructorAccessor15.class
Binary file not shown.
Binary file added
BIN
+825 Bytes
terminator-commons/src/test/resources/classfile/GeneratedMethodAccessor1.class
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters