Skip to content

Instantly share code, notes, and snippets.

@GGLinnk
Last active October 22, 2021 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GGLinnk/0093223e3e6aa50857ce16334d6f2b1a to your computer and use it in GitHub Desktop.
Save GGLinnk/0093223e3e6aa50857ce16334d6f2b1a to your computer and use it in GitHub Desktop.
Group Magic Bytes | Python script that group Magic Bytes of files in folder, show retrived bytes and show associated file. This is not the final version!
#!/bin/env python3
from sys import argv, stderr
from os import listdir, path
import binascii
linkPath = path.normpath(argv[1])
if not path.exists(linkPath):
print(f"Folder doesn't exists: [{linkPath}]", file=stderr)
exit(1)
if not path.isdir(linkPath):
print(f"Not a folder: [{linkPath}]", file=stderr)
exit(1)
fileMagiks = {}
linkFiles = [f for f in listdir(linkPath) if path.isfile(path.join(linkPath, f))]
for f in linkFiles:
fb = open(path.join(linkPath, f), "rb")
fileMagik = fb.read(4)
if fileMagik not in fileMagiks:
fileMagiks[fileMagik] = {"refs": []}
fileMagiks[fileMagik]["refs"].append(f)
fb.close()
for fileMagik in fileMagiks:
print(f"{fileMagik} : {fileMagiks[fileMagik]['refs']}")
#print(f"0x{binascii.hexlify(fileMagik).decode()} : {fileMagik.decode()}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment