def random_walk(self, distance: int) -> list: """ Randomly walks `distance` down the chain and returns the list of transition tokens. Parameters: distance (int): Distance to walk down the chain. Returns: list: List of transition tokens from the walk. """ result = [] if distance > 0: rand_num = int.from_bytes(rand_bytes(1), 'big') winning_probability = rand_num / 256 prob_accumulator = 0.0 for token, subchain in self.transitions.items(): prob_accumulator += subchain.probability if prob_accumulator >= winning_probability: result = [token] + subchain.random_walk(distance - 1) break return result
def test_xor_dictionary_attack(self): key = rand_bytes(16) msgA = b'significant risk' msgB = b'this movie sucks' ciphertexts = [xor_buffs(key, msgA), xor_buffs(key, msgB)] attack = XORDictionaryAttack(EnglishAnalyzer(), top_1000) self.assertIn('significant risk', attack.execute(ciphertexts)[0])
def random(size: int=16, byteorder: str='big'): """ Generates a random Bytes object using /dev/urandom. Parameters: size (int): Number of bytes to generate. byteorder (int): Byteorder. Returns: Bytes: Random Bytes. """ return Bytes(rand_bytes(size), byteorder=byteorder)
def random(size: int = 16, byteorder: str = 'big'): """ Generates a random Bitstring object using /dev/urandom. Parameters: size (int): Number of bytes to generate. byteorder (int): Byteorder. Returns: Bitstring: Random Bitstring. """ return Bitstring(rand_bytes(math.ceil(size / 8)), byteorder=byteorder)[:size]
#!/usr/bin/python3 from samson.utilities.general import rand_bytes from samson.block_ciphers.rijndael import Rijndael from samson.block_ciphers.modes.cbc import CBC from samson.attacks.cbc_iv_key_equivalence_attack import CBCIVKeyEquivalenceAttack import base64 import unittest import logging logging.basicConfig( format='%(asctime)s - %(name)s [%(levelname)s] %(message)s', level=logging.DEBUG) key_size = 16 key = rand_bytes(key_size) iv = key aes = Rijndael(key) cbc = CBC(aes, iv) def sender_encrypt(data): return cbc.encrypt(data) def receiver_decrypt(ciphertext): plaintext = cbc.decrypt(ciphertext, unpad=False) if any(int(byte) > 127 for byte in plaintext): raise Exception('Bad characters in {}'.format( base64.b64encode(plaintext)))
def random_encrypt(data): key = rand_bytes(16) cipher = RC4(key) plaintext = (data + secret) return cipher.generate(len(plaintext)) ^ plaintext
#!/usr/bin/python3 import urllib.parse from samson.utilities.general import rand_bytes from samson.block_ciphers.rijndael import Rijndael from samson.block_ciphers.modes.cbc import CBC from samson.block_ciphers.modes.ctr import CTR from samson.attacks.xor_bitflipping_attack import XORBitflippingAttack from samson.oracles.chosen_plaintext_oracle import ChosenPlaintextOracle import unittest key_size = 16 block_size = 16 key = rand_bytes(key_size) iv = rand_bytes(block_size) nonce = rand_bytes(block_size // 2) aes = Rijndael(key) cbc = CBC(aes, iv) def format_data(data): return ("comment1=cooking%20MCs;userdata=" + urllib.parse.quote(data.decode()) + ";comment2=%20like%20a%20pound%20of%20bacon").encode() # CBC Functions def encrypt_data_cbc(data): return cbc.encrypt(format_data(data))
def aes_cbc_oracle(message): aes = Rijndael(rand_bytes(key_size)) ciphertext = CBC(aes, rand_bytes(block_size)).encrypt(zlib.compress(message)) return len(ciphertext)
from samson.utilities.general import rand_bytes from samson.attacks.cbc_padding_oracle_attack import CBCPaddingOracleAttack from samson.oracles.padding_oracle import PaddingOracle from samson.utilities.exceptions import InvalidPaddingException import random import base64 import unittest import logging logging.basicConfig( format='%(asctime)s - %(name)s [%(levelname)s] %(message)s', level=logging.DEBUG) key_size = 16 block_size = 16 key = rand_bytes(key_size) iv = rand_bytes(block_size) aes = Rijndael(key) cbc = CBC(aes, iv) padder = PKCS7(block_size) plaintext_strings = [ 'MDAwMDAwTm93IHRoYXQgdGhlIHBhcnR5IGlzIGp1bXBpbmc=', 'MDAwMDAxV2l0aCB0aGUgYmFzcyBraWNrZWQgaW4gYW5kIHRoZSBWZWdhJ3MgYXJlIHB1bXBpbic=', 'MDAwMDAyUXVpY2sgdG8gdGhlIHBvaW50LCB0byB0aGUgcG9pbnQsIG5vIGZha2luZw==', 'MDAwMDAzQ29va2luZyBNQydzIGxpa2UgYSBwb3VuZCBvZiBiYWNvbg==', 'MDAwMDA0QnVybmluZyAnZW0sIGlmIHlvdSBhaW4ndCBxdWljayBhbmQgbmltYmxl', 'MDAwMDA1SSBnbyBjcmF6eSB3aGVuIEkgaGVhciBhIGN5bWJhbA==', 'MDAwMDA2QW5kIGEgaGlnaCBoYXQgd2l0aCBhIHNvdXBlZCB1cCB0ZW1wbw==', 'MDAwMDA3SSdtIG9uIGEgcm9sbCwgaXQncyB0aW1lIHRvIGdvIHNvbG8=',