def hamming_distance(payload_1, payload_2, type=None): if type != None: return hamming_distance(_string.conv(payload_1, type), _string.conv(payload_2, type)) hamming_distance = 0 for x, y in zip(payload_1, payload_2): z = ord(x) ^ ord(y) while z: hamming_distance += 1 z -= z & (-z) return hamming_distance
def frequency_matchness(payload, type=None): if type != None: return frequency_matchness(_string.conv(payload, type)) # estimated_freq is obtained from https://ocw.mit.edu/ans7870/6/6.006/s08/lecturenotes/files/t8.shakespeare.txt estimated_freq = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.02280148136802659, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.2370624883890251, 0.0016203149830768323, 8.610900520648024e-05, 1.8321064937548985e-07, 0.0, 1.8321064937548985e-07, 3.847423636885287e-06, 0.005692171665447094, 0.00011505628780780763, 0.00011523949845718312, 1.1542270910655861e-05, 0.0, 0.015238362551156993, 0.001479242783057705, 0.014295010917522596, 9.160532468774493e-07, 5.4779984163271466e-05, 0.0001700194826204546, 6.70550976714293e-05, 6.0459514293911655e-05, 1.7038590391920558e-05, 1.5023273248790168e-05, 1.1542270910655861e-05, 7.511636624395084e-06, 7.328425975019594e-06, 0.00017368369560796437, 0.00033472585640902, 0.0031510399586090502, 8.574258390772925e-05, 1.8321064937548985e-07, 8.079589637459102e-05, 0.0019193147628576318, 1.4656851950039188e-06, 0.008150308948118043, 0.002823825738824425, 0.0039384793296249054, 0.0028732926141558073, 0.007801659082356484, 0.002145946336135113, 0.0020453636896279686, 0.003382435008770294, 0.010224253499048586, 0.00037869641225913756, 0.0011351731835305351, 0.004371039672800437, 0.0029079194268877753, 0.005008612732627142, 0.006084242455110643, 0.0021873519428939735, 0.00021582214496432706, 0.005307612512407941, 0.006231177395909786, 0.0072917838451444965, 0.0025885832650262963, 0.0006558941247642537, 0.003022242872098081, 0.00011102565352154686, 0.0016670336986675822, 9.74680654677606e-05, 0.00038199420394789636, 0.0, 0.00038052851875289246, 0.0, 1.300795610565978e-05, 1.8321064937548985e-07, 0.04482505031880485, 0.008527173253883424, 0.012217951785552668, 0.024509737462803657, 0.07413087616096008, 0.01260544230898183, 0.010449419387131065, 0.04001430508750324, 0.03630941933583208, 0.0004968672811063285, 0.00535194948955681, 0.026778251723370974, 0.017511273867309322, 0.03955957625575327, 0.05155382783841847, 0.008523875462194666, 0.00044043840109867763, 0.03827160539064358, 0.03938625898144406, 0.05312650805265767, 0.021035880339994994, 0.006227146761623525, 0.013354957075576959, 0.0008588915242722964, 0.015622555282897396, 0.00020134850366366336, 0.0, 6.045951429391165e-06, 3.664212987509797e-07, 1.8321064937548985e-07, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] actual_freq = [0] * 256 MAX_SCORE = 0 MIN_SCORE = -(reduce((lambda x, y: x + y**2), estimated_freq) + 1) score = 0 for char in payload: actual_freq[ord(char)] += 1 for pos in range(256): actual_freq[pos] /= float(len(payload)) score -= pow(actual_freq[pos] - estimated_freq[pos], 2) return (score - MIN_SCORE) / (MAX_SCORE - MIN_SCORE)
import os import _string import _cipher b64_suffix = """ Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkg aGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBq dXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUg YnkK """.strip().replace('\n', '') suffix = _string.conv(b64_suffix, 'b64') key = os.urandom(16) def encryption_oracle(plaintext): padded_plaintext = _cipher.pad(plaintext + suffix) return _cipher.aes_encrypt(padded_plaintext, key) def attack(): # Cracks ECB mode: Obtain fixed_suffix given [payload + fixed_suffix] # Get block size and length of suffix expand_sizes = [] plaintext_length = 0 old_ciphertext = encryption_oracle('') while len(expand_sizes) < 2: plaintext_length += 1 ciphertext = encryption_oracle('A' * plaintext_length) if len(ciphertext) > len(old_ciphertext):
import _string import _cipher f = open('source/7.txt') b64_ciphertext = f.read() f.close() ciphertext = _string.conv(b64_ciphertext, 'b64') print _cipher.aes_decrypt(ciphertext, 'YELLOW SUBMARINE')
import _string hex_string = '49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d' b64_string = 'SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t' assert _string.conv(hex_string, 'hex', 'b64') == b64_string
import _string hex_string_1 = '1c0111001f010100061a024b53535009181c' hex_string_2 = '686974207468652062756c6c277320657965' hex_string_3 = '746865206b696420646f6e277420706c6179' string_1 = _string.conv(hex_string_1, 'hex') string_2 = _string.conv(hex_string_2, 'hex') string_3 = _string.conv(hex_string_3, 'hex') assert _string.xor(string_1, string_2) == string_3
import _attack import _string hex_ciphertext = '1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736' ciphertext = _string.conv(hex_ciphertext, 'hex') best_plaintext, best_matchness = _attack.xor_single_byte(ciphertext) print '%s [Matchness: %f]' % (best_plaintext, best_matchness)
import _string import _cipher import _utils plaintext = """ Burning 'em, if you ain't quick and nimble I go crazy when I hear a cymbal """.strip() key = 'ICE' hex_ciphertext = _string.conv(_cipher.xor_encrypt(plaintext, key), None, 'hex') expected_hex_ciphertext = """ 0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272 a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f """.replace('\n', '') assert hex_ciphertext == expected_hex_ciphertext
import _string import _cipher f = open('source/10.txt') ciphertext = _string.conv(f.read(), 'b64') def challenge_aes_decrypt(ciphertext, key, iv): # The decrypt method for this challenge. # Expected to have the same plaintext from AES-CBC mode. plaintext = '' last_ciphertext_block = iv for l in range(0, len(ciphertext), 16): ciphertext_block = ciphertext[l:l + 16] plaintext += _string.xor( _cipher.aes_decrypt(ciphertext_block, key, 'ECB'), last_ciphertext_block) last_ciphertext_block = ciphertext_block return plaintext plaintext = _cipher.aes_decrypt(ciphertext, 'YELLOW SUBMARINE', 'CBC', '\x00' * 16) _plaintext = challenge_aes_decrypt(ciphertext, 'YELLOW SUBMARINE', '\x00' * 16) assert plaintext == _plaintext print _plaintext