3 Directory traversal

3 Directory traversal目录遍历攻击


In this section, we’ll explain what directory traversal is, describe how to carry out path traversal attacks and circumvent common obstacles, and spell out how to prevent path traversal vulnerabilities. 如何实施路径遍历攻击,规避常见障碍,以及如何防范路径遍历漏洞

在这里插入图片描述

一、What

What is directory traversal?

Directory traversal (also known as file path traversal) is a web security vulnerability that allows an attacker to read arbitrary files on the server that is running an application. This might include application code and data, credentials for back-end systems, and sensitive operating system files. In some cases, an attacker might be able to write to arbitrary files on the server, allowing them to modify application data or behavior, and ultimately take full control of the server.
目录遍历(也称为文件路径遍历)是一个web安全漏洞,允许攻击者读取正在运行应用程序的服务器上的任意文件。这可能包括应用程序代码和数据、后端系统的凭据和敏感的操作系统文件。在某些情况下,攻击者可能会写入服务器上的任意文件,从而允许他们修改应用程序数据或行为,并最终完全控制服务器。

二、通过目录遍历读取任意文件

Reading arbitrary files via directory traversal

  1. Consider a shopping application that displays images of items for sale. Images are loaded via some HTML like the following:
    <img src="/loadImage?filename=218.png">

  2. The loadImage URL takes a filename parameter and returns the contents of the specified file. The image files themselves are stored on disk in the location /var/www/images/. To return an image, the application appends the requested filename to this base directory and uses a filesystem API to read the contents of the file. In the above case, the application reads from the following file path:
    loadImage URL使用filename参数,并返回指定文件的内容。 映像文件本身存储在磁盘上的/ var / www / images /位置。 为了返回图像,应用程序将请求的文件名附加到此基本目录,并使用文件系统API读取文件的内容。 在上述情况下,应用程序将从以下文件路径读取:/var/www/images/218.png

  3. The application implements no defenses against directory traversal attacks, so an attacker can request the following URL to retrieve an arbitrary file from the server’s filesystem:应用程序没有实现针对目录遍历攻击的防御,因此攻击者可以请求以下URL从服务器的文件系统中检索任意文件https://insecure-website.com/loadImage?filename=../../../etc/passwd

  4. This causes the application to read from the following file path:/var/www/images/../../../etc/passwd

  5. The sequence ../ is valid within a file path, and means to step up one level in the directory structure. The three consecutive ../ sequences step up from /var/www/images/ to the filesystem root, and so the file that is actually read is:
    顺序../在文件路径中有效,表示在目录结构中上一级。 三个连续的../序列从/ var / www / images /升至文件系统根目录,因此实际读取的文件为/etc/passwd

  6. On Unix-based operating systems, this is a standard file containing details of the users that are registered on the server.在基于unix的操作系统上,这是一个标准文件,包含在服务器上注册的用户的详细信息

以下是Python中二叉树的遍历方法的示例代码: ```python # 定义二叉树节点类 class TreeNode: def __init__(self, data): self.data = data self.left_child = None self.right_child = None # 定义二叉树类 class BinaryTree: def __init__(self): self.root = None # 插入节点 def insert(self, data): new_node = TreeNode(data) if self.root is None: self.root = new_node else: current = self.root while True: if data < current.data: if current.left_child is None: current.left_child = new_node return else: current = current.left_child else: if current.right_child is None: current.right_child = new_node return else: current = current.right_child # 层次遍历 def level_order_traversal(self, start): if start is None: return queue = [] queue.append(start) while len(queue) > 0: print(queue[0].data, end=' ') node = queue.pop(0) if node.left_child is not None: queue.append(node.left_child) if node.right_child is not None: queue.append(node.right_child) # 先序遍历 def preorder_traversal(self, start): if start is None: return print(start.data, end=' ') self.preorder_traversal(start.left_child) self.preorder_traversal(start.right_child) # 中序遍历 def inorder_traversal(self, start): if start is None: return self.inorder_traversal(start.left_child) print(start.data, end=' ') self.inorder_traversal(start.right_child) # 后序遍历 def postorder_traversal(self, start): if start is None: return self.postorder_traversal(start.left_child) self.postorder_traversal(start.right_child) print(start.data, end=' ') ``` 以上代码中,`BinaryTree`类定义了二叉树的节点和插入节点的方法,以及四种遍历方法:层次遍历、先序遍历、中序遍历和后序遍历。其中,层次遍历使用了队列,而其他三种遍历方法使用了递归。 以下是使用示例: ```python # 创建二叉树 tree = BinaryTree() tree.insert(5) tree.insert(3) tree.insert(7) tree.insert(1) tree.insert(4) tree.insert(6) tree.insert(8) # 层次遍历 print('层次遍历:', end=' ') tree.level_order_traversal(tree.root) print() # 先序遍历 print('先序遍历:', end=' ') tree.preorder_traversal(tree.root) print() # 中序遍历 print('中序遍历:', end=' ') tree.inorder_traversal(tree.root) print() # 后序遍历 print('后序遍历:', end=' ') tree.postorder_traversal(tree.root) print() ``` 输出结果为: ``` 层次遍历: 5 3 7 1 4 6 8 先序遍历: 5 3 1 4 7 6 8 中序遍历: 1 3 4 5 6 7 8 后序遍历: 1 4 3 6 8 7 5 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值