mirror of
https://github.com/bvanroll/odiseectf.git
synced 2025-08-29 03:42:41 +00:00
fam
This commit is contained in:
BIN
aesFINISHED/AES-CBC26.png
Normal file
BIN
aesFINISHED/AES-CBC26.png
Normal file
Binary file not shown.
BIN
aesFINISHED/AES-CBC26_decrypted.png
Normal file
BIN
aesFINISHED/AES-CBC26_decrypted.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 150 KiB |
112
aesFINISHED/decryptAESCBC.py
Normal file
112
aesFINISHED/decryptAESCBC.py
Normal file
@@ -0,0 +1,112 @@
|
||||
import sys
|
||||
import os
|
||||
import getpass
|
||||
import itertools
|
||||
from Crypto.Cipher import AES
|
||||
from Crypto.Hash import MD5, SHA256
|
||||
|
||||
|
||||
# generates key based on password
|
||||
def generatekey(pw):
|
||||
password = pw # getpass.getpass('Password for decryption: ')
|
||||
#print(password)
|
||||
h = MD5.new()
|
||||
h.update(password.encode())
|
||||
#h.update(password)
|
||||
return h.digest()
|
||||
|
||||
def generatePass():
|
||||
temp = []
|
||||
alpha = ["a", "z", "e", "r", "t", "y", "u", "i", "o", "p"]
|
||||
for i in itertools.product(alpha, repeat=6):
|
||||
temp.append(generatekey("".join(i)))
|
||||
return temp
|
||||
|
||||
# calculates and return hash for message
|
||||
def calculatehash(message):
|
||||
h = SHA256.new()
|
||||
h.update(message)
|
||||
# use hexdigest to prevent problems with control characters
|
||||
# e.g. \r in charcter 5, appends 4, then overwrites beginning of message with rest of digest
|
||||
return h.hexdigest()
|
||||
|
||||
|
||||
# check integrity and return cleartext or error message
|
||||
def checkintegrity(decryptedcontent):
|
||||
return calculatehash(decryptedcontent[0:-64]).encode() == decryptedcontent[-64:]
|
||||
|
||||
|
||||
# encrypts content in AES CBC mode
|
||||
def decrypt_AES_CBC(inputfilename, encryptedContent):
|
||||
# create encrypted filename, keep extension
|
||||
outputfilename = inputfilename[0:inputfilename.find('.', len(inputfilename) - 5)] \
|
||||
+ '_decrypted' + inputfilename[inputfilename.find('.'):len(inputfilename)]
|
||||
|
||||
i = 0
|
||||
keys = generatePass()
|
||||
print(len(keys))
|
||||
for key in keys:
|
||||
decipher = AES.new(key, AES.MODE_CBC)
|
||||
decryptedcontent = decipher.decrypt(encryptedContent)
|
||||
ivlength = 16
|
||||
# remove iv and padding
|
||||
decryptedcontent = decryptedcontent[ivlength:-decryptedcontent[-1]]
|
||||
# check Integrity and retain cleartext
|
||||
if checkintegrity(decryptedcontent):
|
||||
cleartext = decryptedcontent[0:-64]
|
||||
outputfile = open(outputfilename, 'wb')
|
||||
outputfile.write(cleartext)
|
||||
outputfile.close
|
||||
outputfilename = outputfilename+"EXTRA"
|
||||
else:
|
||||
cleartext = 'Integrity check error'.encode()
|
||||
print("error:{}".format(i))
|
||||
i = i+1
|
||||
print("done?")
|
||||
|
||||
return
|
||||
|
||||
#
|
||||
#
|
||||
# key = generatekey()
|
||||
# decipher = AES.new(key, AES.MODE_CBC)
|
||||
# decryptedcontent = decipher.decrypt(encryptedContent)
|
||||
#
|
||||
# ivlength = 16
|
||||
# # remove iv and padding
|
||||
# decryptedcontent = decryptedcontent[ivlength:-decryptedcontent[-1]]
|
||||
#
|
||||
# # check Integrity and retain cleartext
|
||||
# if checkintegrity(decryptedcontent):
|
||||
# cleartext = decryptedcontent[0:-64]
|
||||
# else:
|
||||
# cleartext = 'Integrity check error'.encode()
|
||||
#
|
||||
# # useful only for decrypted text files
|
||||
# # print(cleartext)
|
||||
#
|
||||
# # write to file
|
||||
# outputfile = open(outputfilename, 'wb')
|
||||
# outputfile.write(cleartext)
|
||||
# outputfile.close()
|
||||
|
||||
|
||||
for i in sys.argv[1:]:
|
||||
|
||||
inputfilename = i
|
||||
|
||||
print('decrypting ' + inputfilename)
|
||||
|
||||
try:
|
||||
inputfile = open(inputfilename, 'rb')
|
||||
except IOError:
|
||||
print("File " + inputfilename + " not found, working directory: " + os.getcwd())
|
||||
continue
|
||||
else:
|
||||
# if file opened, read content into variable
|
||||
content = inputfile.read()
|
||||
inputfile.close()
|
||||
|
||||
# apply symmetric encryption
|
||||
decrypt_AES_CBC(inputfilename, content)
|
||||
# encrypt_AES_ECB(inputfilename,content)
|
BIN
aesFINISHED/example_enc_azerty.png
Normal file
BIN
aesFINISHED/example_enc_azerty.png
Normal file
Binary file not shown.
4
aesFINISHED/uitleg.txt
Normal file
4
aesFINISHED/uitleg.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
Het png bestand bij deze opgave is symmetrisch versleuteld met AES in CBC mode. Voor integriteitscontrole werd het originele bericht aangevuld met een SHA256 hash alvorens dit geheel versleuteld werd.
|
||||
Het script voor decryptie is beschikbaar. Hieruit kan je afleiden dat het vertrekt van een paswoord. Van het paswoord is geweten dat het bestaat uit zes verschillende letters van de bovenste rij van een azerty-toetsenbord (azertyuiop). Het paswoord wordt dankzij een hash functie omgezet in een 128 bit sleutel, die dan gebruikt wordt voor AES.
|
||||
Je moet het script niet gebruiken of uitvoeren, maar als je dat toch wil doen is de pycryptodome library nodig (en niet pycrypto). Voor het testen van het decryptiemechanisme is er ook een bestand toegevoegd dat versleuteld werd met een gekende sleutel (<28>azerty<74>).
|
||||
Brute force is onmogelijk met een 128 bit sleutel.
|
Reference in New Issue
Block a user