Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Crypto.Util.strxor import *
- from pwn import *
- from binascii import unhexlify
- from string import printable as prnt
- import json
- #Puoi balzare questa funzione. trasforma il file binario in 100 stringhe di hex da 1000 caratteri per riga
- def prepare_hex_file(filename):
- with open(filename, 'rb') as f:
- data = f.read()
- KEYSTREAM_SIZE = 1000
- array = []
- with open("file.hex", "w") as f:
- for i in range(0, len(data), KEYSTREAM_SIZE):
- f.write(bytes(data[i:i+KEYSTREAM_SIZE]).hex(sep='\n',bytes_per_sep=1000))
- #f.write('\n')
- def read_and_guess_ks(enc_asBytes: list[bytes], from_mtp_original):
- longest :bytearray = max(enc_asBytes)
- '''ascii_bytes : list[int]= list()
- for i in range(0,256):
- if not isprint(i):
- continue
- ascii_bytes.append(chr(i))'''
- #ascii_bytes.sort(key=sortByFreq)
- ascii_bytes = ' \"\'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ}{-_.;:?!()[]+òàùèé\\|'
- #il keystream è una lista di liste. la lista interna contiene i byte candidati per il keystream
- keystream : list[list[bytes]] = []
- #actual_key = from_mtp_original["key"]
- actual_key = "ba294994ed9812fa18b25afad1e58fe0a36e79b3a7463e0a5481466f6d096776d98089b337dbba70752057f0f60cdf8ae5c052705dcb3778402b2b9f89d590c0cfd81eb591f184cb8e405ca287df87e4b430d2f3e96c090df8bb52a4bbad668804cb95a3abe19041e7b861aaa8a501c6d2dab1508b33a6489fa98526712f59e1________________________________________70__________72__________________________________b1b1d892255a7d97__________________________________________________________________________________________________________________________________________________________________________________________________f793aab3feac16______________c72df9c7fd2f______________________________________________________________________________________________________________________________________________________9d769bab70f792d7fc5bba9572ca8b________________________________________________________________08bb91______________________72b37ef80d7d7ad7836eeabc32bb575a________________________________________________________________________________________________________________________________________________________________________________________2c041699b68a11d55c____________________________________________70__________________________________________97______________________________________________________________________________________4200823c94e46dbc44183bb15b______________________________________b2e42e63e6____________________________________________________________________ec6f169d716ede1a__________________________________________________________________________________________________________________________________________________________a2e9f984ec2b601d10f306c2ef2f2ebb49c23d71924bc7de8cf592a8f3018a9d9bc580e98309e83d7b8ea1bc9decf6326de4dc59eb27b229e224a12dcf86f03a0263dbc102a84f6a5e9ae09d947a5e1e274139876b79fd62046a5af3b6a66f0bc2c1f5fd5c3625a0cfb0f34d06101e35290ed9d9641b657135358e2bffb9a16b721debd3042ffea29be7ca________________________________________________________________9b____________________________________________________________________"
- counter = 0
- #con questo pezzo carico la chiave che ho ricavato con mtp. Puoi balzare
- for i in range(0,len(actual_key), 2):
- if actual_key[i] == '_':
- keystream.insert(i//2, [])
- continue
- keystream.insert(i//2, [int.from_bytes(unhexlify(actual_key[i:i+2]))])
- counter +=1
- print(f"\n {counter}\\1000")
- #!! da qua inizia l'algoritmo vero e proprio
- # per ciascuna colonna dalla lista di ciphertext...
- for col in range(len(longest)):
- if len(keystream[col]) != 0: continue #(mi fido di quello che mi ha già dato mtp)
- guessedKsBytesList :list[bytes] = []
- #...seleziona un byte stampabile...
- for char in ascii_bytes:
- valid_char = True
- #...fanne lo xor con la prima riga (una a caso va bene)...
- guessedKsByte = longest[col] ^ ord(char)
- #...del risultato fanne lo xor con tutti i byte della suddetta colonna
- for line in enc_asBytes:
- #...se non va bene cambia char stampabile
- if chr(line[col] ^ guessedKsByte) not in ascii_bytes:
- valid_char = False
- break
- #...se va bene inserisci nella lista temporanea il byte candidato...
- if valid_char is True:
- guessedKsBytesList.append(guessedKsByte)
- keystream.pop(col) #(accrocchio per tenere assieme [indice col] <-> [posizione keystream])
- #...infine metti la lista temporanea (vuota o piena) nella lista keystream
- keystream.insert(col, guessedKsBytesList)
- #assert len(keystream) <= len(longest)
- print(keystream)
- #verifico quanti byte nuovi ho inserito. (spoiler: un centinaio in più se va bene ma alcuni sono sbagliati)
- counter =0
- for anarray in keystream:
- counter += 1 if len(anarray) ==1 else 0
- print(f"\n {counter}\\1000")
- return keystream
- ''' for i in range(0,len(actual_key), 2):
- if actual_key[i] != '_':
- continue
- #keystream.insert(i//2, [int.from_bytes(unhexlify(actual_key[i:i+2]))])
- if len(keystream[i//2]) != 1:
- continue
- actual_key[i:i+2] = int.to_bytes(keystream[i//2]).hex()'''
- #Qua stampo il plaintext con i byte aggiunti
- def print_ptxt(enc: list[bytes], keystream: list[bytes]|list[list[bytes]]):
- columns = len(max(enc))
- assert columns == len(keystream)
- for line in enc:
- line_to_be_printed = []
- for col in range(columns):
- val = (line[col]) ^ int.from_bytes(keystream[col]) if len(keystream[col]) == 1 else ord('_')
- line_to_be_printed.append(chr(val))
- assert len(line_to_be_printed) == columns
- print("".join(line_to_be_printed))
- def main():
- with open(".\\file.out", "r") as f:
- from_mtp = json.load(f)
- with open(".\\file.hex", 'r') as f:
- enc = f.readlines()
- enc_asBytes : list[bytearray]= [bytearray.fromhex(aline) for aline in enc]
- prepare_hex_file(".\\file.enc")
- keystream = read_and_guess_ks(enc_asBytes, from_mtp)
- print_ptxt(enc_asBytes, keystream)
- '''
- # Qua sotto sta il tuo codice.
- # Se ho capito bene la flag si trova nei primi 56 caratteri di qualche frase ma non l'ho trovata comunque.
- # Ho provato anche con mtp. Comunque sono 100 righe giusto?
- keystream = bytes.fromhex(mtp_original["key"][:56*2])
- for c in enc_asBytes:
- l = min(len(keystream),len(c))
- block= strxor(c[:l],keystream[:l])
- if(b"CRYPTO25" in block):
- print(block)
- #Lorem ipsum dolor sit amet, consectetur adipiscing elit.
- '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement