-
Notifications
You must be signed in to change notification settings - Fork 37
/
script.js
466 lines (386 loc) · 16.7 KB
/
script.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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
let parsedData = [];
let cachedFilteredData = [];
let currentPage = 1;
const itemsPerPage = 20;
const dataSources = {
Vulnhub: 'https://raw.githubusercontent.com/InfoSecWarrior/Vulnerable-Box-Resources/refs/heads/main/Vulnhub-Raw-File-Links.txt',
Infosecwarrior: 'https://raw.githubusercontent.com/InfoSecWarrior/Vulnerable-Box-Resources/refs/heads/main/Infosecwarrior-Raw-File-Links.txt',
HTB: 'https://raw.githubusercontent.com/InfoSecWarrior/Vulnerable-Box-Resources/refs/heads/main/HTB-Raw-File-Links.txt',
Other: 'https://raw.githubusercontent.com/InfoSecWarrior/Vulnerable-Box-Resources/refs/heads/main/Other-Raw-Files-Links.txt'
};
async function fetchAllData() {
console.log('Starting to fetch all data from sources...');
updateStatus('Loading data from all sources...');
parsedData = [];
cachedFilteredData = [];
currentPage = 1;
const fetchPromises = Object.keys(dataSources).map(dataSource => fetchData(dataSource));
try {
await Promise.all(fetchPromises);
updateStatus('Data loaded from all available sources.');
console.log('Data loading completed successfully. Total records:', parsedData.length);
// Update the total machine count
updateTotalMachinesCount();
renderPage(currentPage, parsedData); // Initial render of all data
} catch (error) {
console.error('Error loading data from some sources:', error);
updateStatus('Data loading completed.');
}
}
async function fetchData(dataSource) {
const urlFile = dataSources[dataSource];
updateStatus(`Loading from ${dataSource}...`);
try {
const response = await fetch(urlFile);
if (!response.ok) throw new Error(`HTTP error! Status: ${response.status}`);
const urlText = await response.text();
const urls = urlText.trim().split(/\r?\n/);
// Remove duplicates
const uniqueUrls = urls.filter(url => !parsedData.some(data => data.machineFile === url));
// Remove concurrency limit or increase it
const fetchPromises = uniqueUrls.map(url => fetchAndParseXML(url, dataSource));
await Promise.all(fetchPromises);
updateStatus(`Data loaded from ${dataSource}.`);
} catch (error) {
console.error(`Error fetching URL file ${urlFile}:`, error);
updateStatus(`Error loading data from ${dataSource}.`);
}
}
async function fetchAndParseXML(url, dataSource) {
try {
const xmlResponse = await fetch(url);
if (!xmlResponse.ok) {
console.warn(`Skipping ${url} due to HTTP error! Status: ${xmlResponse.status}`);
return;
}
const xmlText = await xmlResponse.text();
parseXML(xmlText, url, dataSource);
} catch (error) {
console.error(`Error fetching XML from ${url} in ${dataSource}:`, error);
}
}
function parseXML(xmlText, url, platform) {
try {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlText, 'application/xml');
const urlSegments = url.split('/');
const encodedDirectoryName = urlSegments[urlSegments.length - 2];
const directoryName = decodeURIComponent(encodedDirectoryName);
console.log('Original Directory Name:', directoryName);
const modifiedDirectoryName = directoryName.replace(/-/g, ' ');
console.log('Modified Directory Name:', modifiedDirectoryName);
const fullFileName = urlSegments[urlSegments.length - 1];
const fileBase = fullFileName.replace('-nmap-version-scan-output.xml', '');
const hosts = xmlDoc.querySelectorAll('host');
hosts.forEach(host => {
const ports = host.querySelectorAll('port');
const portDetails = Array.from(ports).map(port => {
const portId = port.getAttribute('portid');
const protocol = port.getAttribute('protocol');
return `${portId}/${protocol}`;
}).join(', ');
const serviceDetails = Array.from(host.querySelectorAll('service')).map(service => ({
serviceName: service.getAttribute('name') || 'Unknown',
serviceProduct: service.getAttribute('product') || 'Unknown',
serviceVersion: service.getAttribute('version') || 'Unknown'
}));
const machineFileName = fullFileName.replace(/\.xml$/, '.nmap');
if (!parsedData.some(data => data.machineFile === machineFileName)) {
parsedData.push({
machineName: modifiedDirectoryName,
portDetails,
serviceDetails,
machineDir: encodedDirectoryName,
machineFile: machineFileName,
fileBaseName: fileBase,
platform
});
}
});
} catch (error) {
console.error('Error parsing XML:', error);
}
}
// Helper function to update status text
function updateStatus(message) {
const statusElement = document.getElementById('status');
if (statusElement) {
statusElement.textContent = message;
}
}
function renderPage(page, data, query = '') {
console.log(`Rendering page ${page} with query: "${query}"`);
const startIndex = (page - 1) * itemsPerPage;
const endIndex = startIndex + itemsPerPage;
const paginatedData = data.slice(startIndex, endIndex);
renderMachines(paginatedData, query);
updatePaginationControls(page, data.length);
const totalCountElement = document.getElementById('totalCount');
if (totalCountElement) {
if (query === '') {
// Show total results when there's no search query
totalCountElement.textContent = `Search Results: 0`;
} else {
// Show filtered result count during search
totalCountElement.textContent = `Search Results: ${data.length}`;
}
}
}
function updatePaginationControls(page, totalItems) {
const totalPages = Math.ceil(totalItems / itemsPerPage);
// Enable or disable prev/next buttons
const prevBtn = document.getElementById('prevBtn');
const nextBtn = document.getElementById('nextBtn');
prevBtn.disabled = page === 1;
nextBtn.disabled = page === totalPages;
// Update the "Back" and "Next" button text and click events
prevBtn.innerHTML = '‹ Back';
nextBtn.innerHTML = 'Next ›';
prevBtn.addEventListener('click', () => {
if (page > 1) {
currentPage = page - 1;
renderPage(currentPage, cachedFilteredData.length > 0 ? cachedFilteredData : parsedData);
window.scrollTo({ top: 0 });
}
});
nextBtn.addEventListener('click', () => {
if (page < totalPages) {
currentPage = page + 1;
renderPage(currentPage, cachedFilteredData.length > 0 ? cachedFilteredData : parsedData);
window.scrollTo({ top: 0 });
}
});
// Dynamically create page numbers with ellipsis
const paginationPages = document.querySelector('.pagination-pages');
paginationPages.innerHTML = ''; // Clear previous pagination
const maxVisiblePages = 7; // Limit visible pages
const pagesToShow = [];
// Always show the first page
pagesToShow.push(1);
// Determine if ellipsis is needed before current page set
if (page > maxVisiblePages - 3) {
pagesToShow.push('...');
}
// Show pages around the current page
const startPage = Math.max(2, page - 2); // Show up to 2 pages before the current page
const endPage = Math.min(page + 2, totalPages - 1); // Show up to 2 pages after the current page
for (let i = startPage; i <= endPage; i++) {
if (i > 1 && i < totalPages) {
pagesToShow.push(i);
}
}
// Determine if ellipsis is needed before the last page
if (page < totalPages - 3) {
pagesToShow.push('...');
}
// Always show the last page
if (totalPages > 1) {
pagesToShow.push(totalPages);
}
// Create page buttons dynamically
pagesToShow.forEach(pageNumber => {
if (pageNumber === '...') {
const ellipsis = document.createElement('span');
ellipsis.textContent = '...';
paginationPages.appendChild(ellipsis);
} else {
const pageButton = document.createElement('button');
pageButton.classList.add('page-number');
pageButton.textContent = pageNumber;
// Highlight the active page
if (pageNumber === page) {
pageButton.classList.add('active');
}
// Add click event to load the selected page
pageButton.addEventListener('click', () => {
currentPage = pageNumber;
renderPage(currentPage, cachedFilteredData.length > 0 ? cachedFilteredData : parsedData);
window.scrollTo({ top: 0 });
});
paginationPages.appendChild(pageButton);
}
});
console.log(`Updated pagination: Page ${page} of ${totalPages}`);
}
function renderMachines(data, query = '') {
const tableBody = document.getElementById('table-body');
if (!tableBody) return;
tableBody.innerHTML = '';
const lowerQuery = query.toLowerCase();
const escapeHTML = (str) => {
const div = document.createElement('div');
div.textContent = str;
return div.innerHTML;
};
const highlight = (text, query) => {
if (!query) return escapeHTML(text);
const regex = new RegExp(`(${escapeRegExp(query)})`, 'gi');
return escapeHTML(text).replace(regex, '<span style="color: red;">$1</span>');
};
data.forEach(item => {
const row = document.createElement('tr');
// Determine the appropriate platform class based on item.platform
let platformClass = '';
switch(item.platform.toLowerCase()) {
case 'vulnhub':
platformClass = 'vulnhub-tag';
break;
case 'htb':
platformClass = 'htb-tag';
break;
case 'infosecwarrior':
platformClass = 'infosecwarrior-tag';
break;
default:
platformClass = 'other-tag';
}
// Machine name link
const machineNameLink = `<a href="machine.html?dir=${encodeURIComponent(item.machineDir)}&file=${encodeURIComponent(item.machineFile)}&platform=${encodeURIComponent(item.platform)}" class="machine-name">
${highlight(item.machineName, lowerQuery)}
</a>`;
const platformTag = `<span class="platform-tag ${platformClass}" data-platform="${item.platform}">${item.platform}</span>`;
const portDetails = item.portDetails
.split(', ')
.map(port => `<span class="port-entry">${highlight(port, lowerQuery)}</span>`)
.join('');
const serviceNames = item.serviceDetails
.map(service => `<span class="service-entry">${highlight(service.serviceName, lowerQuery)}</span>`)
.join('');
const serviceProducts = item.serviceDetails
.map(service => `<span class="product-version-entry">${highlight(service.serviceProduct, lowerQuery)} ${highlight(service.serviceVersion, lowerQuery)}</span>`)
.join('');
row.innerHTML = `
<td>${machineNameLink} ${platformTag}</td>
<td>${portDetails}</td>
<td>${serviceNames}</td>
<td>${serviceProducts}</td>
`;
tableBody.appendChild(row);
});
// Add event listener for platform tag search
document.querySelectorAll('.platform-tag').forEach(tag => {
tag.addEventListener('click', function () {
const platform = this.getAttribute('data-platform').toLowerCase();
document.getElementById('searchBar').value = `platform:${platform}`;
handleSearch(`platform:${platform}`);
window.scrollTo({
top: 0
});
});
});
if (data.length === 0) {
const noDataRow = document.createElement('tr');
noDataRow.innerHTML = '<td colspan="4">No results found.</td>';
tableBody.appendChild(noDataRow);
}
console.log(`Rendered ${data.length} machine entries.`);
}
// Escape RegExp special characters
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
// Pagination button event listeners
document.getElementById('prevBtn').addEventListener('click', () => {
if (currentPage > 1) {
currentPage--;
const dataToRender = cachedFilteredData.length > 0 ? cachedFilteredData : parsedData;
renderPage(currentPage, dataToRender, document.getElementById('searchBar').value);
window.scrollTo({
top: 0
});
}
});
document.getElementById('nextBtn').addEventListener('click', () => {
const dataLength = cachedFilteredData.length > 0 ? cachedFilteredData.length : parsedData.length;
const totalPages = Math.ceil(dataLength / itemsPerPage);
if (currentPage < totalPages) {
currentPage++;
const dataToRender = cachedFilteredData.length > 0 ? cachedFilteredData : parsedData;
renderPage(currentPage, dataToRender, document.getElementById('searchBar').value);
window.scrollTo({
top: 0,
});
}
});
// Fetch data on page load
window.addEventListener('DOMContentLoaded', fetchAllData);
// Debounced search handler
let debounceTimeout;
document.getElementById('searchBar').addEventListener('input', function () {
clearTimeout(debounceTimeout);
debounceTimeout = setTimeout(() => {
handleSearch(this.value);
}, 300);
});
function handleSearch(query) {
const trimmedQuery = query.toLowerCase().trim();
let filteredData = parsedData;
if (trimmedQuery === '') {
cachedFilteredData = [];
currentPage = 1;
renderPage(currentPage, parsedData);
return; // No need to update count when search is cleared
}
const patterns = {
machine: /machine:([a-zA-Z0-9-_]+)/i,
port: /port:(\d+)/i,
service: /service:([a-zA-Z0-9-_]+)/i,
product: /product:([a-zA-Z0-9-_]+)/i,
version: /version:([a-zA-Z0-9._-]+)/i,
platform: /platform:(vulnhub|htb|other|infosecwarrior)/i
};
let hasFilters = false;
Object.entries(patterns).forEach(([key, regex]) => {
const match = trimmedQuery.match(regex);
if (match) {
hasFilters = true;
filteredData = filteredData.filter(item => {
switch (key) {
case 'platform':
return item.platform.toLowerCase() === match[1].toLowerCase();
case 'machine':
return item.machineName.toLowerCase().includes(match[1].toLowerCase());
case 'port':
return item.portDetails.includes(match[1]);
case 'service':
return item.serviceDetails.some(service => service.serviceName.toLowerCase().includes(match[1].toLowerCase()));
case 'product':
return item.serviceDetails.some(service => service.serviceProduct.toLowerCase().includes(match[1].toLowerCase()));
case 'version':
return item.serviceDetails.some(service => service.serviceVersion.toLowerCase().includes(match[1].toLowerCase()));
default:
return true;
}
});
}
});
if (!hasFilters) {
filteredData = parsedData.filter(item =>
item.machineName.toLowerCase().includes(trimmedQuery) ||
item.portDetails.includes(trimmedQuery) ||
item.serviceDetails.some(service =>
service.serviceName.toLowerCase().includes(trimmedQuery) ||
service.serviceProduct.toLowerCase().includes(trimmedQuery) ||
service.serviceVersion.toLowerCase().includes(trimmedQuery)
)
);
}
cachedFilteredData = filteredData;
currentPage = 1;
renderPage(currentPage, cachedFilteredData, trimmedQuery);
}
function updateTotalMachinesCount() {
const totalMachinesElement = document.getElementById('totalMachines');
if (totalMachinesElement) {
totalMachinesElement.textContent = `Total Vulnerable Machines: ${parsedData.length}`;
}
}
// Add event listeners to predefined pattern buttons
document.querySelectorAll('.predefined-patterns button').forEach(button => {
button.addEventListener('click', function () {
const pattern = this.getAttribute('data-pattern');
const searchBar = document.getElementById('searchBar');
searchBar.value = pattern;
searchBar.dispatchEvent(new Event('input'));
});
});