-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrt_sh_domains.py
executable file
·138 lines (100 loc) · 3.8 KB
/
crt_sh_domains.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
"""Get all the domains and subdomains related to a domain using crt.sh."""
import sys
import urllib3
import requests
import argparse
import logging
import logging.handlers
# Disable insecure https warnings
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# Set up logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler = logging.handlers.RotatingFileHandler('crt_sh_domains.log', maxBytes=10000000, backupCount=5)
handler.setFormatter(formatter)
logger.addHandler(handler)
# Set up command line arguments
parser = argparse.ArgumentParser(description='Get domains from crt.sh', epilog='Example: python crt_sh_domains.py -d example.com -o domains.txt')
parser.add_argument('-d', '--domain', help='Domain to search for domains', required=True)
parser.add_argument('-o', '--output', help='Output file to write domains to')
args = parser.parse_args()
# Set up variables
domain = args.domain
OUTPUT_FILE = args.output
user_agent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
headers = {"User-Agent": user_agent}
# Set up crt.sh URL
crt_sh_url = 'https://crt.sh/?q=' + domain + '&output=json'
# Get domains from crt.sh
def get_domains() -> list:
"""
Get domains from crt.sh
Returns: list of domains
"""
try:
session = requests.Session()
response = session.get(crt_sh_url, headers=headers)
if response.status_code == 200:
return response.json()
else:
print('Error getting domains from crt.sh, run again')
logger.error('Error getting domains from crt.sh')
sys.exit(1)
except Exception as error:
print('Error getting domains from crt.sh:', error)
logger.error('Error getting domains from crt.sh:', error)
sys.exit(1)
# Filter domains to remove duplicates and unwanted domains
def filter_domains(domains) -> set:
"""
Filter domains to remove duplicates and unwanted domains
domains: list of domains
Returns: set of domains
"""
domain_collection = set()
filters = ['*', '\\', '-', '@']
for domain in domains:
domain_split = domain['name_value'].split('\n')
if len(domain_split) == 1 and all(f not in domain['name_value'] for f in filters):
domain_collection.add(domain['name_value'])
else:
# handle cases where multiple domains are present
for domain_2 in domain_split:
if all(f not in domain_2 for f in filters):
domain_collection.add(domain_2)
return domain_collection
# Write domains to file
def write_domains(domains) -> None:
"""
Write domains to file
domains: list of domains
Returns: None
"""
try:
with open(OUTPUT_FILE, 'w') as output_file:
for domain in domains:
output_file.write(domain + '\n')
except Exception as error:
print('Error writing domains to file:', error)
logger.error('Error writing domains to file:', error)
sys.exit(1)
# Main function
def main() -> None:
"""
Main function
Returns: None
"""
try:
domain_collection = filter_domains(get_domains())
for domain in domain_collection:
print(domain)
# print("Total domains: " + str(len(domain_collection)))
if OUTPUT_FILE:
# print("Writing domains to file: " + OUTPUT_FILE)
write_domains(domain_collection)
except KeyboardInterrupt:
print('Exiting...')
sys.exit(1)
if __name__ == '__main__':
main()