1.上传任意文件
缺陷描述:
允许用户上传文件可能会让攻击者注入危险内容或恶意代码,并在服务器上运行。举例如下:
2.上传漏洞
问题描述:没有对上传文件扩展名进行限制或者限制可以被绕过。
整改方案:
1.校验文件格式
2.通过文件头校验文件格式
3.文件大小校验
4.文件重命名
1.校验文件格式(配置上传白名单)
1.通过文件头校验文件格式(通过获取文件头文件类型和配置上传白名单比较)
public class FileTypeUtil {
private final static Map<String, String> FILE_TYPE_MAP = new HashMap<String, String>();/*** 日志操作类*/private final static Logger logger = LoggerFactory.getLogger(FileTypeUtil.class);private FileTypeUtil() {}static {getAllFileType(); // 初始化文件类型信息}private static void getAllFileType() {FILE_TYPE_MAP.put("ffd8ffe000104a464946", "jpg");FILE_TYPE_MAP.put("89504e470d0a1a0a0000", "png");FILE_TYPE_MAP.put("47494638396126026f01", "gif");FILE_TYPE_MAP.put("49492a00227105008037", "tif");FILE_TYPE_MAP.put("424d228c010000000000", "bmp"); // 16色位图(bmp)FILE_TYPE_MAP.put("424d8240090000000000", "bmp"); // 24位位图(bmp)FILE_TYPE_MAP.put("424d8e1b030000000000", "bmp"); // 256色位图(bmp)FILE_TYPE_MAP.put("41433130313500000000", "dwg");FILE_TYPE_MAP.put("3c21444f435459504520", "html");FILE_TYPE_MAP.put("3c21646f637479706520", "htm");FILE_TYPE_MAP.put("48544d4c207b0d0a0942", "css");FILE_TYPE_MAP.put("696b2e71623d696b2e71", "js");FILE_TYPE_MAP.put("7b5c727466315c616e73", "rtf");FILE_TYPE_MAP.put("38425053000100000000", "psd");FILE_TYPE_MAP.put("46726f6d3a203d3f6762", "eml");FILE_TYPE_MAP.put("d0cf11e0a1b11ae10000", "doc"); // MS Excel、Word、MsiFILE_TYPE_MAP.put("d0cf11e0a1b11ae10000", "vsd");FILE_TYPE_MAP.put("5374616E64617264204A", "mdb");FILE_TYPE_MAP.put("255044462d312e350d0a", "pdf");FILE_TYPE_MAP.put("2e524d46000000120001", "rmvb"); // rmvb、rmFILE_TYPE_MAP.put("464c5601050000000900", "flv"); // flv、f4vFILE_TYPE_MAP.put("00000020667479706d70", "mp4");FILE_TYPE_MAP.put("49443303000000002176", "mp3");FILE_TYPE_MAP.put("000001ba210001000180", "mpg");FILE_TYPE_MAP.put("3026b2758e66cf11a6d9", "wmv"); // wmv、asfFILE_TYPE_MAP.put("52494646e27807005741", "wav");FILE_TYPE_MAP.put("52494646d07d60074156", "avi");FILE_TYPE_MAP.put("4d546864000000060001", "mid");FILE_TYPE_MAP.put("504b0304140000000800", "zip");FILE_TYPE_MAP.put("526172211a0700cf9073", "rar");FILE_TYPE_MAP.put("235468697320636f6e66", "ini");FILE_TYPE_MAP.put("504b03040a0000000000", "jar");FILE_TYPE_MAP.put("4d5a9000030000000400", "exe");FILE_TYPE_MAP.put("3c25402070616765206c", "jsp");FILE_TYPE_MAP.put("4d616e69666573742d56", "mf");FILE_TYPE_MAP.put("3c3f786d6c2076657273", "xml");FILE_TYPE_MAP.put("494e5345525420494e54", "sql");FILE_TYPE_MAP.put("7061636b616765207765", "java");FILE_TYPE_MAP.put("406563686f206f66660d", "bat");FILE_TYPE_MAP.put("1f8b0800000000000000", "gz");FILE_TYPE_MAP.put("6c6f67346a2e726f6f74", "properties");FILE_TYPE_MAP.put("cafebabe0000002e0041", "class");FILE_TYPE_MAP.put("49545346030000006000", "chm");FILE_TYPE_MAP.put("04000000010000001300", "mxp");FILE_TYPE_MAP.put("504b0304140006000800", "docx");FILE_TYPE_MAP.put("d0cf11e0a1b11ae10000", "wps");// WPS(wps、et、dps)FILE_TYPE_MAP.put("6431303a637265617465", "torrent");FILE_TYPE_MAP.put("6D6F6F76", "mov");FILE_TYPE_MAP.put("FF575043", "wpd");FILE_TYPE_MAP.put("CFAD12FEC5FD746F", "dbx");FILE_TYPE_MAP.put("2142444E", "pst");FILE_TYPE_MAP.put("AC9EBD8F", "qdf");FILE_TYPE_MAP.put("E3828596", "pwl");FILE_TYPE_MAP.put("2E7261FD", "ram");}public static String getFileHeader(MultipartFile file) {String fileName = file.getOriginalFilename();String fileType = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase(Locale.ENGLISH);if ("txt".equalsIgnoreCase(fileType) || "ppt".equalsIgnoreCase(fileType) || "tiff".equalsIgnoreCase(fileType) || "cebx".equalsIgnoreCase(fileType)|| "3gp".equalsIgnoreCase(fileType) || "mpeg".equalsIgnoreCase(fileType) || "swf".equalsIgnoreCase(fileType) || "ceb".equalsIgnoreCase(fileType)) {return fileType;} else {String value = null;InputStream is = null;try {is = file.getInputStream();byte[] b = new byte[4];is.read(b, 0, b.length);value = bytesToHexString(b);} catch (IOException e) {logger.error("获取文件头失败");} finally {if (null != is) {try {is.close();} catch (IOException e) {logger.error("关闭输入流失败");}}}String result = null;Iterator<String> keyIter = FILE_TYPE_MAP.keySet().iterator();while (keyIter.hasNext()) {String key = keyIter.next();if (value != null) {if (key.toLowerCase(Locale.ENGLISH).startsWith(value.toLowerCase(Locale.ENGLISH))|| value.toLowerCase(Locale.ENGLISH).startsWith(key.toLowerCase(Locale.ENGLISH))) {result = FILE_TYPE_MAP.get(key);break;}}}if (result == null) {return fileType;}return result;}}/*** 得到上传文件的文件头* * @param src* @return*/private static String bytesToHexString(byte[] src) {StringBuilder stringBuilder = new StringBuilder();if (null == src || src.length <= 0) {return null;}for (int i = 0; i < src.length; i++) {int v = src[i] & 0xFF;String hv = Integer.toHexString(v);if (hv.length() < 2) {stringBuilder.append(0);}stringBuilder.append(hv);}return stringBuilder.toString();}
}
3.文件大小校验(上传的文件大小和配置文件里面文件大小比较)
4.文件重命名
用时间戳命名存储在文件服务器上的文件名称。
如果觉得《上传任意文件或者上传漏洞》对你有帮助,请点赞、收藏,并留下你的观点哦!