(1)风险概述
路径遍历,即利用路径回溯符“../”跳出程序本身的限制目录实现下载任意文件。例如Web应用源码目录、Web应用配置文件、敏感的系统文件(/etc/passwd、/etc/paswd)等。
例如一个正常的Web功能请求:
http://www.test.com/get-files.jsp?file=report.pdf
如果Web应用存在路径遍历漏洞,则攻击者可以构造以下请求服务器敏感文件:
http://www.test.com/get-files.jsp?file=../../../../../../../../../../../../etc/passwd
(2)缺陷编码示例:
以下是一段存在文件路径遍历缺陷的代码,服务端没有对传入的imgName参数进行合法性验证,而imgName参数值就是客户端请求下载的文件,攻击者通过控制imgName参数可以遍历服务器上的敏感文件:
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {try {byte data[] = new byte[1];//取得用户提交的图片文件名,没有检测是否为图片,也没有检测是否包含../../目录跳转的字符String imgName = request.getParameter("imgName");String imgKey = MD5Encrypt.MD5(imgName);//本地if (imageCache.containsKey(imgKey)) {data = (byte[]) imageCache.get(imgKey);} else {String imagePath = Consts.IMG_LOCAL_PAHT + imgName;//没有对该参数进行严格的验证和过滤,就拼接成完整的图片路径InputStream inputStream = null;File imageFile = new File(imagePath);logger.debug(imagePath + " " + imageFile.exists());if (imageFile.exists() && imageFile.isFile()) {inputStream = new FileInputStream(imagePath);int i = inputStream.available();data = new byte[i];inputStream.read(data);inputStream.close();imageCache.put(imgKey, data);} else {}}//将文件内容输出到客户端response.setContentType("image/*");OutputStream outputStream = response.getOutputStream();outputStream.write(data);outputStream.close();}
