Skip to content

Instantly share code, notes, and snippets.

@ipsitamishra16893
Last active November 12, 2022 19:25
Show Gist options
  • Save ipsitamishra16893/7a3db352be576383775d2d5c4becbb50 to your computer and use it in GitHub Desktop.
Save ipsitamishra16893/7a3db352be576383775d2d5c4becbb50 to your computer and use it in GitHub Desktop.
Minimal script to extract .jpg files based on magic bytes
import glob
magic_numbers = {'jpg': bytes([0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00, 0x01])}
for x in glob.glob("*jpg"):
with open(x, 'rb') as fd:
file_data = fd.read()
print("Detected "+str(file_data.count(magic_numbers['jpg']))+" JPG files in file : "+fd.name)
if file_data.count(magic_numbers['jpg']) > 1:
print("Trying to extract embedded files")
for f in range(file_data.count(magic_numbers['jpg'])):
with open(str(f+1)+".jpg", "wb") as ff:
ff.write(magic_numbers['jpg'] + file_data.split(magic_numbers['jpg'])[f+1])
print("Generated file : "+str(f+1)+".jpg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment