This repository has been archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PDF_Parser.py
276 lines (226 loc) · 8.41 KB
/
PDF_Parser.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import sys
import requests
import base64
import json
import os
try:
from prettytable import PrettyTable
except:
res = input("You haven't 'prettytable' library, do you want to install it? [yes][nope] default [yes]")
if res == "" or res == "yes":
os.system("pip install prettytable")
else:
exit()
import sys
try:
from termcolor import colored
except:
res = input("You haven't 'termcolor' library, do you want to install it? [yes][nope] default [yes]")
if res == "" or res == "yes":
os.system("pip install termcolor")
else:
exit()
import platform
import getpass
log_output = ""
def argv_error():
print("\n-p\t\tPDF file path to analyze\n or --path\n")
print("-A\t\tSet your VirusTotal API Key\n or --API-Key\n")
print("-gA\t\tPrint your VirusTotal API Key\n or --Get-API-Key\n")
print("-v\t\tUse this argoument for view a lot more information\n or --verbose\n")
print("-l\t\tSave in a log file all verbose information\n or --log\n")
print("\nExamples:")
print("\nSet your API:\n$ sudo python3 script.py -A <VirusTotal_API_Key>")
print("\nSee your API:\n$ sudo python3 script.py -gA")
print("\nScan a pdf:\n$ sudo python3 script.py -p malicious.pdf")
print("\nVerbose output:\n$ sudo python3 script.py -p malicious.pdf --verbose")
print("\nSave a in a log file:\n$ sudo python3 script.py -p malicious.pdf --log")
def generic_error(error: str) -> None:
print("\nSomething went wrong... check your private key and your pdf path :-/")
print(f"\n{error}")
"""
{
"data": {
"type": "analysis",
"id": "<base64_file_id>"
}
}
"""
def upload_file(File_Path: str, VirusTotal_API_Key: str, verbose: bool) -> str:
url = "https://www.virustotal.com/api/v3/files"
files = {"file": (File_Path, open(File_Path, "rb"), "application/pdf")}
headers = {
"accept": "application/json",
"x-apikey": VirusTotal_API_Key
}
response = requests.post(url, files=files, headers=headers)
if response.status_code == 200:
global log_output
if verbose:
print(response.text)
if log_output != "":
log_output += "\n"+response.text
return response.text
else:
generic_error(f"[*] Error occurred in upload_file function.\nError code: {response.status_code}")
return "-1"
"""
Load json response as a dict
"""
def json_load(json_in_string: str):
return json.loads(json_in_string)
"""
Get data -> id from VirusTotal response
"""
def get_base64_file_id_from_response(response: str) -> str:
response_in_dict = json_load(response)
return response_in_dict["data"]["id"]
"""
Decrypt from base64
"""
def decrypt_from_base64(encrypted_string: str) -> str:
return base64.b64decode(encrypted_string).decode('ascii')
"""
For check the file you must pass as parameters:
- MD5 of a file uploaded
- Your VirusTotal private Key
"""
def check_file(FileMD5: str, VirusTotal_API_Key: str) -> str:
url = f"https://www.virustotal.com/api/v3/files/{FileMD5}"
headers = {
"accept": "application/json",
"x-apikey": VirusTotal_API_Key
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.text
else:
generic_error(f"[*] Error occurred in check_file function.\nError code: {response.status_code}")
return "-1"
"""
Parserize the response
"""
def response_parser(response: str, verbose: bool, File_Path: str):
response_in_dict = json_load(response)
antivirus_supported = response_in_dict["data"]["attributes"]["last_analysis_results"]
malicious = 0
global log_output
cve = {}
for antivirus in antivirus_supported:
if response_in_dict["data"]["attributes"]["last_analysis_results"][f"{antivirus}"]["category"] == "malicious":
malicious += 1
cve[f"{antivirus}"] = response_in_dict["data"]["attributes"]["last_analysis_results"][f"{antivirus}"]["result"]
table = ["Result", "CVE"]
tab = PrettyTable(table)
for antivirus in cve.keys():
tab.add_row([f"{antivirus}", f"{cve[antivirus]}"])
if verbose:
print(tab)
if log_output != "":
log_output += "\n"+str(tab)
else:
for antivirus in antivirus_supported:
if response_in_dict["data"]["attributes"]["last_analysis_results"][f"{antivirus}"]["category"] == "malicious":
malicious += 1
print_string = ""
color = ""
if malicious > 2:
print_string = 'This document is most likely malicious!!!'
color = "red"
elif malicious == 1:
print_string = "A malicious control has been detected but it could be a false positive."
color = "yellow"
else:
print_string = "This file is safe. :-)"
color = "yellow"
print(colored(f'\n{print_string}', color, attrs=['reverse', 'blink']))
if log_output != "":
log_output += "\n"+print_string
if malicious > 1:
res = input("Do you want to remove the file? [yes][no] default:[yes]")
if res == "" or res == "yes":
if platform.system() == "Linux":
os.system(f"sudo rm {File_Path}")
if platform.system() == "Windows":
os.system(f"rm {File_Path}")
def create_config_file(VirusTotal_API_Key: str) -> None:
if check_sudo_permissions():
if platform.system() == "Linux":
os.system(f"sudo echo '{VirusTotal_API_Key}' > ~/.config/VirusTotal_API")
print(f"{os.popen('cat ~/.config/VirusTotal_API').read()[:5]}***", end="")
elif platform.system() == "Windows":
print("Not yet")
else:
print("Not yet")
def get_virustotal_api_key() -> str:
if check_sudo_permissions():
if platform.system() == "Linux":
return os.popen('cat ~/.config/VirusTotal_API').read().strip()
elif platform.system() == "Windows":
print("Not yet")
return "-1"
else:
print("Not yet")
return "-1"
def check_sudo_permissions() -> bool:
if os.geteuid() == 0:
return True
else:
print("You must run the script with sudo or with elevated permissions.", end="")
exit()
if __name__ == "__main__":
File_Path = ""
VirusTotal_API_Key = ""
verbose = False
log = False
for i in range(1, len(sys.argv)):
if((sys.argv[i] == "-p" or sys.argv[i] == "--path") and (len(sys.argv) > i+1)):
File_Path = sys.argv[i+1]
elif((sys.argv[i] == "-A" or sys.argv[i] == "--API-Key")):
if len(sys.argv) != 2:
print("For save the API Key run just:\tsudo python3 PDF_Parser.py -A or --API-Key")
exit()
else:
if check_sudo_permissions():
VirusTotal_API_Key = getpass.getpass("Your Secret API Key: ")
elif((sys.argv[i] == "-gA" or sys.argv[i] == "--Get-API-Key")):
print(get_virustotal_api_key().strip(), end="")
elif((sys.argv[i] == "-v" or sys.argv[i] == "--verbose")):
verbose = True
elif((sys.argv[i] == "-l" or sys.argv[i] == "--log")):
log = True
elif((sys.argv[i] == "-h" or sys.argv[i] == "--help")):
argv_error()
exit()
if VirusTotal_API_Key != "":
create_config_file(VirusTotal_API_Key)
else:
VirusTotal_API_Key = get_virustotal_api_key()
if VirusTotal_API_Key == "-1": exit()
if File_Path != "":
init_print = f"Your File: {File_Path}\nYour API: {VirusTotal_API_Key[:5]}***"
if verbose:
print(init_print)
if log:
log_output += "\n"+init_print
else:
exit()
response = upload_file(File_Path, VirusTotal_API_Key, verbose)
if response == "-1":
exit()
encrypted_FileMD5 = get_base64_file_id_from_response(response)
plaintext_FileMD5 = decrypt_from_base64(encrypted_FileMD5)
FileMD5 = plaintext_FileMD5.split(":")[0]
MD5_print = f"MD5: {FileMD5}"
if verbose:
print(MD5_print)
if log:
log_output += "\n"+MD5_print
VirusTotal_response = check_file(FileMD5, VirusTotal_API_Key)
if VirusTotal_response == "-1":
exit()
response_parser(VirusTotal_response, verbose, File_Path)
if log:
f = open("PDF_Parser.log", "w")
f.write(log_output)
f.close()