Last active
November 12, 2022 19:25
-
-
Save ipsitamishra16893/7a3db352be576383775d2d5c4becbb50 to your computer and use it in GitHub Desktop.
Minimal script to extract .jpg files based on magic bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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