-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenapi-enum.py
More file actions
214 lines (150 loc) · 4.65 KB
/
openapi-enum.py
File metadata and controls
214 lines (150 loc) · 4.65 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
#!/usr/bin/env python3
import argparse
import json
import yaml
import re
import os
import subprocess
import requests
from urllib.parse import urljoin
DEFAULT_FUZZ = "FUZZ"
def load_openapi_from_file(path):
with open(path, "r") as f:
if path.endswith(".yaml") or path.endswith(".yml"):
return yaml.safe_load(f)
return json.load(f)
def load_openapi_from_url(url):
r = requests.get(url, timeout=30)
r.raise_for_status()
try:
return r.json()
except Exception:
return yaml.safe_load(r.text)
def get_base_urls(spec):
bases = []
# OpenAPI v3
if "servers" in spec:
for s in spec["servers"]:
url = s.get("url")
if url:
bases.append(url)
# OpenAPI v2 (Swagger)
elif "host" in spec:
scheme = "https"
if "schemes" in spec and len(spec["schemes"]) > 0:
scheme = spec["schemes"][0]
base_path = spec.get("basePath", "")
bases.append(f"{scheme}://{spec['host']}{base_path}")
return bases
def fuzz_path(path, fuzz_value):
return re.sub(r"\{.*?\}", fuzz_value, path)
def extract_endpoints(spec, fuzz_value):
paths = spec.get("paths", {})
endpoints = []
for path, methods in paths.items():
fuzzed = fuzz_path(path, fuzz_value)
for method in methods.keys():
endpoints.append((method.upper(), fuzzed))
return endpoints
def build_urls(bases, endpoints):
urls = []
for base in bases:
for method, path in endpoints:
full = urljoin(base.rstrip("/") + "/", path.lstrip("/"))
urls.append((method, full))
return urls
def ensure_output_dir(directory):
if not os.path.exists(directory):
os.makedirs(directory)
def write_outputs(urls, outdir):
all_urls = set()
get_urls = set()
post_urls = set()
for method, url in urls:
all_urls.add(url)
if method == "GET":
get_urls.add(url)
if method == "POST":
post_urls.add(url)
all_path = os.path.join(outdir, "endpoints_all.txt")
get_path = os.path.join(outdir, "endpoints_get.txt")
post_path = os.path.join(outdir, "endpoints_post.txt")
with open(all_path, "w") as f:
f.write("\n".join(sorted(all_urls)))
with open(get_path, "w") as f:
f.write("\n".join(sorted(get_urls)))
with open(post_path, "w") as f:
f.write("\n".join(sorted(post_urls)))
return all_path
def run_httpx(input_file, outdir):
output = os.path.join(outdir, "httpx_alive.txt")
print("[+] Running httpx to probe endpoints...")
try:
subprocess.run(
[
"httpx",
"-l", input_file,
"-silent",
"-status-code",
"-mc", "200",
"-o", output
],
check=True
)
except FileNotFoundError:
print("[-] httpx not found in PATH. Skipping probing.")
return
print(f"[+] httpx results saved to: {output}")
def main():
parser = argparse.ArgumentParser(
description="Extract endpoints from OpenAPI specs and probe them with httpx"
)
parser.add_argument(
"-i",
"--input",
help="OpenAPI specification file (json/yaml)"
)
parser.add_argument(
"-u",
"--url",
help="Remote OpenAPI specification URL"
)
parser.add_argument(
"--fuzz-value",
default=DEFAULT_FUZZ,
help="Value used to replace path parameters (default: FUZZ)"
)
parser.add_argument(
"-o",
"--output",
default="results",
help="Output directory"
)
args = parser.parse_args()
if not args.input and not args.url:
print("[-] You must provide either an input file (-i) or a URL (-u)")
return
print("[+] Loading OpenAPI specification...")
try:
if args.input:
spec = load_openapi_from_file(args.input)
else:
spec = load_openapi_from_url(args.url)
except Exception as e:
print(f"[-] Failed to load specification: {e}")
return
bases = get_base_urls(spec)
if not bases:
print("[-] No base URLs found in specification")
return
print(f"[+] Found {len(bases)} base URL(s)")
endpoints = extract_endpoints(spec, args.fuzz_value)
print(f"[+] Extracted {len(endpoints)} endpoints")
urls = build_urls(bases, endpoints)
ensure_output_dir(args.output)
all_file = write_outputs(urls, args.output)
print("[+] Endpoint lists generated")
run_httpx(all_file, args.output)
print("[+] Done.")
if __name__ == "__main__":
main()