Jan Ouwens
JAX London - October 5, 2021
Jan Ouwens
EqualsVerifier.forClass(Foo.class)
.verify();
Demo
Java 11!
In fact… Java 17!
Also annoy your coworkers with
Demo
Demo
Demo
Rating: ☠️💣
Demo
Demo
However…
Demo
Exception in thread "main" java.lang.reflect.InaccessibleObjectException: Unable to make field private final int java.lang.Integer.value accessible: module java.base does not "opens java.lang" to unnamed module @2a0a80c0
--add-opens java.base/java.lang=ALL-UNNAMED --
Demo
Was this hack evil? ✋
Are Calendars and arrays evil? ✋
Are JPA entities evil? 👹
Rating: ☠️💣💥
Demo
Demo
close()
on URLClassLoader
CompilationTask
¯\_(ツ)_/¯
@Test
public void equalsverifierSucceeds_whenOneOfTheFieldsIsSynthetic() {
if (!isJava8Available()) {
return;
}
Class<?> java8ClassWithSyntheticField = compile(JAVA_8_CLASS_WITH_SYNTHETIC_FIELD_NAME, JAVA_8_CLASS_WITH_SYNTHETIC_FIELD);
EqualsVerifier.forClass(java8ClassWithSyntheticField)
.verify();
}
private static final String JAVA_8_CLASS_WITH_SYNTHETIC_FIELD_NAME = "Java8ClassWithSyntheticField";
private static final String JAVA_8_CLASS_WITH_SYNTHETIC_FIELD =
"\nimport java.util.Comparator;" +
"\nimport java.util.Objects;" +
"\n" +
"\npublic final class Java8ClassWithSyntheticField {" +
"\n private static final Comparator<Java8ClassWithSyntheticField> COMPARATOR =" +
"\n (c1, c2) -> 0; // A lambda is a synthetic class" +
"\n" +
"\n private final String s;" +
"\n " +
"\n public Java8ClassWithSyntheticField(String s) {" +
"\n this.s = s;" +
"\n }" +
"\n " +
"\n @Override" +
"\n public boolean equals(Object obj) {" +
"\n if (!(obj instanceof Java8ClassWithSyntheticField)) {" +
"\n return false;" +
"\n }" +
"\n return Objects.equals(s, ((Java8ClassWithSyntheticField)obj).s);" +
"\n }" +
"\n " +
"\n @Override" +
"\n public int hashCode() {" +
"\n return Objects.hash(s);" +
"\n }" +
"\n}";
Rating: ☠️💣💥
use annotations
to trick the Java compiler
into generating bytecode
that does something else
use annotations
to trick the Java runtime
into generating bytecode
that does something else
Objenesis
Demo
Rating: ☠️
“[An enum] provides an ironclad guarantee against multiple instantiation, even in the face of sophisticated serialization or reflection attacks. […] A single-element enum type is often the best way to implement a singleton.”
– Joshua Bloch, Effective Java 3rd Edition
😇
Demo
Rating: ☠️💣
ByteBuddy
&
ByteBuddy Agent
Use cases for agents
Demo
Rating: ☠️💣💥
Demo
Idea shamelessly stolen from
/TOPdesk/time-transformer-agent
There’s more
Demo
mvn clean package
runjava -r src/main/java/demos/libraries/remote/Attack.java target/dont-hack-the-platform-0.1-SNAPSHOT.jar ???
Demo
System.setSecurityManager(new SecurityManager());
Java 17: Deprecated
FOR REMOVAL
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by demos.reflection.Reflector (file:/Users/jqno/w/personal/dont-hack-the-platform-talk/target/classes/) to field java.lang.String.value
WARNING: Please consider reporting this to the maintainers of demos.reflection.Reflector
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
--illegal-access=deny
Who has access to PROD?
Maybe not at work though?
slides & code at
https://jqno.nl/talks/dont-hack-the-platform/
I’m at
jqno