Created
March 13, 2017 17:41
-
-
Save ivanovanton/6461155f315a3a36c66d174f690eeb45 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def task_1(s, size): | |
| try: | |
| if (len(s) % int(size) == 0 ): | |
| s += chr(int(size))*int(size) | |
| return s | |
| else: | |
| s += chr(int(size) - len(s) % int(size))*(int(size) - len(s) % int(size)) | |
| return s | |
| except: | |
| return "Некорректные параметры" | |
| def task_2(s): | |
| try: | |
| byte = s[len(s)-1] | |
| if (s.rfind(byte*ord(byte)) == len(s)-ord(byte)): | |
| return (s[:len(s)-ord(byte)]) | |
| block = s[len(s)-4:] | |
| if (s.rfind(block*int(block[len(block)-1])) == len(s)-len(block)*int(block[len(block)-1])): | |
| return (s[:len(s)-len(block)*int(block[len(block)-1])]) | |
| else: | |
| return 'Строка не имеет дополнения или имеет неправильное дополнение' | |
| except: | |
| return 'Строка не имеет дополнения или имеет неправильное дополнение' | |
| #Задание 1. Реализация дополнения блока до нужной длины по стандарту PKCS7 | |
| try: | |
| k = int(input("Введите длину блока: ")) | |
| text = input("Введите открытый текст: ") | |
| except Exception: | |
| print("Введены некорректные данные") | |
| print("Результат: ") | |
| task_1(k, text) | |
| #YELLOW SUBMARINE | |
| #Задание 2.Проверка дополнения PKCS7 | |
| block = 'ICEICEBABY\x04\x04\x04\x04' | |
| res = task_2(block) | |
| print("Результат: " + str(res)) | |
| #ICE ICE BABY\x05\x05\x05\x05 | |
| from Crypto import Random | |
| from Crypto.Cipher import AES | |
| import base64 | |
| IV = Random.new().read(16) | |
| key = Random.new().read(16) | |
| def encrypt(message, mode): | |
| aes = AES.new(key, mode, IV) | |
| return aes.encrypt(message) | |
| def decrypt(encrypted, mode): | |
| aes = AES.new(key, mode, IV) | |
| return aes.decrypt(encrypted) | |
| #Задание 3. Атака на CBCрежим. Bit-flipping | |
| def func1(text): | |
| mode = AES.MODE_CBC | |
| text = text.replace('=', '').replace(';', '') | |
| plaintext = "comment1=cooking%20MCs;userdata=" + text + "comment2=%20like%20a%20pound%20of%20bacon" | |
| plaintext = task1(16, plaintext) | |
| ciphertext = encrypt(plaintext, mode) | |
| return ciphertext, plaintext | |
| def task3(): | |
| mode = AES.MODE_CBC | |
| my_str = "admin=true" | |
| n = 64 | |
| text = str(input("Введите текст: ")) | |
| ciphertext, plaintext = func1(text) | |
| ciphertext = bytearray(ciphertext) | |
| for i in range(len(my_str)): | |
| ciphertext[n+i-16] = ciphertext[n+i-16] ^ ord(plaintext[n+i]) ^ ord(my_str[i]) | |
| return decrypt(bytes(ciphertext), mode) | |
| #Задание 4.Функция детектирования режима ECB(функция оракул) | |
| import random | |
| def task4(text): | |
| if random.randint(0, 1) == 0: | |
| mode = AES.MODE_CBC | |
| else: | |
| mode = AES.MODE_ECB | |
| text = text.replace('=', '').replace(';', '') | |
| pre = "" | |
| suf = "" | |
| for i in range(random.randint(5, 10)): | |
| pre += chr(random.randint(65, 96)) | |
| for i in range(random.randint(5, 10)): | |
| suf += chr(random.randint(65, 96)) | |
| plaintext = task_1(16, pre + text + suf) | |
| ciphertext = encrypt(plaintext, mode) | |
| my_str = "detect" | |
| n = 16 | |
| ciphertext = bytearray(ciphertext) | |
| for i in range(len(my_str)): | |
| ciphertext[n+i-16] = ciphertext[n+i-16] ^ ord(plaintext[n+i]) ^ ord(my_str[i]) | |
| q = decrypt(bytes(ciphertext), mode) | |
| if q.find(b"detect") > 0: | |
| return "CBC" | |
| else: | |
| return "ECB" | |
| text = input("Введите текст: ") | |
| res = task4(text) | |
| print("Режим шифрования: " + str(res)) | |
| from Crypto import Random | |
| from Crypto.Cipher import AES | |
| IV = Random.new().read(16) | |
| key = Random.new().read(16) | |
| def encrypt(message): | |
| aes = AES.new(key, AES.MODE_CBC, IV) | |
| return aes.encrypt(message) | |
| def decrypt(encrypted): | |
| aes = AES.new(key, AES.MODE_CBC, IV) | |
| return aes.decrypt(encrypted) | |
| #Побайтовое дешифрование режима ECB | |
| import base64 | |
| from Crypto import Random | |
| from Crypto.Cipher import AES | |
| unknownStrBase64 = "Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkgaGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBqdXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUgYnkK" | |
| unknownStr = task_1(base64.b64decode(unknownStrBase64).decode('utf-8'), 16) | |
| key = Random.new().read(16) | |
| mode = AES.MODE_ECB | |
| def AES_128_ECB(text, unknownStr): | |
| aes = AES.new(key, AES.MODE_ECB) | |
| return aes.encrypt(task_1(text + unknownStr, 16)) | |
| #Функция возвращает один блок | |
| def decrypt(block): | |
| dec = '' | |
| text = 'A'*15 | |
| for i in range(16): | |
| for k in range(256): | |
| if (AES_128_ECB(text, block)[:16] == encrypt(text + chr(k), mode)): | |
| dec += chr(k) | |
| break | |
| text = text[1:] + chr(k) | |
| block = block[1:] | |
| return dec | |
| dec = '' | |
| for i in range(0, len(unknownStr), 16): | |
| dec += decrypt(unknownStr[i:i+16]) | |
| print(task_2(dec)) | |
| import urllib.parse as url | |
| from Crypto import Random | |
| from Crypto.Cipher import AES | |
| def string_to_object(string): | |
| return encrypt(task_1(profile_for(dict(url.splitvalue(s) for s in string.split('&'))), 16)) | |
| def profile_for(email): | |
| return url.unquote(url.urlencode([('email',email),('uid',10),('role','user')])) | |
| def encrypt(message): | |
| aes = AES.new(key, AES.MODE_ECB) | |
| return aes.encrypt(message) | |
| def decrypt(encrypted): | |
| aes = AES.new(key, AES.MODE_ECB) | |
| return aes.decrypt(encrypted) | |
| email = '[email protected]' | |
| profile = profile_for(email) | |
| index = profile.find('user') // 16 * 16 | |
| modified_block = task_1(profile[index:].replace('user', 'admin'), 16) | |
| modified_enc = enc[:index] + encrypt(modified_block) | |
| string_to_object(task_2(decrypt(bytes(modified_enc)).decode('utf-8'))) | |
| random_prefix = '' | |
| for i in range(random.randint(0, 100)): | |
| random_prefix += chr(random.randint(65, 90)) | |
| unknownStrBase64 = "Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkgaGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBqdXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUgYnkK" | |
| unknownStr = task_1(base64.b64decode(unknownStrBase64).decode('utf-8'), 16) | |
| key = Random.new().read(16) | |
| def AES_128_ECB(random_prefix, text, unknownStr): | |
| aes = AES.new(key, AES.MODE_ECB) | |
| return aes.encrypt(task_1(random_prefix + text + unknownStr, 16)) | |
| def decrypt(block): | |
| dec = '' | |
| text = 'A'*(len(random_prefix)//16*16 + 16 - len(random_prefix) -1) | |
| for i in range(16): | |
| for k in range(256): | |
| if (AES_128_ECB(random_prefix, text, block)[:len(random_prefix) + len(text) + 1] == encrypt(random_prefix + text + chr(k))): | |
| dec += chr(k) | |
| break | |
| text = text[1:] + chr(k) | |
| block = block[1:] | |
| return dec | |
| dec = '' | |
| for i in range(0, len(unknownStr), 16): | |
| dec += decrypt(unknownStr[i:i+16]) | |
| task_2(dec) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment