Foro de elhacker.net

Programación => Scripting => Mensaje iniciado por: colcrt en 20 Mayo 2022, 23:40 pm



Título: error unpack requires a buffer of 4 bytes
Publicado por: colcrt en 20 Mayo 2022, 23:40 pm
estoy leyendo una archivo .img en formato binario y lo que busco es volcar el contenido, sin embargo el código que estoy utilizando me arroja un error en la línea 23 "end_off = (read_dword(boot_file) + 1) * BLOCK_SIZE + HEADER_SIZE - 1" con el mensaje: struct.error: unpack requires a buffer of 4 bytes

seguramente tiene que ver con la línea 9 la función read_dword; ya he buscado pero no encuentro una solucion, segun este post https://stackoverflow.com/questions/64865868/unpacking-4-byte-unsigned-integers-from-binary-file-gives-struct-error-unpack-r el tamaño de la cadena debe coincidir con el del buffer pero no logro entender es como hacerlo, si alguno fuera tana amable y me hechara un cable con esto?

Código:
HEADER_SIZE = 1024
BLOCK_SIZE = 512
DWORD_SIZE = struct.calcsize("I")
IMAGE_NAME_LEN = 24

def read_dword(f):
    return struct.unpack("<I", f.read(DWORD_SIZE))[0]

def main():
    if len(sys.argv) != 3:
        print("USAGE: <BOOT_IMAGE> UNPACK_DIR")
        return
    script_name, boot_path, unpack_dir = sys.argv
   
    boot_file = open(boot_path, "rb")
    num_images = read_dword(boot_file)
    images = []
    for i in range(0, num_images):
                image_name = boot_file.read(IMAGE_NAME_LEN).rstrip(b'\x00')
                start_off = read_dword(boot_file) * BLOCK_SIZE + HEADER_SIZE
                end_off = (read_dword(boot_file) + 1) * BLOCK_SIZE + HEADER_SIZE - 1
                print("[+] Detected image: %s [0x%08X - 0x%08X]" % (image_name, start_off, end_off))
                images.append((image_name, start_off, end_off))
               
    for(image_name, start_off, end_off) in images:
                print("[+] Dumping image: %s" % image_name)
                boot_file.seek(start_off, os.SEEK_SET)
                image_file = open(os.path.join(unpack_dir, image_name), 'wb')
                image_file.write(boot_file.read(end_off - start_off +1))
                image_file.close()
   
    boot_file.close()
   
if __name__ == "__main__":
    main()