-
Notifications
You must be signed in to change notification settings - Fork 0
/
vt3_tools.py
407 lines (344 loc) · 14.1 KB
/
vt3_tools.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import argparse
import logging
from datetime import datetime
import requests
from dotenv import load_dotenv
from rich.console import Console
from rich.markdown import Markdown
from rich.panel import Panel
from rich.text import Text
from app.DataHandler.utils import get_api_key, get_proxy, get_user_choice
from app.FileHandler.create_table import CustomPrettyTable as cpt
from app.FileHandler.read_file import ValueReader
from app.MISP.vt_tools2misp import misp_choice
from init import Initializator
console = Console()
def setup_logging() -> None:
"""Setup logging configuration."""
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)
def print_welcome_message() -> None:
"""Print the welcome message with ASCII art and title."""
ascii_art = Text(
"""
^77777!~:. :~7?JJJJ?!.
:!JYJJJJJ?!: .~?JJJJJYJ?!^.
.!JYJJJJYJ!. .!!7?JJJJ~:
.~: .!JJJJJJY7 77 ~JJJ~
~YJ7: :7JJJYJJJY~ 7?!!?7!J7. :^
7JJJJ7: ^7JJJ7:~?JJY! :JYY??JYY?^. .^!?JJ
^JJYJ7:^?JJJ7:~?~:?JJ^ ^?JJJ!^^~~JY?7?JJYJY?
!J!:^?JJJ!:!?~:?JJJJ?~. .^?JJJJJJ! ~??J:.~JJJY?:
.:?YYJJJJ?~^JJJJJJJJY?~.^JYJJJJJJJ?JJ?J!~~JJ7^
.^!?JJJYYYJJJJJJJJJ7:7J!:~?YJJJYJ7::^~~~~:.
.:^^^^:^7JJJJJJ: 7YYJ!:^?JJ!:
:7JYJ~ :!~~~!J!:^.
.^:!J!!^:~~~!?JJ7:
:7JJ?^:!J^:~JYY~.~?Y7^
:7JYJJJY?~:~?JJJJ~ ..:7J?^
.::^^^7JJJJJJJJJY?:.~JJ^.~??^!JJJ?^
.~?JYYYJJYJJJJJJJJJ7^ .^7~.^JJYJJJJJ?~.
~JJ7!!!^. !YYJJJJJ7: .^7~:^7?JJJJJY?~.
!YJJ. ^~7JJ7: ^7~.7J?JJJJJYJ!.
JJJ! ^JY~ ^~7^~JJJJJJJJJ!.
JY7. ~YJY^ :!JYJJJ^...~JJ^
^JJJ7^ . !YY7 :7JY? ?Y7
:7JYY!:~????J?~ :!J?~~!7J?.
:~7JJYJJ?7^. .~7?7!^
""",
justify="center",
style="cyan",
)
console.print(ascii_art)
title = Text(
"""
_ __ __ __ _ __ __ ______ __
| | /| / /___ / /____ ___ __ _ ___ / /_ ___ | | / // /_ /_ __/___ ___ / /___
| |/ |/ // -_)/ // __// _ \ / ' \/ -_) / __// _ \ | |/ // __/ / / / _ \/ _ \ / /(_-<
|__/|__/ \__//_/ \__/ \___//_/_/_/\__/ \__/ \___/ |___/ \__/ /_/ \___/\___//_//___/
""",
justify="center",
style="bold yellow",
)
console.print(title)
subtitle = Text(
"""
_ _____ _ _ _ ___ ___ ___ _____
| |__ _ _ |_ _| || | /_\ ___ / __| __| _ \_ _|
| '_ \ || | | | | __ |/ _ \___| (__| _|| / | |
|_.__/\_, | |_| |_||_/_/ \_\ \___|___|_|_\ |_|
|__/
""",
justify="center",
style="bold green",
)
console.print(subtitle)
welcome_message = """
Welcome to the VirusTotal analysis tool by THA-CERT!
This script will retrieve analysis information for a set of values (IP/Hash/URL/Domains) from VirusTotal.
To use the tool, provide your VirusTotal API key and the values you want to analyze.
The tool supports input from various sources, including files, standard input, and command line arguments.
Usage: vt3_tools.py [OPTIONS] VALUES...
Retrieve VirusTotal analysis information for a set of values (IP/Hash/URL/Domains).
"""
console.print(
Panel(
Markdown(welcome_message),
title="[bold green]Welcome![/bold green]",
border_style="green",
)
)
def parse_arguments() -> argparse.Namespace:
"""Parse command line arguments."""
parser = argparse.ArgumentParser()
parser.add_argument(
"--input_file", "-f", type=str, help="Input file containing values to analyze."
)
parser.add_argument(
"--case_id",
"-c",
type=str,
help="ID for the case to create (Or MISP event UUID to create or update)",
)
parser.add_argument(
"--api_key", "-a", type=str, help="VirusTotal API key, default VTAPIKEY env var"
)
parser.add_argument(
"--api_key_file", "-af", type=str, help="VirusTotal API key in a file."
)
parser.add_argument("--proxy", "-p", type=str, help="Proxy to use for requests.")
parser.add_argument(
"values",
type=str,
nargs="*",
help="The values to analyze. Can be IP addresses, hashes, URLs, or domains.",
)
return parser.parse_args()
def get_remaining_quota(api_key: str, proxy: str = None) -> int:
"""Returns the number of hashes that could be queried within this run."""
url = f"https://www.virustotal.com/api/v3/users/{api_key}/overall_quotas"
headers = {"Accept": "application/json", "x-apikey": api_key}
response = requests.get(
url, headers=headers, proxies={"http": proxy, "https": proxy}
)
if response.status_code == 200:
json_response = response.json()
allowed_hourly_queries = json_response["data"]["api_requests_hourly"]["user"][
"allowed"
]
used_hourly_queries = json_response["data"]["api_requests_hourly"]["user"][
"used"
]
return allowed_hourly_queries - used_hourly_queries
else:
logging.error(
"Error retrieving VT Quota (HTTP Status code: %d)", response.status_code
)
return 0
def count_iocs(ioc_dict):
total_iocs = 0
for key, value in ioc_dict.items():
total_iocs += len(value)
return total_iocs
def analyze_values(args: argparse.Namespace, value_types: list[str]) -> None:
"""Analyze values provided as arguments."""
load_dotenv()
api_key = get_api_key(args.api_key, args.api_key_file)
proxy = get_proxy(args.proxy)
case_id = str(args.case_id or 0).zfill(6)
init = Initializator(api_key, proxy, case_id)
database = "vttools.sqlite"
quota_saved = 0
error_values = 0
with init.db_handler.create_connection(database) as conn:
if conn is not None:
init.db_handler.create_schema(conn)
start_time = datetime.now()
console.print("\n[bold blue]Checking for remaining queries...[/bold blue]")
remaining_queries = get_remaining_quota(init.api_key, init.proxy)
if remaining_queries == 0:
console.print(
"[bold yellow]No queries remaining for this hour.[/bold yellow]"
)
console.print("[bold blue]Check your API key before analysis.[/bold blue]")
console.print("[bold green]Thank you for using VT Tools! 👍[/bold green]")
return
console.print(f"Remaining queries for this hour: {remaining_queries}")
values = ValueReader(args.input_file, args.values).read_values()
if not values:
console.print("[bold yellow]No values to analyze.[/bold yellow]")
console.print("Thank you for using VT Tools! [bold green]👍[/bold green]")
return
console.print(
f"[bold blue]This analysis will use {count_iocs(values)} out of your {remaining_queries} hourly quota.[/bold blue]\n"
)
if remaining_queries < count_iocs(values):
console.print(
f"[bold yellow]Warning:[/bold yellow] You have {remaining_queries} queries left for this hour, but you are trying to analyze {len(values)} values."
)
console.print(
"[bold yellow]Some values may be skipped to avoid exceeding the quota.[/bold yellow]\n"
)
console.print("Thank you for using VT Tools! [bold green]👍[/bold green]")
return
for value_type in value_types:
if not values.get(value_type):
console.print(
f"[bold yellow]No {value_type[:-1].upper()} values to analyze.[/bold yellow]"
)
console.print("\n")
continue
console.print(
Panel(
Markdown("## Analysis Started"),
title=f"[bold green]{value_type[:-1].upper()} Analysis[/bold green]",
border_style="green",
)
)
results, skipped_values, error_values = analyze_value_type(
init, value_type, values[value_type], conn
)
quota_saved += skipped_values
if results:
process_results(init, results, value_type)
csv_files_created = list(set(init.output.csvfilescreated))
quota_final = get_remaining_quota(init.api_key, init.proxy)
if quota_saved == 0:
console.print(
"[bold green]Analysis completed. No values were skipped.[/bold green]"
)
elif quota_saved == 1:
console.print(
"[bold green]Analysis completed. 1 value was skipped as it already exists in the database.[/bold green]"
)
else:
console.print(
f"[bold green]Analysis completed. {quota_saved} values were skipped as they already exist in the database.[/bold green]"
)
console.print(f"[bold blue]Errors occurred for {error_values} values.[/bold blue]")
console.print(f"[bold yellow]Remaining queries for this hour: {quota_final}[/bold yellow]")
total_time = datetime.now() - start_time
console.print(f"[bold blue]Total time taken: {total_time}[/bold blue]")
misp_choice(case_str=case_id, csvfilescreated=csv_files_created)
console.print("[bold green]Thank you for using VT Tools!👍[/bold green]")
close_resources(init)
def analyze_value_type(
init: Initializator, value_type: str, values: list[str], conn
):
"""Analyze values of a specific type."""
results = []
skipped_values = 0
error_values = 0
for value in values:
if value_exists(init, value, value_type, conn):
console.print(
f"[bold yellow]Value already exists in LOCAL database: {value}[/bold yellow]"
)
results.append(get_existing_report(init, value, value_type, conn))
skipped_values += 1
else:
result = analyze_value(init, value_type, value)
if result:
results.append(result)
else:
error_values += 1
return results, skipped_values, error_values
def get_existing_report(init: Initializator, value: str, value_type: str, conn) -> dict:
"""Retrieve existing report for a value from the local database."""
try:
if value_type == "hashes":
value_type_str = init.validator.validate_hash(value)
else:
validator_func = getattr(init.validator, f"validate_{value_type[:-1]}")
value_type_str = validator_func(value)
if value_type_str and value_type_str not in [
"Private IPv4",
"Loopback IPv4",
"Unspecified IPv4",
"Link-local IPv4",
"Reserved IPv4",
"SHA-224",
"SHA-384",
"SHA-512",
"SSDEEP",
]:
return init.db_handler.get_report(value, value_type_str.upper(), conn)
except Exception as e:
console.print(
f"[bold red]Error retrieving existing report for {value_type[:-1]}: {value}[/bold red]"
)
def value_exists(init: Initializator, value: str, value_type: str, conn) -> bool:
"""Check if a value exists in the local database."""
check_funcs = {
"hashes": init.db_handler.hash_exists,
"urls": init.db_handler.url_exists,
"domains": init.db_handler.domain_exists,
"ips": init.db_handler.ip_exists,
}
return check_funcs.get(value_type, lambda *args: False)(value, conn)
def analyze_value(init: Initializator, value_type: str, value: str) -> dict:
"""Analyze a single value using VirusTotal API."""
try:
if value_type == "hashes":
value_type_str = init.validator.validate_hash(value)
else:
validator_func = getattr(init.validator, f"validate_{value_type[:-1]}")
value_type_str = validator_func(value)
if value_type_str and value_type_str not in [
"Private IPv4",
"Loopback IPv4",
"Unspecified IPv4",
"Link-local IPv4",
"Reserved IPv4",
"SHA-224",
"SHA-384",
"SHA-512",
"SSDEEP",
]:
return init.reporter.get_report(value_type_str.upper(), value)
else:
console.print(f"[bold red]Invalid {value_type[:-1]}: {value}[/bold red]")
except Exception as e:
console.print(
f"[bold red]Error analyzing {value_type[:-1]}: {value}[/bold red]"
)
return None
def process_results(init: Initializator, results: list[dict], value_type: str) -> None:
"""Process the analysis results."""
header_rows = []
value_rows = []
for result in results:
if result:
for row in result["rows"]:
if row[0] not in header_rows:
header_rows.append(row[0])
value_rows.append(row[1:])
table = cpt(header_rows, value_rows)
strtable = table.create_table()
total_csv_report = [result["csv_report"] for result in results]
init.output.output_to_csv(
total_csv_report, "HASH" if value_type == "hashes" else value_type[:-1].upper()
)
init.output.output_to_txt(
strtable, "HASH" if value_type == "hashes" else value_type[:-1].upper()
)
console.print(
Panel(
Markdown("### Analysis ended successfully"),
title=f"[bold green]{value_type[:-1].upper()} Analysis[/bold green]",
border_style="green",
)
)
def close_resources(init: Initializator) -> None:
"""Close resources."""
init.client.close()
def main() -> None:
"""Main function to run the script."""
setup_logging()
print_welcome_message()
args = parse_arguments()
value_type = get_user_choice()
analyze_values(args, value_type)
if __name__ == "__main__":
main()