Advertisement
buonaseva_fatelo

Untitled

Jun 6th, 2025 (edited)
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.78 KB | None | 0 0
  1. from Crypto.Util.strxor import *
  2. from pwn import *
  3. from binascii import unhexlify
  4. from string import printable as prnt
  5. import json
  6.  
  7. #Puoi balzare questa funzione. trasforma il file binario in 100 stringhe di hex da 1000 caratteri per riga
  8. def prepare_hex_file(filename):
  9.     with open(filename, 'rb') as f:
  10.         data = f.read()
  11.  
  12.     KEYSTREAM_SIZE = 1000
  13.     array = []
  14.     with open("file.hex", "w") as f:  
  15.         for i in range(0, len(data), KEYSTREAM_SIZE):
  16.             f.write(bytes(data[i:i+KEYSTREAM_SIZE]).hex(sep='\n',bytes_per_sep=1000))
  17.             #f.write('\n')
  18.  
  19. def read_and_guess_ks(enc_asBytes: list[bytes], from_mtp_original):
  20.     longest :bytearray = max(enc_asBytes)
  21.     '''ascii_bytes : list[int]= list()
  22.    for i in range(0,256):
  23.        if not isprint(i):
  24.            continue
  25.        ascii_bytes.append(chr(i))'''
  26.     #ascii_bytes.sort(key=sortByFreq)
  27.     ascii_bytes = ' \"\'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ}{-_.;:?!()[]+òàùèé\\|'
  28.    
  29.     #il keystream è una lista di liste. la lista interna contiene i byte candidati per il keystream
  30.     keystream : list[list[bytes]] = []
  31.  
  32.     #actual_key = from_mtp_original["key"]
  33.     actual_key = "ba294994ed9812fa18b25afad1e58fe0a36e79b3a7463e0a5481466f6d096776d98089b337dbba70752057f0f60cdf8ae5c052705dcb3778402b2b9f89d590c0cfd81eb591f184cb8e405ca287df87e4b430d2f3e96c090df8bb52a4bbad668804cb95a3abe19041e7b861aaa8a501c6d2dab1508b33a6489fa98526712f59e1________________________________________70__________72__________________________________b1b1d892255a7d97__________________________________________________________________________________________________________________________________________________________________________________________________f793aab3feac16______________c72df9c7fd2f______________________________________________________________________________________________________________________________________________________9d769bab70f792d7fc5bba9572ca8b________________________________________________________________08bb91______________________72b37ef80d7d7ad7836eeabc32bb575a________________________________________________________________________________________________________________________________________________________________________________________2c041699b68a11d55c____________________________________________70__________________________________________97______________________________________________________________________________________4200823c94e46dbc44183bb15b______________________________________b2e42e63e6____________________________________________________________________ec6f169d716ede1a__________________________________________________________________________________________________________________________________________________________a2e9f984ec2b601d10f306c2ef2f2ebb49c23d71924bc7de8cf592a8f3018a9d9bc580e98309e83d7b8ea1bc9decf6326de4dc59eb27b229e224a12dcf86f03a0263dbc102a84f6a5e9ae09d947a5e1e274139876b79fd62046a5af3b6a66f0bc2c1f5fd5c3625a0cfb0f34d06101e35290ed9d9641b657135358e2bffb9a16b721debd3042ffea29be7ca________________________________________________________________9b____________________________________________________________________"
  34.     counter = 0
  35.  
  36.     #con questo pezzo carico la chiave che ho ricavato con mtp. Puoi balzare
  37.     for i in range(0,len(actual_key), 2):
  38.         if actual_key[i] == '_':
  39.             keystream.insert(i//2, [])
  40.             continue
  41.         keystream.insert(i//2, [int.from_bytes(unhexlify(actual_key[i:i+2]))])
  42.         counter +=1
  43.     print(f"\n {counter}\\1000")
  44.  
  45.     #!! da qua inizia l'algoritmo vero e proprio
  46.     # per ciascuna colonna dalla lista di ciphertext...
  47.     for col in range(len(longest)):
  48.         if len(keystream[col]) != 0: continue #(mi fido di quello che mi ha già dato mtp)
  49.         guessedKsBytesList :list[bytes] = []
  50.         #...seleziona un byte stampabile...
  51.         for char in ascii_bytes:
  52.             valid_char = True
  53.             #...fanne lo xor con la prima riga (una a caso va bene)...
  54.             guessedKsByte = longest[col] ^ ord(char)
  55.             #...del risultato fanne lo xor con tutti i byte della suddetta colonna
  56.             for line in enc_asBytes:
  57.                 #...se non va bene cambia char stampabile
  58.                 if chr(line[col] ^ guessedKsByte) not in ascii_bytes:
  59.                     valid_char = False
  60.                     break
  61.             #...se va bene inserisci nella lista temporanea il byte candidato...
  62.             if valid_char is True:
  63.                 guessedKsBytesList.append(guessedKsByte)
  64.  
  65.         keystream.pop(col) #(accrocchio per tenere assieme [indice col] <-> [posizione keystream])
  66.         #...infine metti la lista temporanea (vuota o piena) nella lista keystream
  67.         keystream.insert(col, guessedKsBytesList)
  68.         #assert len(keystream) <= len(longest)
  69.  
  70.     print(keystream)
  71.     #verifico quanti byte nuovi ho inserito. (spoiler: un centinaio in più se va bene ma alcuni sono sbagliati)
  72.     counter =0
  73.     for anarray in keystream:
  74.         counter += 1 if len(anarray) ==1 else 0
  75.     print(f"\n {counter}\\1000")
  76.     return keystream
  77. '''    for i in range(0,len(actual_key), 2):
  78.            if actual_key[i] != '_':
  79.                continue
  80.            #keystream.insert(i//2, [int.from_bytes(unhexlify(actual_key[i:i+2]))])
  81.            if len(keystream[i//2]) != 1:
  82.                continue
  83.            actual_key[i:i+2] = int.to_bytes(keystream[i//2]).hex()'''
  84.  
  85. #Qua stampo il plaintext con i byte aggiunti
  86. def print_ptxt(enc: list[bytes], keystream: list[bytes]|list[list[bytes]]):
  87.     columns = len(max(enc))
  88.     assert columns == len(keystream)
  89.     for line in enc:
  90.         line_to_be_printed = []
  91.         for col in range(columns):
  92.             val = (line[col]) ^ int.from_bytes(keystream[col]) if len(keystream[col]) == 1 else ord('_')
  93.             line_to_be_printed.append(chr(val))
  94.         assert len(line_to_be_printed) == columns
  95.         print("".join(line_to_be_printed))
  96.  
  97. def main():
  98.     with open(".\\file.out", "r") as f:
  99.         from_mtp = json.load(f)
  100.  
  101.     with open(".\\file.hex", 'r') as f:
  102.         enc = f.readlines()
  103.     enc_asBytes : list[bytearray]= [bytearray.fromhex(aline) for aline in enc]
  104.  
  105.  
  106.     prepare_hex_file(".\\file.enc")
  107.     keystream = read_and_guess_ks(enc_asBytes, from_mtp)
  108.     print_ptxt(enc_asBytes, keystream)
  109.  
  110.  
  111. '''
  112. # Qua sotto sta il tuo codice.
  113. # Se ho capito bene la flag si trova nei primi 56 caratteri di qualche frase ma non l'ho trovata comunque.
  114. # Ho provato anche con mtp. Comunque sono 100 righe giusto?
  115.  
  116. keystream = bytes.fromhex(mtp_original["key"][:56*2])
  117. for c in enc_asBytes:
  118.    l = min(len(keystream),len(c))
  119.    block= strxor(c[:l],keystream[:l])
  120.    if(b"CRYPTO25" in block):
  121.        print(block)
  122.  
  123. #Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  124. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement