前言

我们被扫了一个漏洞,SpringBoot Actuator 未授权访问,漏洞描述是这样的:
Actuator 是 springboot 提供的用来对应用系统进行自省和监控的功能模块,借助于 Actuator 开发者可以很方便地对应用系统某些监控指标进行查看、统计等。在 Actuator 启用的情况下,如果没有做好相关权限控制,非法用户可通过访问默认的执行器端点(endpoints)来获取应用系统中的监控信息,从而导致信息泄露甚至服务器被接管的事件发生

正文

如果没有对admin的端点进行鉴权,那么对于开放的网关服务,可以直接通过xx/actuator访问,这将是非常危险的,如果你还暴露了所有端点,那么还可以获取环境中的账号密码信息,即使admin做了脱敏。
要对端点进行鉴权,也非常简单,只需要要引入spring-security依赖即可,下面是Spring Cloud Gateway中的配置。

1、引入xml依赖
spring-boot-starter-web scope 是provided,引入gateway中不能有web

 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-security</artifactId>
 </dependency>
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
     <scope>provided</scope>
 </dependency>

2、针对Admin端点认证的配置,只对/actuator/**进行认证,其他地址放行,使用业务自身认证。

package com.frame.ops.admin.client.config;

import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.web.server.SecurityWebFilterChain;

/**
 * 对客户端的actuator接口进行鉴权
 */
@EnableWebFluxSecurity
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
public class ReactiveAdminSecurityConfig {

    @Bean
    @Order(1)
    public SecurityWebFilterChain authorizationServerSecurityFilterChain(ServerHttpSecurity http) {
        http.authorizeExchange()
                //只对actuator接口认证
                .pathMatchers("/actuator/**").authenticated()
                .and().httpBasic()
                .and().csrf().disable().logout().disable();
        return http.build();
    }

}

3、如果是Servlet,配置为

package com.frame.ops.admin.client.config;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.regex.Pattern;

/**
 * 对客户端的actuator接口进行鉴权
 */
@Order(1)
@EnableWebSecurity
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class AdminSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.requestMatchers()
                //只有actuator开头的请求才需要认证,其他接口不走该过滤器
                .antMatchers("/actuator/**")
                .and().authorizeRequests((authorize -> authorize.anyRequest().authenticated()))
                .httpBasic().and().csrf().disable().logout().disable();
    }
}

总结

⁉️ 奇怪的事来了,在本地测试gateway 请求正常,访问/actuator端点需要认证,但是在服务器上测试一些业务接口就会卡住超时,请求也没到下游服务,过段时间后gateway 假死,任何请求不通。

这个问题只发生在gateway中,对应servlet并没有发现这个问题,不清楚跟Gateway 使用Reactive 有没有关系, 这个问题暂未解决。但也不影响处理漏洞,只要去掉admin依赖,actuator依赖就行了。

原因知道了,原来是因为我们的gateway 使用了oauth2鉴权,但是校验token的时候再网关中再次调用了自己,还有获取用户信息的接口,导致gateway 卡死,将代码改成直接用feign调用即可


作者其他要推荐的文章,欢迎来学习:
Prometheus 系列文章

  1. Prometheus 的介绍和安装
  2. 直观感受PromQL及其数据类型
  3. PromQL之选择器和运算符
  4. PromQL之函数
  5. Prometheus 告警机制介绍及命令解读
  6. Prometheus 告警模块配置深度解析
  7. Prometheus 配置身份认证
  8. Prometheus 动态拉取监控服务
  9. Prometheus 监控云Mysql和自建Mysql

Grafana 系列文章,版本:OOS v9.3.1

  1. Grafana 的介绍和安装
  2. Grafana监控大屏配置参数介绍(一)
  3. Grafana监控大屏配置参数介绍(二)
  4. Grafana监控大屏可视化图表
  5. Grafana 查询数据和转换数据
  6. Grafana 告警模块介绍
  7. Grafana 告警接入飞书通知

Logo

一起探索未来云端世界的核心,云原生技术专区带您领略创新、高效和可扩展的云计算解决方案,引领您在数字化时代的成功之路。

更多推荐