本文主要研究一下directory traversal attack及其防范

directory traversal attack

又称Path Traversal attack,即目录遍历攻击,旨在访问web服务器根目录外的文件/目录。通过是通过url或变量里头传递"../"来进行目录遍历。

通过url

比如

http://some_site.com.br/../../../../some dir/some file
复制代码

或者

http://some_site.com.br/../../../../etc/shadow
复制代码

通过变量名

通常是在文件下载接口中,比如

http://some_site.com.br/get-files?file=/etc/passwd
复制代码

或者

http://some_site.com.br/get-files?file=../../../../some dir/some file
复制代码

防范

针对url

spring security提供了DefaultHttpFirewall来进行处理,是为了防止一些web框架没有遵循servlet规范而进行的防范。 spring-security-web-4.2.3.RELEASE-sources.jar!/org/springframework/security/web/firewall/DefaultHttpFirewall.java

/*** Default implementation which wraps requests in order to provide consistent* values of the {@code servletPath} and {@code pathInfo}, which do not contain* path parameters (as defined in* <a href="http://www.ietf.org/rfc/rfc2396.txt">RFC 2396</a>). Different* servlet containers interpret the servlet spec differently as to how path* parameters are treated and it is possible they might be added in order to* bypass particular security constraints. When using this implementation, they* will be removed for all requests as the request passes through the security* filter chain. Note that this means that any segments in the decoded path* which contain a semi-colon, will have the part following the semi-colon* removed for request matching. Your application should not contain any valid* paths which contain semi-colons.* <p>* If any un-normalized paths are found (containing directory-traversal* character sequences), the request will be rejected immediately. Most* containers normalize the paths before performing the servlet-mapping, but* again this is not guaranteed by the servlet spec.** @author Luke Taylor*/
public class DefaultHttpFirewall implements HttpFirewall {private boolean allowUrlEncodedSlash;@Overridepublic FirewalledRequest getFirewalledRequest(HttpServletRequest request) throws RequestRejectedException {FirewalledRequest fwr = new RequestWrapper(request);if (!isNormalized(fwr.getServletPath()) || !isNormalized(fwr.getPathInfo())) {throw new RequestRejectedException("Un-normalized paths are not supported: " + fwr.getServletPath()+ (fwr.getPathInfo() != null ? fwr.getPathInfo() : ""));}String requestURI = fwr.getRequestURI();if (containsInvalidUrlEncodedSlash(requestURI)) {throw new RequestRejectedException("The requestURI cannot contain encoded slash. Got " + requestURI);}return fwr;}@Overridepublic HttpServletResponse getFirewalledResponse(HttpServletResponse response) {return new FirewalledResponse(response);}/*** <p>* Sets if the application should allow a URL encoded slash character.* </p>* <p>* If true (default is false), a URL encoded slash will be allowed in the* URL. Allowing encoded slashes can cause security vulnerabilities in some* situations depending on how the container constructs the* HttpServletRequest.* </p>** @param allowUrlEncodedSlash*            the new value (default false)*/public void setAllowUrlEncodedSlash(boolean allowUrlEncodedSlash) {this.allowUrlEncodedSlash = allowUrlEncodedSlash;}private boolean containsInvalidUrlEncodedSlash(String uri) {if (this.allowUrlEncodedSlash || uri == null) {return false;}if (uri.contains("%2f") || uri.contains("%2F")) {return true;}return false;}/*** Checks whether a path is normalized (doesn't contain path traversal* sequences like "./", "/../" or "/.")** @param path*            the path to test* @return true if the path doesn't contain any path-traversal character*         sequences.*/private boolean isNormalized(String path) {if (path == null) {return true;}for (int j = path.length(); j > 0;) {int i = path.lastIndexOf('/', j - 1);int gap = j - i;if (gap == 2 && path.charAt(i + 1) == '.') {// ".", "/./" or "/."return false;} else if (gap == 3 && path.charAt(i + 1) == '.' && path.charAt(i + 2) == '.') {return false;}j = i;}return true;}}
复制代码

这里会对url进行判断

通过变量

这种框架没有内置进行判断,需要自己在开发应用服务的时候额外关注。这里谈谈几种防范方法。

  • 对变量名进行过滤
final Pattern INVALID_PATH_PATTERN = Pattern.compile("(\\.\\.\\/|\\.\\.\\\\)");
if(INVALID_PATH_PATTERN.matcher(path).find()){throw new BadRequestException("invalid path");
}
复制代码
  • 利用absolutePath与canonicalPath

absolutePath不会处理../之类的,而canonicalPath会翻译../,判断两者是否相等即可判断是否有../

