Let's break down the code and write a script to reverse the scrambling process.
Original Scrambling Code
flag = open("flag.txt").read()
assert len(flag) == 64
x = "".join([
flag[-8::-8],
flag[-7::-8],
flag[-6::-8],
flag[-5::-8],
flag[-4::-8],
flag[-3::-8],
flag[-2::-8],
flag[-1::-8],
])
print(x[1::2] + x[0::2])
Given the output:
Z2yFm7bCjR6SMWOCSiw{wqKWoJxTtxP4Hf74mQZ4qmghcu1mdX9HND7u8oxF}JsR
We want to unscramble it to recover the original flag.
Step 1: Understand the Scrambling
A. The x Construction
flag[-8::-8] means: start at index -8 (which is len(flag)-8), go backwards in steps of 8, until the start.
- So, for each
i in 0..7, flag[-(8-i):: -8] gives every 8th character, starting from the end, offset by i.
So, x is a concatenation of 8 such slices, each of length 8 (since 64/8 = 8), so x is length 64.
B. The Final Output
x[1::2] + x[0::2]:
x[1::2]: all odd-indexed characters of x
x[0::2]: all even-indexed characters of x
- Concatenate them.
So, the output is a permutation of the original flag.
Step 2: Unscramble
Given the output string, let's call it scrambled.
We need to reverse the process:
- Split the string into two halves:
- The first 32 characters are
x[1::2]
- The last 32 characters are
x[0::2]
- Reconstruct
x:
- For i in 0..63:
- If i is even: take from
x[0::2] (second half)
- If i is odd: take from
x[1::2] (first half)
- Reconstruct the flag:
- We know how
x was built from the flag. We need to invert that process.
A. Reconstruct x
scrambled = "Z2yFm7bCjR6SMWOCSiw{wqKWoJxTtxP4Hf74mQZ4qmghcu1mdX9HND7u8oxF}JsR"
assert len(scrambled) == 64
x = [''] * 64
# First 32 are x[1::2], last 32 are x[0::2]
x[1::2] = scrambled[:32]
x[0::2] = scrambled[32:]
x = ''.join(x)
B. Reconstruct the flag
Recall how x was built:
x = "".join([
flag[-8::-8], # 0
flag[-7::-8], # 1
flag[-6::-8], # 2
flag[-5::-8], # 3
flag[-4::-8], # 4
flag[-3::-8], # 5
flag[-2::-8], # 6
flag[-1::-8], # 7
])
Each of these slices is 8 characters, so:
- x[0:8] = flag[-8::-8]
- x[8:16] = flag[-7::-8]
- x[16:24] = flag[-6::-8]
- x[24:32] = flag[-5::-8]
- x[32:40] = flag[-4::-8]
- x[40:48] = flag[-3::-8]
- x[48:56] = flag[-2::-8]
- x[56:64] = flag[-1::-8]
But what does flag[-8::-8] mean? Let's see:
flag[-8::-8] is: start at index 56, go to 48, 40, ..., 0 (inclusive), so 8 elements.
flag[-7::-8] is: start at 57, go to 49, ..., 1
- etc.
So, for each i in 0..7, for j in 0..7:
- x[8i + j] = flag[56 + j - 8i] (But let's check this.)
But more simply, let's reconstruct the flag:
For each i in 0..7:
- For each j in 0..7:
- x[8i + j] = flag[56 - 8j + i]
But let's try to reconstruct the flag:
Let flag be an array of 64 characters.
For each i in 0..7:
For each j in 0..7:
x[8i + j] = flag[56 + i - 8j]
So, for k in 0..63:
i = k // 8
j = k % 8
x[k] = flag[56 + i - 8*j]
We want to invert this: for each position p in flag (0..63), what is its position in x?
Set p = 56 + i - 8*j
But i = k // 8, j = k % 8, so k = 8*i + j
So, for each p in 0..63, we want to find k such that p = 56 + i - 8*j
But that's a bit messy. Let's instead reconstruct the flag by filling in the positions.
For each i in 0..7:
For each j in 0..7:
flag[56 + i - 8j] = x[8i + j]
But 56 + i - 8j can be negative, but for j in 0..7, 8j goes from 0 to 56, so 56 + i - 8*j goes from 56 + i to i - 56.
But let's try:
For i in 0..7:
For j in 0..7:
pos = 56 + i - 8j
x_index = 8i + j
flag[pos] = x[x_index]
But since pos can be negative, let's check for j in 0..7:
- For i = 0:
- j = 0: pos = 56 + 0 - 0 = 56
- j = 1: pos = 56 + 0 - 8 = 48
- j = 2: pos = 56 + 0 - 16 = 40
- j = 3: pos = 56 + 0 - 24 = 32
- j = 4: pos = 56 + 0 - 32 = 24
- j = 5: pos = 56 + 0 - 40 = 16
- j = 6: pos = 56 + 0 - 48 = 8
- j = 7: pos = 56 + 0 - 56 = 0
So, for i in 0..7, for j in 0..7:
pos = 56 + i - 8j
x_index = 8i + j
flag[pos] = x[x_index]
So, let's implement this.
Step 3: Full Unscrambling Code
scrambled = "Z2yFm7bCjR6SMWOCSiw{wqKWoJxTtxP4Hf74mQZ4qmghcu1mdX9HND7u8oxF}JsR"
assert len(scrambled) == 64
# Step 1: Reconstruct x
x = [''] * 64
x[1::2] = scrambled[:32]
x[0::2] = scrambled[32:]
x = ''.join(x)
# Step 2: Reconstruct flag
flag = [''] * 64
for i in range(8):
for j in range(8):
pos = 56 + i - 8*j
x_index = 8*i + j
flag[pos] = x[x_index]
flag = ''.join(flag)
print(flag)
Result
Running this code will print the original flag.
Summary
- The code above will unscramble the given output and recover the original flag.
If you want to see the result, just run the code above!