Evil Java hacks with Reflection

suggest change

The Reflection API could be used to change values of private and final fields even in the JDK default library. This could be used to manipulate the behaviour of some well known classes as we will see.

What is not possible

Let's start with a field this approach cannot change: the Java SecurityManager. The snippets below describe OpenJDK 8. Later JDKs hide these fields behind modules, and Java 24 disables the security manager. It is declared in java.lang.System as

private static volatile SecurityManager security = null;

But it won’t be listed in the System class if we run this code

for(Field f : System.class.getDeclaredFields())
    System.out.println(f);

That's because of the fieldFilterMap in sun.reflect.Reflection. On OpenJDK 8 that map hides itself and the security field of System, so reflective access cannot clear the security manager. It is not the only filtered field.

Crazy Strings

Each Java String is an instance of the String class. The JVM reuses one instance for a string literal, and for any string passed to String.intern(). If "hello" appears more than once in source, those occurrences are the same object.

Strings are supposed to be immutable, but on Java 8 the value field is a char[] and reflection can replace it. On Java 9 and later that field is a byte[], and the java.base module refuses setAccessible unless it is opened. The empty catch hides that failure. The example below shows the Java 8 mutation:

public class CrazyStrings {
    static {
        try {
            Field f = String.class.getDeclaredField("value");
            f.setAccessible(true);
            f.set("hello", "you stink!".toCharArray());
        } catch (Exception e) {
        }
    }
    public static void main(String args[])  {
        System.out.println("hello");
    }
}

On Java 8 this prints you stink! because println uses the same interned literal.

1 = 42

The same idea can be used with Integer. It works only because Integer.valueOf(1) returns the cached instance whose value was overwritten. 1 is inside the default cache (-128 through 127).

public class CrazyMath {
    static {
        try {
            Field value = Integer.class.getDeclaredField("value");    
            value.setAccessible(true);          
            value.setInt(Integer.valueOf(1), 42);      
        } catch (Exception e) {
        }
    }
    public static void main(String args[])  {
        System.out.println(Integer.valueOf(1));
    }
}

Everything is true

And according to this Stack Overflow post we can use reflection to do something really evil. Clearing Field.modifiers is unreliable after Java 12, and the empty catch hides the failure.

public class Evil {    
    static {
        try {
            Field field = Boolean.class.getField("FALSE");
            field.setAccessible(true);
            Field modifiersField = Field.class.getDeclaredField("modifiers");
            modifiersField.setAccessible(true);
            modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
            field.set(null, true);
        } catch (Exception e) {
        }
    }
    public static void main(String args[]){
        System.out.format("Everything is %s", false);
    }
}

Note that what we are doing here is going to cause the JVM to behave in inexplicable ways. This is very dangerous.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents