题
我有一个jar,其中所有类文件都有它们的魔术号码和删除,我对这个特定区域并不非常了解。有关在0xCafeBabe中加入的最佳方式以及重新加入每个类文件并重新包装罐子?
编辑:我已经检查过,只缺少魔术号,如果手动添加它,则文件完好无损。
解决方案
如果您想在运行时执行此操作,您可以创建自己的ClassLoader。我附上了一些可能会让你的伪代码:
public class MyClassLoader extends SecureClassLoader {
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
...
FileInputStream fis = new FileInputStream(brokenClassFile);
BufferedInputStream bis = new BufferedInputStream(fis);
ByteArrayOutputStream bas = new ByteArrayOutputStream((int) encryptedClassFile.length());
byte[] wrongBytes = bas.toByteArray();
byte[] goodbytes = ...
// add a new byte[] and put in the appropiate missing bytes for the cafebabe and magic number
CodeSource cs = new CodeSource(jarfile.toURI().toURL(), (CodeSigner[]) null);
return super.defineClass(name, goodbytes, 0, bytes.length, cs);
}
}
.
但我想最好使用一些操作系统的工具来修复jar文件。
其他提示
如果您只想将魔术号添加回课程文件,您可以使用一个简单的shell脚本(假设您在Linux上,或在Windows上有Cygwin)。
首先使用标题的4个字节(cafebabe)创建文件。
然后,从jar中提取类文件到某个目录,并在root运行此命令:
find . -name "*.class" | while read file; do
mv ${file} ${file}-old
cat /path/to/file/with/header ${file}-old > $file
rm ${file}-old
done
.
注意:上面的脚本在bash中工作,但您应该能够为任何shell编写类似的东西,甚至是窗口。
但你的意思是“有他们的魔法号码和类型删除”?如果字节码被以任何方式进行了清醒,则更改可能更难以修复,如果不是不可能的话。
不隶属于 StackOverflow