
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

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

Here a comparison:
- 47 4e 50 89 (File Given)
- 89 50 4e 47 (Correct File Signature)
We need a program that will do the following:
- Eliminate all the spaces in the file so we have groups of 8 Hex Characters.
- Take the characters at positions 7 and 8 and place them in positions 1 2
- Take the characters at positions 5 and 6 and place them in position 3 and 4
- Take the characters at positions 3 and 4 and place them in positions 5 and 6
- Place the characters at positions 1 and 2 at the end of the line.
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