        if (!file.getAbsolutePath().equals(file.getCanonicalPath())) {throw new BadRequestException("invalid path");}
复制代码

小结

在编写文件下载服务的时候,需要特别关注directory traversal attack。通常url层面的web框架会帮你防范,但是变量层面的,则需要自己开发额外注意。

doc

  • Path Traversal
  • Directory traversal attack

聊聊directory traversal attack相关推荐

  1. 3 Directory traversal

    3 Directory traversal目录遍历攻击 目录 3 Directory traversal目录遍历攻击 一.What 二.通过目录遍历读取任意文件 Lab: File path trav ...

  2. Directory traversal in Spring framework漏洞修复

    Directory traversal in Spring framework 提示CSS.JS 等静态文件有问题需要提升Spring的版本问题,根据官方问题解释4.1.x必须升级到4.1.2及以上版 ...

  3. 【常见Web应用安全问题】---4、Directory traversal

    Web应用程序的安全性问题依其存在的形势划分,种类繁多,这里不准备介绍所有的,只介绍常见的一些.  常见Web应用安全问题安全性问题的列表: 1.跨站脚本攻击(CSS or XSS, Cross Si ...

  4. 目录遍历(Directory traversal)

    笔者burpsuite的在线安全学院的目录遍历学习笔记.限于本人水平,笔记质量不是很高,假如有看到的大佬轻喷,很多地方是Google翻译的. 文章目录 什么是目录遍历? 通过目录遍历读取任意文件 La ...

  5. burpsuit 靶场(Directory traversal)

    文件路径遍历,简单案例 ../../../../etc/passwd 文件路径遍历,绕过绝对路径阻塞的遍历序列 ../../../../etc/passwd 绝对路径可以执行 /etc/passwd ...

  6. 目录/文件攻击防范策略研究

    目录 0x1:目录穿越攻击 0x2:远程文件引入攻击 0x3:防范的方法 目录穿越攻击 目录穿越(Directory Traversal)攻击是黑客能够在Web应用程序所在的根目录以外的文件夹上,任意 ...

  7. CTF Web方向考点总结

    CTF Web 0X00 前言 做题已经快四个月了,接触了大大小小的题型,收藏的大师傅们的解题思路.题型总结的博客已经很多了,每次都要一个一个翻很麻烦,于是写下了这一个总结,实际上是把各大博客内容汇总 ...

  8. 文件包含漏洞(LFI、RFI)(require()、include()函数)

    文章目录 一.文件包含漏洞分类 二.文件包含漏洞原理 三.文件包含函数 四.测试是否存在本地文件包含(LFI)漏洞 五.文件包含漏洞实例 "百度杯"CTF比赛 2017 二月场in ...

  9. Python: Monitoring a Directory

    2019独角兽企业重金招聘Python工程师标准>>> Basic mtime + checksum + directory traversal Code #!/usr/bin/en ...

最新文章

  1. 【每日一念经】四轮面试,我如何拿到美团的offer?
  2. runnable和handler联合实现计时循环
  3. CodeForces - 1465E Poman Numbers(推公式+贪心)
  4. Can‘t attach to the process: ptrace(PTRACE_ATTACH, ..)
  5. 壁式框架内力计算_4种类型剪力墙的计算要点
  6. 关于socket组播和ssdp(一)[修改1.2]
  7. 再见!经典版Edge!
  8. Sass:@error
  9. 黄聪:微信h5支付demo微信H5支付demo非微信浏览器支付demo微信wap支付
  10. 联邦学习数学公式纯手推
  11. 金士顿固态硬盘修复,慧荣SM2246EN开卡记录,附量产工具
  12. IC之无毛刺时钟切换
  13. Task运行过程分析1
  14. VS发布网站时,报错提示:“未能将文件xxx复制到xxx,未能找到文件xx”三种解决方案!...
  15. STM32的矩阵按键程序思路
  16. python爱情动画_人生苦短,我用Python-从Houdini里导出RBD解算的Skin动画
  17. Hot Restart Hot Reload
  18. 二极管在电路设计中的应用
  19. 系统集成项目管理工程师资料分享
  20. 现场总线快速讲解之一

热门文章

  1. Alien Skin X7PS调色滤镜插件下载及PS调色滤镜教程
  2. 先吃奶油还是先吃蛋糕--推迟满足感
  3. 第五次项目《超市会员管理Plus》
  4. flink报错:The types of the interface org.apache.flink.util.OutputTag could not be inferred.
  5. R代码模拟世界杯1000次,速成2018世界杯预言帝
  6. Virtualbox虚拟机设置共享文件夹
  7. Stream和方法引用
  8. redis底层数据结构之跳跃表
  9. Word文档进行XXE攻击
  10. 码蹄集第23周赛(买礼物,召唤神龙,大促销,轨道探测)