Skip to content

Instantly share code, notes, and snippets.

@volticks
Created June 3, 2021 18:18
Show Gist options
  • Save volticks/fb062ee8ea98140f367fae81a102940f to your computer and use it in GitHub Desktop.
Save volticks/fb062ee8ea98140f367fae81a102940f to your computer and use it in GitHub Desktop.
# Creates a simple directory maze. May be useful for CTF challenges where file races are involved (hi pwn.college :) )
import os
import shutil
import sys
# How many directories do you want to make? 50 works quite well.
dirdepth = 50
# For some reason the os.symlink() was messing up (probably because of me), so i made my own, worse version
# that works for my purposes
def symlink_v2(dest, src):
return os.system("ln -s -f " + dest + " " + src)
def makedirs(safepath, rootname):
os.chdir(safepath)
for i in range(0, dirdepth):
successful = False
while not successful:
try:
os.mkdir(str(i))
except FileExistsError:
shutil.rmtree(str(i))
continue
os.chdir(str(i))
successful = True
cwd = os.getcwd()
# Make the root symlink and end symlink so we can matrix
if (symlink_v2("/", cwd + "/" + rootname)):# or symlink_v2(cwd, safepath + "/end")):
print("[!] 'ln' failed for some reason, it probably shouldn't be doing this, exiting...")
exit(0)
# Get the full dir
return cwd
def make_matrix(path, matrix_cwd, rootname):
matrix_path = ""
symlinks_max = 40
maxpath = 4096
symlinks_current = 0
fullroot = matrix_cwd + "/" + rootname
while (((len(matrix_path) + (len(fullroot))) <= (maxpath - (len(path)+1))) and (symlinks_current <= symlinks_max)):
matrix_path += fullroot
symlinks_current += 1
if (path[0] == "/"):
matrix_path += path
return matrix_path
else:
matrix_path += "/" + path
return matrix_path
def main():
if (len(sys.argv) < 3):
print(sys.argv[0] + " [matrix path] [name/path to file] [file to save matrix as]")
exit(0)
# 'root' refers to the matrix path
cwd_1 = makedirs(sys.argv[1], "root")
# Matrix using mostly filepaths
mat = make_matrix(sys.argv[2], cwd_1, "root")
# Finally write to a file so you can expand it in your shell: "cat $(cat matrix.txt)"
print(sys.argv[1] + "/" + sys.argv[3])
f = open("/tmp/" + sys.argv[3], "w")
f.write(mat)
f.close()
print("[*] All done, you can find the matrix in /tmp/" + sys.argv[3])
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment