【问题标题】:Custom Class Loader In Java [closed]Java中的自定义类加载器[关闭]
【发布时间】:2013-10-12 10:45:21
【问题描述】:

是否可以在 java 中制作我自己的自定义类加载器。如果是,请指导我。 我想更改类文件而不是类混淆,以便任何工具都无法将其反转

【问题讨论】:

  • 请在寻求解决方案之前进行研究。
  • 如果字节码不能被“任何工具”理解,那么JVM也将无法读取它,因此它不会运行
  • 攻击者需要做的就是读取自定义类加载器提供给 JVM 的字节码。
  • 好的,我知道我的观点并不清楚,但在问这个问题之前我做了研究,我只是想知道如何保护我的 java 应用程序不被反转。因为互联网上没有关于如何加密类/Jar 的答案,而且我不想混淆类文件。所以答案基本上是没有办法保护Java应用程序如果我错了请纠正我。

标签: java jvm classloader bytecode


【解决方案1】:
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 * Simple custom class loader implementation
 * 
 */
public class CustomClassLoader extends ClassLoader {

    /**
     * The HashMap where the classes will be cached
     */
    private Map<String, Class<?>> classes = new HashMap<String, Class<?>>();

    @Override
    public String toString() {
        return CustomClassLoader.class.getName();
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {

        if (classes.containsKey(name)) {
            return classes.get(name);
        }

        byte[] classData;

        try {
            classData = loadClassData(name);
        } catch (IOException e) {
            throw new ClassNotFoundException("Class [" + name
                    + "] could not be found", e);
        }

        Class<?> c = defineClass(name, classData, 0, classData.length);
        resolveClass(c);
        classes.put(name, c);

        return c;
    }

    /**
     * Load the class file into byte array
     * 
     * @param name
     *            The name of the class e.g. com.codeslices.test.TestClass}
     * @return The class file as byte array
     * @throws IOException
     */
    private byte[] loadClassData(String name) throws IOException {
        BufferedInputStream in = new BufferedInputStream(
                ClassLoader.getSystemResourceAsStream(name.replace(".", "/")
                        + ".class"));
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int i;

        while ((i = in.read()) != -1) {
            out.write(i);
        }

        in.close();
        byte[] classData = out.toByteArray();
        out.close();

        return classData;
    }

    /**
     * Simple usage of the CustomClassLoader implementation
     * 
     * @param args
     * @throws ClassNotFoundException
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws SecurityException
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalArgumentException
     */
    public static void main(String[] args) throws ClassNotFoundException,
            InstantiationException, IllegalAccessException,
            NoSuchMethodException, SecurityException, IllegalArgumentException,
            InvocationTargetException
    {
        CustomClassLoader loader = new CustomClassLoader();
        // This class should be in your application class path
        Class<?> c = loader.findClass("net.codeslices.test.TestClass");
        Object o = c.newInstance();
        Method m = c.getMethod("toString");
        System.out.println(m.invoke(o));
    }

}

【讨论】:

    【解决方案2】:

    您可以使用一些混淆工具,例如 ProGuard。

    一个自写的 ClassLoader,必须放在一个标准的 .class 文件中,JVM 可以加载它。然后可以对您的安全加载程序进行逆向工程。

    不要自己做。在不了解加密算法的情况下编写“安全”代码将导致容易出错的不安全代码

    【讨论】:

    • 这不是问题,因为密码学无论如何也无济于事。 (没有办法保留密钥)
    • 我只是想解释一下,大多数认为自己通过添加混淆或“隐蔽安全”使程序更安全的开发人员会使程序更容易出错且更不安全。
    • 您当然是正确的,但在这种情况下,默默无闻是唯一的选择。如果他的目标足够小,攻击者甚至可能懒得投入对手工构建系统进行逆向工程所需的工作。
    • 我想我会使用如下加密和解密方法来加密 jar 文件:examples.javacodegeeks.com/core-java/crypto/… 但问题是未加密的加密类加载器,其中包含秘密。
    猜你喜欢
    • 2010-10-07
    • 1970-01-01
    • 2023-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多