侧边栏壁纸
  • 累计撰写 48 篇文章
  • 累计创建 19 个标签
  • 累计收到 7 条评论

目 录CONTENT

文章目录

SpringBoot Actuator未授权访问漏洞的解决方法

轨迹
2024-08-29 / 0 评论 / 0 点赞 / 291 阅读 / 739 字 / 正在检测是否收录...

一、Actuator 是什么

Actuator 是 SpringBoot 提供的用来对应用系统进行自省和监控的功能模块,借助于 Actuator 开发者可以很方便地对应用系统某些监控指标进行查看、统计等。
引入了actuator 相关依赖后一般以

http://ip:端口/项目前缀(如果有的话)/actuator/

二、未授权访问漏洞是怎么产生的

Actuator在带来方便的同时,如果没有管理好,会导致一些敏感的信息泄露;可能会导致我们的服务器,被暴露到外网,服务器可能会沦陷。
例如:
image
很多关键的信息都会暴露出来,线上服务等同于裸奔

三、解决方案都有哪些

1. 关闭端口(大多数是不会采取这种方案)

在配置文件中添加配置

management:
endpoints:
enabled-by-default: false
# 版本低的配置
# management:
#   endpoints: 
#     enabled: false

大多数是不会采取这种方案,但也不排除生产上感觉actuator没什么实质作用的同学,可以选择关闭暴露端点
但毕竟使用actuator是主观意向,主要是为了配合 SpringAdmin 或 Prometheus 来监控的,很多人都会需求其他的解决方案

2. 网关限制(最简单粗暴)

如果使用nginx作为网关控制,直接屏蔽掉相关接口即可

2.1 nginx 配置文件

屏蔽actuator路径

location ~ .*actuator.* {
   deny all;
}
2.2 k8s-ingress 配置文件

屏蔽actuator路径

metadata:
annotations:
nginx.ingress.kubernetes.io/configuration-snippet: |
      location ~ .*actuator.* {
        deny all;
      }

3. Actuator 开启认证授权(存在代码污染风险)

主要是借助 spring security 来实现的,如果当前项目有使用security,需要自己解决一下合并问题

3.1 引入Maven包坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
3.2 增加Java 权限控制代码
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.firewall.HttpFirewall;
import org.springframework.security.web.firewall.StrictHttpFirewall;
/**
 * Actuator 监控端点权限
 *
 * @author Parker
 */
@Configuration
public class ActuatorSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
        http.httpBasic().and()
                .authorizeRequests()
                    .antMatchers("/actuator/**")
                        .authenticated()
                    .anyRequest()
                        .permitAll();
        http
// 关闭csrf token认证不需要csrf防护
            .csrf().disable()
// 关闭Session会话管理器 JWT 不需要
            .sessionManagement().disable()
// 关闭记住我功能 JWT 不需要
            .rememberMe().disable();
    }
/**
     * 配置地址栏不能识别 // 的情况
     * @return
     */
@Bean
public HttpFirewall allowUrlEncodedSlashHttpFirewall() {
        StrictHttpFirewall firewall = new StrictHttpFirewall();
//此处可添加别的规则,目前只设置 允许双 //
        firewall.setAllowUrlEncodedDoubleSlash(true);
return firewall;
    }
}
3.3 Spirngboot 工程增加配置
# 开启SpringBoot Admin的监控
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
spring:
# 自身权限校验账号密码
security:
user:
name:
password:
# 上报账号密码    
boot:
admin:
client:
url: Admin URL
username:
password:
instance:
metadata:
user.name: ${spring.security.user.name}
user.password: ${spring.security.user.password}
0

评论区