shiro安全框架扩展教程--上传文件的安全控制

           相信每一个项目都会存在文件上传功能,最常见的就是图片,音频,视频等上传,但是如果用户多的应用都会存在用户恶意上传动作,包括伪装成可上传文件,

按照我们以往的方式,普通地判断后缀名,那是不可行,除了一些内部系统可以这样简单的玩耍,那对外的互联网应用项目是不够严格的


下面举例个简单的攻击


1. 我们的上传代码中只判断了,request里面的文件类型

2. 恶意用户把一个jsp文件,修改了头文件信息,这样就可以骗过浏览器上传过程中是显示文件是image/jpeg

3. 然后文件就可以直接保存成功了,很明显这是由于服务端判断不严谨导致的,用户访问xxxx路径下的上传jsp文件然后就可以执行jsp里面的脚本,你的资料基本就泄漏的差不多了


然后下面如果说是加个后缀名判断,也差不多可以这样绕过


扫描二维码关注公众号,回复: 1650135 查看本文章

我们都知道文件都有所谓的文件头信息,但是除了jsp,txt等一些特殊的是没有固定的,所以我们要排除这些


下面看看一些常用的文件头信息匹配


                fileHeaderType.put(".jpg", "FFD8FF"); // JPEG (jpg)
		fileHeaderType.put(".png", "89504E47"); // PNG (png)
		fileHeaderType.put(".gif", "47494638"); // GIF (gif)
		fileHeaderType.put(".tif", "49492A00"); // TIFF (tif)
		fileHeaderType.put(".bmp", "424D"); // Windows Bitmap (bmp)
		
		fileHeaderType.put(".zip", "504B0304");
		fileHeaderType.put(".rar", "52617221");
		fileHeaderType.put(".7z", "377abcaf");
		
		fileHeaderType.put(".xls", "D0CF11E0");
		fileHeaderType.put(".xlsx", "504b030414");
		fileHeaderType.put(".doc", "D0CF11E0"); 
		fileHeaderType.put(".docx", "504b0304"); 
		
		fileHeaderType.put(".dwg", "41433130"); // CAD (dwg)
		fileHeaderType.put(".html", "68746D6C3E"); // HTML (html)
		fileHeaderType.put(".rtf", "7B5C727466"); // Rich Text Format (rtf)
		fileHeaderType.put(".xml", "3C3F786D6C");
		
		fileHeaderType.put(".psd", "38425053"); // Photoshop (psd)
		fileHeaderType.put(".eml", "44656C69766572792D646174653A"); // Email

		fileHeaderType.put(".dbx", "CFAD12FEC5FD746F"); // Outlook Express (dbx)
		fileHeaderType.put(".pst", "2142444E"); // Outlook (pst)
		
		fileHeaderType.put(".mdb", "5374616E64617264204A"); // MS Access (mdb)
		fileHeaderType.put(".wpd", "FF575043"); // WordPerfect (wpd)
		fileHeaderType.put(".eps", "252150532D41646F6265");
		fileHeaderType.put(".ps", "252150532D41646F6265");
		fileHeaderType.put(".pdf", "255044462D312E"); // Adobe Acrobat (pdf)
		fileHeaderType.put(".qdf", "AC9EBD8F"); // Quicken (qdf)
		fileHeaderType.put(".pwl", "E3828596"); // Windows Password (pwl)
		fileHeaderType.put(".wav", "57415645"); // Wave (wav)
		fileHeaderType.put(".avi", "41564920");
		fileHeaderType.put(".ram", "2E7261FD"); // Real Audio (ram)
		fileHeaderType.put(".rm", "2E524D46"); // Real Media (rm)
		fileHeaderType.put(".mpg", "000001BA"); //    
		fileHeaderType.put(".mov", "6D6F6F76"); // Quicktime (mov)
		fileHeaderType.put(".asf", "3026B2758E66CF11"); // Windows Media (asf)
		fileHeaderType.put(".mid", "4D546864"); // MIDI (mid)

如何使用呢?我们应该读取文件的前面一些字节,然后匹配这些固定的字符串,如果可以匹配上那证明是合法的


/** 获取文件头 */
	private static String getFileHeader(File file, int byteSize) throws IOException {

		InputStream in = new FileInputStream(file);

		byte[] b = new byte[byteSize];
		in.read(b, 0, b.length);

		if (b == null || b.length <= 0) {
			return null;
		}

		StringBuffer buffer = new StringBuffer();

		for (int i = 0, len = b.length; i < len; i++) {
			int v = b[i] & 0xFF;
			String hv = Integer.toHexString(v);
			if (hv.length() < 2) {
				buffer.append(0);
			}
			buffer.append(hv);
		}
		return buffer.toString().toLowerCase();
	}


然后使用对应的类型和对应的文件头信息匹配,但是有个比较麻烦的问题,如果图片把jpg后缀改成png,应该算是正常合法的,所以这个时候应该把当前后缀全部匹配下图片的魔数

如果找到一个合法则认为通过,这样处理应该是比较正确的


猜你喜欢

转载自blog.csdn.net/shadowsick/article/details/40510139