This repository has been archived by the owner on Sep 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
domains_to_csv_output.py
68 lines (49 loc) · 1.89 KB
/
domains_to_csv_output.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
#!/usr/bin/env python3
# Use tcex library for TC API
from tcex import TcEx
import dns.resolver
import sys
dns_servers = ['1.1.1.1', '1.0.0.1'] # CloudFlare public DNS
# adopted from https://github.com/xn-twist/xn-twist/blob/b0316f3af0ffa1121179efc2035cce07cfb8944f/xn_twist/xn_twist.py#L86
def get_domain_dns(domain):
"""Get the DNS record, if any, for the given domain."""
dns_records = list()
# set DNS server for lookup
dns_resolver = dns.resolver.Resolver()
dns_resolver.nameservers = dns_servers
try:
# get the dns resolutions for this domain
dns_results = dns_resolver.query(domain)
dns_records = [ip.address for ip in dns_results]
#print(dns_records)
except dns.resolver.NXDOMAIN as e:
# the domain does not exist so dns resolutions remain empty
dns_records = 'NXDOMAIN'
except dns.resolver.NoAnswer as e:
# the resolver is not answering so dns resolutions remain empty
print("the DNS server(s) %s did not answer" % dns_servers, e)
dns_records = 'SERVFAIL'
except dns.resolver.NoNameservers as e:
# the resolver is not answering so dns resolutions remain empty
print("the nameservers did not answer", e)
dns_records = 'SERVFAIL'
return dns_records
tcex = TcEx()
tcex.tcex_args.config_file('app_config.json')
args = tcex.args
owner = "Owner"
_id = sys.argv[1]
group_type = 'Event'
parameters = {'includes': ['additional', 'attributes', 'labels', 'tags']}
ti = tcex.ti.group(group_type=group_type, owner=owner, unique_id=_id)
#response = ti.single(params=parameters)
#print(response)
# get indicator associations
csv = "Domain,IP address,Third column\r\n"
for indicator in ti.indicator_associations():
#print(indicator)
host = indicator['summary']
#print(host)
res = get_domain_dns(host)
csv += "%s,%s, \r\n" % (host, str(res))
print("CSV :\n%s\n" % csv)