-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
executable file
·170 lines (132 loc) · 5.37 KB
/
Copy pathutils.py
File metadata and controls
executable file
·170 lines (132 loc) · 5.37 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import hashlib
import os
linux_folders = {
"1": ["Scrivania", "Desktop"],
"2": ["Scaricati", "Downloads"],
"3": ["Documenti", "Documents"],
"4": ["Immagini", "Pictures"]
}
windows_folders = {
"1": "Desktop",
"2": "Downloads",
"3": "Documents",
"4": "Pictures"
}
protected_paths = [
"C:\\Windows", "C:\\Program Files", "C:\\Program Files (x86)", "C:\\ProgramData",
"/bin", "/boot", "/dev", "/etc", "/lib", "/lib64", "/opt", "/proc", "/root", "/sbin", "/sys", "/usr", "/var"
]
def get_valid_linux_path(choice):
""" Return the correct path of the selected folder on Linux, handling languages """
home = os.path.expanduser("~")
if choice in linux_folders:
for folder in linux_folders[choice]:
path = os.path.join(home, folder)
if os.path.exists(path):
return path
return None
def get_valid_windows_path(choice):
""" Return the correct path of the selected folder on Windows """
home = os.path.expanduser("~")
if choice in windows_folders:
path = os.path.join(home, windows_folders[choice])
if os.path.exists(path):
return path
return None
def sanitize_filepath(filepath: str) -> str | None:
""" Remove any double quotes present in the path """
# Check if the path is enclosed in double quotes
path = filepath.strip().strip('"').strip("\n")
# Check if the path is not a directory
if os.path.isdir(path):
print(" [!] ERROR: Cannot compute the hash of a directory!\n")
return None
# Check if the path refers to protected paths (system directories)
for protected_path in protected_paths:
if path.startswith(protected_path):
print(" [!] ERROR: Protected directories cannot be used!")
return None
# Check if the path refers to a valid file
if not os.path.isfile(path):
print(" [!] ERROR: The path inserted does not refer to a valid file!")
return None
return path
def sanitize_hash(_hash: str) -> str | None:
""" Remove any spaces or double quotes present in the hash """
_hash = _hash.strip().strip('"').strip("\n").replace(" ", "")
# Check if the hash is valid
if len(_hash) == 0:
print("[!] ERROR: The hash provided is empty!\n")
return None
return _hash
def calculate_file_hash(filepath: str, hash_function: str) -> str | None:
""" Compute the hash of the file using the provided algorithm """
hash_func = getattr(hashlib, hash_function)
hasher = hash_func()
try:
with open(filepath, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hasher.update(chunk)
return hasher.hexdigest()
except Exception as e:
print(f"[!] ERROR: {e}")
return None
def get_hash_algorithms():
""" Print the available hash functions """
algorithms = list(hashlib.algorithms_available)
print("[*] Available hash functions:")
for i in range(0, len(algorithms), 5):
print(" " + " | ".join(algorithms[i:i + 5]))
return algorithms
def select_filepath():
""" Select the root folder or manually insert the path """
if os.name == "posix": # Linux/macOS
for key, names in linux_folders.items():
print(f" {key}) ~/{names[0]}")
while True:
choice = input("\n[+] Select a root folder or enter a complete path: ").strip()
if choice.isdigit():
if choice in linux_folders:
root_path = get_valid_linux_path(choice)
if root_path:
filename = input(f"\n[+] Enter the file name (or its relative path): {root_path}/").strip()
filepath = os.path.join(root_path, filename)
break
elif choice == "0":
print("[!] Exiting... Bye Bye!")
exit()
else:
print("[!] ERROR: Invalid choice!")
else:
# Get the full path
filepath = sanitize_filepath(choice)
if filepath is not None:
break
else: # Windows
for key, name in windows_folders.items():
print(f" {key}) ~\\{name}")
while True:
choice = input("\n[+] Select a root folder or enter the complete path ([0] to exit): ").strip()
# Check if choice is an integer
if choice.isdigit():
if choice in windows_folders:
root_path = get_valid_windows_path(choice)
if root_path:
filename = input(f" Enter the file name (or its relative path): {root_path}\\").strip()
filepath = os.path.join(root_path, filename)
filepath = sanitize_filepath(filepath)
if filepath is not None:
break
elif choice == "0":
print("[!] Exiting... Bye Bye!")
exit()
else:
print("[!] ERROR: Invalid choice!")
else:
# Get the full path
filepath = sanitize_filepath(choice)
if filepath is not None:
break
if filepath is not None:
print(f" Path sanitized: {filepath}")
return filepath