-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge_12.py
149 lines (120 loc) · 4.39 KB
/
challenge_12.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#! /usr/bin/python3
import os, binascii, random, sys, base64, string
from Crypto.Cipher import AES
# from random import randrange
randomKey = os.urandom(16)
obj = AES.new(randomKey,AES.MODE_ECB)
def encrypt(stringToEncypt, key):
a = bytes(a ^ b for (a, b) in zip(stringToEncypt, key))
return a
def aesCBC_encrypt(plaintextBytes, iv):
print("cbc")
blocks = len(plaintextBytes)//16
ciphertext = bytes()
for i in range(blocks):
temp = plaintextBytes[i*16:(i+1)*16]
xorResult = encrypt(temp,iv)
cipher = obj.encrypt(xorResult)
ciphertext += cipher
iv = cipher
return ciphertext
def aesCBC_decrypt(ciphertext, iv):
blocks = len(ciphertext)//16
plaintext = bytes()
for i in range(blocks):
temp = ciphertext[i*16:(i+1)*16]
# print(type(temp))
xor = obj.decrypt(temp)
plaintext += encrypt(xor, iv)
iv = temp
return plaintext
def aesECB_encrypt(ciphertext, iv):
# print("ecb")
# obj = AES.new(key, AES.MODE_ECB)
return obj.encrypt(ciphertext)
# print(plaintext)
def aesECB_decrypt(ciphertext, iv):
# obj = AES.new(key, AES.MODE_ECB)
return obj.decrypt(ciphertext)
# print(plaintext)
def pkcs7(plaintext, blocksize):
padding = blocksize - len(plaintext) % blocksize
pad = chr(padding)
# print(bytes(''.join((plaintext, pad*padding)),'utf-8'))
return ''.join((plaintext, pad*padding))
def randomEncrypt(randomString):
randomString = str(randomString)
iv = os.urandom(16)
randomString = pkcs7(randomString, 16)
randomString = bytes(randomString, 'utf-8')
x = random.randint(2,2)
ciphertext = aesCBC_encrypt(randomString,iv) if x==1 else aesECB_encrypt(randomString,iv)
return ciphertext
def detectECB(ciphertext):
blocksNum = len(ciphertext)//16
blocks = []
for i in range(blocksNum):
blocks.append(ciphertext[i*16:(i+1)*16])
blocks = list(dict.fromkeys(blocks))
if(len(blocks) != blocksNum):
print("ECB DETECTED")
def find_block_size():
current_ctxt = None
for i in range(2, 20):
previous_ctxt = current_ctxt or aesECB_encrypt(pkcs7("A"*1,16),21)
current_ctxt = aesECB_encrypt(pkcs7("A"*i,16),12)
if previous_ctxt[:16] == current_ctxt[:16]:
# print("length::::" + str(i-1))
return i-1
# print(randomKey)
# randomString = os.urandom(16)
# input = sys.argv[1]
input = "AAAAAAAAAAAAAAAA"
# inputLen = len(sys.argv[1])
randomString = bytes(input, 'utf-8')
find_block_size()
decoded = base64.b64decode("Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkgaGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBqdXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUgYnkK")
# print(obj.block_size)
randomString += decoded
decoded = decoded.decode('utf-8')
# print(randomString)
# cipher = randomEncrypt(randomString)
# print(cipher[:16])
# print(cipher[16:32])
cipher = aesECB_encrypt(pkcs7(input,16),None)
# print(cipher[:16])
# print(cipher[16:32])
# ascii = list(range(ord('a'), ord('z') + 1)) +list(range(ord('A'), ord('Z') + 1)) +[ord(' ')]
paddings = ""
for blocksize in range(16):
paddings += str(blocksize)
allChars = string.ascii_letters + string.digits + string.punctuation + string.whitespace + string.printable + string.hexdigits + string.whitespace
# allChars = [chr(i) for i in range(127)]
foundChars = ""
for j in range(len(pkcs7(decoded,16))//16):
for i in range(16):
dict = {}
trancatedInput = input[i:len(input) - 1]
# print("trancatedInput: " + trancatedInput)
cipher = aesECB_encrypt(pkcs7(trancatedInput + decoded,16), None)
ccc = (pkcs7(trancatedInput+decoded,16)[(j*16):(j*16 +16)])
# print("--------")
# print(ccc)
# print(bytes(ccc[len(ccc)-1],'utf-8'))
# for ch in ccc:
# print(ch in allChars)
for c in allChars:
# print(c)
candidate = trancatedInput + foundChars + c
candidate = candidate[len(candidate) - 16: len(candidate)]
# print(candidate)
dict[aesECB_encrypt(candidate, None)] = candidate
# print(dict)
# print(dict[cipher[:16]][15])
# if(len(dict[cipher[(j*16):(j*16 +16)]]) == 16):
try:
key = cipher[(j*16):(j*16 +16)]
foundChars += dict[key][15]
except:
print()
print("found text: " + foundChars)