The following script will perform a port scanning on localhost target.
https://portswigger.net/research/exposing-intranets-with-reliable-browser-based-port-scanning
let url = 'http://localhost';
let validPorts = [];
let port = 7995,
iframe = document.createElement('iframe'),
a = document.getElementById('anchor'),
timer;
iframe.name = a.target = 'probe' + Date.now();
iframe.src = url + ':' + port;
iframe.style = 'display: none;';
a.href = iframe.src + '#';
document.body.appendChild(iframe);
// If iframe loads, then the port is open
iframe.onload = () => {
validPorts.push(port);
clearTimeout(timer);
loop();
};
// Loop to scan for ports
let loop = () => {
port++;
iframe.src = url + ':' + port;
a.href = iframe.src + '#';
a.click();
// If the iframe.onload event will not trigger before the timeout, then port is closed
timer = setTimeout(() => {
document.getElementById(
'caption'
).innerHTML = `Testing port <b>${port}</b> <br> There are <b>${validPorts.length}</b> opened ports found so far: ${validPorts.length >
0
? validPorts
: 'empty'}`;
loop();
}, 5000);
};
loop();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cool Port Scanner</title>
</head>
<body>
<span id="caption" style="font-size: larger;"></span>
<a id="anchor" href=""></a>
<!-- Code -->
<script src="portscan.js"></script>
</body>
</html>