简单看下最近的Spring Secrurity、Spring漏洞(CVE-2024-22234、CVE-2024-22243)

2024-03-04 0 1,378

最近的这两个cve我看国内很多情报将其评为高危,所以想着去看看原理看完发现都比较简单,利用要求的场景也相对有限(特别是第一个),所以就随便看下就行了

Spring Security 用户认证绕过(CVE-2024-22234)

先看下官网的公告(https://spring.io/security/cve-2024-22234)

In Spring Security, versions 6.1.x prior to 6.1.7 and versions 6.2.x prior to 6.2.2, an application is vulnerable to broken access control when it directly uses the AuthenticationTrustResolver.isFullyAuthenticated(Authentication) method.

Specifically, an application is vulnerable if:

  • The application uses AuthenticationTrustResolver.isFullyAuthenticated(Authentication) directly and a null authentication parameter is passed to it resulting in an erroneous true return value.

An application is not vulnerable if any of the following is true:

  • The application does not use AuthenticationTrustResolver.isFullyAuthenticated(Authentication) directly.
  • The application does not pass null to AuthenticationTrustResolver.isFullyAuthenticated
  • The application only uses isFullyAuthenticated via Method Security or HTTP Request Security

大概意思是直接调用``AuthenticationTrustResolver.isFullyAuthenticated(Authentication)` ,若Authentication为null,则方法会永远返回真,从而产生一些与预期相反的结果。

AuthenticationTrustResolver 接口中的 isFullyAuthenticated 方法用于检查 Authentication 对象是否完全经过身份验证,即是否不是匿名用户。在 Spring Security 中,可以使用这个方法来确定用户是否已经进行了完整的身份验证。

影响版本为:

  • 6.1.0 to 6.1.6
  • 6.2.0 to 6.2.1

环境搭建

引入pom,实际调用:

<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>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

增加下密码验证和/index的无鉴权的配置(交给应用手动配置)

@Configuration
@EnableWebSecurity
public class WebSecurityConfig {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
                .authorizeHttpRequests((requests) -> requests
                        .requestMatchers("/", "/index").permitAll()  // 端点/、/index 无需鉴权,交给应用直接控制
                        .anyRequest().authenticated()
                )
                .formLogin((form) -> form
                        .loginPage("/login")
                        .permitAll()
                )
                .logout((logout) -> logout.permitAll());

        return http.build();
    }

    @Bean
    public UserDetailsService userDetailsService() {
        UserDetails user =
                User.withDefaultPasswordEncoder()
                        .username("user")
                        .password("password")
                        .roles("USER")
                        .build();

        return new InMemoryUserDetailsManager(user);
    }
}

新增控制器并配置需要用户手动输入密码(isFullyAuthenticated)后才能访问的逻辑:

    @GetMapping("/index")
    @ResponseBody
    public String index(){

        // CVE-2024-22234

        // 获取当前的认证对象
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        System.out.println(authentication);

        // 创建 AuthenticationTrustResolver 实例
        AuthenticationTrustResolver trustResolver = new AuthenticationTrustResolverImpl();

        // 使用 isFullyAuthenticated 方法检查是否完全经过身份验证
        boolean fullyAuthenticated = trustResolver.isFullyAuthenticated(authentication);  // 传递null返回即为true
        String msg = "";
        if (fullyAuthenticated) {
            msg = "用户已完全经过身份验证";
        } else {
            msg = "用户可能是匿名用户或者仅部分经过身份验证";
        }
        return msg;
    }

复现

正常情况下,如果没有经过认证,返回的页面为:

简单看下最近的Spring Secrurity、Spring漏洞(CVE-2024-22234、CVE-2024-22243)

进入登录页面正常登录后

简单看下最近的Spring Secrurity、Spring漏洞(CVE-2024-22234、CVE-2024-22243)

返回的页面为:

简单看下最近的Spring Secrurity、Spring漏洞(CVE-2024-22234、CVE-2024-22243)

如果开发在某些情况,比如手动清除SecurityContextHolder中的Authentication信息或通过异步处理导致在异步线程中没有可用的信息getAuthentication()返回null, 则会导致认证校验的失效,我们这里为了复现就手动置为null,

boolean fullyAuthenticated = trustResolver.isFullyAuthenticated(null);

重启应用,在不登陆的情况下,重新访问/index ,发现isFullyAuthenticated已经直接返回了true 。访问鉴权后的页面

![image-20240227115847155](/Users/shellfeel/Library/Application Support/typora-user-images/image-20240227115847155.png)

修复

修复方式也比较简单在isFullyAuthenticated 中增加了对authentication对象为空的判断

简单看下最近的Spring Secrurity、Spring漏洞(CVE-2024-22234、CVE-2024-22243)

Spring Framework SSRF or open redirect( CVE-2024-22243)

Applications that use UriComponentsBuilder to parse an externally provided URL (e.g. through a query parameter) AND perform validation checks on the host of the parsed URL may be vulnerable to a open redirect attack or to a SSRF attack if the URL is used after passing validation checks.

这个看官网描述只知道使用UriComponentsBuilder这个方法来做host校验,会导致重定向和ssrf,粗看下源码不知道是怎么回事,看了下代码更新记录,很简单只是将uri匹配中userinfo匹配的正则表达式去掉[

简单看下最近的Spring Secrurity、Spring漏洞(CVE-2024-22234、CVE-2024-22243)

pre:

private static final String USERINFO_PATTERN = "([^@\\\\[/?#]*)";

now:

private static final String USERINFO_PATTERN = "([^@/?#]*)";

环境搭建

这里假设存在一个场景,后端会将用户输入的url交给UriComponentsBuilder进行验证,通过后进行正常的访问,后端有个简单的黑名单host判断(evil.com) :

String url = "http://xxx.com";
UriComponents uriComponents = UriComponents uriComponents = UriComponentsBuilder.fromUriString(url).build();
String host = uriComponents.getHost();
System.out.println("userinfo: " + uriComponents.getUserInfo());
System.out.println("host: " + host);
// 如果host为 evil.com 则会被拦截
if (host != null && host.equals("evil.com")) {
    System.out.println("403");

}else {
    System.out.println("pass");
}

简单场景,排除使用302、ip、rebind等方式,单纯从UriComponentsBuilder来进行绕过有什么办法?

复现

一般情况下我们知道绕过ssrf会用到@,如果url为http://A.com@B.com ,部分的host校验库会识别这个urlHost为A.com,而浏览器或者http client实际会访问B.com 利用这种差异就能绕过部分黑名单限制,直接访问恶意网站。

试下UriComponentsBuilder 可不可以:

简单看下最近的Spring Secrurity、Spring漏洞(CVE-2024-22234、CVE-2024-22243)

很明显,在这个方法中,直接这么用是不行的,但根据漏洞的修复删除的正则表达式符号来看,我们在userinfo最后增加一个[,测试一下

成功绕过:

简单看下最近的Spring Secrurity、Spring漏洞(CVE-2024-22234、CVE-2024-22243)

简单看下最近的Spring Secrurity、Spring漏洞(CVE-2024-22234、CVE-2024-22243)

不过这样绕过后大部分情况下不能直接使用原url进行访问,因为url中存在[ 会让程序报错:

简单看下最近的Spring Secrurity、Spring漏洞(CVE-2024-22234、CVE-2024-22243)

所以更多利用场景我猜可能是使用UriComponentsBuilder取的host重新进行url拼接来进行访问

总结

Spring Security中这个漏洞可能对于实战利用不大,因为黑盒测未授权都能测试出来不需要什么用户可控的绕过姿势,相对而言Spring Framework这个在实战中对于url可控的地方增加xxx[@yyy.com 可能会有奇效。

资源下载此资源下载价格为1小猪币,终身VIP免费,请先
由于本站资源来源于互联网,以研究交流为目的,所有仅供大家参考、学习,不存在任何商业目的与商业用途,如资源存在BUG以及其他任何问题,请自行解决,本站不提供技术服务! 由于资源为虚拟可复制性,下载后不予退积分和退款,谢谢您的支持!如遇到失效或错误的下载链接请联系客服QQ:442469558

:本文采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可, 转载请附上原文出处链接。
1、本站提供的源码不保证资源的完整性以及安全性,不附带任何技术服务!
2、本站提供的模板、软件工具等其他资源,均不包含技术服务,请大家谅解!
3、本站提供的资源仅供下载者参考学习,请勿用于任何商业用途,请24小时内删除!
4、如需商用,请购买正版,由于未及时购买正版发生的侵权行为,与本站无关。
5、本站部分资源存放于百度网盘或其他网盘中,请提前注册好百度网盘账号,下载安装百度网盘客户端或其他网盘客户端进行下载;
6、本站部分资源文件是经压缩后的,请下载后安装解压软件,推荐使用WinRAR和7-Zip解压软件。
7、如果本站提供的资源侵犯到了您的权益,请邮件联系: 442469558@qq.com 进行处理!

猪小侠源码-最新源码下载平台 Java教程 简单看下最近的Spring Secrurity、Spring漏洞(CVE-2024-22234、CVE-2024-22243) http://www.20zxx.cn/808520/xuexijiaocheng/javajc.html

猪小侠源码,优质资源分享网

常见问题
  • 本站所有资源版权均属于原作者所有,均只能用于参考学习,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,建议提前注册好百度网盘账号,使用百度网盘客户端下载
查看详情

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务