本文转载自微信公众号「Kirito的技术分享」,作者kiritomoe。转载本文请联系Kirito的技术分享公众号。

目前创新互联已为近1000家的企业提供了网站建设、域名、网络空间、网站托管、服务器托管、企业网站设计、永仁网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
一年一度的 HW 行动开始了,最近也是被各种安全漏洞搞的特别闹心,一周能收到几十封安全团队扫描出来的漏洞邮件,这其中有一类漏洞很容易被人忽视,但影响面却极广,危害也极大,我说出它的名字你应该也不会感到陌生,正是 Spring Boot Actuator 。
写这篇文章前,我跟我的朋友做了一个小调查,问他们对 Spring Boot Actuator 的了解,结果惊人的一致,大家都知道 Spring Boot 提供了 spring-boot-starter-actuator 的自动配置,但却很少有人真正用到它相关的特性。在继续往下面看这篇文章时,大家也可以先思考下几个问题:
好久没翻过 spring 的文档了,为了解释这个还算陌生的名词 Actutor,我特地去翻了下它的文档,找到了官方的定义
Definition of Actuator
An actuator is a manufacturing term that refers to a mechanical device for moving or controlling something. Actuators can generate a large amount of motion from a small change.
好家伙,看了等于白看,以我 CET-6 的水平,理解这段话着实有点难度,希望能有英语比较好的同学帮我翻译下。只能按照我个人对 Spring Boot Actuator 功能的理解来意译下了:我们可以借助于 Spring Boot Actuator 来对 Spring Boot 应用的健康状态、环境配置、Metrics、Trace、Spring 上下文等信息进行查看,除了一系列查看功能之外,它还实现了 Spring Boot 应用的上下线和内存 dump 功能。
第一步 引入依赖
tips:spring-boot-starter-actuator 在不同版本 Spring Boot 中有一定的配置差异,本文采用的是目前最新的 2.4.4 版本
org.springframework.boot spring-boot-starter-actuator 2.4.4 
第二步 了解 endpoint
endpoint 是我们使用 Spring Boot Actuator 最需要关心的对象,列举一些你可能感兴趣的 endpoint
| ID | Description | 
|---|---|
| beans | 查看 Spring 容器中的所有对象 | 
| configprops | 查看被 @ConfigurationProperties 修饰的对象列表 | 
| env | 查看 application.yaml 配置的环境配置信息 | 
| health | 健康检查端点 | 
| info | 应用信息 | 
| metrics | 统计信息 | 
| mappings | 服务契约 @RequestMapping 相关的端点 | 
| shutdown | 优雅下线 | 
例如 health,只需要访问如下 endpoint 即可获取应用的状态
- curl "localhost:8080/actuator/health"
 
第三步 了解 endpoint 的 enable 和 exposure 状态
Spring Boot Actuator 针对于所有 endpoint 都提供了两种状态的配置
enabled 不启用时,相关的 endpoint 的代码完全不会被 Spring 上下文加载,所以 enabled 为 false 时,exposure 配置了也无济于事。
几个典型的配置示例如下
启用并暴露所有 endpoint
- management:
 - endpoints:
 - web:
 - exposure:
 - include: "*"
 - endpoint:
 - shutdown:
 - enabled: true
 
只启用并暴露指定 endpoint
- management:
 - endpoints:
 - enabled-by-default: false
 - endpoint:
 - info:
 - enabled: true
 - endpoints:
 - web:
 - exposure:
 - include: "info"
 
禁用所有 endpoint
- management:
 - endpoints:
 - enabled-by-default: false
 
或者,去除掉 spring-boot-starter-actuator 依赖!
从上文的介绍可知,有一些 Spring Boot Actuator 提供的 endpoint 是会将应用重要的信息暴露出去的,以 env 为例来感受下一个典型的 application.yaml 的示例。
- server:
 - port: 8080
 - spring:
 - datasource:
 - url: jdbc:mysql://testDbHost:3306/kirito
 - username: kirito
 - password: 123456
 - kirito:
 - ak: kirito@xxx_ak
 - sk: kirito@xxx_sk
 - management:
 - endpoints:
 - web:
 - exposure:
 - include: "*"
 
上面的配置再经典不过,我们看看访问 localhost:8080/actuator/env 之后的返回值
- {
 - "activeProfiles": [],
 - "propertySources": [
 - {
 - "name": "server.ports",
 - "properties": {
 - "local.server.port": {
 - "value": 8080
 - }
 - }
 - },
 - {
 - "name": "Config resource 'class path resource [application.yaml]' via location 'optional:classpath:/'",
 - "properties": {
 - "server.port": {
 - "value": 8080,
 - "origin": "class path resource [application.yaml] - 2:9"
 - },
 - "spring.datasource.url": {
 - "value": "jdbc:mysql://testDbHost:3306/kirito",
 - "origin": "class path resource [application.yaml] - 5:44"
 - },
 - "spring.datasource.username": {
 - "value": "kirito",
 - "origin": "class path resource [application.yaml] - 6:15"
 - },
 - "spring.datasource.password": {
 - "value": "******",
 - "origin": "class path resource [application.yaml] - 7:15"
 - },
 - "kirito.ak": {
 - "value": "kirito@xxx_ak",
 - "origin": "class path resource [application.yaml] - 10:7"
 - },
 - "kirito.sk": {
 - "value": "kirito@xxx_sk",
 - "origin": "class path resource [application.yaml] - 11:7"
 - },
 - "management.endpoints.web.exposure.include": {
 - "value": "*",
 - "origin": "class path resource [application.yaml] - 17:18"
 - }
 - }
 - }
 - ]
 - }
 
可以发现,对于内置的敏感配置信息 spring.datasource.password,Spring Boot Actuator 是进行了脱敏的,但是对于自定义的一些敏感配置,如 kirito.ak 和 kirito.sk 却被暴露出来了。
可能有的读者会立马提出质疑:我们的机器都部署内网,并且一般都是通过反向代理对外暴露的服务,这类 endpoint 是不会被外部用户访问到的。那我只能说太天真了,例如以下情况都是导致安全漏洞的真实 case:
针对 Spring Boot Actuator 提供的 endpoint,采取以下几种措施,可以尽可能降低被安全攻击的风险
今天,你使用 Spring Boot Actuator 了吗?
                网页标题:警惕SpringBootActuator引发的安全问题
                
                地址分享:http://www.csdahua.cn/qtweb/news31/307131.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网