How to modify boolean variable value in java byte code?

2k views Asked by At

As i find that some java jar using a boolean variable to decide whether output the log, the example is as follow:

public final class a {
    private static boolean a = false;
    private static boolean b = false;
    private static Logger c;
    public static void a(String var0, String var1) {
        if(a) {
            var0 = "Log-" + var0;
            if(b && c != null) {
                c.log(Level.INFO, var0 + ": " + var1);
            } else {
                Log.v(var0, var1);
            }
        }
    }

the above code is decompiled by Intellij idea. Since:

  1. There is no source code, only jar file includes .class files
  2. The code is obfuscated(as you see the member variable is all a, b, c), so can reflection work?

Here i want to modify the member private static boolean a,change from false to true in order to output the log. I try jclasslib but i can only get the variables as org.gjt.jclasslib.structures.FieldInfo object, i can not find a way to modify its value. Can anyone help?

Second, i write a class that defines a boolean variable like:

public class BroadcastHook {
    private static boolean testBoolean=true;

What confuses me is that whether the testBoolean is true or false, the testBoolean Fields is same in Bytecode viewer, below is the screenshots the testBoolean Fields structure in Bytecode viewer

Why can't i find the false value of testBoolean in Fields?

1

There are 1 answers

0
Filippo On

You need to find the right one but the change is simple:

To change the boolean variable from true to false hex edit a 0x04 to 0x03.