javascript
【Web安全】JSP内存马研究
前言
最近在研究webshell免殺的問題,到了內(nèi)存馬免殺部分發(fā)現(xiàn)傳統(tǒng)的Filter或者Servlet查殺手段比較多,不太容易實(shí)現(xiàn)免殺,比如有些工具會(huì)將所有注冊(cè)的Servlet和Filter拿出來,排查人員仔細(xì)一點(diǎn)還是會(huì)被查出來的,所以
我們要找一些其他方式實(shí)現(xiàn)的內(nèi)存馬。比如我今天提到的JSP的內(nèi)存馬(雖然本質(zhì)上也是一種Servlet類型的馬) 。
【學(xué)習(xí)資料】
JSP加載流程分析
在Tomcat中jsp和jspx都會(huì)交給JspServlet處理,所以要想實(shí)現(xiàn)JSP駐留內(nèi)存,首先得分析JspServlet的處理邏輯。
<servlet><servlet-name>jsp</servlet-name><servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>...</servlet> ... <servlet-mapping><servlet-name>jsp</servlet-name><url-pattern>*.jsp</url-pattern><url-pattern>*.jspx</url-pattern></servlet-mapping>下面分析JspServlet#service方法,主要的功能是接收請(qǐng)求的URL,判斷是否預(yù)編譯,核心的方法是serviceJspFile。
public void service (HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String jspUri = jspFile;jspUri = (String) request.getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH);if (jspUri != null) {//檢查請(qǐng)求是否是通過其他Servlet轉(zhuǎn)發(fā)過來的String pathInfo = (String) request.getAttribute(RequestDispatcher.INCLUDE_PATH_INFO);if (pathInfo != null) {jspUri += pathInfo;}} else {//獲取ServletPath和pathInfo作為jspUrijspUri = request.getServletPath();String pathInfo = request.getPathInfo();if (pathInfo != null) {jspUri += pathInfo;}}}try {//是否預(yù)編譯boolean precompile = preCompile(request);//核心方法serviceJspFile(request, response, jspUri, precompile);} catch (RuntimeException | IOException | ServletException e) {throw e;} catch (Throwable e) {ExceptionUtils.handleThrowable(e);throw new ServletException(e);}}preCompile中只有當(dāng)請(qǐng)求參數(shù)以jsp_precompile開始才會(huì)進(jìn)行預(yù)編譯,否則不進(jìn)行預(yù)編譯。
boolean preCompile(HttpServletRequest request) throws ServletException {String queryString = request.getQueryString();if (queryString == null) {return false;}// public static final String PRECOMPILE = System.getProperty("org.apache.jasper.Constants.PRECOMPILE", "jsp_precompile");int start = queryString.indexOf(Constants.PRECOMPILE);if (start < 0) {return false;}queryString =queryString.substring(start + Constants.PRECOMPILE.length());if (queryString.length() == 0) {return true; // ?jsp_precompile}if (queryString.startsWith("&")) {return true; // ?jsp_precompile&foo=bar...}if (!queryString.startsWith("=")) {return false; // part of some other name or value}...}那么預(yù)編譯的作用是什么?當(dāng)進(jìn)行預(yù)編譯后會(huì)怎么樣?答案在JspServletWrapper#service中,當(dāng)預(yù)編譯后,請(qǐng)求便不會(huì)調(diào)用對(duì)應(yīng)JSP的servlet的service方法進(jìn)行處理,所以要想讓我們的JSP能正常使用,當(dāng)然是不要預(yù)編譯的,默認(rèn)情況下也不會(huì)預(yù)編譯。
public void service(HttpServletRequest request,HttpServletResponse response,boolean precompile)throws ServletException, IOException, FileNotFoundException {Servlet servlet;...// If a page is to be precompiled only, return.if (precompile) {return;.../** (4) Service request*/if (servlet instanceof SingleThreadModel) {// sync on the wrapper so that the freshness// of the page is determined right before servicingsynchronized (this) {``.service(request, response);}} else {servlet.service(request, response);}...下面再來看serviceJspFile方法,該方法判斷JSP是否已經(jīng)被注冊(cè)為一個(gè)Servlet,不存在則創(chuàng)建JspServletWrapper并put到JspRuntimeContext中,JspServletWrapper.service是核心方法。
private void serviceJspFile(HttpServletRequest request,HttpServletResponse response, String jspUri,boolean precompile)throws ServletException, IOException { // 首先判斷JSP是否已經(jīng)被注冊(cè)為一個(gè)Servlet,ServletWrapper是Servlet的包裝類,所有注冊(cè)的JSP servlet都會(huì)被保存在JspRuntimeContext的jsps屬性中,如果我們第一次請(qǐng)求這個(gè)JSP,當(dāng)然是找不到wrapper的。JspServletWrapper wrapper = rctxt.getWrapper(jspUri);if (wrapper == null) {synchronized(this) {wrapper = rctxt.getWrapper(jspUri);if (wrapper == null) {//檢查JSP文件是否存在if (null == context.getResource(jspUri)) {handleMissingResource(request, response, jspUri);return;}//創(chuàng)建JspServletWrapperwrapper = new JspServletWrapper(config, options, jspUri,rctxt);//添加wrapper到JspRuntimeContext的jsps屬性中rctxt.addWrapper(jspUri,wrapper);}}}try {//核心方法wrapper.service(request, response, precompile);} catch (FileNotFoundException fnfe) {handleMissingResource(request, response, jspUri);}}JspServletWrapper.service主要做了如下操作。
- 根據(jù)jsp生成java文件并編譯為class
- 將class文件注冊(cè)為servlet
- 調(diào)用servlet.service方法完成調(diào)用
JSP生成java和class文件主要由下面的代碼完成,這里的options.getDevelopment()代表的是部署模式。
tomcat的開發(fā)模式和生產(chǎn)模式的設(shè)定是通過conf文件夾下面的web.xml文件來配置的。
在開發(fā)模式下,容器會(huì)經(jīng)常檢查jsp文件的時(shí)間戳來決定是否進(jìn)行編譯,如果jsp文件的時(shí)間戳比對(duì)應(yīng)的.class文件的時(shí)間戳晚就證明jsp又進(jìn)行了修改,需要再次編譯,但是不斷地進(jìn)行時(shí)間戳的比對(duì)開銷很大,會(huì)影響系統(tǒng)性能,而在生產(chǎn)模式下系統(tǒng)不會(huì)經(jīng)常想的檢查時(shí)間戳。所以一般在開發(fā)過程中使用開發(fā)模式,這樣可以在jsp修改后再次訪問就可以見到修改后的效果非常方便,而系統(tǒng)上線之后就要改為生產(chǎn)模式,雖然生產(chǎn)模式下會(huì)導(dǎo)致jsp的修改需要重啟服務(wù)器才可以生效,但是上線后的改動(dòng)較少而且性能很重要。
默認(rèn)Tomcat是以開發(fā)模式運(yùn)行的。一般我們遇到的Tomcat都是以開發(fā)模式運(yùn)行的,所以會(huì)由JspCompilationContext#compile進(jìn)行編譯。
if (options.getDevelopment() || mustCompile) {synchronized (this) {if (options.getDevelopment() || mustCompile) {ctxt.compile();mustCompile = false;}}} else {if (compileException != null) {// Throw cached compilation exceptionthrow compileException;}}下面我們看下編譯部分都做了什么,Tomcat默認(rèn)使用JDTCompiler編譯,首先通過isOutDated判斷是否需要編譯,再去檢查JSP文件是否存在,刪除原有的java和Class文件,通過jspCompiler.compile()編譯。【網(wǎng)絡(luò)安全學(xué)習(xí)資料·攻略】
public void compile() throws JasperException, FileNotFoundException {//獲取編譯器,默認(rèn)使用JDTCompiler編譯createCompiler();//通過isOutDated決定是否編譯if (jspCompiler.isOutDated()) {if (isRemoved()) {throw new FileNotFoundException(jspUri);}try {//刪除已經(jīng)生成的java和Class文件jspCompiler.removeGeneratedFiles();jspLoader = null;//編譯jspCompiler.compile();jsw.setReload(true);jsw.setCompilationException(null);...}下面我們分析如何將生成的class文件注冊(cè)為Servlet。首先判斷theServlet是否為空,如果為空則表示還沒有為JSP文件創(chuàng)建過Servlet,則通過InstanceManager.newInstance創(chuàng)建Servlet,并將創(chuàng)建的Servlet保存在theServlet屬性中。
public Servlet getServlet() throws ServletException { // getReloadInternal是否Reload默認(rèn)為False,也就是說如果theServlet為true就會(huì)直接返回。if (getReloadInternal() || theServlet == null) {synchronized (this) {if (getReloadInternal() || theServlet == null) {//如果theServlet中有值則銷毀該Servlet.destroy();final Servlet servlet;try {//創(chuàng)建Servlet實(shí)例InstanceManager instanceManager = InstanceManagerFactory.getInstanceManager(config);servlet = (Servlet) instanceManager.newInstance(ctxt.getFQCN(), ctxt.getJspLoader());} catch (Exception e) {Throwable t = ExceptionUtils.unwrapInvocationTargetException(e);ExceptionUtils.handleThrowable(t);throw new JasperException(t);}//初始化servletservlet.init(config);if (theServlet != null) {ctxt.getRuntimeContext().incrementJspReloadCount();}//將servlet保存到theServlet中,theServlet由volatile修飾,在線程之間可以共享。theServlet = servlet;reload = false;}}}return theServlet;}下面有一個(gè)小知識(shí)點(diǎn),theServlet是由volatile修飾的,在不同的線程之間可以共享,再通過synchronized (this)加鎖,也就是說無論我們請(qǐng)求多少次,無論是哪個(gè)線程處理,只要this是一個(gè)值,那么theServlet屬性的值是一樣的,而this就是當(dāng)前的jspServletWrapper,我們?cè)L問不同的JSP也是由不同的jspServletWrapper處理的。
最后就是調(diào)用servlet.service方法完成請(qǐng)求處理。
內(nèi)存駐留分析
上面我們已經(jīng)分析完了JSP的處理邏輯,要想要完成內(nèi)存駐留,我們要解決下面的問題。
- 請(qǐng)求后不去檢查JSP文件是否存在
- theServlet中一直保存著我們的servlet,當(dāng)我們請(qǐng)求對(duì)應(yīng)url還能交給我們的servlet處理
第二個(gè)問題比較容易,theServlet能否獲取到Servlet或者獲取到哪個(gè)Servlet和jspServletWrapper是有關(guān)的,而在JspServlet#serviceJspFile中,如果我們已經(jīng)將Servlet注冊(cè)過,可以根據(jù)url從JspRuntimeContext中獲取得到對(duì)應(yīng)的jspServletWrapper。
private void serviceJspFile(HttpServletRequest request,HttpServletResponse response, String jspUri,boolean precompile)throws ServletException, IOException {JspServletWrapper wrapper = rctxt.getWrapper(jspUri);if (wrapper == null) {...}try {wrapper.service(request, response, precompile);} catch (FileNotFoundException fnfe) {handleMissingResource(request, response, jspUri);}}繞過方法一
下面解決請(qǐng)求后不去檢查JSP文件是否存在問題,首先我想繞過下面的判斷,如果我們能讓options.getDevelopment()返回false就不會(huì)進(jìn)入complie部分。
if (options.getDevelopment() || mustCompile) {synchronized (this) {if (options.getDevelopment() || mustCompile) {// The following sets reload to true, if necessaryctxt.compile();mustCompile = false;}}}development并不是一個(gè)static屬性,所以不能直接修改,要拿到options的對(duì)象。
private boolean development = true;options對(duì)象被存儲(chǔ)在JspServlet中,
public class JspServlet extends HttpServlet implements PeriodicEventListener { ...private transient Options options;MappingData中保存了路由匹配的結(jié)果,MappingData的wrapper字段包含處理請(qǐng)求的wrapper,在Tomcat中,Wrapper代表一個(gè)Servlet,它負(fù)責(zé)管理一個(gè) Servlet,包括的 Servlet的裝載、初始化、執(zhí)行以及資源回收。在Wrapper的instance屬性中保存著servlet的實(shí)例,因此我們可以從MappingData中拿到JspServlet進(jìn)而更改options的development屬性值。【網(wǎng)絡(luò)安全學(xué)習(xí)資料·攻略】
public class MappingData {public Host host = null;public Context context = null;public int contextSlashCount = 0;public Context[] contexts = null;public Wrapper wrapper = null;public boolean jspWildCard = false; }
所以我們可以通過反射對(duì)development的屬性修改,下面代碼參考Tomcat容器攻防筆記之JSP金蟬脫殼
既然已經(jīng)分析好了,我們做一個(gè)測(cè)試, 當(dāng)我們第二次請(qǐng)求我們的腳本development
的屬性值已經(jīng)被改為false,即使我們刪除對(duì)應(yīng)的jsp\java\Class文件,仍然還可以還可以正常請(qǐng)求shell。
那么經(jīng)過修改后會(huì)不會(huì)導(dǎo)致后來上傳的jsp文件都無法執(zhí)行的問題呢?
不會(huì),因?yàn)槊恳粋€(gè)JSP文件,只有已經(jīng)編譯并且注冊(cè)為Servlet后,mustCompile屬性才會(huì)為False,默認(rèn)為True,并且mustCompile也是由volatile修飾并且在synchronized加鎖的代碼塊中,只有同一個(gè)jspServletWrapper的mustCompile的修改在下次請(qǐng)求時(shí)還有效。當(dāng)然也不是說完全沒有影響,
如果我們想修改一個(gè)已經(jīng)加載為Servlet 的JSP文件,即使修改了也不會(huì)生效。
繞過方法二
下一個(gè)我們有機(jī)會(huì)繞過的點(diǎn)在compile中,如果我們能讓isOutDated返回false,也可以達(dá)到繞過的目的。
public void compile() throws JasperException, FileNotFoundException {createCompiler();if (jspCompiler.isOutDated()) {...}}注意看下面的代碼,在isOutDated中,當(dāng)滿足下面的條件則會(huì)返回false。jsw中保存的是jspServletWarpper對(duì)象,所以是不為null的,并且modificationTestInterval默認(rèn)值是4也滿足條件,所以我們現(xiàn)在要做的就是讓modificationTestInterval*1000大于System.currentTimeMillis(),所以
只要將modificationTestInterval 修改為一個(gè)比較大的值也可以達(dá)到繞過的目的。
modificationTestInterval也保存在options屬性中,所以修改的方法和方法一類似,就不羅列代碼了。
public final class EmbeddedServletOptions implements Options { ...private int modificationTestInterval = 4;... }查殺情況分析
tomcat-memshell-scanner
這款工具會(huì)Dump出所有保存在servletMappings中的Servlet的信息,不過我們的JSPServlet并沒有保存在servletMappings中,而是在JspRuntimeContext#jsps字段中,因此根本查不到。
copagent
JSP本質(zhì)上也就是Servlet,編譯好的Class繼承了HttpJspBase,類圖如下所示。【網(wǎng)絡(luò)安全學(xué)習(xí)資料·攻略】
copagent流程分析
copagent首先獲取所有已經(jīng)加載的類,并創(chuàng)建了幾個(gè)數(shù)組。
- riskSuperClassesName中保存了HttpServlet,用于獲取Servlet,因?yàn)槲覀冏?cè)的Servlet會(huì)直接或者間接繼承HttpServlet
- riskPackage保存了一些惡意的包名,比如冰蝎的包名為net.rebeyond,使用冰蝎連接webshell時(shí)會(huì)將自己的惡意類加載到內(nèi)存,而這個(gè)惡意類也是以net.rebeyond為包名的
- riskAnnotations保存了SpringMVC中注解注冊(cè)Controller的類型,顯然是為了抓出所有SpringMVC中通過注解注冊(cè)的Controller
下面代碼完成主要的檢測(cè)邏輯,首先會(huì)檢測(cè)包名和SpringMVC注解的類,檢測(cè)到則添加到resultClasses中,并且修改not_found標(biāo)志位為False,表示不檢測(cè)Servelt/Filter/Listener類型的shell。
for(Class<?> clazz: loadedClasses){Class<?> target = clazz;boolean not_found = true;//檢測(cè)包名是否為惡意包名,如果是則設(shè)置not_found為false,代表已經(jīng)被shell連接過了,跳過后面Servlet和Filter內(nèi)存馬部分的檢測(cè)并Dump出惡意類的信息。for(String packageName: riskPackage){if(clazz.getName().startsWith(packageName)){resultClasses.add(clazz);not_found = false;ClassUtils.dumpClass(ins, clazz.getName(), false, Integer.toHexString(target.getClassLoader().hashCode()));break;}}//判斷是否使用SpringMVC的注解注冊(cè)Controller,如果是則Dump出使用注解的Controller的類的信息if(ClassUtils.isUseAnnotations(clazz, riskAnnotations)){resultClasses.add(clazz);not_found = false;ClassUtils.dumpClass(ins, clazz.getName(), false, Integer.toHexString(target.getClassLoader().hashCode()));}//檢測(cè)Servelt/Filter/Listener類型Webshellif(not_found){// 遞歸查找while (target != null && !target.getName().equals("java.lang.Object")){// 每次都重新獲得目標(biāo)類實(shí)現(xiàn)的所有接口interfaces = new ArrayList<String>();for(Class<?> cls: target.getInterfaces()){interfaces.add(cls.getName());}if( // 繼承危險(xiǎn)父類的目標(biāo)類(target.getSuperclass() != null && riskSuperClassesName.contains(target.getSuperclass().getName())) ||// 實(shí)現(xiàn)特殊接口的目標(biāo)類target.getName().equals("org.springframework.web.servlet.handler.AbstractHandlerMapping") ||interfaces.contains("javax.servlet.Filter") ||interfaces.contains("javax.servlet.Servlet") ||interfaces.contains("javax.servlet.ServletRequestListener")){...if(loadedClassesNames.contains(clazz.getName())){resultClasses.add(clazz);ClassUtils.dumpClass(ins, clazz.getName(), false, Integer.toHexString(clazz.getClassLoader().hashCode()));}else{...}break;}target = target.getSuperclass();}}我們主要關(guān)注Servlet的檢測(cè),首先獲取當(dāng)前Class的實(shí)現(xiàn)接口,如果Class的父類不為空并且父類不是HttpServlet,并且沒有實(shí)現(xiàn)Serlvet\Filter\ServletRequestListener等接口則不會(huì)被添加到resultClasses但會(huì)遞歸的去檢查父類。由于JSP文件實(shí)際繼承了HttpJspBase,相當(dāng)于間接繼承了HttpServlet,所以是繞不過這里的檢查的,不過沒關(guān)系,這一步只是檢查是否是Servlet,并不代表被檢測(cè)出來了。
while (target != null && !target.getName().equals("java.lang.Object")){// 每次都重新獲得目標(biāo)類實(shí)現(xiàn)的所有接口interfaces = new ArrayList<String>();for(Class<?> cls: target.getInterfaces()){interfaces.add(cls.getName());}if( // 繼承危險(xiǎn)父類的目標(biāo)類(target.getSuperclass() != null && riskSuperClassesName.contains(target.getSuperclass().getName())) ||// 實(shí)現(xiàn)特殊接口的目標(biāo)類target.getName().equals("org.springframework.web.servlet.handler.AbstractHandlerMapping") ||interfaces.contains("javax.servlet.Filter") ||interfaces.contains("javax.servlet.Servlet") ||interfaces.contains("javax.servlet.ServletRequestListener")){if(loadedClassesNames.contains(clazz.getName())){resultClasses.add(clazz);ClassUtils.dumpClass(ins, clazz.getName(), false, Integer.toHexString(clazz.getClassLoader().hashCode()));}else{LogUtils.logit("cannot find " + clazz.getName() + " classes in instrumentation");}break;...}target = target.getSuperclass();}下面是判斷是否為惡意內(nèi)容的核心,只有當(dāng)resultClasses中包含了關(guān)鍵下面的關(guān)鍵字才會(huì)被標(biāo)記為high,這里如果我們使用自定義馬的話也是可以繞過的,但是如果要使用冰蝎,一定會(huì)被javax.crypto.加密包的規(guī)則檢測(cè)到,如果是自定義加密算法也是可以繞過的。
List<String> riskKeyword = new ArrayList<String>();riskKeyword.add("javax.crypto.");riskKeyword.add("ProcessBuilder");riskKeyword.add("getRuntime");riskKeyword.add("shell"); ...for(Class<?> clazz: resultClasses){File dumpPath = PathUtils.getStorePath(clazz, false);String level = "normal";String content = PathUtils.getFileContent(dumpPath);for(String keyword: riskKeyword){if(content.contains(keyword)){level = "high";break;}}自刪除
上面只是分析了如何讓我們的JSP在刪除了JSP\java\Class文件后還能訪問,下面我們分析如何在JSP中實(shí)現(xiàn)刪除JSP\java\Class文件,在JspCompilationContext保存著JSP編譯的上下文信息,我們可以從中拿到j(luò)ava/class的絕對(duì)路徑。【網(wǎng)絡(luò)安全學(xué)習(xí)資料·攻略】
request.request.getMappingData().wrapper.instance.rctxt.jsps.get("/jsp.jsp")
下面是代碼實(shí)現(xiàn)
最后有個(gè)不兼容的小BUG,tomcat7和8/9的MappingData類包名發(fā)生了變化
tomcat7:<%@ page import="org.apache.tomcat.util.http.mapper.MappingData" %> tomcat8/9:<%@ page import="org.apache.catalina.mapper.MappingData" %>參考文獻(xiàn)
總結(jié)
雖然不能使用冰蝎等webshell繞過這兩款工具的檢測(cè),但是當(dāng)我們了解了查殺原理,將自己的webshell稍微改一下,也是可以繞過的
最后
關(guān)注我持續(xù)更新優(yōu)質(zhì)文章,私我獲取【網(wǎng)絡(luò)安全學(xué)習(xí)資料·攻略】
總結(jié)
以上是生活随笔為你收集整理的【Web安全】JSP内存马研究的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 常见的钓鱼招式,可千万别入坑哦
- 下一篇: gradle idea java ssm