-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSiFiPy.py
80 lines (51 loc) · 2.33 KB
/
SiFiPy.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import getopt
import sys
import time
from file_signature_dict import file_signature_dict, help_menu_text
def get_file_signature_from_file_object(file_obj, block_size=16):
block = file_obj.read(block_size).hex().upper() # Convert the
return " ".join(block[i:i + 2] for i in range(0, len(block), 2))
def get_file_signature_from_directory(directory, block_size=16):
with open(f"{directory}", "rb") as f:
block = f.read(block_size).hex().upper()
return " ".join(block[i:i + 2] for i in range(0, len(block), 2))
def find_signature_in_dict(dictionary, file_header):
split_file_header = file_header.split(" ")
file_signature_found = ""
for key in (dictionary.keys()):
if ' '.join(split_file_header[0:len(key.split(" "))]) == key:
if len(key) > len(file_signature_found):
file_signature_found = key
print(f"File Signature - {key}\n")
for sig_details in dictionary[key]:
print(f"{sig_details[0]} | {sig_details[1]}")
print("\n")
try:
return dictionary[file_signature_found]
except KeyError:
sys.exit("Unable To Find Matching File Signature -"
" Please Submit Your File Signature to the Discord for Manual Inspection")
def main():
short_opts = "hf:b:"
long_opts = ["help", "file=", "block_size="]
argument_list = sys.argv[1:]
# Block size is automatically set to the largest one in the file signature dictionary unless changed.
dependent_block_size = len(max(file_signature_dict, key=len))
filename = None
try:
arguments, values = getopt.getopt(argument_list, short_opts, long_opts)
except getopt.error as error:
sys.exit(f"{error}\n")
for argument, value in arguments:
if argument in ("-h", "--help"):
print(f"\n{help_menu_text}\n")
elif argument in ("-f", "file="):
filename = str(value)
elif argument in ("-b", "block_size="):
dependent_block_size = int(value)
if not filename:
sys.exit("File Signature Match Failed - No File Provided - Try Using -f [filename]")
header = get_file_signature_from_directory(filename, block_size=dependent_block_size)
find_signature_in_dict(dictionary=file_signature_dict, file_header=header)
if __name__ == "__main__":
main()