SecuInside - crypto - 200

Challenge Text

Before sleeping, wooyaggo always read weird articles from internet. All you got is some binaries(not perfect) and network traffic from following command.

wooyaggo@genius_anncc:~$ python pillow_reader.pyc 5151 "secret" [AdminKey]

Follow instructions in the "secret" to get a flag.

Challenge code available here.

Opening up the pcap shows a partially encrypted conversation:

Welcome to Pillow Reader. Here are some articles from online.

Not too helpful yet; let's examine the source code. Oh... compiled python. Let's import it!

>>> import pillow_reader
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: bad magic number in 'pillow_reader': b'l\x0c\r\n'

Boo. Fortunately, the code is (mostly) decompilable and Decompyle++ did a pretty good job at it (just a couple touch ups required). shows how to talk with the server, but nothing unexpected. reveals the cryptosystem though:

def gen_keypair(bits = 512):
    p = PrimeUtils.get_prime(bits // 2)
    q = PrimeUtils.get_prime(bits // 2)
    n = p * q
    return (PrivKey(p, q, n), PubKey(n))

def encrypt(self, plain):
    while True:
        r = PrimeUtils.get_prime(int(round(math.log(self.pubkey.n, 2))))
        if r &gt; 0 and r &lt; self.pubkey.n:
            cipher = pow(self.pubkey.g, plain, self.pubkey.n_sq) * pow(r, self.pubkey.n, self.pubkey.n_sq) % self.pubkey.n_sq
            return cipher

def decrypt(self, cipher):
    if not self.privkey is not None:
        raise AssertionError('Private key must be exist')
    if not self.pubkey is not None:
        raise AssertionError('Public key must be exist')
    pubkey = self.pubkey
    privkey = self.privkey
    plain = privkey.m * ((pow(cipher, privkey.l, pubkey.n_sq) - 1) // pubkey.n) % pubkey.n
    return plain

This is a Paillier Cryptosystem. Unfortunately, no obvious vulnerabilities stand out in the implementation, but homomorphic encryption is rare enough to raise eyebrows. We now have enough source code to attempt a connection:

$ python 5151 secret
Welcome to Pillow Reader. Here are some articles from online.
Read [secret]
to get flag, just concat flag1 and flag2.
$ python 5151 flag1
Welcome to Pillow Reader. Here are some articles from online.
Read [flag1]
Permission denied
your account is guest

So we need to escalate privileges and we don't know the password to do so. The trick is that the server always uses the same key and and, while we don't know the password, remember that we do have an encrypted request from a (presumably) privileged account in the pcap. Normally, we couldn't modify the request without rendering the decrypted version nonsense, but homomorphic encryption allows additive changes to survive decryption! Specifically, E(p0) * E(p1) = E(p0 + p1 mod m^2).

>>> raw_cmd(solve.i2s(solve.s2i(binascii.unhexlify(encrypted_blob))*solve.g_pki_server.encrypt(0)))
Read [secret]
to get flag, just concat flag1 and flag2.
>>> raw_cmd(solve.i2s(solve.s2i(binascii.unhexlify(encrypted_blob))*solve.g_pki_server.encrypt(10)))
Read [secret^T]
File secret^T not exists
>>> raw_cmd(solve.i2s(solve.s2i(binascii.unhexlify(encrypted_blob))*solve.g_pki_server.encrypt((256-ord('e')+ord(' '))*256**5)))
Read [fecret]
File fecret not exists

Progress! Now it's really just a matter of brute forcing the correct offset (or solving it intelligently - I went for the former):

>>> s=raw((256-ord('e')+ord(' '))*256**5+(ord('f')-ord('c')-1)*256**4+(256-ord('r')+ord('l')-1)*256**3+(256-ord('e')+ord('a')-1)*256**2+(256-ord('t')+ord('g'))*256+(ord('1')+ord('1')-ord(';')))
Read [flag1]
The flag is "7h053 wh0 bu1ld b3n347h
>>> s=raw((256-ord('e')+ord(' '))*256**5+(ord('f')-ord('c')-1)*256**4+(256-ord('r')+ord('l')-1)*256**3+(256-ord('e')+ord('a')-1)*256**2+(256-ord('t')+ord('g'))*256+(ord('1')+ord('2')-ord(';')))
Read [flag2]
 7h3 574r5 bu1ld 700 l0w."(without quote)

Flag: 7h053 wh0 bu1ld b3n347h 7h3 574r5 bu1ld 700 l0w.

Code available on Github

- Kelson (