shiro-cve-2020-17523 漏洞的两种绕过姿势分析(带漏洞环境)

Overview

Apache Shiro 两种姿势绕过认证分析(CVE-2020-17523)

0x01 漏洞描述

Apache Shiro是一个强大且易用的Java安全框架,执行身份验证、授权、密码和会话管理。使用Shiro的易于理解的API,您可以快速、轻松地获得任何应用程序,从最小的移动应用程序到最大的网络和企业应用程序。

当它和 Spring 结合使用时,在一定权限匹配规则下,攻击者可通过构造特殊的 HTTP 请求包完成身份认证绕过。

影响范围:Apache Shiro < 1.7.1

0x02 漏洞环境搭建

shiro 1.7.0

https://github.com/jweny/shiro-cve-2020-17523 两种姿势的漏洞环境均已更新。

0x03 poc测试

姿势一:

http://127.0.0.1:8080/admin/%20http://127.0.0.1:8080/admin/%20/

使用空格等空字符,可绕过shiro身份验证。

image-20210205120522547

姿势二:

经过和p0desta师傅交流,发现还有另一种特殊场景下的利用方式。

http://127.0.0.1:8080/admin/%2ehttp://127.0.0.1:8080/admin/%2e/

但是.(还有/)在Spring的路径匹配的规则中是代表路径分隔符的,不作为普通字符进行匹配。因此在默认条件下访问 /admin/.会返回404。

但是在开启全路径的场景下setAlwaysUseFullPath(true)是可以正常匹配的。

image-20210205102100797

0x04 漏洞分析

Shiro中对于URL的获取及匹配在org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver#getChain

先简单看下这个getChain方法:

carbon (2)

image-20210205103341569

该方法先检查requestURI是否以/结尾,如果是,就删掉最后一个/

然后在匹配路径的循环中,会先判断下路径规则pathPattern是否以/结尾,如果是也会删除。然后再去调用pathMatches()方法进行路径匹配。

因此两种利用方式中,是否以/结尾都没有关系,因为开始经过getChain方法就会被删除。

4.1 空格绕过分析

关注下pathMatches()方法:

调出Evaluate,分别计算一下pathMatches("/admin/*","/admin/1")pathMatches("/admin/*","/admin/ "),前者正常匹配,后者匹配失败。

image-20210203134044268

image-20210203134119174

