-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
260 lines (234 loc) · 6.52 KB
/
index.js
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
const puppeteer = require("puppeteer");
const fs = require("fs");
const path = require("path");
const minimist = require("minimist");
const helpText = () => {
(async () => {
const chalk = (await import("chalk")).default;
const logo = chalk.blueBright(
`
__ __ __ __
.-----.| |--.-----.--| |.---.-.-----.______.-----.----.---.-.| |--.| |--.-----.----.
|__ --|| | _ | _ || _ | |______| _ | _| _ || _ || _ | -__| _|
|_____||__|__|_____|_____||___._|__|__| |___ |__| |___._||_____||_____|_____|__|
|_____| v1.0.0
`
);
const description = chalk.blueBright(
"Shodan-Grabber\n" +
"===============\n" +
"\n" +
"A Node.js tool for scraping IP addresses and other information\n" +
"from Shodan's web interface. It utilizes Puppeteer for web\n" +
"scraping and handles rate limits by implementing retries with\n" +
"delays. The tool can run multiple scraping tasks in parallel and\n" +
"outputs the data to text files.\n"
);
const usage = chalk.blueBright(
" Usage:\n" +
" node index.js --query=<query> --help=<help> --max_parallel=<max_parallel=100> --retry_delay=<retry_delay=30000>"
);
const example = chalk.blueBright(
"\n Example:\n" + ' node index.js --query="bmw.com"'
);
console.log(chalk.blueBright(logo));
console.log(chalk.blueBright(description));
console.log(chalk.blueBright(usage));
console.log(chalk.blueBright(example));
process.exit(0);
})();
};
(async () => {
const chalk = (await import("chalk")).default;
// Parse command line arguments
let args = null;
try {
// Parse arguments
args = minimist(process.argv.slice(2));
} catch (_) {
helpText();
}
// Search query argument
const query = args.query;
// Max parallel tasks and delay between retries
const MAX_PARALLEL = args.max_parallel || 100;
const RETRY_DELAY_MS = args.retry_delay || 30000;
if (args.help || args.h || !query) {
helpText();
console.error(
chalk.red("⚠️ Please specify a query with the --query argument.")
);
} else {
if (!query) {
console.error(
chalk.red("⚠️ Please specify a query with the --query argument.")
);
process.exit(1);
}
}
// Ensure output directory exists
if (!fs.existsSync("output")) {
fs.mkdirSync("output");
}
// List of different categories
const categories = [
"asn",
"bitcoin.ip",
"bitcoin.ip_count",
"bitcoin.port",
"bitcoin.user_agent",
"bitcoin.version",
"city",
"cloud.provider",
"cloud.region",
"cloud.service",
"country",
"cpe",
"device",
"domain",
"has_screenshot",
"hash",
"http.component",
"http.component_category",
"http.favicon.hash",
"http.headers_hash",
"http.html_hash",
"http.robots_hash",
"http.status",
"http.title",
"http.waf",
"ip",
"isp",
"link",
"mongodb.database.name",
"ntp.ip",
"ntp.ip_count",
"ntp.more",
"ntp.port",
"org",
"os",
"port",
"postal",
"product",
"redis.key",
"region",
"rsync.module",
"screenshot.hash",
"screenshot.label",
"snmp.contact",
"snmp.location",
"snmp.name",
"ssh.cipher",
"ssh.fingerprint",
"ssh.hassh",
"ssh.mac",
"ssh.type",
"ssl.alpn",
"ssl.cert.alg",
"ssl.cert.expired",
"ssl.cert.extension",
"ssl.cert.fingerprint",
"ssl.cert.issuer.cn",
"ssl.cert.pubkey.bits",
"ssl.cert.pubkey.type",
"ssl.cert.serial",
"ssl.cert.subject.cn",
"ssl.chain_count",
"ssl.cipher.bits",
"ssl.cipher.name",
"ssl.cipher.version",
"ssl.ja3s",
"ssl.jarm",
"ssl.version",
"state",
"tag",
"telnet.do",
"telnet.dont",
"telnet.option",
"telnet.will",
"telnet.wont",
"uptime",
"version",
"vuln",
"vuln.verified",
];
async function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
(async () => {
const browser = await puppeteer.launch();
// Function to process a single category
async function processCategory(category) {
const page = await browser.newPage();
const url = `https://www.shodan.io/search/facet?query=${encodeURIComponent(
query
)}&facet=${category}`;
let response;
let retries = 0;
// Retry loop to handle rate limiting
while (retries < 3) {
response = await page.goto(url);
// Check if we received a 200 response
if (response && response.status() === 200) {
console.log(
chalk.greenBright(
`✔️ Successfully retrieved data for ${chalk.cyanBright(
category
)}`
)
);
break;
} else {
console.log(
chalk.yellow(
`⏳ Rate limit detected for ${chalk.magenta(
category
)}. Retrying in ${RETRY_DELAY_MS / 1000} seconds...`
)
);
retries++;
await delay(RETRY_DELAY_MS);
}
}
// Scrape IPs from the page
const cat = await page.evaluate(() => {
const catElements = document.querySelectorAll("strong");
return Array.from(catElements).map((e) =>
e.innerHTML.replace(/["']/g, "")
);
});
if (cat.length === 0) {
console.error(
chalk.redBright(
`❌ Failed to retrieve data for ${chalk.cyan(category)}`
)
);
await page.close();
return;
}
// Save IPs to file
const filePath = path.join("output", `${category}.txt`);
fs.writeFileSync(filePath, cat.join("\n"));
console.log(
chalk.bold.green(
`💾 data for ${chalk.cyanBright(category)} saved to ${chalk.underline(
filePath
)}`
)
);
await page.close();
}
// Process categories in batches of MAX_PARALLEL
console.log(
chalk.blueBright("\n🚀 Starting multithreaded Shodan scraping...\n")
);
for (let i = 0; i < categories.length; i += MAX_PARALLEL) {
const batch = categories.slice(i, i + MAX_PARALLEL);
await Promise.all(batch.map((category) => processCategory(category)));
}
console.log(
chalk.greenBright("\n🎉 All tasks completed! Closing browser...\n")
);
await browser.close();
})();
})();