-
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 feature to detect runtime generated classes (#64)
- Loading branch information
1 parent
19b7597
commit a74461d
Showing
10 changed files
with
117 additions
and
21 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
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
23 changes: 23 additions & 0 deletions
23
...in/java/io/github/algomaster99/terminator/commons/fingerprint/classfile/RuntimeClass.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,23 @@ | ||
package io.github.algomaster99.terminator.commons.fingerprint.classfile; | ||
|
||
import java.util.Objects; | ||
import org.objectweb.asm.ClassReader; | ||
import org.objectweb.asm.Opcodes; | ||
|
||
public class RuntimeClass { | ||
|
||
private RuntimeClass() {} | ||
|
||
/** | ||
* Proxy classes are not synthetic classes, but they are runtime generated. | ||
*/ | ||
public static boolean isProxyClass(byte[] classfileBytes) { | ||
ClassReader reader = new ClassReader(classfileBytes); | ||
return Objects.equals(reader.getSuperName(), "java/lang/reflect/Proxy"); | ||
} | ||
|
||
public static boolean isSynthetic(byte[] classfileBytes) { | ||
ClassReader reader = new ClassReader(classfileBytes); | ||
return (reader.getAccess() & Opcodes.ACC_SYNTHETIC) != 0; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...t/java/io/github/algomaster99/terminator/commons/fingerprint/classfile/ClassfileTest.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,62 @@ | ||
package io.github.algomaster99.terminator.commons.fingerprint.classfile; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.objectweb.asm.ClassReader; | ||
import org.objectweb.asm.ClassWriter; | ||
import org.objectweb.asm.Opcodes; | ||
import org.objectweb.asm.tree.ClassNode; | ||
import org.objectweb.asm.util.TraceClassVisitor; | ||
|
||
public class ClassfileTest { | ||
|
||
private static final Path TEST_RESOURCES = Path.of("src/test/resources"); | ||
|
||
@Nested | ||
class IsRuntimeGeneratedClass { | ||
private static final Path CLASSFILE = TEST_RESOURCES.resolve("classfile"); | ||
|
||
@Test | ||
void isAProxyClass_true() throws IOException { | ||
Path proxy39 = CLASSFILE.resolve("$Proxy39.class"); | ||
Path proxy40 = CLASSFILE.resolve("$Proxy40.class"); | ||
|
||
assertThat(RuntimeClass.isProxyClass(Files.readAllBytes(proxy39))).isTrue(); | ||
assertThat(RuntimeClass.isProxyClass(Files.readAllBytes(proxy40))).isTrue(); | ||
} | ||
|
||
@Test | ||
void isProxyClass_false() throws IOException { | ||
Path maven = CLASSFILE.resolve("Maven.class"); | ||
|
||
assertThat(RuntimeClass.isProxyClass(Files.readAllBytes(maven))).isFalse(); | ||
} | ||
|
||
@Test | ||
void isSyntheticClass_true() throws IOException { | ||
Path synthetic = CLASSFILE.resolve("$Proxy39.class"); | ||
|
||
byte[] unmodifiedBytes = Files.readAllBytes(synthetic); | ||
assertThat(RuntimeClass.isSynthetic(unmodifiedBytes)).isFalse(); | ||
|
||
byte[] modifiedBytes = makeClassfileSynthetic(Files.readAllBytes(synthetic)); | ||
assertThat(RuntimeClass.isSynthetic(modifiedBytes)).isTrue(); | ||
} | ||
|
||
private static byte[] makeClassfileSynthetic(byte[] classfileBytes) { | ||
ClassNode classNode = new ClassNode(); | ||
ClassReader classReader = new ClassReader(classfileBytes); | ||
classReader.accept(classNode, 0); | ||
classNode.access |= Opcodes.ACC_SYNTHETIC; | ||
|
||
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES); | ||
classNode.accept(new TraceClassVisitor(writer, null)); | ||
return writer.toByteArray(); | ||
} | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
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