tomcat WebappClassLoader 加密class文件

tomcat WebappClassLoader 加密class文件
 
 
    对class文件时行加密,tomcat加载class文件时再进行解密.可以达到隐藏代码的目的.下面用commons-codec中的base64对class进行encode,再在tomcat加载时对class进行decode.
1.对要encode的class进行编码:
public static void main(String[] args) throws IOException {
FileInputStream file = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
file = new FileInputStream(
"./target/classes/kevin/ClassLoader.class");
bis = new BufferedInputStream(file);
byte[] b = new byte[bis.available()];
bis.read(b);
byte[] encodeB = Base64.encodeBase64(b);

fos = new FileOutputStream("ClassLoader.class");
bos = new BufferedOutputStream(fos);
bos.write(encodeB);
bos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
file.close();
bis.close();
fos.close();
bos.close();
}
}
2.将生成的class放入部署好的工程相应位置.
3.到tomcat.apache.org下载tomcat的源代码.
4.将eclipse.classpath, eclipse.project修改成.classpath, .project后,直接用eclipse导入工程apache-tomcat-6.0.32-src,再导入缺少的包.
5.在工程apache-tomcat-6.0.32-src中找到org.apache.catalina.loader.WebappClassLoader文件,再找到protected Class findClassInternal(String name) throws ClassNotFoundException 方法,将内容:
clazz = defineClass(name, entry.binaryContent, 0,
entry.binaryContent.length, new CodeSource(
entry.codeBase, entry.certificates));
修改为:
if (name.indexOf("ClassLoader") >= 0) {
byte[] base64Array = entry.binaryContent;
byte[] orginByteArray = Base64.decodeBase64(base64Array);
clazz = defineClass(name, orginByteArray, 0,
orginByteArray.length, new CodeSource(
entry.codeBase, entry.certificates));
} else {
clazz = defineClass(name, entry.binaryContent, 0,
entry.binaryContent.length, new CodeSource(
entry.codeBase, entry.certificates));
}
6.编译完成后,将WebappClassLoader.class及其子类文件替换运行时的tomcat/lib下的catalina.jar包中相同文件.
7.当tomcat运行需加载ClassLoader.class时,将会先对其进行decode,达到加密的目的. 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值