1---2name: java-serialization-audit3description: |4 Java source code audit for serialization-class vulnerabilities. Triggered during Java white-box audits when serialization/deserialization related vulnerabilities need to be detected.5 Covers 3 risk classes: native Java deserialization (ObjectInputStream/Gadget Chain/ysoserial),6 XXE (five parsers: DocumentBuilderFactory/SAXParser/XMLInputFactory/TransformerFactory/SchemaFactory),7 and template injection SSTI (code execution via Velocity/FreeMarker/Thymeleaf template engines).8 Java deserialization is the most distinctive high-risk vulnerability class in the Java ecosystem, and gadget chain analysis is the core difficulty.9zh-signals: "Java 源码序列化类漏洞审计,覆盖反序列化、XXE 与模板注入 SSTI 的白盒检测。信号:Java反序列化,XXE,SSTI,gadget链,源码审计"10when-to-use: >-11 Use when auditing Java source code for deserialization, XXE, or SSTI12 vulnerabilities, especially when ObjectInputStream or XML parsers process13 user-controlled input.14metadata:15 source: https://skillmd.com/skills/dangeroussong/java-serialization-audit16 tags: deserialization, xxe, ssti, objectinputstream, gadget chain, ysoserial, xml, velocity, freemarker, thymeleaf, deserialization, template injection, java source audit17 category: code-audit18---1920# Java Serialization-Class Vulnerability Source Code Audit21- 攻击大类:C09 Web应用漏洞利用22This skill focuses on source-level judgement of "whether deserialization/XXE/SSTI holds". The core work is: white-box confirmation of deserialization entry points, tracing data sources, assessing the gadget chains available in the classpath, verifying the secure configuration of XML parsers, and checking the rendering context of template engines. Runtime black-box techniques such as crafting exploit payloads and sending remote exploit chains fall within the scope of the corresponding exploit skill.2324## Further Reading2526- Dangerous patterns / secure pattern code comparisons / Gadget dependency tables / EVID evidence examples for the 3 vulnerability classes → [references/serialization-patterns.md](references/serialization-patterns.md)2728---2930## Quick Reference Table for the 3 Vulnerability Classes3132| Type | Typical Sink | Exploitation Condition | Severity |33|------|-----------|----------|--------|34| Java deserialization | `ObjectInputStream.readObject()`, `XMLDecoder.readObject()` | User-controllable serialized stream + a usable gadget chain in the classpath | Critical |35| XXE | `DocumentBuilderFactory`, `SAXParser`, `XMLInputFactory`, `TransformerFactory`, `SchemaFactory` | External entities/DTD not disabled + user-controllable XML input | High-Critical |36| SSTI | `Velocity.evaluate`, `FreeMarker Template`, Thymeleaf pre-processing expressions | User input flows directly into the template compilation/rendering context | Critical |3738## Java Deserialization Audit Points3940- **Entry point identification**: `ObjectInputStream.readObject()`, `ObjectInputStream.readUnshared()`, `XMLDecoder.readObject()`, plus the extra attack surface in custom `readObject`/`readResolve`/`readExternal` implementations41- **Data source tracing**: HTTP request body (magic bytes `AC ED 00 05` or Base64 encoded `rO0AB`), RMI/JMX remote calls, JMS message queues, Redis/Memcached cache deserialization, file reads, ViewState, Cookie/Session serialized storage42- **Gadget availability assessment**: check `pom.xml`/`build.gradle` and the classpath for libraries with known gadget chains (commons-collections 3.x/4.x, commons-beanutils, spring-core, Groovy, hibernate-core, etc.), and whether the versions fall within the affected ranges43- **Defense mechanism verification**: whether JEP 290 `ObjectInputFilter` is configured, allowlist vs blacklist strategy, whether third-party protection libraries such as `SerialKiller`/`NotSoSerial` are enabled, whether `resolveClass` has been overridden with allowlist validation44- **readResolve/readObject custom logic**: even without a generic gadget, file operations, network requests, and dynamic class loading inside custom deserialization methods also constitute attack surface4546## XXE Audit Points4748- **Secure configuration for the 5 XML parsers**:49 - `DocumentBuilderFactory`: `setFeature("http://apache.org/xml/features/disallow-doctype-decl", true)`50 - `SAXParserFactory`: same as above + `setFeature("http://xml.org/sax/features/external-general-entities", false)` + `setFeature("http://xml.org/sax/features/external-parameter-entities", false)`51 - `XMLInputFactory` (StAX): `setProperty(XMLInputFactory.SUPPORT_DTD, false)` + `setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false)`52 - `TransformerFactory`: `setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "")` + `setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "")`53 - `SchemaFactory`: `setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "")` + `setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "")`54- **Data flow analysis**: XML input source (HTTP request body, file upload, SOAP message, SVG/Office file) → enters the parser → parsed result echoed back (direct XXE) or not echoed (blind injection / out-of-band OOB)55- **Java-specific protocols**: the `jar:` protocol can trigger remote file download and extraction, and the `netdoc:` protocol (older JDKs) is equivalent to `file://`5657## Template Injection SSTI Audit Points5859- **Velocity**: `Velocity.evaluate(context, writer, tag, userTemplate)` — if the `userTemplate` parameter is user-controllable it is a direct RCE60- **FreeMarker**: `new Template("t", new StringReader(userInput), cfg).process(dataModel, writer)` — user input used as the template source is dangerous once compiled61- **Thymeleaf**: controller return value concatenation leads to SSTI; with `return "user/" + input`, the input can inject the `__${expr}__` pre-processing expression to trigger SpEL → RCE62- **Secure patterns**: template name/path allowlist, FreeMarker setting `TemplateClassResolver.ALLOWS_NOTHING_RESOLVER` to disable the `?new()` built-in function, Velocity using `SecureUberspector`, Thymeleaf restricting view names so user input cannot be concatenated6364## Detection Checklist6566- [ ] All `ObjectInputStream.readObject()`/`XMLDecoder.readObject()` call sites have been reviewed one by one67- [ ] Data sources of deserialization entry points (HTTP/RMI/JMS/cache/file) have been traced and confirmed68- [ ] Classpath dependencies have been checked for known gadget chain libraries and their affected versions69- [ ] Defense mechanisms such as JEP 290 ObjectInputFilter / SerialKiller / allowlist resolveClass have been verified70- [ ] Extra attack surface in custom readObject/readResolve methods has been ruled out71- [ ] Secure feature configuration of the 5 XML parsers has been confirmed one by one72- [ ] XML input sources (request body/upload/SOAP/SVG) and the echo behavior have been analyzed73- [ ] User-input controllability of the Velocity/FreeMarker/Thymeleaf template rendering entry points has been checked74- [ ] Template engine secure patterns (sandbox/allowlist/disabled dangerous functions) have been verified75- [ ] Severity scoring used the unified formula, consistent with the pipeline