DEV Community


IvyFinal CTF (Crypto 1) - Warmup 50 points

A Software Engineer trying to venture out to the Security world... Documenting my learning journey.
・1 min read

You can get the file here.

Warmup - 50 points

It's important to warmup before physical activity.

Free hint, don't get used to it: XOR cipher was used for this flag's encryption with a single byte key.

Let's Begin

So we practically need to find the key that was xored. Since its just 1 byte, its easy to bruteforce.

And the flag is always in the format if ivyctf{...}.

So lets find the key that simply xors to match the first letter i.

In [19]: def get_key(contents):
    ...:     for i in range(256):
    ...:         if ord(contents[0]) ^ i == ord('i'):
    ...:             return i
Enter fullscreen mode Exit fullscreen mode

Above is just a function that gets the content of the file and goes through the value of a byte, 0 to 255.
For each of the value, we xor it with the first letter and see if it hits the letter i.

Once we get the key, we simply xor it with the rest of the file.

In [21]: def crack_flag(contents, key):
    ...:     flag = []
    ...:     for ch in contents:
    ...:         flag.append(chr(ord(ch) ^ key))
    ...:     print(''.join(flag))
Enter fullscreen mode Exit fullscreen mode

Once we get our functions right, lets run them.

In [23]: contents = open('flag.enc', 'r').read()

In [24]: crack_flag(contents, get_key(contents))
Enter fullscreen mode Exit fullscreen mode

Discussion (0)