Don’t hack the JVM? β˜ οΈπŸ’£πŸ’₯

Jan Ouwens

ΰ² _ΰ² 

About me πŸ€“

Jan Ouwens

jqno
#JFall

EqualsVerifier


About this talk

Things you shouldn’t mess with:

  • Language β€˜features’
  • Reflection
  • Annotations
  • External libraries

Ratings

  • ☠️
  • β˜ οΈπŸ’£
  • β˜ οΈπŸ’£πŸ’₯

☠️ Language β€˜features’

True Lambda

Demo

True Lambda

Java 11!

Demo

Unicode symbols

  • Hard to type
  • Hard to read
  • Rating: ☠️

False is True

Demo

False is True

Scala demo

False is True

Kotlin demo

False is True

Rating: β˜ οΈπŸ’£

Unicode converter

Demo

Unicode escapes

  • Harder to type and read
  • Maven says Β―\_(ツ)_/Β―
  • Rating: β˜ οΈπŸ’£

Emoji

Demo

Emoji

Rating: πŸŽ‰

Redacted by NLJUG

β˜ οΈπŸ’£πŸ’₯

Hash-code

Demo

Hash-code

  • Fun when coworkers insist on hashCode as key
  • Rating: ☠️

Hash-code

However…

☠️ Reflection

Introducing: Reflector

Loopy

Demo

Loopy

Was this hack evil? βœ‹

Are Calendars and arrays evil? βœ‹

Are JPA entities evil? πŸ‘Ή

Loopy

Rating: β˜ οΈπŸ’£πŸ’₯

Interning

Demo

Oh man πŸ˜’

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

--add-opens java.base/java.lang=ALL-UNNAMED

Interning

  • Fun way to mess up unit tests!
  • Rating: β˜ οΈπŸ’£

dirty_CLASS

Demo

You should probably

  • Call close() on URLClassLoader
  • Report compilation errors with CompilationTask
  • Handle exceptions


Β―\_(ツ)_/Β―

Multi-release JAR file

EqualsVerifier

JavaCompiler

Rating: β˜ οΈπŸ’£πŸ’₯

☠️ Annotations

Lombok 🌢

use annotations

to trick the Java compiler

into generating bytecode

that does something else

Spring & Hibernate

use annotations

to trick the Java runtime

into generating bytecode

that does something else






Boring

☠️ External libraries

Objenesis

Constructors are tedious

Demo

Constructors are tedious

Rating: ☠️

Singletons

β€œ[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 2nd Edition

Singletons

β€œ[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

πŸ˜‡

Singletons

Demo

Singletons

Rating: β˜ οΈπŸ’£

Confusing card game

Confusing card game

Demo

Confusing card game

Rating: β˜ οΈπŸ’£πŸ’₯

Confusing card game

External libraries

ByteBuddy

&

ByteBuddy Agent

Disclaimer

Use cases for agents

  • there are many
  • they’re legitimate

Time Traveling πŸ•™πŸ•š

Demo





Idea shamelessly stolen from
/TOPdesk/time-transformer-agent

Time Traveling πŸ•™πŸ•š

  • Unit testing legacy code
  • Messing up any code
  • Rating: β˜ οΈπŸ’£πŸ’₯

But wait

There’s more

Victim / Attack

Demo



mvn clean package

mvn exec:java -DmainClass=demos.libraries.remote.Attack -Darg0=target/dont-hack-the-platform-0.1-SNAPSHOT.jar -Darg1=???

Victim / Attack

Rating:

😱😱😱😱😱😱😱😱😱😱😱😱😱😱😱😱😱😱😱😱😱😱😱😱😱😱😱😱😱😱😱😱😱😱😱

</>

Awareness

Emoji & unicode

  • Checkstyle
  • SonarQube

Security manager

Demo



Security manager

Modularisation

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

Do try this at home!

Maybe not at work though?

β˜ οΈπŸ’£πŸ’₯

Please rate my talk in the official J-Fall app!




…and don’t forget to like and subscribe!

Questions?



slides & code at
/jqno/dont-hack-the-platform-talk

I’m at
jqno

#JFall