javascript
springboot需要tomcat服务器吗_嵌入式 Tomcat AJP 协议对 SpringBoot 应用的影响
前言
2020 年 1 月 6 日,國(guó)家信息安全漏洞共享平臺(tái)(CNVD)收錄了由北京長(zhǎng)亭科技有限公司發(fā)現(xiàn)并報(bào)送的 Apache Tomcat 文件包含漏洞。Tomcat AJP 協(xié)議由于存在實(shí)現(xiàn)缺陷導(dǎo)致相關(guān)參數(shù)可控,攻擊者利用該漏洞可通過(guò)構(gòu)造特定參數(shù),讀取服務(wù)器 webapp 下的任意文件。若服務(wù)器端同時(shí)存在文件上傳功能,攻擊者可進(jìn)一步實(shí)現(xiàn)遠(yuǎn)程代碼的執(zhí)行。
Tomcat AJP 協(xié)議的這種實(shí)現(xiàn)缺陷,對(duì)于 SpringBoot 開(kāi)發(fā)的 web 應(yīng)用是否受影響了?且聽(tīng)筆者一一道來(lái)。
AJP 協(xié)議
根據(jù) apache 官網(wǎng) AJP 協(xié)議相關(guān)介紹:
The original document was written by Dan Milstein, danmil@shore.net on December 2000.
Overview of the protocol
The ajp13 protocol is packet-oriented. A binary format was presumably chosen over the more readable plain text for reasons of performance. The web server communicates with the servlet container over TCP connections. To cut down on the expensive process of socket creation, the web server will attempt to maintain persistent TCP connections to the servlet container, and to reuse a connection for multiple request/response cycles.
Once a connection is assigned to a particular request, it will not be used for any others until the request-handling cycle has terminated. In other words, requests are not multiplexed over connections. This makes for much simpler code at either end of the connection, although it does cause more connections to be open at once.
AJP13 協(xié)議全稱(chēng)為 Apache JServ Protocol version 1.3 ,最初由 Dan Milstein 于2000年12月發(fā)表,后被 Apache 軟件基金會(huì) Jakarta 項(xiàng)目采納。
AJP13 是一種二進(jìn)制 TCP 傳輸協(xié)議,通過(guò)在網(wǎng)絡(luò)傳輸二進(jìn)制包(packet)來(lái)完成 Tomcat 與 http 服務(wù)器的請(qǐng)求與響應(yīng),顯然這種方式比純文本(如 text、xml等)傳輸?shù)?http 協(xié)議效率要高的多。
但是 AJP13 協(xié)議對(duì)瀏覽器支持不夠好,由于其定位是 “ Tomcat 與 HTTP 服務(wù)器之間通信的協(xié)議”,這或許是 http 協(xié)議比較盛行的原因吧。
說(shuō)完了 Tomcat AJP13 協(xié)議的來(lái)龍去脈,接著,進(jìn)入主題。
SpringBoot 為什么這么火?
spring-boot-starter-web
對(duì) Spring 比較熟悉的話, 基于 SpringBoot 開(kāi)發(fā) web 應(yīng)用時(shí),引入 spring-boot-starter-web 組件是必不可少的,spring-boot-starter-web 的職責(zé)是負(fù)責(zé) web 應(yīng)用的啟動(dòng) 、初始化、運(yùn)行和停止。而 spring-boot-starter-web 組件之所以能夠跑起來(lái),少不了 http 協(xié)議的支持,典型代表就是 tomcat 服務(wù)器。
通過(guò)翻閱 spring-boot-starter-web 組件的相關(guān) maven 依賴(lài),我們找到了 spring-boot-starter-tomcat 組件。
??org.springframework.boot?? spring-boot-starter-tomcat ??2.1.5.RELEASE??compile接著查找 spring-boot-starter-tomcat 的 maven 依賴(lài), 其引入了 tomcat-embed-core 、tomcat-embed-el 、tomcat-annotations-api 等嵌入式組件。
javax.annotation javax.annotation-api 1.3.2compileorg.apache.tomcat.embed tomcat-embed-core 9.0.19compile tomcat-annotations-api org.apache.tomcatorg.apache.tomcat.embed tomcat-embed-el 9.0.19compileorg.apache.tomcat.embed tomcat-embed-websocket 9.0.19compile正是由于這些嵌入式組件的加入,免去了 Tomcat 單獨(dú)安裝部署的繁雜步驟,我想這也是 SpringBoot 非常火的原因之一吧。
SpringBoot 對(duì) AJP 協(xié)議的支持
通過(guò)閱讀 tomcat-embed-core 組件,說(shuō)明嵌入式 tomcat 是支持 AJP 協(xié)議的,相關(guān)代碼在 org.apache.coyote.ajp 目錄下。
但是奇怪的是,在 SpringBoot 的 yml 文件配置中,并沒(méi)有找到 ajp 協(xié)議相關(guān)的 server 參數(shù)配置。
筆者猜測(cè),雖然 Tomcat 集成了 ajp 協(xié)議,但是不推薦使用吧。但是有沒(méi)有辦法支持 ajp 協(xié)議了,答案是可以的,具體代碼如下:
@Configurationpublic class AJPConfig { private static final String PROTOCOL = "AJP/1.3"; @Value("${tomcat.ajp.port:8009}") private int ajpPort; @Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory(); Connector ajpConnector = new Connector(); ajpConnector.setProtocol(PROTOCOL); ajpConnector.setPort(ajpPort); tomcat.addAdditionalTomcatConnectors(ajpConnector); return tomcat; }}筆者上述配置需要依賴(lài) SpringBoot 1.x 環(huán)境。
AjpProcessor
如果對(duì) tomcat 架構(gòu)比較了解的話,tomcat 大部分請(qǐng)求與響應(yīng)由協(xié)議(Protocol)中的處理器(Processor)完成的。
所以在 tomcat 服務(wù)器接收 ajp 請(qǐng)求時(shí), 由 AjpProcessor 來(lái)處理 ajp 真實(shí)的 request 請(qǐng)求消息。
然后,通過(guò) prepareRequest 方法將 ajp 請(qǐng)求內(nèi)容取出來(lái),設(shè)置成 request 對(duì)象的 Attribute 屬性
因此,黑客通過(guò)設(shè)置 request_uri、path_info 、servlet_path 屬性值,從而可以讀取到 /WEB-INF 下面的所有敏感文件,不限于class、xml、jar等文件。
javax.servlet.include.request_urijavax.servlet.include.path_infojavax.servlet.include.servlet_path至于漏洞的詳細(xì)分析不是本文重點(diǎn),傳送門(mén):
https://blog.csdn.net/u012206617/article/details/104416626/
總結(jié)
以上是生活随笔為你收集整理的springboot需要tomcat服务器吗_嵌入式 Tomcat AJP 协议对 SpringBoot 应用的影响的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java controller json
- 下一篇: gradle idea java ssm