image.png

This time we have a Hex file where the bytes are suspiciously out of order.

Print the first 10 lines by using the head command

image.png

The .png file header is super common and should stand out when seen. (Even if out of order.)

If we look up .png at Gary Kessler’s File Signature lookup table, we’ll see this:

image.png

Here a comparison:

We need a program that will do the following:

Here is a program that will get that job done.

def invert_hex_bytes(input_file, output_file):
    with open(input_file, 'r') as f:
        hex_data = f.read().strip()

    hex_data = hex_data.replace(" ", "").replace("\n", "")
    print(len(hex_data))

    inverted_hex = ""
    for i in range(0, len(hex_data), 8):
        chunk = hex_data[i:i+8]
        inverted_chunk = chunk[6:8] + chunk[4:6] + chunk[2:4] + chunk[0:2]
        inverted_hex += inverted_chunk

    with open(output_file, 'w') as f:
        for i in range(0, len(inverted_hex), 8):
            f.write(inverted_hex[i:i+8] + "\n")

    print(f"Inverted hex dump written to {output_file}.")

input_file = 'flag.hex'
output_file = 'inverted_hex_dump.txt'

invert_hex_bytes(input_file, output_file)

If we run this file in the same directory, we’ll get back a file called inverted_hex_dump.txt

image.png