**Tags:** ast python reversing

Rating:

For this challenge, you are given a single Python AST/CST file.

The first task is to convert it to Python code. Luckily, the file is a [LibCST](https://libcst.readthedocs.io/), not an [AST](https://docs.python.org/3/library/ast.html), so we can convert it byte for byte into code.

We get this code:

```

import binascii

plaintext = "REDACTED"

def exor(a, b):

temp = ""

for i in range(n):

if (a[i] == b[i]):

temp += "0"

else:

temp += "1"

return temp

def BinaryToDecimal(binary):

string = int(binary, 2)

return string

# encryption

PT_Ascii = [ord(x) for x in plaintext]

PT_Bin = [format(y, '08b') for y in PT_Ascii]

PT_Bin = "".join(PT_Bin)

n = 26936

K1 = ... # length n string of 0s and 1s

K2 = ... # length n string of 0s and 1s

L1 = PT_Bin[0:n]

R1 = PT_Bin[n::]

f1 = exor(R1, K1)

R2 = exor(f1, L1)

L2 = R1

f2 = exor(R2, K2)

R3 = exor(f2, L2)

L3 = R2

R3 = ... # length n string of 0s and 1s

L3 = ... # length n string of 0s and 1s

cipher = L3+R3

# decryption (redacted)

plaintext = L6+R6

plaintext = int(plaintext, 2)

plaintext = binascii.unhexlify('%x' % plaintext)

print(plaintext)

```

We are given K1, K2, L3, and R3, and we have to find L1 and R1. Then, we can concatenate L1 and R1, convert it to ASCII, and retrieve the flag.

`exor` is the classical XOR operation, so we can use the fact that `(a ^ b) ^ c == a ^ (b ^ c)`, to figure out f1, f2, L2, R2.

For example,

```

R3 = f2 ^ L2

R3 = R2 ^ K2 ^ L2

R3 = R2 ^ K2 ^ R1

R3 = L3 ^ K2 ^ R1

R1 = L3 ^ K2 ^ R3

```

We can use a similar method to reverse L1.

**Flag:** `3k{almost_done_shizzle_up_my_nizzle}`