Tags: misc
Rating: 5.0
You got nested folders for each letter of the flag until the last folder contained two files with one number each. Each folder has an operation described (+/x) which should be performed on the two numbers in that folder.
I wrote a recursion that runs for each folder to the end of each folder structure and executes the operation on the two numbers there.
If you do this recursively you get a number for each letter of the flag.
Because I didn't know if they used more than the + and x operator I wrote a function which evals the operations
```
def exec_op(one, two, op):
try:
if op == '+':
return one + two
elif op == 'x':
return one * two
elif op == '-':
return one - two
elif op == '/':
return one / two
else:
print("Dont know operator: " + op)
return 0
except:
print('Error in operation: ' + op)
```
This is the recursive function i wrote to either calc the operation between two numbers directly or between two folders recusivly:
```
def calc_value(path, operation):
number = 0
for root, dirs, files in os.walk(path):
if len(files) > 0:
if len(files) != 2:
f=open(root + '/' + files[0], "r")
number += int(f.read())
for dir in dirs:
op = dir[-1:]
number = exec_op(number, calc_value(root+'/'+dir, op), operation)
return number
else:
f=open(root + '/' + files[0], "r")
f2 = open(root + '/' + files[1], "r")
try:
return exec_op(int(f.read()), int(f2.read()), operation)
except:
print(root)
return 0
else:
return exec_op(calc_value(path+'/'+dirs[0], dirs[0][-1:]), calc_value(path+'/'+dirs[1], dirs[1][-1:]), operation)
```
There is one edge case
```
if len(files) != 2:
f=open(root + '/' + files[0], "r")
number += int(f.read())
for dir in dirs:
op = dir[-1:]
number = exec_op(number, calc_value(root+'/'+dir, op), operation)
return number
```
It could happen that a folder contained another folder as well as a file with a number.
At the end i called the recusive function for each letter in the flag
```
flag = ''
for i in range(0, 37):
try:
flag += chr(calc_value('./tree/flag['+str(i)+']/0_+', '+'))
except:
flag += chr(calc_value('./tree/flag['+str(i)+']/0_x', 'x'))
print(flag)
```
`Flag: BAMBOOFOX{Dir_3xpres5i0n_tre3e33eeee}`