Spring Data REST 远程代码执行漏洞(CVE-2017-8046)分析与复现

前言

2009年9月Spring 3.0 RC1发布后,Spring就引入了SpEL(Spring Expression Language)。对于开发者而言,引入新的工具显然是令人兴奋的,但是对于运维人员,也许是噩耗的开始。类比Struts 2框架,会发现绝大部分的安全漏洞都和ognl脱不了干系。尤其是远程命令执行漏洞,占据了多少甲方乙方工程师的夜晚/周末,这导致Struts 2越来越不受待见。

因此,我们有理由相信Spring引入SpEL必然增加安全风险。事实上,过去多个Spring CVE都与其相关,如CVE-2017-8039、CVE-2017-4971、CVE-2016-5007、CVE-2016-4977等。

本文分析的CVE-2017-8046同样也与SpEL有关。如果急于查看自己的应用是否受影响和修复建议,请查看官方公告,或者跳至0x07漏洞修复。

Spring Data REST简介

Spring Data REST是Spring Data的一个子项目。关于Spring Data,引用官方介绍如下: > Spring Data’s mission is to provide a familiar and consistent, Spring-based programming model for data access while still retaining the special traits of the underlying data store.

It makes it easy to use data access technologies, relational and non-relational databases, map-reduce frameworks, and cloud-based data services. This is an umbrella project which contains many subprojects that are specific to a given database. The projects are developed by working together with many of the companies and developers that are behind these exciting technologies.

一句话概括:Spring Data是对数据访问的更高抽象。通过它,开发者进一步从数据层解放出来,更专注于业务逻辑。不管是关系型数据还是非关系型数据,利用相应接口,开发者可以使用非常简单的代码构建对数据的访问(当然,Spring Data还有很多特性和功能,感兴趣的可参考官方文档)。

回过头看Spring Data REST,它是一个构建在Spring Data之上,为了帮助开发者更加容易地开发REST风格的Web服务,官方声称完成demo只需15分钟。

官方提供的Demo

参照官方文档,笔者使用Maven构建Spring-boot应用,数据库为H2 Database。

1) 添加依赖,pom.xml内容来自官方示例文档。 2) 编写实体类Person。

//import 省略@Entity
public class Person {@Id@GeneratedValue(strategy = GenerationType.AUTO)private long id;          //自增主健private String firstName;private String lastName;
//getter setter省略
}

3) 编写接口。

//import 省略//在/people处创建RESTful入口点
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
//接口继承了PagingAndSortingRepository,此接口封装了对Person实体类的CURD,并且具备分页和排序
}

4) Spring Boot执行入口。

//import 省略@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}

5) 编译运行。 编译运行

数据操作测试

1)测试是否成功 数据操作测试

2)使用POST方法添加一个数据 数据操作测试

3)查看新加入的数据 数据操作测试

4)使用PATCH请求方法更新数据 数据操作测试

对于JSON Patch请求方法IETF制定了标准RFC6902。JSON Patch方法提交的数据必须包含一个path成员,用于定位数据,同时还必须包含op成员,可选值如下:

op含义
add添加数据
remove删除数据
replace修改数据
move移动数据
copy拷贝数据
test测试给定数据与指定位置数据是否相等

比如对于上面添加的Person数据,修改其lastName属性,请求数据如下: > [{ “op”: “replace”, “path”: “/lastName”, “value”: “Zhang” }]

有两点需要注意:

① 必须将Content-Type指定为application/json-patch+json。 ② 请求数据必须是json数组。

漏洞分析

漏洞分析涉及的源码比较多,为了减少歧义和减小篇幅,约定两点: ① 代码以片段[a-z]标识; ② 提到某个方法不会包含完整的方法签名,仅提供方法名,需联系上下文识别。

1)根据官方公告,结合GitHub 的commit,猜测漏洞出在path参数值的处理上。尝试提交非法的path参数值,查看异常堆栈信息:

