def edit(cipher, offset, new_plain): global key new_cipher = aes_ctr(new_plain, key, 0) return cipher[:offset] + new_cipher + new_cipher[offset + len(new_cipher):]
from chall10 import decrypt_aes_cbc from chall18 import aes_ctr import base64, os def edit(cipher, offset, new_plain): global key new_cipher = aes_ctr(new_plain, key, 0) return cipher[:offset] + new_cipher + new_cipher[offset + len(new_cipher):] def pwn_edit(cipher): payload = b'A' * len(cipher) new_cipher = edit(cipher, 0, payload) return bytes([x1 ^ x2 ^ ord('A') for x1, x2 in zip(cipher, new_cipher)]) iv = b'\0' * 16 key = b'YELLOW SUBMARINE' cipher = base64.b64decode( b"CRIwqt4+szDbqkNY+I0qbNXPg1XLaCM5etQ5Bt9DRFV/xIN2k8Go7jtArLIyP605b071DL8C+FPYSHOXPkMMMFPAKm+Nsu0nCBMQVt9mlluHbVE/yl6VaBCjNuOGvHZ9WYvt51uR/lklZZ0ObqD5UaC1rupZwCEK4pIWf6JQ4pTyPjyiPtKXg54FNQvbVIHeotUG2kHEvHGS/w2Tt4E42xEwVfi29J3yp0O/TcL7aoRZIcJjMV4qxY/uvZLGsjo1/IyhtQp3vY0nSzJjGgaLYXpvRn8TaAcEtH3cqZenBooxBH3MxNjD/TVf3NastEWGnqeGp+0D9bQx/3L0+xTf+k2VjBDrV9HPXNELRgPN0MlNo79p2gEwWjfTbx2KbF6htgsbGgCMZ6/iCshy3R8/abxkl8eK/VfCGfA6bQQkqs91bgsT0RgxXSWzjjvh4eXTSl8xYoMDCGa2opN/b6Q2MdfvW7rEvp5mwJOfQFDtkv4M5cFEO3sjmU9MReRnCpvalG3ark0XC589rm+42jC4/oFWUdwvkzGkSeoabAJdEJCifhvtGosYgvQDARUoNTQAO1+CbnwdKnA/WbQ59S9MU61QKcYSuk+jK5nAMDot2dPmvxZIeqbB6ax1IH0cdVx7qB/Z2FlJ/U927xGmC/RUFwoXQDRqL05L22wEiF85HKx2XRVB0F7keglwX/kl4gga5rk3YrZ7VbInPpxUzgEaE4+BDoEqbv/rYMuae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ndoRu1+XCsup/AtkLidsSXMFTo/2Ka739+BgYDuRt1mE9EyuYyCMoxO/27sn1QWMMd1jtcv8Ze42MaM4y/PhAMp2RfCoVZALUS2K7XrOLl3s9LDFOdSrfD8GeMciBbfLGoXDvv5Oqq0S/OvjdID94UMcadpnSNsist/kcJJV0wtRGfALG2+UKYzEj/2TOiN75UlRvA5XgwfqajOvmIIXybbdhxpjnSB04X3iY82TNSYTmLLAzZlX2vmV9IKRRimZ2SpzNpvLKeB8lDhIyGzGXdiynQjFMNcVjZlmWHsH7eItAKWmCwNkeuAfFwir4TTGrgG1pMje7XA7kMT821cYbLSiPAwtlC0wm77F0Ta7jdMrLjMO29+1958CEzWPdzdfqKzlfBzsba0+dS6mcW/YTHaB4bDyXechZBk/35fUg+4geMj6PBTqLNNWXBX93dFC7fNyda+Lt9cVJnlhIi/61fr0KzxOeXNKgePKOC3Rz+fWw7Bm58FlYTgRgN63yFWSKl4sMfzihaQq0R8NMQIOjzuMl3Ie5ozSa+y9g4z52RRc69l4n4qzf0aErV/BEe7FrzRyWh4PkDj5wy5ECaRbfO7rbs1EHlshFvXfGlLdEfP2kKpT9U32NKZ4h+Gr9ymqZ6isb1KfNov1rw0KSqYNP+EyWCyLRJ3EcOYdvVwVb+vIiyzxnRdugB3vNzaNljHG5ypEJQaTLphIQnlP02xcBpMNJN69bijVtnASN/TLV5ocYvtnWPTBKu3OyOkcflMaHCEUgHPW0fmGfld4i9Tu35zrKvTDzfxkJX7+KJ72d/V+ksNKWvwn/wvMOZsa2EEOfdCidmoql027IS5XvSHynQtvFmw0HTk9UXt8HdVNTqcdy/jUFmXpXNP2Wvn8PrU2DhkkIzWhQ5Rxd/vnM2QQr9Cxa2J9GXEV3kGDiZV90+PCDSVGY4VgF8y7GedI1h" ) plain = decrypt_aes_cbc(cipher, key, iv) key = os.urandom(16) cipher = aes_ctr(plain, key, 0) print(pwn_edit(cipher))
from chall18 import aes_ctr from chall16 import check_admin import os key = bytes(bytearray(os.urandom(16))) def oracle26(a): b = b"comment1=cooking%20MCs;userdata=" + a.replace(b';', b'').replace( b'=', b'') + b";comment2=%20like%20a%20pound%20of%20bacon" return aes_ctr(b, key, 0) def pwn_oracle26(target): payload = b"A" * (len(target) + 1) print(payload) out = oracle26(payload) print(out) pre = len("comment1=cooking%20MCs;userdata=") post = len(";comment2=%20like%20a%20pound%20of%20bacon") new = out[:pre + 1] print(new) for i in range(len(target)): new += bytes([out[pre + 1 + i] ^ ord('A') ^ target[i]]) return new + out[-post:] print(aes_ctr(pwn_oracle26(b";admin=true"), key, 0))
def oracle26(a): b = b"comment1=cooking%20MCs;userdata=" + a.replace(b';', b'').replace( b'=', b'') + b";comment2=%20like%20a%20pound%20of%20bacon" return aes_ctr(b, key, 0)
score += CHARACTER_FREQ.get(chr(byte).lower(), 0) return score def singlechar_xor_brute_force(ciphertext): candidates = [] for key_candidate in range(256): plaintext_candidate = singlechar_xor(ciphertext, key_candidate) candidate_score = get_english_score(plaintext_candidate) result = { 'key': key_candidate, 'score': candidate_score, 'plaintext': plaintext_candidate } candidates.append(result) return sorted(candidates, key=lambda c: c['score'], reverse=True)[0] plains = [b'SSBoYXZlIG1ldCB0aGVtIGF0IGNsb3NlIG9mIGRheQ==', b'Q29taW5nIHdpdGggdml2aWQgZmFjZXM=', b'RnJvbSBjb3VudGVyIG9yIGRlc2sgYW1vbmcgZ3JleQ==', b'RWlnaHRlZW50aC1jZW50dXJ5IGhvdXNlcy4=', b'SSBoYXZlIHBhc3NlZCB3aXRoIGEgbm9kIG9mIHRoZSBoZWFk', b'T3IgcG9saXRlIG1lYW5pbmdsZXNzIHdvcmRzLA==', b'T3IgaGF2ZSBsaW5nZXJlZCBhd2hpbGUgYW5kIHNhaWQ=', b'UG9saXRlIG1lYW5pbmdsZXNzIHdvcmRzLA==', b'QW5kIHRob3VnaHQgYmVmb3JlIEkgaGFkIGRvbmU=', b'T2YgYSBtb2NraW5nIHRhbGUgb3IgYSBnaWJl', b'VG8gcGxlYXNlIGEgY29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pcyBwYXJ0', b'SW4gdGhlIGNhc3VhbCBjb21lZHk7', b'SGUsIHRvbywgaGFzIGJlZW4gY2hhbmdlZCBpbiBoaXMgdHVybiw=', b'VHJhbnNmb3JtZWQgdXR0ZXJseTo=', b'QSB0ZXJyaWJsZSBiZWF1dHkgaXMgYm9ybi4='] key = bytes(bytearray(os.urandom(16))) ciphers = [] for i in plains: print(i) i = base64.b64decode(i) ciphers.append(aes_ctr(i, key, 0)) lenghts = [len(x) for x in ciphers] max_len = max(lenghts) guessed_key =b'' for i in range(max_len): single_key_xor = b''.join([bytes([cipher[i]]) if len(cipher) > i else b'' for cipher in ciphers]) guessed_key += bytes([singlechar_xor_brute_force(single_key_xor)["key"]]) for cipher in ciphers: print(bytes([b1 ^ b2 for b1, b2 in zip(cipher, guessed_key)]))