webgoat-Path traversal 目录遍历漏洞

01 Path traversal

路径(目录)遍历是一种漏洞,攻击者能够访问或存储外部的文件和目录 应用程序运行的位置。这可能会导致从其他目录读取文件,如果是文件,则会导致读取文件 上传覆盖关键系统文件。

它是如何工作的?
例如,假设我们有一个应用程序,它托管了一些文件,并且可以在下面请求它们 格式:http://example.com/file=report.pdf
现在,作为攻击者,您当然对其他文件感兴趣,所以 你试试http://example.com/file=…/…/…/…/…/etc/passwd/etc/passwd…/在这种情况下,您尝试爬到文件系统的根目录 然后进入以获取对此文件的访问权限。称为点-点-斜杠,这是这种攻击的另一个名称
参考:https://blog.csdn.net/qq_53079406/article/details/127140512

0x02 linux版本

题目要求把文件传入/home/webgoat/.webgoat-8.2.2/PathTraversal/username目录下,
1、直接上传文件,发现文件被传到/home/webgoat/.webgoat-8.2.2/PathTraversal/test/test
2、猜想存在注入,修改fullname为…/test,即可上传到/home/webgoat/.webgoat-8.2.2/PathTraversal/test
在这里插入图片描述

0x02 win版本

FullName输入…/test 通过。

在这里插入图片描述
uploadedfile为C:\Users\Administrator.webgoat-2023.5-SNAPSHOT\PathTraversal\yangyali…\test
在这里插入图片描述
结果就是这个文件被上传至PathTraversal目录下
在这里插入图片描述
C:\Users\Administrator.webgoat-2023.5-SNAPSHOT\PathTraversal… 会回到 PathTraversal的父目录。

03 Path traversal while uploading files 移除…/

The developer became aware of the vulnerability and implemented a fix that removed the …/ from the input. Again the same assignment, but can you bypass the implemented fix? 开发意识到了漏洞,并从输入中移除了…/

OS Location
Windows 10 C:\Users\Administrator/.webgoat-2023.5-SNAPSHOT/PathTraversal

过滤了…/,但是可以使用双写绕过…//test
在这里插入图片描述

04 Path traversal while uploading files 读取文件名

The developer again became aware of the vulnerability by not validating the input of the full name input field. A fix was applied in an attempt to solve this vulnerability.

Again the same assignment, but can you bypass the implemented fix?

OS LocationWindows 10 C:\Users\Administrator/.webgoat-2023.5-SNAPSHOT/PathTraversal
本次上传文件时,未使用用输入的full name,而是直接使用了图片名。考虑可以直接命名文件为…/test,但windows下不允许以/命名文件。
在这里插入图片描述
请求后抓包,修改文件名,通过。
在这里插入图片描述

05 Retrieving other files with a path traversal

Path traversals are not limited to file uploads; when retrieving files, it can be the case that a path traversal is possible to retrieve other files from the system. In this assignment, try to find a file called path-traversal-secret.jpg
题目要求找到图片path-traversal-secret.jpg
在这里插入图片描述
抓包, 查看请求和响应,发现response有个location字段显示了id
在这里插入图片描述
id=7.jpg,可以看到返回了cats目录的所有文件,但是没有我们要找的。
在这里插入图片描述
直接输入id,查看到可以返回图片。
在这里插入图片描述
用id查,无返回。
在这里插入图片描述
看了下这个文件,在PathTraversal目录下,所以需要遍历目录。
在这里插入图片描述
输入 id=…/path-traversal-secret 返回字符不合法
源码中对…和/进行了过滤

 if (queryParams != null && (queryParams.contains("..") || queryParams.contains("/"))) {
      return ResponseEntity.badRequest()
          .body("Illegal characters are not allowed in the query params");
    }

考虑对URL编码绕过
id=…/…/path-traversal-secret 编码为
id=%2E%2E%2F%2E%2E%2Fpath-traversal-secret 返回了结果,提示对自己的username进行编码得到flag

在这里插入图片描述
在这里插入图片描述
https://coding.tools/cn/sha512

06 Zip Slip vulnerability 漏洞

作为一名开发人员,您在很多情况下都需要处理zip文件。例如,考虑一下上传工具或处理作为zip文件上传的大量CSV文件。Snyk Security团队发现并负责任地披露了一个漏洞。它使用路径遍历,可以在提取文件时使用。通过路径遍历,您尝试覆盖目标文件夹外的文件。例如,您可以在提取zip文件时覆盖ls命令。一旦这个命令被替换为一些额外的恶意行为,每次用户输入ls时,您都可以将列表的结果发送到您的服务器,然后再向用户显示实际的命令。因此,您最终会执行远程命令。

Problem
The problem occurs with how we extract zip files in Java; a common way to do this is:

