-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathURL-TO-IP-VY.py
More file actions
79 lines (70 loc) · 2.87 KB
/
URL-TO-IP-VY.py
File metadata and controls
79 lines (70 loc) · 2.87 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
"""
VirusTotal Domain Resolution Lookup
-----------------------------------
Fetches latest DNS resolutions for domains from a file, using VirusTotal API.
Instructions:
- Place your domain list (one per line) in a local file (e.g., "domains.txt").
- Do not include API keys in public repositories. Supply your VirusTotal API key at runtime or via environment variable VT_API_KEY.
- Output files: vt_details.txt (verbose), output_ips.txt (just resolved IPs).
Requirements:
pip install requests
"""
import os
import requests
def get_api_key():
"""Get API key from env or user input."""
key = os.environ.get("VT_API_KEY")
if not key:
key = input("Enter your VirusTotal API key: ").strip()
return key
def read_domains(filename):
"""Read domain list from file."""
try:
with open(filename, "r") as f:
return [line.strip() for line in f if line.strip()]
except Exception as e:
print(f"Error reading '{filename}': {e}")
exit(1)
def get_domain_ips(api_key, domains):
"""Query VirusTotal for each domain's latest resolutions."""
base_url = "https://www.virustotal.com/api/v3/domains/"
tail = "/relationships/resolutions?limit=10"
headers = {
"accept": "application/json",
"x-apikey": api_key
}
results = []
with open("vt_details.txt", "w", encoding="utf-8") as detail_out, \
open("output_ips.txt", "w", encoding="utf-8") as ip_out:
for domain in domains:
url = base_url + domain + tail
try:
response = requests.get(url, headers=headers, timeout=30)
if response.status_code != 200:
print(f"HTTP error {response.status_code} for {domain}: {response.text}")
continue
resp_json = response.json()
except Exception as e:
print(f"Request error for {domain}: {e}")
continue
# Extract IPs from resolutions
ips = []
if resp_json.get("data"):
for res in resp_json["data"]:
ip = res.get("id", "")
if ip:
ips.append(ip)
detail_out.write(f"Domain: {domain}\nResolved IP: {ip}\n\n")
# Write just the IPs to output_ips.txt
for ip in ips:
ip_out.write(f"{ip}\n")
else:
print(f"No resolution data for domain: {domain}")
detail_out.write(f"Domain: {domain}\nNo IPs found.\n\n")
print("\nLookup complete. Results saved to 'vt_details.txt' and 'output_ips.txt'.")
if __name__ == "__main__":
print("VirusTotal Domain Resolution Lookup\n")
domain_file = input("Enter the path to your domain list file: ").strip()
vt_api_key = get_api_key()
domains = read_domains(domain_file)
get_domain_ips(vt_api_key, domains)