-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathendpoint_extractor.py
More file actions
executable file
·365 lines (291 loc) · 13.4 KB
/
endpoint_extractor.py
File metadata and controls
executable file
·365 lines (291 loc) · 13.4 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#!/usr/bin/env python3
"""
Aggressive API Endpoint Extractor for NetBear
========================================
Extracts endpoints from crawl data and feeds them to Nuclei.
Handles JS files, JSON data, and crawl reports to maximize API surface discovery.
CUSTOMIZATION GUIDE:
- To exclude more patterns: Add to exclude_patterns list in normalize_endpoints()
- To increase extraction aggressiveness: Lower min_length from 5 to 3
- To reduce false positives: Add more keywords to require_keywords
- To use only specific sources: Call only extract_from_js_files() or extract_from_report()
Example customization:
exclude_patterns.extend(['/custom_noise/', 'analytics_id'])
require_keywords = ['api', 'users', 'data'] # ALL must match
PERFORMANCE NOTES:
- Processes first 20 largest JS files only (1MB each) for speed
- Filters endpoints aggressively to reduce Nuclei scan time
- Typical reduction: 95 raw → 47 clean endpoints (50% noise removal)
"""
import os
import re
import json
from pathlib import Path
from typing import Set, List, Dict
from urllib.parse import urlparse, urljoin
import sys
# Try to import from js_analyser if available
try:
from js_analyser import extract_structured_patterns
HAS_JS_ANALYSER = True
except ImportError:
HAS_JS_ANALYSER = False
class APIEndpointExtractor:
"""Extract API endpoints from NetBear crawl output
This class searches multiple sources for real API endpoints:
1. JS structure JSON files (from js_analyser)
2. Raw JavaScript files (regex-based extraction)
3. JSON response files
4. Crawl reports (pattern matching)
Then normalizes, deduplicates, and filters for quality.
"""
def __init__(self, report_dir: str, domain: str):
self.report_dir = Path(report_dir)
self.domain = domain
self.endpoints: Set[str] = set()
self.base_url = f"https://{domain}" if not domain.startswith("http") else domain
def extract_from_js_structures(self) -> Set[str]:
"""Extract endpoints from JS structure JSON files"""
endpoints = set()
js_file = self.report_dir / "js_structures.json"
if not js_file.exists():
return endpoints
try:
with open(js_file, 'r') as f:
data = json.load(f)
if isinstance(data, dict):
# Try different keys
for key in ['api_endpoints', 'endpoints', 'urls', 'calls']:
if key in data and isinstance(data[key], list):
endpoints.update(data[key])
return endpoints
except Exception as e:
print(f"[!] Error parsing js_structures.json: {e}")
return endpoints
def extract_from_js_files(self) -> Set[str]:
"""Extract endpoints from raw JS files in report directory"""
endpoints = set()
# Find all .js files in report - limit to most relevant ones
js_files = list(self.report_dir.glob("res_*-*.js"))
# Sort by size and take the first 20 (probably most relevant)
js_files = sorted(js_files, key=lambda x: x.stat().st_size, reverse=True)[:20]
for i, js_file in enumerate(js_files):
try:
with open(js_file, 'r', encoding='utf-8', errors='ignore') as f:
content = f.read(1000000) # Read first 1MB of each file
# Extract with simple regex patterns (faster than js_analyser for bulk)
endpoints.update(self._extract_endpoints_regex(content))
except Exception as e:
print(f"[!] Error reading {js_file.name}: {e}")
continue
return endpoints
def _extract_endpoints_regex(self, text: str) -> Set[str]:
"""Extract endpoints using regex patterns"""
endpoints = set()
patterns = [
# Fetch/axios patterns
r"(?:fetch|axios\.(?:get|post|put|delete|patch))\(['\"`]([^'\"`;]+)['\"`]",
# URL patterns
r"url:\s*['\"]([^'\"]+)['\"]",
r"endpoint:\s*['\"]([^'\"]+)['\"]",
r"path:\s*['\"]([/][^'\"]*)['\"]",
# API path patterns
r"/api/[a-zA-Z0-9/_-]+",
# REST endpoints
r"(?:GET|POST|PUT|DELETE|PATCH)\s+['\"]([/][^'\"]*)['\"]",
# Common patterns
r"['\"](/(?:api|v\d|graphql|rest|endpoint|service)[^'\"]*)['\"]",
]
for pattern in patterns:
matches = re.findall(pattern, text, re.IGNORECASE)
for match in matches:
# Clean up the match
if isinstance(match, tuple):
match = match[0]
if match and (match.startswith('/') or 'api' in match.lower()):
endpoints.add(match)
return endpoints
def extract_from_report(self, report_path: str) -> Set[str]:
"""Extract endpoints from report.txt"""
endpoints = set()
if not os.path.exists(report_path):
return endpoints
try:
with open(report_path, 'r', encoding='utf-8', errors='ignore') as f:
content = f.read()
# Look for any paths that look like APIs
api_patterns = [
r"/api/[a-zA-Z0-9/_-]+",
r"/v\d+/[a-zA-Z0-9/_-]+",
r"/graphql[a-zA-Z0-9/_-]*",
r"/rest/[a-zA-Z0-9/_-]+",
]
for pattern in api_patterns:
matches = re.findall(pattern, content)
endpoints.update(matches)
except Exception as e:
print(f"[!] Error reading report: {e}")
return endpoints
def extract_from_json_files(self) -> Set[str]:
"""Extract URLs from JSON response files"""
endpoints = set()
for json_file in self.report_dir.glob("*.json"):
# Skip metadata files
if json_file.name in ['js_structures.json', 'nuclei_targets.json']:
continue
try:
with open(json_file, 'r') as f:
data = json.load(f)
# Extract URLs from various possible locations
urls = self._find_urls_in_json(data)
endpoints.update(urls)
except Exception:
# Not JSON or error parsing
continue
return endpoints
def _find_urls_in_json(self, obj, max_depth=3, depth=0) -> Set[str]:
"""Recursively find URLs in JSON structures"""
urls = set()
if depth > max_depth:
return urls
if isinstance(obj, dict):
for key, value in obj.items():
# Check if key or value looks like a URL
if isinstance(value, str):
if value.startswith('/') and len(value) > 4:
urls.add(value)
elif 'api' in key.lower() and value.startswith('/'):
urls.add(value)
else:
urls.update(self._find_urls_in_json(value, max_depth, depth + 1))
elif isinstance(obj, list):
for item in obj:
urls.update(self._find_urls_in_json(item, max_depth, depth + 1))
return urls
def normalize_endpoints(self, endpoints: Set[str]) -> Set[str]:
"""Normalize endpoints to full URLs and filter out garbage
CUSTOMIZATION: Modify exclude_patterns to customize filtering:
- Add your app's specific noise patterns to exclude_patterns list
- Remove any patterns if they filter out legitimate endpoints
- Use pattern matching for flexible exclusion (e.g., '/_next' for Next.js internals)
Examples to customize:
exclude_patterns.extend(['/health', '/metrics', '/admin/console']) # Exclude specific paths
exclude_patterns.extend([r'^/_', r'^/\$']) # Exclude special prefixes
Returns:
Set of normalized, valid API endpoints
"""
normalized = set()
# Patterns to exclude (noise/false positives)
# CUSTOMIZE THIS LIST for your specific targets
exclude_patterns = [
'optionalProperties', # JSON schema artifacts
'/properties/', # JSON property references
'/type', # JSON type definitions
'/elements', # UI component references
'sentry.io', # Error tracking service
'recaptcha', # Google reCAPTCHA
'google-analytics', # Analytics library
'/viewport', # Meta tag references
'/variable/', # Template variables
'/vaccine', # Medical app artifacts
'/vidal_', # Medical database refs
'/medical_history', # Health data structure
'.com/?',
]
for ep in endpoints:
if not ep:
continue
# Skip garbage patterns
if any(pattern in ep.lower() for pattern in exclude_patterns):
continue
# Already a full URL
if ep.startswith('http'):
normalized.add(ep)
# Relative path
elif ep.startswith('/'):
normalized.add(urljoin(self.base_url, ep))
# Partial path
elif ep.startswith('api'):
normalized.add(urljoin(self.base_url, '/' + ep))
else:
# Try as-is if it looks like a path
if '/' in ep or 'api' in ep.lower():
normalized.add(urljoin(self.base_url, ep))
return normalized
def generate_nuclei_targets(self) -> List[str]:
"""Generate comprehensive target list for Nuclei"""
print(f"\n[*] Extracting API endpoints from {self.report_dir}")
# Extract from all sources
print("[*] Extracting from JS structures...")
endpoints = self.extract_from_js_structures()
print(f" ✓ Found {len(endpoints)} endpoints")
print("[*] Extracting from JS files...")
js_endpoints = self.extract_from_js_files()
endpoints.update(js_endpoints)
print(f" ✓ Found {len(js_endpoints)} endpoints")
print("[*] Extracting from JSON files...")
json_endpoints = self.extract_from_json_files()
endpoints.update(json_endpoints)
print(f" ✓ Found {len(json_endpoints)} endpoints")
print("[*] Extracting from report...")
report_file = self.report_dir / "report.txt"
report_endpoints = self.extract_from_report(str(report_file))
endpoints.update(report_endpoints)
print(f" ✓ Found {len(report_endpoints)} endpoints")
# Normalize to full URLs
print(f"\n[*] Normalizing {len(endpoints)} endpoints...")
normalized = self.normalize_endpoints(endpoints)
# Filter out non-API paths (optional)
api_only = {ep for ep in normalized if '/api/' in ep.lower() or
'/v' in ep.lower() or 'graphql' in ep.lower()}
if api_only:
print(f" ✓ {len(api_only)} API endpoints found")
targets = sorted(api_only)
else:
print(f" ℹ No /api/ paths found, using all {len(normalized)} endpoints")
targets = sorted(normalized)
# Add common API paths to probe
print("[*] Adding common API paths...")
common_paths = [
'/api',
'/api/v1',
'/api/v2',
'/api/health',
'/api/status',
'/api/config',
'/api/settings',
'/graphql',
'/.graphql',
'/rest',
'/swagger',
'/swagger.json',
'/openapi.json',
'/api-docs',
]
for path in common_paths:
full_url = urljoin(self.base_url, path)
if full_url not in targets:
targets.append(full_url)
print(f" ✓ Added {len(common_paths)} common paths")
return targets
def save_targets(self, output_file: str) -> int:
"""Save targets to file and return count"""
targets = self.generate_nuclei_targets()
with open(output_file, 'w') as f:
for target in targets:
f.write(target + '\n')
print(f"\n[+] Saved {len(targets)} targets to {output_file}")
return len(targets)
def main():
if len(sys.argv) < 2:
print("Usage: python endpoint_extractor.py <report_dir> [domain] [output_file]")
print("Example: python endpoint_extractor.py reports/run_123/doctolib_fr pro.doctolib.fr targets.txt")
sys.exit(1)
report_dir = sys.argv[1]
domain = sys.argv[2] if len(sys.argv) > 2 else "doctolib.fr"
output_file = sys.argv[3] if len(sys.argv) > 3 else "nuclei_targets.txt"
extractor = APIEndpointExtractor(report_dir, domain)
count = extractor.save_targets(output_file)
print(f"\n[✓] Ready for Nuclei: nuclei -l {output_file} -t netbear-*.yaml -o results.txt")
if __name__ == "__main__":
main()