forked from bee-san/pyWhat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagic_numbers.py
33 lines (27 loc) · 1.17 KB
/
magic_numbers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import binascii
import os
import json
class FileSignatures:
def __init__(self):
path = "Data/file_signatures.json"
fullpath = os.path.join(os.path.dirname(os.path.abspath(__file__)), path)
with open(fullpath, "r", encoding="utf8", errors="ignore") as myfile:
self.file_sigs = json.load(myfile)
def open_file_loc(self, file_loc):
with open(file_loc, "r", encoding="utf8", errors="ignore") as myfile:
r = myfile.readlines()
return r
def open_binary_scan_magic_nums(self, file_loc):
with open(file_loc, "rb") as myfile:
header = myfile.read(24)
header = str(binascii.hexlify(header))[2:-1]
return self.check_magic_nums(header)
def check_magic_nums(self, text):
for i in self.file_sigs:
to_check = i["Hexadecimal File Signature"]
# Say we have "16 23 21", the [0, len()] prevents it from executing
# as magic numbers only count at the start of the file.
if to_check == text[0 : len(to_check)]:
# A file can only be one type
return i
return None