Spring Boot Actuator 是 Spring Boot 提供的一個用于監控和管理應用程序的模塊。它提供了許多有用的端點(endpoints),如健康檢查、環境信息、日志配置等。然而,如果這些端點未經過適當的保護,可能會導致未授權訪問漏洞,從而暴露敏感信息或允許攻擊者執行惡意操作。本文將詳細介紹如何修復 Spring Boot Actuator 未授權訪問漏洞。
Spring Boot Actuator 的端點默認情況下是開放的,這意味著任何人都可以訪問這些端點并獲取應用程序的敏感信息。例如,/actuator/env
端點可以暴露環境變量,/actuator/health
端點可以暴露應用程序的健康狀態。如果這些端點未經過適當的保護,攻擊者可以利用這些信息進行進一步的攻擊。
/actuator/shutdown
)關閉應用程序,導致服務中斷。默認情況下,Spring Boot Actuator 會啟用多個端點。如果某些端點不需要使用,可以通過配置禁用這些端點。
management:
endpoints:
web:
exposure:
include: health,info
exclude: env,beans,metrics
在上述配置中,只暴露了 health
和 info
端點,禁用了 env
、beans
和 metrics
端點。
為了防止未授權訪問,可以為 Actuator 端點啟用身份驗證??梢酝ㄟ^ Spring Security 來實現這一點。
首先,在 pom.xml
中添加 Spring Security 依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
然后,在 application.yml
或 application.properties
中配置 Spring Security:
spring:
security:
user:
name: admin
password: password
接下來,配置 Actuator 端點的訪問權限??梢酝ㄟ^ WebSecurityConfigurerAdapter
來實現:
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/actuator/**").hasRole("ADMIN")
.anyRequest().permitAll()
.and()
.httpBasic();
}
}
在上述配置中,只有具有 ADMIN
角色的用戶才能訪問 /actuator/**
端點。
為了防止信息在傳輸過程中被竊取,建議使用 HTTPS 來保護 Actuator 端點的通信。
可以使用 keytool
生成自簽名 SSL 證書:
keytool -genkeypair -alias myapp -keyalg RSA -keysize 2048 -storetype PKCS12 -keystore myapp.p12 -validity 3650
在 application.yml
或 application.properties
中配置 Spring Boot 使用 HTTPS:
server:
port: 8443
ssl:
key-store: classpath:myapp.p12
key-store-password: changeit
key-password: changeit
可以通過配置防火墻或使用 Spring Security 來限制訪問 Actuator 端點的 IP 地址。
可以通過 WebSecurityConfigurerAdapter
來限制訪問 Actuator 端點的 IP 地址:
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/actuator/**").hasIpAddress("192.168.1.100")
.anyRequest().permitAll()
.and()
.httpBasic();
}
}
在上述配置中,只有 IP 地址為 192.168.1.100
的用戶才能訪問 /actuator/**
端點。
如果默認的 Actuator 端點無法滿足需求,可以自定義端點并控制其訪問權限。
可以通過實現 Endpoint
接口來創建自定義端點:
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;
@Component
@Endpoint(id = "custom")
public class CustomEndpoint {
@ReadOperation
public String custom() {
return "Custom Endpoint";
}
}
可以通過 WebSecurityConfigurerAdapter
來配置自定義端點的訪問權限:
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/actuator/custom").hasRole("ADMIN")
.anyRequest().permitAll()
.and()
.httpBasic();
}
}
在上述配置中,只有具有 ADMIN
角色的用戶才能訪問 /actuator/custom
端點。
Spring Boot Actuator 提供了強大的監控和管理功能,但如果未經過適當的保護,可能會導致未授權訪問漏洞。通過禁用不必要的端點、啟用身份驗證、使用 HTTPS、限制訪問 IP 和使用自定義端點,可以有效地修復 Spring Boot Actuator 未授權訪問漏洞,保護應用程序的安全。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。