fastjson反序列化TemplatesImpl链
FastJson的用法
首先熟悉一下FastJson如何使用,FastJson是阿里巴巴开发的一个解析Json格式的第三方包。
序列化
FastJson在序列化A时会调用A的get方法获得数据。
另外toJSONString()
方法在进行序列化时还可以指定一个可选的SerializerFeature.WriteClassName
参数,指定了该参数后,在序列化时json数据中会写入一个 @type 选项。
json数据中的 @type 选项用于指定反序列化的类,也就是说所,当这段json数据被反序列化时,会按照 @type 选项中指定的类全名反序列化成java对象。
反序列化
fastjson提供了两个反序列化函数:parseObject()
和parse()
public class FastJsonDemo01 {
public static void main(String[] args) {
Student student = new Student("A",11);
String jsonString = JSON.toJSONString(student, SerializerFeature.WriteClassName);
System.out.println(jsonString);
System.out.println("============1==============");
JSONObject jsonObject = JSON.parseObject(jsonString);
System.out.println("jsonObject --->" + jsonObject);
}
}
/*
method: Student(String name, int age)
method: getAge()
method: getName()
{"@type":"fastJsonDemo.Student","age":11,"name":"A"}
============1==============
method: Student()
method: setAge(int age)
method: setName(String name)
method: getAge()
method: getName()
jsonObject --->{"name":"A","age":11}
*/
JSON.parseObject(jsonString)
在反序列化过程中会调用对象的setter和getter方法。
public class FastJsonDemo01 {
public static void main(String[] args) {
Student student = new Student("A",11);
String jsonString = JSON.toJSONString(student, SerializerFeature.WriteClassName);
System.out.println(jsonString);
System.out.println("============2==============");
Student jsonStudent = JSON.parseObject(jsonString,Student.class);
System.out.println("jsonStudent --->" + jsonStudent);
}
}
/*
============2==============
method: Student()
method: setAge(int age)
method: setName(String name)
jsonStudent --->fastJsonDemo.Student@497470ed
*/
parseObject(jsonString,Student.class)
这种指定了反序列化类型的方式只会调用Student的set方法。
public class FastJsonDemo01 {
public static void main(String[] args) {
Student student = new Student("A",11);
String jsonString = JSON.toJSONString(student, SerializerFeature.WriteClassName);
System.out.println(jsonString);
System.out.println("============3==============");
Object obj = JSON.parse(jsonString);
System.out.println("obj --->" + obj);
}
}
/*
============3==============
method: Student()
method: setAge(int age)
method: setName(String name)
obj --->fastJsonDemo.Student@63c12fb0
*/
JSON.parse(jsonString)
在反序列化过程中会调用对象的setter方法。
而当序列化对象中的某个set变成了private权限,那么这个field将不会被反序列化,除非加上Feature.SupportNonPublicField
参数。
可以参考13-java安全——fastjson1.2.24反序列化TemplatesImpl利用链分析
TemplatesImpl利用链
首先来看com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl
类,
/**
* This method generates an instance of the translet class that is
* wrapped inside this Template. The translet instance will later
* be wrapped inside a Transformer object.
*/
private Translet getTransletInstance()
throws TransformerConfigurationException {
try {
if (_name == null) return null;
if (_class == null) defineTransletClasses();
// The translet needs to keep a reference to all its auxiliary
// class to prevent the GC from collecting them
AbstractTranslet translet = (AbstractTranslet) _class[_transletIndex]