-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSub_Recon.py
294 lines (241 loc) · 9.64 KB
/
Sub_Recon.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import subprocess
import os
import time
import xml.etree.ElementTree as ET
from typing import Set
import sys
from colorama import init, Fore
init(autoreset=True)
def print_success(message):
print(Fore.GREEN + f"[+] {message}")
def print_error(message):
print(Fore.RED + f"[!] {message}")
def print_warning(message):
print(Fore.YELLOW + f"[-] {message}")
def print_info(message):
print(Fore.CYAN + f"[i] {message}")
if len(sys.argv) < 5 or sys.argv[1] != '-d' or sys.argv[3] != '-w' or sys.argv[5] != '-r':
print_error("Usage: python3 Sub_Recon.py -d <domain> -w <wordlist.txt> -r <resolver.txt>")
sys.exit(1)
domain = sys.argv[2]
wordlist = sys.argv[4]
resolvers = sys.argv[6]
required_tools = [
"subfinder",
"sublist3r",
"amass",
"assetfinder",
"findomain",
"dnsrecon",
"gobuster",
"theharvester",
"knockpy"
]
def get_installed_version(tool: str) -> str:
"""Check the installed version of a tool."""
try:
result = subprocess.check_output([tool, "--version"], stderr=subprocess.STDOUT)
version = result.decode().splitlines()[0]
return version
except subprocess.CalledProcessError:
return None
def update_tool(tool: str):
"""Update the tool using its installation method."""
try:
print_info(f"Updating {tool}...")
subprocess.run(["sudo", "apt", "install", "--only-upgrade", "-y", tool], check=True)
print_success(f"{tool} has been updated successfully.")
except Exception as e:
print_error(f"Error updating {tool}: {str(e)}")
def install_required_tools():
for tool in required_tools:
try:
subprocess.run(["which", tool], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print_success(f"{tool} is already installed.")
installed_version = get_installed_version(tool)
print(f" Installed version of {tool}: {installed_version}")
update_tool(tool)
except subprocess.CalledProcessError:
print_warning(f"{tool} not found. Installing {tool}...")
try:
subprocess.run(["sudo", "apt", "install", "-y", tool], check=True)
print_success(f"{tool} has been installed successfully.")
except Exception as e:
print_error(f"Error installing {tool}: {str(e)}")
class SubdomainScanner:
def __init__(self, domain: str, wordlist: str, resolvers: str):
self.domain = domain
self.subdomains: Set[str] = set()
self.report_dir = f"subdomain_reports_{self.domain}"
self.wordlist = wordlist
self.resolvers = resolvers
os.makedirs(self.report_dir, exist_ok=True)
def run_knockpy(self):
try:
output_file = f"{self.report_dir}/knockpy.json"
subprocess.run([
"knockpy",
"-d", self.domain,
"--bruteforce",
"--json"
], check=True)
knockpy_json_file = f"{self.domain}.json"
if os.path.exists(knockpy_json_file):
os.rename(knockpy_json_file, output_file)
import json
with open(output_file, "r") as f:
data = json.load(f)
for subdomain in data.get("subdomains", []):
self.subdomains.add(subdomain)
except Exception as e:
print_error(f"Knockpy error: {str(e)}")
def run_sublist3r(self):
try:
output_file = f"{self.report_dir}/sublist3r.txt"
subprocess.run([
"sublist3r",
"-v",
"-d", self.domain,
"-o", output_file
], check=True)
with open(output_file, "r") as f:
self.subdomains.update(line.strip() for line in f if line.strip())
except Exception as e:
print_error(f"Sublist3r error: {str(e)}")
def run_subfinder(self):
try:
output_file = f"{self.report_dir}/subfinder.txt"
subprocess.run([
"subfinder",
"-d", self.domain,
"-o", output_file
], check=True)
with open(output_file, "r") as f:
self.subdomains.update(line.strip() for line in f if line.strip())
except Exception as e:
print_error(f"Subfinder error: {str(e)}")
def run_amass(self):
try:
output_file = f"{self.report_dir}/amass.txt"
subprocess.run([
"amass",
"enum",
"-active",
"-brute",
"-d", self.domain,
"-o", output_file
], check=True)
with open(output_file, "r") as f:
self.subdomains.update(line.strip() for line in f if line.strip())
except Exception as e:
print_error(f"Amass error: {str(e)}")
def run_assetfinder(self):
try:
result = subprocess.check_output([
"assetfinder",
"--subs-only",
self.domain
]).decode("utf-8")
output_file = f"{self.report_dir}/assetfinder.txt"
with open(output_file, "w") as f:
f.write(result)
self.subdomains.update(result.splitlines())
except Exception as e:
print_error(f"Assetfinder error: {str(e)}")
def run_findomain(self):
try:
output_file = f"{self.report_dir}/findomain.txt"
subprocess.run([
"findomain",
"-t", self.domain,
"-o"
], check=True)
temp_file = f"{self.domain}.txt"
if os.path.exists(temp_file):
os.rename(temp_file, output_file)
with open(output_file, "r") as f:
self.subdomains.update(line.strip() for line in f if line.strip())
except Exception as e:
print_error(f"Findomain error: {str(e)}")
def run_dnsrecon(self):
try:
xml_output = f"{self.report_dir}/dnsrecon.xml"
subprocess.run([
"dnsrecon",
"-d", self.domain,
"-x", xml_output
], check=True)
tree = ET.parse(xml_output)
root = tree.getroot()
for record in root.findall(".//record"):
if record.get('type') in ['A', 'CNAME']:
name = record.get('name')
if name:
self.subdomains.add(name.strip().lower())
except Exception as e:
print_error(f"DNSrecon error: {str(e)}")
def run_gobuster(self):
try:
result = subprocess.check_output([
"gobuster",
"dns",
"-d", self.domain,
"-w", self.wordlist,
"--wildcard",
"-q"
]).decode("utf-8")
output_file = f"{self.report_dir}/gobuster.txt"
with open(output_file, "w") as f:
f.write(result)
for line in result.splitlines():
if "Found: " in line:
self.subdomains.add(line.split(" ")[1])
except Exception as e:
print_error(f"Gobuster error: {str(e)}")
def run_theharvester(self):
try:
output_file = f"{self.report_dir}/theharvester.xml"
subprocess.run([
"theHarvester",
"-d", self.domain,
"-b", "all",
"-s",
"-c",
"-f", output_file
], check=True)
tree = ET.parse(f"{output_file}.xml")
root = tree.getroot()
for host in root.findall(".//host"):
hostname = host.get('name')
if hostname:
self.subdomains.add(hostname.strip().lower())
except Exception as e:
print_error(f"theHarvester error: {str(e)}")
def run_all_tools(self):
tools = [
self.run_knockpy,
self.run_sublist3r,
self.run_subfinder,
self.run_amass,
self.run_assetfinder,
self.run_findomain,
self.run_dnsrecon,
self.run_gobuster,
self.run_theharvester
]
print_info(f"\n[*] Starting subdomain enumeration for {self.domain}")
for tool in tools:
print_info(f"\n[+] Running {tool.__name__}...")
start_time = time.time()
tool()
elapsed = time.time() - start_time
print_success(f"Completed in {elapsed:.2f} seconds")
final_file = f"{self.report_dir}/all_subdomains.txt"
with open(final_file, "w") as f:
f.write("\n".join(sorted(self.subdomains)))
print_success(f"\n[+] Found {len(self.subdomains)} unique subdomains")
print_info(f"\n[*] Results saved to: {final_file}")
if __name__ == "__main__":
scanner = SubdomainScanner(domain, wordlist, resolvers)
install_required_tools()
scanner.run_all_tools()