File destinationDir = new File("/tmp/zip");
Enumeration<? extends ZipEntry> entries = zip.entries();
while (entries.hasMoreElements()) {
  ZipEntry e = entries.nextElement();
  File f = new File(destinationDir, e.getName());
  InputStream is = zip.getInputStream(e);
  IOUtils.copy(is, write(f));
}

乍一看,这看起来不错,你也写了同样的内容。正如我们在之前的作业中所看到的,问题是您可以使用路径遍历来突破destinationDir并开始走向不同的位置。

但是,如果我们收到包含以下内容的 zip 文件怎么办:
orders.csv
…/…/…/…/…/…/…/tmp/evil.sh
if you extract the zip file with the code above the file will be saved in /tmp/evil.sh.
在上传zip文件的地方,上传压缩包文件,后端解压压缩包保存其中的文件到服务器本地。

漏洞成因:待上传的压缩包中可以构造条目名,后端保存文件的时候,常常将条目名提取出来并和保存目录拼接作为最后的保存文件路径,但是压缩包是可控的,从而其中保存的原始条目名也是可控的,因此可以在文件名处利用…/跳转到任意目录,从而向任意目录写入新文件或者覆盖旧文件
如果一个zip文件包含内容为 …/…/…/…/…/…/…/tmp/evil.sh
那么这个文件最后会被保存到/tmp/evil.sh

07 Zip Slip assignment

This time the developers only allow you to upload zip files. However, they made a programming mistake in uploading the zip file will extract it, but it will not replace your image. Can you find a way to overwrite your current image bypassing the programming mistake?

To make the assignment a bit easier below you will find the location of the profile image you need to replace:

OS LocationWindows 10
C:\Users\Administrator/.webgoat-2023.5-SNAPSHOT/PathTraversal/yangyali/yangyali.jpg
题目要求上传一个zip文件,包含图片,并将图片解压到如上目录下。
在这里插入图片描述

做这个题对我的难点竟然是路径,搞了很久。
将一个图片进行打包为zip,使用7-zip的open archive进行打开,直接rename为如下 因为文件解压后,我们不知道解压到哪个目录,所以使用多个…/回溯到根目录,并在最后写上需要解压到的目录,注意这里的C:不用写,因为写上会不合法。 windows路径不允许后面包含分号。
…/…/…/…/…/…/…/…/…/Users/Administrator/.webgoat-2023.5-SNAPSHOT/PathTraversal/yangyali/yangyali.jpg
在这里插入图片描述
在页面上传这个zip debug可以看到有一个生成的临时目录,但是e是构造的上传文件路径。最后的文件路径f为
C:\Users\ADMINI~1\AppData\Local\Temp\yangyali17948117268383897513…\Users\Administrator.webgoat-2023.5-SNAPSHOT\PathTraversal\yangyali\yangyali.jpg 经过回溯,文件被放到了我们想放到的地方,。
**加粗样式**

在这里插入图片描述
在这里插入图片描述
然后我翻到第8节,发现有答案。。

08 Solution

First, let’s create a zip file with an image inside:

curl -o webwolf.jpg http://127.0.0.1:9090/images/wolf.png
zip profile.zip webwolf.jpg

Now let’s upload this as our profile image. We can see nothing happens as mentioned in the assignment there is a bug in the software, and the result we see on the screen is:

Zip file extracted successfully failed to copy the image. Please get in touch with our helpdesk.
Let’s create a zip file that traverses to the top and then back into the given directory in the assignment.

First, create the directory structure:

mkdir -p C:\Users\Administrator/.webgoat-2023.5-SNAPSHOT/PathTraversal/yangyali
cd C:\Users\Administrator/.webgoat-2023.5-SNAPSHOT/PathTraversal/yangyali
curl -o yangyali.jpg http://127.0.0.1:9090/images/wolf.png
zip profile.zip ../../../../../../../..C:\Users\Administrator/.webgoat-2023.5-SNAPSHOT/PathTraversal/yangyali/yangyali.jpg

这个方法,我zip就失败,提示name not matched。
Now, if we upload this zip file, it solves the assignment.

Why did this work?

In the code, the developers used the following fragment:

ZipFile zip = new ZipFile(uploadedZipFile);
Enumeration<? extends ZipEntry> entries = zip.entries();
while (entries.hasMoreElements()) {
  ZipEntry e = entries.nextElement();
  File profilePicture = new File(uploadDirectory, e.getName());
  InputStream is = zip.getInputStream(e);
  Files.copy(is, f.toPath(), StandardCopyOption.REPLACE_EXISTING);
}

修改这个漏洞需要确保第5行的目录是你所期望的目录,File profilePicture = new File(uploadDirectory, e.getName());同目录遍历防护一样,使用
profilePicture.getCanonicalPath() 确保路径是你所期望的路径。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值