shiro权限绕过漏洞CVE-2020-1957

0x00 漏洞复现

pom.xml

        
            org.apache.shiro
            shiro-core
            1.4.2
        
        
            org.apache.shiro
            shiro-spring
            1.4.2
        

applications.properties

server.context-path=/test

ShiroConfig

    @Bean
    ShiroFilterFactoryBean shiroFilterFactoryBean() {
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(securityManager());
        bean.setLoginUrl("/login");
        bean.setSuccessUrl("/index");
        bean.setUnauthorizedUrl("/unauthorizedurl");
        Map map = new LinkedHashMap();
        map.put("/hello/*", "authc");
        //map.put("/hello/**", "authc"); //in version 1.7.0 will not trigger CVE-2020-17523
        bean.setFilterChainDefinitionMap(map);
        return bean;
    }

spring

    @RequestMapping("/hello/{name}")
    public String hello2(@PathVariable String name) {
        return "auth hello/{_" + name + "_}, there ";
    }

权限绕过请求,当访问请求如下时:
http://127.0.0.1:8080/test/hello/a/
response为authc
正常请求,当访问请求如下时:
http://127.0.0.1:8080/test/hello/a
response跳转到login

0x01 漏洞分析

在shiro的路径处理中,path为"/hello/a/"时,不与pattern"/hello/*"匹配。(因为在ant风格中,*表示一个或者多个字符,**表示一个或者多个路径)
在spring的框架逻辑中,"/hello/a/"与"/hello/a"映射为相同的资源。
两者处理的不一致导致了,此权限的绕过。
如果配置修改为map.put("/hello/**", "authc"),则不会触发此漏洞。

0x02 1.4.2 v 1.5.2的diff分析

https://github.com/apache/shiro/compare/shiro-root-1.4.2...shiro-root-1.5.2
在PathMatchingFilter.java中,增加了如下代码,去掉了requestURI中结尾的"/",因此能够匹配成功。

        // in spring web, the requestURI "/resource/menus" ---- "resource/menus/" bose can access the resource
        // but the pathPattern match "/resource/menus" can not match "resource/menus/"
        // user can use requestURI + "/" to simply bypassed chain filter, to bypassed shiro protect
        if(requestURI != null && !DEFAULT_PATH_SEPARATOR.equals(requestURI)
                && requestURI.endsWith(DEFAULT_PATH_SEPARATOR)) {
            requestURI = requestURI.substring(0, requestURI.length() - 1);
        }

0x03 1.5.1 v 1.5.2的diff分析

https://github.com/apache/shiro/compare/shiro-root-1.5.1...shiro-root-1.5.2
主要变动点在于修改了WebUtils.java中的getRequestUri方法,修改了uri的获取方式。
在1.5.2版本中,该方法实现如下:

public static String getRequestUri(HttpServletRequest request) {
        String uri = (String) request.getAttribute(INCLUDE_REQUEST_URI_ATTRIBUTE);
        if (uri == null) {
            uri = valueOrEmpty(request.getContextPath()) + "/" +
                  valueOrEmpty(request.getServletPath()) +
                  valueOrEmpty(request.getPathInfo());
        }
        return normalize(decodeAndCleanUriString(request, uri));
    }

    private static String valueOrEmpty(String input) {
        if (input == null) {
            return "";
        }
        return input;
    }

在1.5.1版本中,该方法为:

    public static String getRequestUri(HttpServletRequest request) {
        String uri = (String) request.getAttribute(INCLUDE_REQUEST_URI_ATTRIBUTE);
        if (uri == null) {
            uri = request.getRequestURI();
        }
        return normalize(decodeAndCleanUriString(request, uri));
    }

观察更新的单元测试案例,可以构造相应的payload。(注意此条需要在burp中测试,不知道为啥在浏览器里输入''..''总是会被吞掉)
http://127.0.0.1:8080/test/asdf;/../hello/a
返回的报文如下:
auth hello/{_a_}, there 。

在1.5.1版本中:
request.getRequestURI() == "/test/asdf;/../hello/a"
经过decodeAndCleanUriString方法的作用,会去除掉";"后面的数据,因此,path变成了"/test/asdf",不会匹配上"/hello/*",绕过了权限验证。
在1.5.2版本中:
valueOrEmpty(request.getContextPath()) = "test"
valueOrEmpty(request.getServletPath()) = "/hello/a"
valueOrEmpty(request.getPathInfo()) = ""
因此,不会绕过权限验证。

References

https://www.freebuf.com/vuls/231909.html
https://blog.csdn.net/xuandao_ahfengren/article/details/108218864

你可能感兴趣的:(shiro权限绕过漏洞CVE-2020-1957)