at org.springframework.expression.spel.ast.MethodReference$MethodValueRef.setValue(MethodReference.java:355) ~[spring-expression-4.3.10.RELEASE.jar:4.3.10.RELEASE]at org.springframework.expression.spel.ast.CompoundExpression.setValue(CompoundExpression.java:95) ~[spring-expression-4.3.10.RELEASE.jar:4.3.10.RELEASE]at org.springframework.expression.spel.standard.SpelExpression.setValue(SpelExpression.java:438) ~[spring-expression-4.3.10.RELEASE.jar:4.3.10.RELEASE]at org.springframework.data.rest.webmvc.json.patch.PatchOperation.setValueOnTarget(PatchOperation.java:167) ~[spring-data-rest-webmvc-2.6.6.RELEASE.jar:na]at org.springframework.data.rest.webmvc.json.patch.ReplaceOperation.perform(ReplaceOperation.java:41) ~[spring-data-rest-webmvc-2.6.6.RELEASE.jar:na]at org.springframework.data.rest.webmvc.json.patch.Patch.apply(Patch.java:64) ~[spring-data-rest-webmvc-2.6.6.RELEASE.jar:na]at org.springframework.data.rest.webmvc.config.JsonPatchHandler.applyPatch(JsonPatchHandler.java:91) ~[spring-data-rest-webmvc-2.6.6.RELEASE.jar:na]at org.springframework.data.rest.webmvc.config.JsonPatchHandler.apply(JsonPatchHandler.java:83) ~[spring-data-rest-webmvc-2.6.6.RELEASE.jar:na]at org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.readPatch(PersistentEntityResourceHandlerMethodArgumentResolver.java:206) ~[spring-data-rest-webmvc-2.6.6.RELEASE.jar:na]at org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.read(PersistentEntityResourceHandlerMethodArgumentResolver.java:184) ~[spring-data-rest-webmvc-2.6.6.RELEASE.jar:na]at org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.resolveArgument(PersistentEntityResourceHandlerMethodArgumentResolver.java:141) ~[spring-data-rest-webmvc-2.6.6.RELEASE.jar:na]at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:158) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]//省略部分堆栈信息

2)既然是Patch请求方法,我们从org.springframework.data.rest.webmvc.config.JsonPatchHandler.apply(JsonPatchHandler.java:83)入手分析。

//片段a:public <T> T apply(IncomingRequest request, T target) throws Exception {Assert.notNull(request, "Request must not be null!");Assert.isTrue(request.isPatchRequest(), "Cannot handle non-PATCH request!");Assert.notNull(target, "Target must not be null!");if (request.isJsonPatchRequest()) {//return applyPatch(request.getBody(), target);} else {return applyMergePatch(request.getBody(), target);}}

片段a中的if判断决定了请求Content-Type须指定application/json-patch+json。

//片段b:
public boolean isJsonPatchRequest() {return isPatchRequest() //是否是PATCH请求方法//Content-Type是否与application/json-patch+json兼容&& RestMediaTypes.JSON_PATCH_JSON.isCompatibleWith(contentType);}

片段a中的if判断为true的话,进入applyPatch方法:

//片段c:
@SuppressWarnings("unchecked")
<T> T applyPatch(InputStream source, T target) throws Exception {return getPatchOperations(source).apply(target, (Class<T>) target.getClass());
}

跟进getPatchOperations方法:

//片段d:
private Patch getPatchOperations(InputStream source) {try {return new JsonPatchPatchConverter(mapper).convert(mapper.readTree(source));//通过Jackson 生成对应的对象实例} catch (Exception o_O) {throw new HttpMessageNotReadableException(String.format("Could not read PATCH operations! Expected %s!", RestMediaTypes.JSON_PATCH_JSON), o_O);}
}

片段d通过Jackson实例化对象,我们看看相关构造函数:

//片段e:
public Patch(List<PatchOperation> operations) {this.operations = operations;
}
//片段f:
public PatchOperation(String op, String path, Object value) {this.op = op;this.path = path;this.value = value;this.spelExpression = pathToExpression(path);
}

对于PatchOperation对象,成员spelExpression根据path转化而来,这一点对于PoC构造非常重要,笔者一开始就坑在这里。 pathToExpression完整的调用链比较长,影响PoC的构造关键在于下面两个方法。

//片段g:
private static String pathToSpEL(String path) {return pathNodesToSpEL(path.split("\\/"));//跟据斜杠分割成字符数组
}
//片段h:
private static String pathNodesToSpEL(String[] pathNodes) {StringBuilder spelBuilder = new StringBuilder();for (int i = 0; i < pathNodes.length; i++) {String pathNode = pathNodes[i];if (pathNode.length() == 0) {continue;}if (APPEND_CHARACTERS.contains(pathNode)) {if (spelBuilder.length() > 0) {spelBuilder.append(".");}spelBuilder.append("$[true]");continue;}try {int index = Integer.parseInt(pathNode);spelBuilder.append('[').append(index).append(']');} catch (NumberFormatException e) {if (spelBuilder.length() > 0) {//使用.拼接字符数组//如笔者尝试执行touch /tmp/file,spelBuilder.append('.');  //并未在/tmp中发现file文件,后来发现应用目录中多了隐藏文件,}                             //原因就在此处spelBuilder.append(pathNode);}}String spel = spelBuilder.toString();if (spel.length() == 0) {spel = "#this";}return spel;
}

回到片段C,继续看apply:

//片段i:
public <T> T apply(T in, Class<T> type) throws PatchException {for (PatchOperation operation : operations) {operation.perform(in, type);}return in;
}

在RFC6902的标准中,一次PATCH请求允许多个操作,比如:

[{ "op": "test", "path": "/a/b/c", "value": "foo" },{ "op": "remove", "path": "/a/b/c" },{ "op": "add", "path": "/a/b/c", "value": [ "foo", "bar" ] }
]

对于上面的请求数据,将会顺序执行test、remove、add操作(当前操作的”文档”为上一次操作更新后的”文档”)。

因此,在代码片段i中循环每一个”操作”。假设我们提交了一个PATCH请求op为replace,我们接着看PatchOperation子类ReplaceOperation的perform方法:

//片段j:
<T> void perform(Object target, Class<T> type) {setValueOnTarget(target, evaluateValueFromTarget(target, type));
}

调用父类PatchOperation的evaluateValueFromTarget方法:

//片段k:
protected <T> Object evaluateValueFromTarget(Object targetObject, Class<T> entityType) {return value instanceof LateObjectEvaluator? ((LateObjectEvaluator) value).evaluate(spelExpression.getValueType(targetObject)) : value;
}

官方在evaluateValueFromTarget方法中打了补丁,补丁的修复逻辑是检查路径是否合法,如果不合法则会抛出PatchException。完整的补丁信息可以从GitHub看对应commit。

//片段l:protected <T> Object evaluateValueFromTarget(Object targetObject, Class<T> entityType) {
-       return value instanceof LateObjectEvaluator
-               ? ((LateObjectEvaluator) value).evaluate(spelExpression.getValueType(targetObject)) : value;
+       verifyPath(entityType);
+
+       return evaluate(spelExpression.getValueType(targetObject));
+   }++   protected final <T> Object evaluate(Class<T> type) {
+       return value instanceof LateObjectEvaluator ? ((LateObjectEvaluator) value).evaluate(type) : value;
+   }++   /**
+    * Verifies that the current path is available on the given type.
+    * 
+    * @param type must not be {@literal null}.
+    * @return the {@link PropertyPath} representing the path. Empty if the path only consists of index lookups or append
+    *         characters.
+    */+   protected final Optional<PropertyPath> verifyPath(Class<?> type) {
+
+       String pathSource = Arrays.stream(path.split("/"))//
+               .filter(it -> !it.matches("\\d")) // no digits
+               .filter(it -> !it.equals("-")) // no "last element"s
+               .filter(it -> !it.isEmpty()) //
+               .collect(Collectors.joining("."));
+
+       if (pathSource.isEmpty()) {
+           return Optional.empty();
+       }
+
+       try {
+           return Optional.of(PropertyPath.from(pathSource, type)); //根据对象和路径获取PropertyPath
+       } catch (PropertyReferenceException o_O) {
+           throw new PatchException(String.format(INVALID_PATH_REFERENCE, pathSource, type, path), o_O);
+       }}

回过头看代码片段j,setValueOnTarget再往后走就是SpEL解析了。由于SpEL非该漏洞核心,本文不再深入。

漏洞复现

明白了漏洞原理之后,复现就非常简单了。注入表达式没有太多限制。 漏洞复现

漏洞修复

漏洞在9月21日披露,虽然定位为严重。但是笔者持续跟踪,并未发现国内哪些站点在跟进,不排除攻击者利用此漏洞攻击未打补丁的受影响应用。

漏洞信息来源于官方公告。 漏洞修复

值得注意的是,本次漏洞问题出现在 spring-data-rest-webmvc中。由于Spring 提供内建的依赖解决,因此可能并不会在依赖配置文件(如Maven的pom.xml)显式看到 spring-data-rest-webmv的依赖配置,这就是为什么官方公告还提及Spring Boot和Spring Data的缘故。

漏洞触发条件:网站使用Spring Data REST提供REST Web服务,版本在受影响范围内。

修复建议:及时升级。

参考链接

  1. https://pivotal.io/security/cve-2017-8046
  2. https://github.com/spring-projects/spring-data-rest/commit/8f269e28fe8038a6c60f31a1c36cfda04795ab45
  3. http://projects.spring.io/spring-data-rest/
  4. https://tools.ietf.org/html/rfc6902

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/478512.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

体验paddle2.0rc版本API-Model--实现Mnist数据集模型训练

原文链接&#xff1a;体验paddle2.0rc版本API-Model–实现Mnist数据集模型训练&#xff1a;https://blog.csdn.net/weixin_44604887/article/details/109566281 尝试Mnist训练 导入需要的包导入Mnist数据集组网搭建网络查看模型参数查看模型网络&#xff08;自动命名&#xff0…

LeetCode 648. 单词替换(Trie树)

1. 题目 在英语中&#xff0c;我们有一个叫做 词根(root)的概念&#xff0c;它可以跟着其他一些词组成另一个较长的单词——我们称这个词为 继承词(successor)。例如&#xff0c;词根an&#xff0c;跟随着单词 other(其他)&#xff0c;可以形成新的单词 another(另一个)。 现…

99%算法工程师不知道的if/else优化技巧

文 | IT技术控知乎、灵剑知乎观点一&#xff08;IT技术控&#xff09;前期迭代懒得优化&#xff0c;来一个需求&#xff0c;加一个if&#xff0c;久而久之&#xff0c;就串成了一座金字塔。当代码已经复杂到难以维护的程度之后&#xff0c;只能狠下心重构优化。那&#xff0c;有…

人工智能在线特征系统中的生产调度

在上篇博客《人工智能在线特征系统中的数据存取技术》中&#xff0c;我们围绕着在线特征系统存储与读取这两方面话题&#xff0c;针对具体场景介绍了一些通用技术&#xff0c;此外特征系统还有另一个重要话题&#xff1a;特征生产调度。本文将以美团点评酒旅在线特征系统为原型…

LeetCode 211. 添加与搜索单词 - 数据结构设计(Trie树)

1. 题目 设计一个支持以下两种操作的数据结构&#xff1a; void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串&#xff0c;字符串只包含字母 . 或 a-z 。 . 可以表示任何一个字母。 示例: addWord("bad") addWord("dad&quo…

研究综述 - TKDE2020 | 基于知识图谱的推荐系统

作者 | 郭庆宇转载公众号 | 读芯术TKDE 2020综述&#xff1a;基于知识图谱的推荐系统A Survey on Knowledge Graph-Based Recommender Systems中科院计算所、百度、港科大、中科大、微软原文Qingyu Guo, Fuzhen Zhuang, Chuan Qin, Hengshu Zhu, Xing Xie, Hui Xiong, Qing He…

谢撩,人在斯坦福打SoTA

文 | Jazon编 | 小戏小编注&#xff1a;不知道大家还记不记得卖萌屋之前人在斯坦福&#xff0c;刚上CS224n的Jazon小哥发来的关于斯坦福神课CS224n上半学期的报道&#xff1f;今天&#xff0c;Jazon又在斯坦福前线发来了关于他在CS224n下半学期的经历&#xff0c;那么现在让我们…

前端感官性能的衡量和优化实践

本文已发表在2017年8月《程序员》杂志。 我们为什么需要关注站点的性能&#xff0c;性能为什么如此重要呢&#xff1f;如今任何互联网产品首先重要的都是流量&#xff0c;流量最终会转换为商业价值。所以在互联网产品中&#xff0c;流量、转化率和留存率基本上是产品经理或者业…

LeetCode 421. 数组中两个数的最大异或值(Trie树)

1. 题目 给定一个非空数组&#xff0c;数组中元素为 a0, a1, a2, … , an-1&#xff0c;其中 0 ≤ ai < 231 。 找到 ai 和aj 最大的异或 (XOR) 运算结果&#xff0c;其中0 ≤ i, j < n 。 你能在O(n)的时间解决这个问题吗&#xff1f; 示例:输入: [3, 10, 5, 25, 2,…

论文浅尝 - EMNLP2020 | 基于知识库的多跳关系推理

笔记整理 | 谢辛&#xff0c;浙江大学硕士研究方向 | 自然语言处理&#xff0c;知识图谱Feng Y, Chen X, Lin B Y, et al. Scalable multi-hop relational reasoning for knowledge-aware question answering[J]. 2020.emnlp-main.99链接&#xff1a;https://arxiv.org/pdf/200…

智能工单处理,达观数据助力运营商实现业务流程智能化改造

智能工单处理&#xff0c;达观数据助力运营商实现业务流程智能化改造 https://m.sohu.com/a/466386308_383123 智能工单处理&#xff0c;达观数据助力运营商实现业务流程智能化改造 达观数据 05-14 14:04 订阅 运营商一线业务运营亟待智能化改造 近几年&#xff0c;运营商领域…

CV和NLP中的无监督预训练(生成式BERT/iGPT和判别式SimCLR/SimCSE)

文 | Smarter在之前的文章中讲过unsupervised learning主要分为生成式和判别式&#xff0c;那么unsupervised pretrain自然也分为生成式和判别式。目前CV和NLP都出现了非常强大的无监督预训练&#xff0c;并且在生成式和判别式都各有造诣&#xff0c;本文主要想归纳一下CV和NLP…

Android Binder漏洞挖掘技术与案例分享

本文由作者根据其在KCon 2016黑客大会上的演讲内容整理而成。演讲稿链接&#xff1a;Binder fuzzing based on drozer。 文章开始&#xff0c;先来看几个我在工作生活中发现的Android漏洞。其中包括Android系统锁屏密码绕过&#xff08;影响了所有安全补丁在2016年10月份以前的…

Transformer太深不行?NUS字节发现注意力坍缩,提出重注意机制!

文 | 陈萍、杜伟源 | 机器之心CNN 通过堆叠更多的卷积层来提高性能&#xff0c;而 transformer 在层次更深时会很快进入饱和。基于此&#xff0c;来自新加坡国立大学和字节跳动 AI Lab 的研究者引入了 Re-attention 机制&#xff0c;以很小的计算代价重新生成注意力图以增强各层…

LeetCode 212. 单词搜索 II(Trie树+DFS)

1. 题目 给定一个二维网格 board 和一个字典中的单词列表 words&#xff0c;找出所有同时在二维网格和字典中出现的单词。 单词必须按照字母顺序&#xff0c;通过相邻的单元格内的字母构成&#xff0c;其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的…

研究综述 | 多关系知识图谱表示学习综述

作者 | CHEONG转载自公众号 | 机器学习与自然语言处理本文分享一篇多关系知识图谱表示学习汇报ppt&#xff0c;介绍近几年及2020新出的共七篇处理异质图的模型。先列出该汇报ppt中将要介绍的七篇论文&#xff1a;Motivation异质知识图谱研究的对象便是如何处理多关系知识图谱&a…

PaddleHub教程合集

原文链接&#xff1a;https://aistudio.baidu.com/aistudio/projectdetail/2168053 PaddleHub教程合集 PaddleHub是基于PaddlePaddle生态下的预训练模型管理和迁移学习工具&#xff0c;可以结合预训练模型更便捷地开展迁移学习工作。通过PaddleHub&#xff0c;您可以&#xff1…

人物志 | KDD Cup 2017双料冠军燕鹏

2017年数据挖掘领域最有影响力的赛事KDD Cup近日揭晓&#xff0c;Convolution队从全球70个国家的3582支队伍里脱颖而出&#xff0c;包揽两项任务的冠军。这支双料冠军队成员名单里&#xff0c;有一个我们熟悉的名字——美团点评高级技术专家燕鹏。 说燕鹏可能大家并不一定知道&…

论文浅尝 - IJCAI2020 | KGNN:基于知识图谱的图神经网络预测药物与药物相互作用...

转载公众号 | AI TIME 论道药物间相互作用&#xff08;DDI&#xff09;预测是药理学和临床应用中一个具有挑战性的问题&#xff0c;在临床试验期间&#xff0c;有效识别潜在的DDI对患者和社会至关重要。现有的大多数方法采用基于AI的计算模型&#xff0c;通常倾向于集成多个数…

LeetCode 79. 单词搜索(回溯DFS)

1. 题目 给定一个二维网格和一个单词&#xff0c;找出该单词是否存在于网格中。 单词必须按照字母顺序&#xff0c;通过相邻的单元格内的字母构成&#xff0c;其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。 示例: board [[…