-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparamspider.ts
executable file
·241 lines (206 loc) · 5.16 KB
/
paramspider.ts
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
#!/usr/bin/env bun
import chalk from "chalk";
import { program } from "commander";
import * as fs from "fs";
import { join } from "path";
import { fetchUrlContent, paramspiderText } from "./client";
declare global {
var stream_output: boolean;
var outputDir: string;
}
globalThis.stream_output = false;
globalThis.outputDir = "./results";
const HARDCODED_EXTENSIONS = [
".jpg",
".jpeg",
".png",
".gif",
".pdf",
".svg",
".json",
".css",
".js",
".webp",
".woff",
".woff2",
".eot",
".ttf",
".otf",
".mp4",
".txt",
];
function hasExtention(url: string, extentions: string[]) {
const parsedUrl = new URL(url);
const ext = parsedUrl.pathname.split(".").pop();
return ext && extentions.includes("." + ext.toLowerCase());
}
function cleanHostname(url: string) {
if (!url.includes(".")) return null;
if (!url.startsWith("http")) {
url = "http://" + url;
}
try {
return new URL(url).hostname;
} catch (error) {
return null;
}
}
function cleanUrl(url: string): string {
/**
* Clean the URL by removing redundant port information for HTTP and HTTPS URLs.
*
* Args:
* url (string): The URL to clean.
*
* Returns:
* string: Cleaned URL.
*/
const parsedUrl = new URL(url);
if (
(parsedUrl.port === "80" && parsedUrl.protocol === "http:") ||
(parsedUrl.port === "443" && parsedUrl.protocol === "https:")
) {
parsedUrl.port = "";
}
return parsedUrl.toString();
}
function cleanUrls(
urls: string[],
extensions: string[],
placeholder: string
): string[] {
/**
* Clean a list of URLs by removing unnecessary parameters and query strings.
*
* Args:
* urls (string[]): List of URLs to clean.
* extensions (string[]): List of file extensions to check against.
* placeholder (string): Placeholder to replace query parameters.
*
* Returns:
* string[]: List of cleaned URLs.
*/
const cleanedUrls = new Set<string>();
urls.forEach((url) => {
if (!url) return;
let cleanedUrl = cleanUrl(url);
if (!hasExtention(cleanedUrl, extensions)) {
const parsedUrl = new URL(cleanedUrl);
const queryParams = new URLSearchParams(parsedUrl.search);
const cleanedParams = new URLSearchParams();
queryParams.forEach((_, key) => {
cleanedParams.append(key, placeholder);
});
parsedUrl.search = cleanedParams.toString();
cleanedUrl = parsedUrl.toString();
cleanedUrls.add(cleanedUrl);
}
});
return Array.from(cleanedUrls);
}
async function fetch_and_clean_urls(
domain: string,
extensions: string[] = HARDCODED_EXTENSIONS,
// , stream_output,proxy,
placeholder: string = "FUZZ"
) {
const wayback_uri = `https://web.archive.org/cdx/search/cdx?url=${domain}/*&output=txt&collapse=urlkey&fl=original&page=/`;
console.log(
"[" +
chalk.green("INFO") +
"] Fetching URLs from " +
chalk.green(domain) +
"..."
);
const response = await fetchUrlContent(wayback_uri);
const urls = response.split("\n");
const cleaned_urls = cleanUrls(urls, extensions, placeholder);
console.log(
"[" +
chalk.green("INFO") +
"] Found " +
chalk.blue(cleaned_urls.length) +
" URLS for " +
chalk.green(domain) +
"..."
);
if (!fs.existsSync(globalThis.outputDir)) {
fs.mkdirSync(globalThis.outputDir);
}
const resultFile = join(globalThis.outputDir, `${cleanHostname(domain)}.txt`);
const fileStream = fs.createWriteStream(resultFile, { flags: "w" });
console.log(
"[" +
chalk.green("INFO") +
"] Extracting URLS with parameters from " +
chalk.green(domain) +
"..."
);
cleaned_urls.forEach((url) => {
if (url.includes("?")) {
fileStream.write(url + "\n");
if (globalThis.stream_output) {
console.log("[" + chalk.blue("FOUND") + "] " + url);
}
}
});
fileStream.end();
console.log(
"[" +
chalk.green("INFO") +
"] Saved cleaned urls to " +
chalk.blue(resultFile)
);
}
console.log(paramspiderText);
program.name("paramspider");
const options = program
.option("-d, --domain <string...>", "Domain to crawl")
.option("-l, --list <list...>", "List of domains to crawl")
.option(
"-p, --placeholder <string>",
"Placeholder to replace query parameters",
"FUZZ"
)
.option("-o, --output-dir <string>", "Output directory", globalThis.outputDir)
.parse(process.argv)
.opts();
if (!(options.domain || options.list)) {
program.error("Please provide either the -d option or the -l option.");
}
const domains = new Set<string>();
if (options.domain)
options.domain.map((domain: string) => {
const hostname = cleanHostname(domain);
if (!hostname) {
console.warn(`Error parsing URL ${domain}. Skipping...`);
return;
}
domains.add(hostname);
});
if (options.list) {
await options.list.map(async (filepath: string) => {
const list = fs.readFileSync(filepath, "utf-8");
const lines = list.split("\n");
lines.forEach((line) => {
// skip empty line
if (!line) return;
const hostname = cleanHostname(line);
if (!hostname) {
console.warn(`Error parsing URL ${line}. Skipping...`);
return;
}
domains.add(hostname);
});
});
}
globalThis.stream_output = options.stream;
await Promise.all(
Array.from(domains).map(async (domain) => {
await fetch_and_clean_urls(
domain,
HARDCODED_EXTENSIONS,
options.placeholder
);
})
);