开始调试,调试开始会经过一阵漫长的F7。一直到doMatch("/admin/*","/admin/ ")。可见,tokenizeToStringArray返回的pathDirs已经没有第二层路径了。因此会导致/admin/* /admin 不匹配。

image-20210203150854085

跟一下tokenizeToStringArray方法,发现其调用tokenizeToStringArray方法时的trimTokens参数为true。

image-20210203150959413

tokenizeToStringArray方法,在参数trimTokens为true时,会经过trim()处理,因此导致空格被清除。再次返回getChain时最后一个/被删除。因此tokenizeToStringArray返回的pathDirs没有第二层路径。

image-20210203151053344

总结一下:存在漏洞的shiro版本,由于调用tokenizeToStringArray方法时,trimTokens参数默认为true,空格会经过trim()处理,因此导致空格被清除。再次返回getChain时最后一个/被删除,所以/admin/admin/*匹配失败,导致鉴权绕过。而Spring接受到的访问路径为/admin/%20,按照正常逻辑返回响应,因此导致权限被绕过。

4.2 /./绕过分析

看到第二种姿势的/././,是不是想起了某个熟悉方法?没错,就是normalize()

carbon (3)

简单翻译下就是:

条件 示例
正斜杠处理成反斜杠 \ -> /
双反斜杠处理成反斜杠 // -> /
以/.或者/..结尾,则在结尾添加/ /. -> /./ /.. -> /../
归一化处理/./ /./ -> /
路径跳跃 /aaa/../bbb -> /bbb

所以/admin/.在被处理成/admin/./之后变成了/admin/

image-20210205113301788

在经过org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver#getChain处理,由于/结尾,如果是,就删掉最后一个/,变成了/admin。``/admin/admin/*`不匹配,因此绕过了shiro鉴权。

image-20210205113518970

而此时Spring收到的请求为/admin/.。**如果没有开启全路径匹配的话,在Spring中./是作为路径分隔符的,不参与路径匹配。**因此会匹配不到mapping,返回404。

image-20210205114350972

开启全路径匹配的话,会匹配整个url,因此Spring返回200。

这里附上开启全路径匹配的代码:

@SpringBootApplication
public class SpringbootShiroApplication extends SpringBootServletInitializer implements BeanPostProcessor {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(SpringbootShiroApplication.class);
    }

    public static void main(String[] args) {

        SpringApplication.run(SpringbootShiroApplication.class, args);
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName)
            throws BeansException {
        if (bean instanceof RequestMappingHandlerMapping) {
            ((RequestMappingHandlerMapping) bean).setAlwaysUseFullPath(true);
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName)
            throws BeansException {
        return bean;
    }
}

0x05 官方的修复方案

经过以上的分析,造成shiro权限绕过的原因有两个:

  1. tokenizeToStringArray函数没有正确处理空格。
  2. 处理最后一个/的逻辑,不应在循环匹配路径的逻辑之前。

因此官方的修复方案为:

https://github.com/apache/shiro/commit/0842c27fa72d0da5de0c5723a66d402fe20903df

  1. tokenizeToStringArraytrimTokens参数置为false。image-20210203154342100
  2. 调整删除最后一个/的逻辑。修改成先匹配原始路径,匹配失败后再去走删除最后一个/的逻辑。image-20210205115522098

0x06 关于trim

原理上来说trim()会清空字符串前后所有的whitespace,空格只是其中的一种,但是在测试中发现除了空格以外的其他whitespace,例如%08%09%0a,spring+tomcat 处理时都会返回400。

因此第一种姿势除了空格,尚未发现其他好用的payload。

0x07 参考

https://github.com/apache/shiro/commit/0842c27fa72d0da5de0c5723a66d402fe20903df

https://www.anquanke.com/post/id/216096

https://www.cnblogs.com/syp172654682/p/9257282.html

You might also like...

【多模块微服务脚手架平台——Ancba】前后端分离架构SpringBoot 2.x、SpringCloud、SpringAdmin、Spring Security、Mybatis-plus、(Shiro)、JWT、Feign、Nacos、Knif4j等。

【多模块微服务脚手架平台——Ancba】前后端分离架构SpringBoot 2.x、SpringCloud、SpringAdmin、Spring Security、Mybatis-plus、(Shiro)、JWT、Feign、Nacos、Knif4j等。

Ancba 打造Blog.Core项目的SpringBoot微服务版,但是更强大 👏 Ancba (Another New CLI By Alacrity) 另一个全新的敏捷脚手架(单体/模块化/微服务都可支持)。 核心知识点与进度 📣 在 ..../resources/application-

Nov 29, 2022

ShiroExploit 是一款 Shiro 可视化利用工具,集成密钥爆破,命令回显内存马注入等功能

ShiroExploit 是一款 Shiro 可视化利用工具,集成密钥爆破,命令回显内存马注入等功能

ShiroExploit Shiro 可视化利用工具(beta 免责声明 该项目仅供合法的渗透测试以及爱好者参考学习,请各位遵守《中华人民共和国网络安全法》以及相应地方的法律,禁止使用该项目进行违法操作,否则自行承担相关责任 目前已实现: 支持密钥爆破以及 CBC/GCM 两种加密模式 可修改特征头

Jan 25, 2022

A personal blog based on Vue+SpringBoot+MySql+Redis+Shiro+JWT

A personal blog based on Vue+SpringBoot+MySql+Redis+Shiro+JWT

项目:Vue-SpringBoot-PersonalBlog 个人博客网址:http://www.huchao.vip/blogs CSDN:毛_三月 介绍 一个基于SpringBoot + Vue+MybatisPlus+Shiro+JWT+Redis开发的前后端分离博客项目,带有超级详细开发文档

Dec 20, 2022

CVE-2021-2109 && Weblogic Server RCE via JNDI

CVE-2021-2109 && Weblogic Server RCE via JNDI

Description Vulnerability in the Oracle WebLogic Server product of Oracle Fusion Middleware (component: Console). Supported versions that are affected

Nov 21, 2022

openam-CVE-2021-35464 tomcat 执行命令回显

openam-CVE-2021-35464 tomcat 执行命令回显

openam CVE-2021-35464 tomcat 执行命令回显. 项目基于 ysoserial 和 Java-Rce-Echo 构建项目需要在依赖中加入ysoserial.jar和jato-14.6.3.jar POST /OpenAM/ccversion/Version HTTP/1.1

Dec 15, 2022

Apache Log4j2 CVE-2021-44228 RCE Demo with RMI and LDAP

Apache Log4j2 CVE-2021-44228 RCE Demo with RMI and LDAP

CVE-2021-44228-Demo 利用 CVE-2021-44228,通过 RMI 和 LDAP 两种方式远程注入代码的示例。 Exploit class from RMI Server loaded Hello, ${jndi:rmi://127.0.0.1:1099/exploit} Ex

Dec 14, 2021

log4j2 Log4Shell CVE-2021-44228 proof of concept

log4j2 Log4Shell CVE-2021-44228 proof of concept

Log4Shell CVE-2021-44228 proof of concept Requirement Java (JDK/JRE) 8 or later version curl exploitable Simple spring boot application that serves a

Dec 21, 2021

Spring Cloud Netflix Hystrix Dashboard template resolution vulnerability CVE-2021-22053

CVE-2021-22053: Spring Cloud Netflix Hystrix Dashboard template resolution vulnerability Severity High Vendor Spring by VMware Description Application

Dec 16, 2022

Small example repo for looking into log4j CVE-2021-44228

log4j CVE-2021-44228 Lame useless repo to look into log4j CVE-2021-44228. Setup The repository contains a .idea/ folder which is a IntelliJ IDEA proje

Dec 13, 2022
Comments
  • 关于第三种姿势

    关于第三种姿势

    http://127.0.0.1:8080/admin 直接就能过


    个人觉得这个bug利用空间不大,一是Shiro配置admin下所有的路径一般是map.put("/admin/**", "authc");这么写; 二是SpringBoot里,我一般也不会这样写@GetMapping("/admin/*")

    opened by insistandinsist 1
Owner
Qihoo360 CloudSec Team. Security Engineer or Developer
null
Original FishHack from the very beginning of 2020 (WARNING: VERY BAD CODE)

FishHack Original FishHack from the very beginning of 2020 (when I did not know what was I doing) Credits to BleachHack for my first ever coding knowl

null 3 Dec 2, 2022
Spring 2019-2020 Java Programming course lab -- Chongqing University. Include my source codes and lab reports.

JAVA_GUI_File_Manager Spring 2019-2020 JAVA Programming course homeworks -- Chongqing University. Include my source codes and reports. Contents: Draw

Chase/Jiaxuan Cai 6 Nov 11, 2022
Spring 2019-2020 Java Programming course lab -- Chongqing University. Include my source codes and lab reports.

JAVA_GUI_File_Manager Spring 2019-2020 JAVA Programming course homeworks -- Chongqing University. Include my source codes and reports. Contents: achie

Chase/Jiaxuan Cai 6 Jun 29, 2022
Robot code for FRC 2020 challenge

frc-code Robot code for FRC 2020 challenge STUFF I FOUND ABOUT THE GYROSCOPE also we all need to read the docs from last year. this Ian guy is our god

null 1 Jan 22, 2022
The Java implementation of "A Survey of Trajectory Distance Measures and Performance Evaluation". VLDBJ 2020

A Survey of Trajectory Distance Measures and Performance Evaluation The Java implementation of the following paper: Han Su, Shuncheng Liu, Bolong Zhen

Shuncheng Liu 99 Oct 19, 2022
springboot 框架与其它组件结合如 jpa、mybatis、websocket、security、shiro、cache等

致歉 由于自己懒以及身体对issuse 解决的不及时。请大家以后提issuse 的时候写清楚 模块名 比如“springboot-SpringSecurity4” 和问题,我会抽时间抓紧解决。 springboot-SpringSecurity0 包含两部分代码: 第一是 博客 springboot

abel 5.9k Jan 5, 2023
循序渐进,学习Spring Boot、Spring Boot & Shiro、Spring Batch、Spring Cloud、Spring Cloud Alibaba、Spring Security & Spring Security OAuth2,博客Spring系列源码:https://mrbird.cc

Spring 系列教程 该仓库为个人博客https://mrbird.cc中Spring系列源码,包含Spring Boot、Spring Boot & Shiro、Spring Cloud,Spring Boot & Spring Security & Spring Security OAuth2

mrbird 24.8k Jan 6, 2023
提供一套基于Spring Boot-Shiro-Vue的权限管理思路.前后端都加以控制,做到按钮/接口级别的权限

Spring Boot-Shiro-Vue 提供一套基于SpringBoot-shiro-vue的权限管理思路. 前后端都加以控制,做到按钮/接口级别的权限 DEMO 测试地址 admin/123456 管理员身份登录,可以新增用户,角色. 角色可以分配权限 控制菜单是否显示,新增/删除按钮是否显示

null 4.2k Jan 5, 2023
Guns基于SpringBoot 2,致力于做更简洁的后台管理系统,完美整合springmvc + shiro + mybatis-plus + beetl!Guns项目代码简洁,注释丰富,上手容易,同时Guns包含许多基础模块(用户管理,角色管理,部门管理,字典管理等10个模块),可以直接作为一个后台管理系统的脚手架!

Guns基于Spring Boot2,致力于做更简洁的后台管理系统。包含系统管理,代码生成,多数据库适配,SSO单点登录,工作流,短信,邮件发送,OAuth2登录,任务调度,持续集成,docker部署等功。支持Spring Cloud Alibaba微服务。社区活跃,版本迭代快,加群免费技术支持。

冯硕楠 3.6k Jan 5, 2023