CVE-2022-22978,是一个Spring-security身份绕过漏洞
当Spring-security使用 RegexRequestMatcher 进行权限配置,由于RegexRequestMatcher正则表达式配置权限的特性,
正则表达式中包含“.”时,未经身份验证攻击者可以通过构造恶意数据包绕过身份认证。
<?xml version="1.0" encoding="UTF-8"?>
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.7.0
person.xu
vulEnv
0.0.1-SNAPSHOT
cve_2022_22978
Demo project for Spring Boot
1.8
5.6.3
org.springframework.boot
spring-boot-starter-security
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.security
spring-security-test
test
org.springframework.boot
spring-boot-maven-plugin
书写控制器,用来处理请求
package person.xu.vulEnv;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.websocket.server.PathParam;
<span class="label label-primary">@Controller#CTL{n}public</span> class WebController {
<span class="label label-primary">@GetMapping(</span>"/")
<span class="label label-primary">@ResponseBody#CTL{n}</span> public String index() {
return "welcome";
}
<span class="label label-primary">@GetMapping(</span>"/admin/{name}")
<span class="label label-primary">@ResponseBody#CTL{n}</span> public String admin(<span class="label label-primary">@PathVariable</span> String name) {
return "welcome " + name;
}
}
书写spring security的配置类,用来配置spring security的
package person.xu.vulEnv;
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.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;
import static org.springframework.security.config.Customizer.withDefaults;
<span class="label label-primary">@Configuration#CTL{n}</span><span class="label label-primary">@EnableWebSecurity#CTL{n}public</span> class AuthConfig {
<span class="label label-primary">@Bean#CTL{n}</span> public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
// <span class="label label-primary">@formatter</span>:off
http
.authorizeHttpRequests((authorize) -> authorize
.regexMatchers("/admin/.*").authenticated()
)
.httpBasic(withDefaults())
.formLogin(withDefaults());
// <span class="label label-primary">@formatter</span>:on
return http.build();
}
// <span class="label label-primary">@formatter</span>:off
<span class="label label-primary">@Bean#CTL{n}</span> public InMemoryUserDetailsManager userDetailsService() {
UserDetails user = User.withDefaultPasswordEncoder()
.username("user")
.password("password")
.roles("USER")
.build();
return new InMemoryUserDetailsManager(user);
}
// <span class="label label-primary">@formatter</span>:on
}
<span class="label label-primary">@EnableWebSecurity注解</span>:就是表示开启spring security,并且书写类来配置spring security
regexMatchers("/admin/.*"):表示检测的路由为/admin/下面的所以子目录
authenticated():表示需要进行身份验证
authorizeHttpRequests((authorize)->authorize
.regexMatchers("/admin/.*").authenticated())
当在spring security这么进行配置的时候,regexMatchers表示利用正则表达式进行匹配
("/admin/.*")这个就是一个正则表达式,整句话的意思是当访问/admin/下的任意路径
都需要进行检验,但是.*这个正则表达式可以出现绕过!!!
正则表达式中元字符“.”是匹配除换行符(\n、\r)之外的任何单个字符,在java中的正则
默认情况下“.”也同样不会包含\n、\r字符,所以.*这个正则表达式的意思是除换行符(\n、\r)
之外的任何单个字符的n个,当我们使用\n、\r字符,就会导致"/admin/.*"这个正则表达式
无法匹配,从而实现绕过
http://www.baidu.com/admin/%0a
http://www.baidu.com/admin/%0d
http://www.baidu.com/admin/%0a%0d
%0d表示回车。%0a表示换行,这两个都可以进行绕过!
使用idea启动环境
成功绕过
打赏我,让我更有动力~
© 2016 - 2024 掌控者 All Rights Reserved.
Track-魔方
发表于 2023-8-8
文章标题请改一下哈,影响阅读
评论列表
加载数据中...