-
Notifications
You must be signed in to change notification settings - Fork 0
/
VT_Domain_Checker.py
114 lines (95 loc) · 3.78 KB
/
VT_Domain_Checker.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
import requests
import datetime
import sys
import csv
import os
def timestamp_to_date(timestamp):
return datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
def save_to_csv(data, filename):
with open(filename, 'a', newline='') as csvfile:
fieldnames = ['domain', 'VT Link', 'last_analysis_date', 'last_dns_records_date', 'harmless', 'malicious', 'suspicious', 'undetected', 'timeout']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
if csvfile.tell() == 0:
writer.writeheader()
writer.writerow(data)
def check_domain(domain, csv_output=None, print_output=True):
url = f"https://www.virustotal.com/api/v3/domains/{domain}"
headers = {
"accept": "application/json",
"x-apikey": "<API_KEY>"
}
response = requests.get(url, headers=headers)
data = response.json()
if 'data' in data and 'attributes' in data['data']:
attributes = data['data']['attributes']
vt_link = f"https://www.virustotal.com/gui/domain/{domain}/detection"
last_analysis_date = timestamp_to_date(attributes.get('last_analysis_date', 0))
last_dns_records_date = timestamp_to_date(attributes.get('last_dns_records_date', 0))
last_analysis_stats = attributes.get('last_analysis_stats', {})
if print_output:
print(f"Domain: {domain}")
print(f"VT Link: {vt_link}")
print(f"last_analysis_date: {last_analysis_date}")
print(f"last_dns_records_date: {last_dns_records_date}")
print("last_analysis_stats: ")
for key, value in last_analysis_stats.items():
print(f" {key}: {value}")
else:
print(f"Processing domain: {domain}")
if csv_output:
csv_data = {
"domain": domain,
"VT Link": vt_link,
"last_analysis_date": last_analysis_date,
"last_dns_records_date": last_dns_records_date,
**last_analysis_stats
}
save_to_csv(csv_data, csv_output)
def main():
csv_output = None
print_output = True
if "-d" in sys.argv:
try:
domain_index = sys.argv.index("-d")
domain = sys.argv[domain_index + 1]
except IndexError:
print("Error: No domain provided.")
sys.exit(1)
if "--csv" in sys.argv:
try:
csv_index = sys.argv.index("--csv")
csv_output = sys.argv[csv_index + 1]
print_output = False
except IndexError:
print("Error: No filename provided for CSV output.")
sys.exit(1)
check_domain(domain, csv_output, print_output)
elif "-f" in sys.argv:
try:
file_index = sys.argv.index("-f")
filename = sys.argv[file_index + 1]
except IndexError:
print("Error: No filename provided.")
sys.exit(1)
if "--csv" in sys.argv:
try:
csv_index = sys.argv.index("--csv")
csv_output = sys.argv[csv_index + 1]
print_output = False
except IndexError:
print("Error: No filename provided for CSV output.")
sys.exit(1)
with open(filename, 'r') as file:
domains = file.readlines()
for domain in domains:
domain = domain.strip()
if domain:
check_domain(domain, csv_output, print_output)
else:
print("Usage: python VT_Domain_Checker.py -d [domain] or -f [filename] [--csv filename.csv]")
sys.exit(1)
if csv_output:
full_path = os.path.abspath(csv_output)
print(f"File saved at: {full_path}")
if __name__ == "__main__":
main()