Java代码审计之FileUpload-文件上传漏洞审计与修复

java代码审计系统课程--代码审计视频教程-信息安全-CSDN程序员研修院少写“漏洞” 了解常见代码安全 提升代码安全能力 代码不被黑客黑-https://edu.csdn.net/course/detail/32634

访问url为http://localhost:8080/file/any

直接对上传的文件保存在了指定路径下,

@PostMapping("/upload")
public String singleFileUpload(@RequestParam("file") MultipartFile file,
                               RedirectAttributes redirectAttributes) {
    if (file.isEmpty()) {
        // 赋值给uploadStatus.html里的动态参数message
        redirectAttributes.addFlashAttribute("message", "Please select a file to upload");
        return "redirect:/file/status";
    }
 
    try {
        // Get the file and save it somewhere
        byte[] bytes = file.getBytes();
        Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
        Files.write(path, bytes);
 
        redirectAttributes.addFlashAttribute("message",
                                             "You successfully uploaded '" + UPLOADED_FOLDER + file.getOriginalFilename() + "'");
 
    } catch (IOException e) {
        redirectAttributes.addFlashAttribute("message", "upload failed");
        logger.error(e.toString());
    }
 
    return "redirect:/file/status";
​
}

没有任何的后缀名及内容过滤,可以上传任意的恶意文件。

在这里上传目录在/tmp下,同时文件的写入时用的Files.write(path, bytes),这里的path就是保存的路径,由于是保存的目录/tmp直接拼接了文件名,就可以在文件名中利用../来达到目录穿越的目的,从而将任意文件保存在任意目录下。 pic

限制只能上传图片,同时进行了多重验证。

对文件后缀名进行白名单限制,只能为白名单中的图片后缀名。AAA.JSP.PNG

String[] picSuffixList = {".jpg", ".png", ".jpeg", ".gif", ".bmp", ".ico"};
boolean suffixFlag = false;
for (String white_suffix : picSuffixList) {
    if (Suffix.toLowerCase().equals(white_suffix)) {
        suffixFlag = true;
        break;
    }
​
}

对MIME类型进行了黑名单限制,不过这个可以进行抓包修改绕过。

String[] mimeTypeBlackList = {
    "text/html",
    "text/javascript",
    "application/javascript",
    "application/ecmascript",
    "text/xml",
    "application/xml"
};
for (String blackMimeType : mimeTypeBlackList) {
    // 用contains是为了防止text/html;charset=UTF-8绕过
    if (SecurityUtil.replaceSpecialStr(mimeType).toLowerCase().contains(blackMimeType)) {
        logger.error("[-] Mime type error: " + mimeType);
        deleteFile(filePath);
        return "Upload failed. Illeagl picture.";
    }
​
}

文件保存的时候路径是通过 来获取,Path path = excelFile.toPath();就避免了路径穿越的实现。

        File excelFile = convert(multifile);//文件名字做了uuid处理
        String filePath = excelFile.getPath();
        // 判断文件内容是否是图片 校验3
        boolean isImageFlag = isImage(excelFile);
        if (!isImageFlag) {
            logger.error("[-] File is not Image");
            deleteFile(filePath);
            return "Upload failed. Illeagl picture.";
        }
​
​
​
判断上传的文件是否为图片,通过ImageIO.read对文件进行读取来判断。
​
private static boolean isImage(File file) throws IOException {
    BufferedImage bi = ImageIO.read(file);
    return bi != null;
​
}
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mingzhi61

你的打赏,是我创造最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值