We need to reverse engineer a python file.

Solution


To reverse engineer code we first need to understand what it does. So I have taken the original source file and commented what each part of the code does:

# 1. stores a user input into the variable flag
flag = input()

# 2. reverses flag and converts it to an array ascii values
flag = list(map(ord,flag[::-1]))

# 3. at each position in the array i, i//2 is subtracted from the ascii value 
for i in range(len(flag)):
    flag[i] -= i//2

# 4. the reverse of flag is appended to itself, twice
for i in range(2):
    flag = flag + flag[::-1]

# result is printed, separated by + symbols
print("Well, here's your output...")
print(*map(chr,flag),sep="+")

Given the output, we need to deduce what the user must have input, and that will be our flag. A good way to do this is create a python script to "travel backwards" - each step undoing what is done in the original file. So we write a python script to take an input, do the opposite of step 4, then do the opposite of step 3, etc and we will get the original input to the original file.

Here is some sample python code that does this:

# remove + separators then store user input as a list of ascii values
encoded = input().split("+")
encoded = list(map(ord,encoded))

# reverse of step 4
# encoded is cut in half, twice
for i in range(2):
	encoded = encoded[:len(encoded)//2]
print(encoded)

# reverse of step 3
# at each position in the array i, i//2 is added to the ascii value
for i in range(len(encoded)):
    encoded[i] += i//2

# reverse of step 2
# encoded is reversed and converted from ascii values into a string
encoded = ''.join(map(chr, encoded[::-1]))

# output result, which will be the flag
print(encoded)

Our flag is:

<aside> 💡 flag{challenge5-qYBqjQwLtFWKWkcbHsMyGmUxs}

</aside>