Beispiel #1
0
def main():
    """
    decrypt contents of a file encrypted using AES-128 in ECB Mode
    encrypt decrypted plaintext using AES-128 in CTR Mode
    break CTR Mode encryption using `edit_oracle()`
    """
    key = b"YELLOW SUBMARINE"
    cipher = AES.new(key, AES.MODE_ECB)
    ecb = ECBMode(16, cipher.encrypt, cipher.decrypt)

    input_filename = os.path.join(pathlib.Path(__file__).parent, "input")
    with open(input_filename, "r") as input_file:
        base64_str = input_file.read()
    base64_bytes = base64_str.replace('\n', '').encode("utf-8")
    ecb_encrypted = base64.decodebytes(base64_bytes)

    plaintext = ecb.decrypt(ecb_encrypted)

    nonce = bytes(8)
    unknown_key = rand_bytes_gen(16)
    cipher = AES.new(unknown_key, AES.MODE_ECB)

    ctr = CTRMode(
        blksize=16,
        encrypt_blk=cipher.encrypt,
        decrypt_blk=cipher.decrypt,
        nonce=nonce
    )

    encrypted = ctr.encrypt(plaintext)

    edit_oracle = gen_edit_oracle(unknown_key, nonce)

    print(break_rand_access_ctr(encrypted, edit_oracle).decode())
Beispiel #2
0
    def encryption_oracle(b: bytes) -> bytes:
        """
        params:
            b: bytes to encrypt
        returns:
            `b` encrypted using AES-128 with a random key using either
            ECB mode or CBC mode randomly (one in two chance)
            also prepends 5-10 random bytes and appends 5-10 random bytes to
            `b` before encryption
            if CBC mode is used random bytes are used for the IV
        """
        prefix_size = random.randint(5, 10)
        prefix = rand_bytes_gen(prefix_size)
        suffix_size = random.randint(5, 10)
        suffix = rand_bytes_gen(suffix_size)

        plain = PKCS7Padding.apply(prefix+b+suffix, blksize)
        key = rand_bytes_gen(blksize)
        cipher = AES.new(key, AES.MODE_ECB)

        if use_ecb:
            # print("ECB")  # for manual verification
            ecb = ECBMode(blksize, cipher.encrypt, cipher.decrypt)
            return ecb.encrypt(plain)

        iv = rand_bytes_gen(blksize)
        cbc = CBCMode(
            blksize=blksize,
            encrypt_blk=cipher.encrypt,
            decrypt_blk=cipher.decrypt,
            iv=iv
        )

        # print("CBC")
        return cbc.encrypt(plain)
Beispiel #3
0
    def test_encrypt_bytes_of_proper_length(self):
        ecb = ECBMode(2, self.mock_fun, self.mock_fun)
        b = b"\x00\x01\x00\x01"
        expected_bytes = b"\x00\x00\x00\x00"

        actual_bytes = ecb.encrypt(b)

        self.assertEqual(expected_bytes, actual_bytes)
Beispiel #4
0
    def test_encrypt_decrypt_integration_case(self):
        key = b"YELLOW SUBMARINE"
        cipher = AES.new(key, AES.MODE_ECB)
        ecb = ECBMode(16, cipher.encrypt, cipher.decrypt)
        plaintext = (b"Lorem ipsum dolo"
                     b"r sit amet, cons"
                     b"ectetur adipisci"
                     b"ng elitAAAAAAAAA")

        encrypted = ecb.encrypt(plaintext)
        decrypted = ecb.decrypt(encrypted)

        self.assertEqual(plaintext, decrypted)
Beispiel #5
0
def decrypt_profile(encrypted: bytes) -> str:
    """
    params:
        encrypted: profile encrypted using `encrypted_profile()`
    returns:
        encoded profile
    """
    cipher = AES.new(CONSISTENT_KEY, AES.MODE_ECB)
    ecb = ECBMode(16, cipher.encrypt, cipher.decrypt)

    padded = ecb.decrypt(encrypted)

    return PKCS7Padding.unapply(padded).decode()
Beispiel #6
0
def encrypt_profile(profile: str) -> bytes:
    """
    params:
        profile: encoded profile
    returns:
        `profile` encrypted using AES-128 ECB mode with a consistent key
    """
    blksize = 16
    plain = PKCS7Padding.apply(profile.encode(), blksize)
    cipher = AES.new(CONSISTENT_KEY, AES.MODE_ECB)
    ecb = ECBMode(blksize, cipher.encrypt, cipher.decrypt)

    return ecb.encrypt(plain)
Beispiel #7
0
    def encryption_oracle(b: bytes) -> bytes:
        """
        params:
            b: bytes to encrypt
        returns:
            `b` encrypted using AES-128-ECB
            appends `unknownstr` to `b` before encrypting
        """
        plain = PKCS7Padding.apply(b + unknownstr, blksize)
        cipher = AES.new(CONSISTENT_KEY, AES.MODE_ECB)
        ecb = ECBMode(blksize, cipher.encrypt, cipher.decrypt)

        return ecb.encrypt(plain)
Beispiel #8
0
def main():
    """
    decrypt contents of a file encrypted using AES-128 in ECB mode
    """
    key = b"YELLOW SUBMARINE"
    cipher = AES.new(key, AES.MODE_ECB)
    ecb = ECBMode(16, cipher.encrypt, cipher.decrypt)

    input_filename = os.path.join(pathlib.Path(__file__).parent, "input")
    with open(input_filename, 'r') as input_file:
        base64_str = input_file.read()
    base64_bytes = base64_str.replace('\n', '').encode("utf-8")
    encrypted = base64.decodebytes(base64_bytes)

    message = ecb.decrypt(encrypted)

    print(message.decode())
Beispiel #9
0
 def test_init_negative_blksize_raises(self):
     with self.assertRaises(ValueError):
         ECBMode(-1, self.mock_fun, self.mock_fun)
Beispiel #10
0
 def test_init_zero_blksize_raises(self):
     with self.assertRaises(ValueError):
         ECBMode(0, self.mock_fun, self.mock_fun)
Beispiel #11
0
    def test_decrypt_bytes_not_padded_raises(self):
        ecb = ECBMode(2, self.mock_fun, self.mock_fun)
        b = b"\x00\x01\x00\x01\x23"

        with self.assertRaises(ValueError):
            ecb.decrypt(b)
Beispiel #12
0
 def test_encrypt_none_bytes_raises_typeerror(self):
     ecb = ECBMode(1, self.mock_fun, self.mock_fun)
     with self.assertRaises(TypeError):
         ecb.encrypt(None)