遍历 URL 的目录到 Python 的根目录

Traverse directory at URL to root in Python

如何遍历目录以到达 Python 中的根目录?我使用 BeautifulSoup 编写了一些代码,但它显示 'module not found'。所以我有这个:

#
# There is a directory traversal vulnerability in the
# following page http://127.0.0.1:8082/humantechconfig?file=human.conf
# Write a script which will attempt various levels of directory
# traversal to find the right amount that will give access
# to the root directory. Inside will be a human.conf with the flag.
#
# Note: The script can timeout if this occurs try narrowing
# down your search


import urllib.request
import os

req = urllib.request.urlopen("http://127.0.0.1:8082/humantechconfig?file=human.conf")
dirName = "/tmp"
def getListOfFiles(dirName):

    listOfFile = os.listdir(dirName)
    allFiles = list()

    for entry in listOfFile:
        # Create full path
        fullPath = os.path.join(dirName, entry)

        if os.path.isdir(fullPath):
            allFiles = allFiles + getListOfFiles(fullPath)
        else:
            allFiles.append(fullPath)

    return allFiles
listOfFiles = getListOfFiles(dirName)
print(listOfFiles)

for file in listOfFiles:
 if file.endswith(".conf"):
 f = open(file, "r")
 print(f.read())

这输出:

/tmp/level-0/level-1/level-2/human.conf

User : Human 66

Flag: Not-Set (Must be Root Human)

不过。如果我将 URL 更改为 'http://127.0.0.1:8082/humantechconfig?file=../../../human.conf' 它会给我输出:

User : Human 66

Flag: Not-Set (Must be Root Human)




User : Root Human

Flag: Well done the flag is: {}

它所处的目录遍历级别波动很大,从/tmp/level-2/tmp/level-15;如果是我写的那个,那么它说我是 'Root Human'。但是它不会给我 flag,尽管我突然 'Root Human'。我遍历目录的方式有问题吗?

如果我把req = urllib.request.urlopen("http://127.0.0.1:8082/humantechconfig?file=human.conf")那一行去掉,好像一点关系都没有。我怎样才能真正将代码发送到 URL? 谢谢!

网络发现月球基地挑战? 对于这个,您需要在 human.conf 前面添加 '../'(例如“http://127.0.0.1:8082/humantechconfig?file=../human.conf”),这将成为您的 URL。这个URL你需要申请(using urllib.request.urlopen(URL))。 挑战的主要部分是多次附加 ../ ,使用简单的循环应该不会很难。您不需要使用 OS。 确保在找到标志后打破循环(否则它会进入无限循环并给你错误)。