目录遍历
目录遍历, 应用系统在处理下载文件时未对文件进行过滤,系统后台程序程序中如果不能正确地过滤客户端提交的../和./之类的目录跳转符,攻击者可以通过输入../进行目录跳转,从而下载、删除任意文件。
运行
漏洞代码
@GetMapping("/download") public String download(String filename, HttpServletRequest request, HttpServletResponse response) { String filePath = System.getProperty("user.dir") + "/logs/" + filename; try { File file = new File(filePath); InputStream is = new BufferedInputStream(new FileInputStream(file)); byte[] buffer = new byte[is.available()]; is.read(buffer); is.close();
运行
安全代码 - 过滤
public static boolean check_traversal(String content) { return content.contains("..") || content.contains("/"); }
编码建议
避免路径拼接 文件目录避免外部参数拼接。保存文件目录建议后台写死并对文件名进行校验(字符类型、长度)。建议文件保存时,将文件名替换为随机字符串。 如因业务需要不能满足1.2.3的要求,文件路径、文件命中拼接了不可行数据,需判断请求文件名和文件路径参数中是否存在../或..\(windows), 如存在应判定路径非法并拒绝请求。