JSB '19 - P5

View as PDF

Submit solution

Points: 15
Time limit: 5.0s
Memory limit: 64M

Author:
Problem type
Allowed languages
Java

Please output the flag.

Download the Java agent here.

Download the source code of the agent here.

You can test locally using the command java -javaagent:p5_agent.jar YourClass.

Please note that this jar is signed, and as such you should sign the jar yourself if you plan on compiling the agent to test.

Source Code

File: agent/Agent.java
package agent;
import java.lang.instrument.*;
import java.security.ProtectionDomain;
import secret.Secret;

public class Agent {
    public static void premain(final String agentArgs, final Instrumentation inst) throws Exception {
        inst.addTransformer(new ClassFileTransformer() {
            public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) {
                if (className.equals("secret/Secret")) {
                    StackTraceElement[] stack = Thread.currentThread().getStackTrace();
                    boolean legal = false;
                    for (StackTraceElement e : stack) {
                        if (e.getClassName().startsWith("secret.")) {
                            legal = true;
                            break;
                        }
                    }
                    if (!legal) {
                        Runtime.getRuntime().halt(7);
                    }
                }
                return classfileBuffer;
            }
        });
    }
}
File: secret/Dummy.java
public class Dummy {
    String x;
    public Dummy(String s) {
        x = s;
    }

    void test(String b) {
        x += b;
    }
}
File: secret/Secret.java
package secret;

public class Secret {
    public static void flag() {
        System.out.print("CTF-");
        int[] encrypted = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        for (int i : encrypted) {
            System.out.print((char) (i <= 9 ? '0' + i : 'A' + (i % 10)));
        }
    }
}

Comments

There are no comments at the moment.