Example #1
0
 def test_challenge_9(self):
     """
     PKCS#7 padding
     """
     data = KEY
     output = KEY + b'\x04\x04\x04\x04'
     self.assertEqual(pkcs7pad(data, 20), output)
Example #2
0
 def test_challenge_9(self):
     """
     PKCS#7 padding
     """
     data = KEY
     output = KEY + b'\x04\x04\x04\x04'
     self.assertEqual(pkcs7pad(data, 20), output)
Example #3
0
def challenge_13():
    min_len = len(profile_for(''))
    for i in range(BLOCKSIZE):
        if len(profile_for('x' * i)) > min_len:
            break
    spacing = i
    chunks = chunk_into(
        profile_for(spacing * b'x' + pkcs7pad(b'admin', BLOCKSIZE)), BLOCKSIZE)
    cut = chunk_into(
        profile_for(spacing * b'x' + pkcs7pad(b'user', BLOCKSIZE)),
        BLOCKSIZE)[1]
    paste = chunks[1]
    for i in range(BLOCKSIZE):
        chunks = chunk_into(profile_for(i * b'x'), BLOCKSIZE)
        if chunks[-1] == cut:
            chunks[-1] = paste
            return parse_profile(b''.join(chunks)).items()
Example #4
0
 def encrypt(self, data):
     if not len(data) % 16 == 0:
         data = pkcs7pad(data, 16)
     blocks = chunk_into(data, 16)
     ciphertext = list()
     for block in blocks:
         cipherblock = ECB(self.key).encrypt(xor_with_key(block, self._state))
         ciphertext.append(cipherblock)
         self._state = cipherblock
     return b''.join(ciphertext)
Example #5
0
 def encrypt(self, data):
     if not len(data) % 16 == 0:
         data = pkcs7pad(data, 16)
     blocks = chunk_into(data, 16)
     ciphertext = list()
     for block in blocks:
         cipherblock = ECB(self.key).encrypt(
             xor_with_key(block, self._state))
         ciphertext.append(cipherblock)
         self._state = cipherblock
     return b''.join(ciphertext)
Example #6
0
def challenge_14():
    oracle = C14Oracle()
    minimal_ct = oracle(b'')
    min_len = len(minimal_ct)
    logger.info("CT with empty payload {}: {}".format(min_len, minimal_ct))
    pushout = hdr = None
    prefix_pad_to_block_boundary = 0
    for i in range(1, BLOCKSIZE + 1):
        pairs = zip(chunk_into(oracle(i * b'x'), BLOCKSIZE),
                    chunk_into(oracle(i * b'y'), BLOCKSIZE))
        chunk_equality = [a == b for a, b in pairs]
        if hdr is None:
            hdr = chunk_equality.index(False) + 1
        falses = chunk_equality.count(False)
        logger.debug('i: {}, {}'.format(i, falses))
        logger.debug(chunk_equality)
        if not prefix_pad_to_block_boundary and falses > 1:
            prefix_pad_to_block_boundary = i - 1
        if pushout is None and len(chunk_equality) > min_len / BLOCKSIZE:
            pushout = i + 1
    prefix_len = hdr * BLOCKSIZE - prefix_pad_to_block_boundary
    plaintext_len = min_len - prefix_len - pushout + 1
    discard = hdr * BLOCKSIZE
    logger.info('Prefix len: {}'.format(prefix_len))
    logger.info('Plaintext len: {}'.format(plaintext_len))
    logger.info('Prefix pad to block boundary: {}'.format(
        prefix_pad_to_block_boundary))
    logger.info('Discard: {}'.format(discard))
    known = list()
    logger.info('Push out: {}'.format(pushout))
    goldindex = min_len
    while len(known) < plaintext_len:
        insert = (len(known) + pushout) * b'x'
        ct = oracle(insert)
        gold = ct[goldindex:]
        for c in string.printable:
            char = bytes((ord(c), ))
            filler = prefix_pad_to_block_boundary * b'x'
            plaintext = pkcs7pad(char + b''.join(known), BLOCKSIZE)
            compare_to = oracle(filler + plaintext)[discard:]
            # assert len(compare_to) == len(gold)
            # logger.info(compare_to)
            if compare_to.startswith(gold):
                known.insert(0, char)
                break
            logger.debug(known)

    return b''.join(known)
Example #7
0
 def encrypt(self, data):
     if not len(data) % 16 == 0:
         data = pkcs7pad(data, 16)
     e = self.cipher.encryptor()
     return e.update(data) + e.finalize()
Example #8
0
 def __call__(self, data, *args, **kwargs):
     plain = self.prefix + data + self.secret
     return ECB(self.key).encrypt(pkcs7pad(plain, 16))
Example #9
0
 def encrypt(self, data):
     if not len(data) % 16 == 0:
         data = pkcs7pad(data, 16)
     e = self.cipher.encryptor()
     return e.update(data) + e.finalize()
Example #10
0
 def __call__(self, data, *args, **kwargs):
     plain = self.prefix + data + self.secret
     return ECB(self.key).encrypt(pkcs7pad(plain, 16))
Example #11
0
def encrypt():
    plain = random.choice(PLAINTEXTS)
    iv = os.urandom(BLOCKSIZE)
    c = aes.CBC(KEY, iv)
    return iv + c.encrypt(pkcs7pad(plain, BLOCKSIZE))
Example #12
0
def encrypt():
    plain = random.choice(PLAINTEXTS)
    iv = os.urandom(BLOCKSIZE)
    c = aes.CBC(KEY, iv)
    return iv + c.encrypt(pkcs7pad(plain, BLOCKSIZE))