-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
194 lines (172 loc) · 8.46 KB
/
main.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
import os
import sys
import os
import argparse
from dotenv import load_dotenv
from Utils import WildCard
from sender import Telegram
from certsh import Database
from Utils import Tools
import time
import subprocess
menu = argparse.ArgumentParser(description="Submon - A tool to find subdomains and certificates and monitor them for changes.")
menu.add_argument("-f", "--file", help="file with domains to monitor", required=False, default="domains.txt")
menu.add_argument("-d", "--delete", help="delete a domain and its subdomains from the database", required=False, default=None)
menu.add_argument("-l", "--list", help="list all domains in the database", required=False, action="store_true")
menu.add_argument("-ls", "--listsub", help="list all subdomains for a domain", required=False, default=None)
menu.add_argument("-sd", "--singledomain", help="check a single domain", required=False, default=None)
menu.add_argument("-n", "--nuclei", help="run nuclei on the new subdomains", required=False, action="store_true") ## TODO improve this
menu.add_argument("-is", "--iserver", help="OOB server to run nuclei with", required=False, default=None)
menu.add_argument("-se", "--severity", help="severity to run your templates (use comma separated values foo,bar,john)", required=False, default="high,critical")
menu.add_argument("-t", "--templates", help="path where your templates are saved", required=False, default=None)
args = menu.parse_args()
if len(sys.argv) < 1:
menu.print_help()
print("Usage: python3 main.py -f domains.txt")
sys.exit(1)
domains_file = args.file
delete_domain = args.delete
list_domains = args.list
run_nuclei = args.nuclei
list_subdomains = args.listsub
single_domain = args.singledomain
nuclei_iserver = args.iserver
nuclei_severity = args.severity
nuclei_templates_path = args.templates
if domains_file == "" and single_domain == None:
print("You must specify a file with domains to monitor or a single domain to check.")
menu.print_help()
sys.exit(1)
if not os.path.isfile(domains_file):
print(f"File '{domains_file}' not found.")
sys.exit(1)
if delete_domain:
cert_obj = Database.CertDatabase()
cert_obj.delete_domain_and_subdomains(delete_domain)
sys.exit(0)
if list_subdomains:
print(f"[*] Listing all subdomains in the database from domain: {list_subdomains}")
cert_obj = Database.CertDatabase()
domains = cert_obj.list_subdomains_for_domain(list_subdomains)
if len(domains) < 1:
print(f"[*] No subdomains found for domain: {list_subdomains}")
sys.exit(0)
for domain in domains:
print(domain)
sys.exit(0)
if list_domains:
cert_obj = Database.CertDatabase()
domains = cert_obj.list_all_domains()
for domain in domains:
print(domain)
def main():
load_dotenv()
TOKEN = os.getenv("API_TOKEN_TELEGRAM")
CHAT = os.getenv("CHAT_ID")
current_time = time.ctime()
cert_obj = Database.CertDatabase()
wild_card_obj = WildCard.WildCardFilter()
telegram = Telegram.Telegram(TOKEN, CHAT)
if nuclei_iserver != None and nuclei_severity:
tools_obj = Tools.Tools(nuclei_iserver, nuclei_severity)
elif nuclei_severity and nuclei_iserver and nuclei_templates_path:
tools_obj = Tools.Tools(nuclei_iserver, nuclei_severity, nuclei_templates_path)
elif nuclei_severity and nuclei_templates_path:
tools_obj = Tools.Tools(nuclei_severity, nuclei_templates_path)
else:
tools_obj = Tools.Tools()
if delete_domain:
cert_obj = Database.CertDatabase()
cert_obj.delete_domain_and_subdomains(delete_domain)
sys.exit(0)
if list_domains:
cert_obj = Database.CertDatabase()
domains = cert_obj.list_all_domains()
for domain in domains:
print(domain)
sys.exit(0)
if args.singledomain:
single_domain = args.singledomain
print(f"[*] Checking domain: {single_domain}")
if not wild_card_obj.is_wildcard_domain(single_domain):
single_domain = single_domain.strip()
results = cert_obj.monitor_single_domain(single_domain)
if len(results) == 0:
print(f"[*] No subdomains found for domain: {single_domain}")
sys.exit(0)
else:
if len(results) > 20:
file_write = f"{single_domain}_subdomains.txt"
with(open(file_write, 'w')) as file:
for line in results:
file.write(f"{line}\n")
telegram.send(f"📦 New subdomains from {single_domain} found. See attached file. {current_time}")
telegram.send_file(file_write)
new_subdomains = cert_obj.get_new_subdomains_to_notify()
cert_obj.mark_subdomains_as_notified(new_subdomains)
if run_nuclei:
print(f"[*] Running nuclei on the new subdomains")
tools_obj.check_tools()
file_new_subdomains = os.path.abspath(file_write)
tools_obj.run_nuclei(file_new_subdomains)
print(f"[*] Nuclei scan completed")
if os.path.isfile('nuclei_results.txt'):
telegram.send_file('nuclei_results.txt')
print(f"[*] File 'nuclei_results.txt' sent to Telegram {current_time}")
os.remove('nuclei_results.txt')
os.remove(file_write)
else:
for subdomain in results:
template_telegram = f"🧙🏻♂️ New subdomain found: {subdomain}\n{current_time}\n"
telegram.send(template_telegram)
new_subdomains = cert_obj.get_new_subdomains_to_notify()
cert_obj.mark_subdomains_as_notified(new_subdomains)
if run_nuclei:
print(f"[*] Running nuclei on the new subdomains")
tools_obj.check_tools()
tools_obj.run_single_nuclei(subdomain)
print(f"[*] Nuclei scan completed")
if os.path.isfile('nuclei_results.txt'):
telegram.send_file('nuclei_results.txt')
print(f"[*] Scan completed for {subdomain} and sent to Telegram {current_time}")
os.remove('nuclei_results.txt')
os.remove(file_write)
if domains_file:
file_path = os.path.abspath(domains_file)
with open(file_path, 'r') as file:
for domain in file.readlines():
# filter wildcard subdomains
print(f"[*] Checking domain: {domain}")
if not wild_card_obj.is_wildcard_domain(domain):
domain = domain.strip()
results = cert_obj.lookup(domain)
for result in results:
print(result)
new_subdomains = cert_obj.get_new_subdomains_to_notify()
if new_subdomains:
if len(new_subdomains) > 20:
telegram.send(f"📦 New subdomains found. See attached file. {current_time}")
with open('new_subdomains.txt', 'a') as file:
for line in new_subdomains:
file.write(f"{line}\n")
telegram.send_file('new_subdomains.txt')
print(f"File 'new_subdomains.txt' sent to Telegram {current_time}")
if run_nuclei:
print(f"[*] Running nuclei on the new subdomains")
tools_obj.check_tools()
file_new_subdomains = os.path.abspath('new_subdomains.txt')
tools_obj.run_nuclei(file_new_subdomains)
print(f"[*] Nuclei scan completed")
if os.path.isfile('nuclei_results.txt'):
telegram.send_file('nuclei_results.txt')
print(f"[*] File 'nuclei_results.txt' sent to Telegram {current_time}")
os.remove('nuclei_results.txt')
os.remove('new_subdomains.txt') ## if you want to keep the file comment this line
cert_obj.mark_subdomains_as_notified(new_subdomains)
else:
for line in new_subdomains:
template_telegram = f"🧙🏻♂️ New subdomain found: {line}\n{current_time}\n"
telegram.send(template_telegram)
cert_obj.mark_subdomains_as_notified(new_subdomains)
if __name__ == "__main__":
main()