-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
308 lines (281 loc) · 11.9 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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env python3
import asyncio
import aiohttp
import async_timeout
import argparse
import logging
import re
import sys
import socket
import time
import os
import json
import csv
from bs4 import BeautifulSoup
import defusedxml.ElementTree as ET
from tqdm import tqdm
import pyfiglet
# ==============================
# Global configuration defaults
# ==============================
DEFAULT_CONCURRENCY = 10
DEFAULT_TIMEOUT = 10 # seconds
RETRY_ATTEMPTS = 3
RETRY_BACKOFF = 1 # initial backoff in seconds
# ==============================
# Utility functions
# ==============================
def validate_domain(domain: str) -> str:
"""Ensure that the domain has a proper protocol.
Try HTTPS first, and fallback to HTTP if needed."""
domain = domain.strip()
if not domain.startswith("http://") and not domain.startswith("https://"):
test_url = "https://" + domain
try:
# A quick synchronous check before scanning
requests_timeout = 5
import requests # using requests here only for a quick test
r = requests.get(test_url, timeout=requests_timeout)
if r.status_code < 400:
return "https://" + domain
except Exception:
pass
# Fallback to HTTP
return "http://" + domain
return domain
def check_internet_connection() -> bool:
"""Perform a simple connectivity check."""
try:
import requests
requests.get("https://www.google.com", timeout=5)
return True
except Exception:
return False
# ==============================
# The core scanner class
# ==============================
class TraceboundScanner:
def __init__(self, base_url: str, phrase: str, *,
regex: bool = False,
concurrency: int = DEFAULT_CONCURRENCY,
timeout: int = DEFAULT_TIMEOUT,
output_format: str = "txt",
debug: bool = False):
self.base_url = base_url.rstrip('/')
self.phrase = phrase
self.regex = regex
self.concurrency = concurrency
self.timeout = timeout
self.output_format = output_format.lower()
self.debug = debug
# Compile regex or prepare plain-text search
if self.regex:
self.phrase_pattern = re.compile(phrase, re.IGNORECASE)
else:
self.phrase_lower = phrase.lower()
# Containers for results and state
self.found_urls = []
self.visited_sitemaps = set() # avoid re-parsing the same sitemap
# Setup logger for structured logging
self.logger = logging.getLogger("TraceboundScanner")
self.logger.debug("Initialized TraceboundScanner")
async def fetch(self, url: str, session: aiohttp.ClientSession,
retries: int = RETRY_ATTEMPTS) -> str:
"""Fetch a URL with exponential backoff retry."""
for attempt in range(1, retries + 1):
try:
async with async_timeout.timeout(self.timeout), session.get(url) as response:
if response.status == 200:
self.logger.debug(f"Fetched {url} successfully.")
return await response.text()
else:
self.logger.warning(f"{url} returned status {response.status}")
return ""
except (aiohttp.ClientError, asyncio.TimeoutError) as e:
self.logger.error(f"Attempt {attempt}: Error fetching {url}: {e}")
await asyncio.sleep(RETRY_BACKOFF * (2 ** (attempt - 1)))
self.logger.error(f"Failed to fetch {url} after {retries} attempts.")
return ""
async def parse_sitemap(self, sitemap_url: str, session: aiohttp.ClientSession) -> list:
"""Recursively parse a sitemap to extract page URLs."""
if sitemap_url in self.visited_sitemaps:
self.logger.debug(f"Already visited sitemap: {sitemap_url}")
return []
self.visited_sitemaps.add(sitemap_url)
self.logger.info(f"Parsing sitemap: {sitemap_url}")
content = await self.fetch(sitemap_url, session)
if not content:
return []
try:
root = ET.fromstring(content)
except ET.ParseError as e:
self.logger.error(f"XML parse error at {sitemap_url}: {e}")
return []
# Namespace for sitemaps
ns = {"s": "http://www.sitemaps.org/schemas/sitemap/0.9"}
urls = []
for loc in root.findall(".//s:loc", ns):
url_text = loc.text.strip()
if "sitemap" in url_text.lower():
# Nested sitemap – recurse
nested_urls = await self.parse_sitemap(url_text, session)
urls.extend(nested_urls)
else:
urls.append(url_text)
return urls
async def get_all_page_urls(self, session: aiohttp.ClientSession) -> list:
"""Attempt to locate common sitemap files and return all page URLs."""
sitemap_paths = ['/sitemap.xml', '/sitemap_index.xml']
tasks = []
for path in sitemap_paths:
sitemap_url = f"{self.base_url}{path}"
self.logger.info(f"Looking for sitemap at: {sitemap_url}")
tasks.append(self.parse_sitemap(sitemap_url, session))
results = await asyncio.gather(*tasks, return_exceptions=True)
page_urls = []
for result in results:
if isinstance(result, list):
page_urls.extend(result)
else:
self.logger.error(f"Error retrieving sitemap: {result}")
unique_urls = list(set(page_urls))
self.logger.info(f"Found {len(unique_urls)} unique page URLs in sitemaps.")
return unique_urls
async def scan_page(self, page_url: str, session: aiohttp.ClientSession, semaphore: asyncio.Semaphore) -> None:
"""Scan a single page for the phrase."""
async with semaphore:
content = await self.fetch(page_url, session)
if not content:
return
soup = BeautifulSoup(content, 'html.parser')
text = soup.get_text(separator=" ", strip=True)
found = False
if self.regex:
if self.phrase_pattern.search(text):
found = True
else:
if self.phrase_lower in text.lower():
found = True
if found:
self.logger.info(f"Phrase found at: {page_url}")
self.found_urls.append(page_url)
else:
self.logger.debug(f"Phrase not found at: {page_url}")
async def run(self) -> None:
"""Run the full scan: retrieve sitemaps, scan pages concurrently, and write results."""
connector = aiohttp.TCPConnector(limit=self.concurrency)
headers = {
"User-Agent": "Tracebound/2.0 (https://tracebound.example.com)",
"Accept": "text/html,application/xhtml+xml,application/xml"
}
async with aiohttp.ClientSession(connector=connector, headers=headers) as session:
# Get all page URLs from sitemap(s)
page_urls = await self.get_all_page_urls(session)
if not page_urls:
self.logger.error("No page URLs found. Exiting scan.")
return
self.logger.info("Beginning page scan...")
semaphore = asyncio.Semaphore(self.concurrency)
tasks = [self.scan_page(url, session, semaphore) for url in page_urls]
# Use tqdm to display progress
for f in tqdm(asyncio.as_completed(tasks), total=len(tasks), desc="Scanning pages"):
try:
await f
except Exception as e:
self.logger.error(f"Error scanning a page: {e}")
self.logger.info(f"Scan complete. Found phrase on {len(self.found_urls)} pages.")
await self.write_results()
async def write_results(self) -> None:
"""Write found URLs to file in the chosen output format."""
timestamp = int(time.time())
if self.output_format == "json":
filename = f"results_{timestamp}.json"
data = {"found_urls": self.found_urls}
with open(filename, "w") as f:
json.dump(data, f, indent=4)
elif self.output_format == "csv":
filename = f"results_{timestamp}.csv"
with open(filename, "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["URL"])
for url in self.found_urls:
writer.writerow([url])
else:
# Default to plain text
filename = f"results_{timestamp}.txt"
with open(filename, "w") as f:
for url in self.found_urls:
f.write(url + "\n")
self.logger.info(f"Results written to {filename}")
# ==============================
# Main entry point
# ==============================
def main():
# Parse command-line arguments
parser = argparse.ArgumentParser(
description="Tracebound - A scalable, asynchronous domain-based phrase scanner."
)
parser.add_argument("domain", nargs="?", help="Domain to scan (e.g. example.com)")
parser.add_argument("phrase", nargs="?", help="Phrase to search for")
parser.add_argument("--regex", action="store_true", help="Interpret the phrase as a regular expression")
parser.add_argument("--concurrency", type=int, default=DEFAULT_CONCURRENCY, help="Number of concurrent requests")
parser.add_argument("--timeout", type=int, default=DEFAULT_TIMEOUT, help="Request timeout in seconds")
parser.add_argument("--output", choices=["txt", "json", "csv"], default="txt", help="Output format for results")
parser.add_argument("--debug", action="store_true", help="Enable debug logging output")
args = parser.parse_args()
# Configure logging
log_level = logging.DEBUG if args.debug else logging.INFO
logging.basicConfig(
level=log_level,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
logger = logging.getLogger("Tracebound")
# Clear screen and print ASCII art welcome message
os.system('cls' if os.name == 'nt' else 'clear')
try:
ascii_logo = pyfiglet.figlet_format("Tracebound v2.0")
print(ascii_logo)
except Exception:
print("Tracebound v2.0")
# Check internet connectivity
if not check_internet_connection():
logger.error("No internet connection detected. Exiting.")
sys.exit(1)
# Interactive input if not provided as arguments
if not args.domain:
domain_input = input("Enter the domain you want to scan (e.g. example.com): ").strip()
else:
domain_input = args.domain.strip()
if not args.phrase:
phrase_input = input("Enter the phrase you want to search for: ").strip()
else:
phrase_input = args.phrase.strip()
# Validate and prepare the domain URL
domain_url = validate_domain(domain_input)
logger.info(f"Scanning domain: {domain_url}")
logger.info(f"Searching for phrase: {phrase_input}")
# Optionally, check that the domain resolves to an IP address
try:
resolved_ip = socket.gethostbyname(domain_input)
logger.debug(f"Domain {domain_input} resolves to {resolved_ip}")
except socket.gaierror:
logger.warning(f"Warning: The domain '{domain_input}' did not resolve to an IP address.")
# Create and run the scanner
scanner = TraceboundScanner(
base_url=domain_url,
phrase=phrase_input,
regex=args.regex,
concurrency=args.concurrency,
timeout=args.timeout,
output_format=args.output,
debug=args.debug
)
try:
asyncio.run(scanner.run())
except KeyboardInterrupt:
logger.info("Scan interrupted by user.")
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
if __name__ == "__main__":
main()