Sploitus

Exploit for Improper Authentication in Apache Shiro

gitee · 2021-06-30

Exploit Code

MARKDOWN91 lines
## https://sploitus.com/exploit?id=32E85746-3B0C-57A1-9377-E1F008EC2BBF
# Apache Shiro 两种姿势绕过认证分析(CVE-2020-17523)

## 0x01 漏洞描述

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

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

影响范围:Apache Shiro   /                 |
| 双反斜杠处理成反斜杠           | // -> /                  |
| 以/.或者/..结尾,则在结尾添加/ | /. -> /./    /.. -> /../ |
| 归一化处理/./                  | /./ -> /                 |
| 路径跳跃                       | /aaa/../bbb ->  /bbb     |

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

![image-20210205113301788](README.assets/10.png)

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

![image-20210205113518970](README.assets/11.png)

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

![image-20210205114350972](README.assets/12.png)

开启全路径匹配的话,会匹配整个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. 将`tokenizeToStringArray`的`trimTokens`参数置为false。![image-20210203154342100](README.assets/13.png)
2. 调整删除最后一个`/`的逻辑。修改成先匹配原始路径,匹配失败后再去走删除最后一个`/`的逻辑。![image-20210205115522098](README.assets/14.png)	

## 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