#!/usr/bin/python3 -uimport osfrom binascii import hexlify, unhexlifyflag =open("./flag","rb").read()classbb(object):def__init__(self,key): self.meh = [x for x inrange(256)] j =0for i inrange(256): j = (j + self.meh[i]+ key[i%len(key)])&0xff self.meh[i], self.meh[j]= self.meh[j], self.meh[i] self.cat =0 self.mouse =0defcrypt(self,string): out = []for c in string: self.cat = (self.cat+1)&0xff self.mouse = (self.cat+self.meh[self.cat])&0xff self.meh[self.cat], self.meh[self.mouse]= self.meh[self.mouse], self.meh[self.cat] k = self.meh[ (self.meh[self.cat]+self.meh[self.mouse])&0xff]//2 out.append((c+k)&0xff)returnbytearray(out)cipher =bb(os.urandom(32))whileTrue:print("Commands: \n(e)ncrypt msg or (p)rint flag") choice =input()if choice =='e': message =input()print(hexlify(cipher.crypt(unhexlify(message))))elif choice =='p':print(hexlify(cipher.crypt(flag)))else:print("meh!")
Solution
So I'm not sure about this task's title and description since only 4 teams managed to solve it, and I'm also not sure why there were only 4 solves since it was pretty simple.
We are given a service which runs the given Python script. We are able to encrypt messages and print an encrypted version of the flag:
Commands:
(e)ncrypt msg or (p)rint flag
e
61626364
b'dba4abb9'
Commands:
(e)ncrypt msg or (p)rint flag
p
b'73bf75c1d4a8ac5fd1cc9bd9388290906dadc7388298789b97879291598bd3d58582c8787c89c1d6af882b'
It looks like the service initializes some kind of cryptographic stream with a random 32-byte key.
The algorithm looks a lot like RC4, except for two (not so) small details: addition is used instead of XOR, and the keystream byte is divided by 2 before being used.
Knowing about RC4 was not needed to solve this challenge, you just have to understand that the random key is used to produce a pseudo-random infinite stream of bytes that is used to encrypt plaintexts, here using addition modulo 256 instead of XOR.
Asking for the flag twice will encrypt it twice, but the keystream will be at a different position so the output is different.
Commands:
(e)ncrypt msg or (p)rint flag
p
b'ba9acd8cfab0c47b8ebaf4af6dc450656ba8798f7cadd6c47acebaae9789c3b777d2bd8f697bcff2add369'
Commands:
(e)ncrypt msg or (p)rint flag
p
b'6c767a8cb94c9848957dc282a6c75aa2cc8fdf9b9cb2f1788c698ca8b08b6dc24da1d87b5371a0a7ea9e3f'
Obviously, the weakness lies in the division by 2 of k in the crypt method. What this means is that k can only take values in 0, ..., 127 before it is added to our plaintext byte.
Since we know the flag will be a readable ASCII string, this reduces the amount of possibilities for a plaintext character. For instance, if we consider this encrypted version of the flag:
...it starts with 0xBA, and if we note p[0] the first character of the flag, we have p[0] + k = 0xBA.
As k can only take values between 0 and 127, we know p[0] can only take values between 0x3B and 0xBA, and since we assume it is readable ASCII, we know p[0] is somewhere between 0x3B and 0x7F.
Now we can do this for every byte of the plaintext but there's still too many possibilities... We cannot retrieve the flag like that.
Of course, the idea was to exploit the fact that we can encrypt the flag several times. For each byte, the higher its encrypted value is, the smallest the space of possibilities for the associated plaintext character is.
Therefore, each time we ask for a new encrypted flag, there is a probability that we're reducing the number of flag candidates. If we're asking for enough encrypted flags, we can thus reconstruct it with a high probability.
Here's the exploit:
from binascii import unhexlify as unheximport sockets = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.connect(('13.233.66.116', 5000))s.recv(4096)L = []charset = b'{}ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'count =0whileTrue: s.send(b'p\n') res = s.recv(4096)try: res =unhex(res.split(b'\n')[0].split(b"'")[1])except:continue L.append(res) plaintext = b''for i inrange(43):for o in charset: good =Truefor k inrange(len(L)): q = (L[k][i] - o) &255if q >=128: good =Falsebreakif good: plaintext +=bytes([o])breakprint(count, plaintext) count +=1