Skip to content

Instantly share code, notes, and snippets.

@tsumarios
Created July 2, 2020 09:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsumarios/4a51f3d91a1a70c0fa0ea2738816ea39 to your computer and use it in GitHub Desktop.
Save tsumarios/4a51f3d91a1a70c0fa0ea2738816ea39 to your computer and use it in GitHub Desktop.
Browser-based Port Scanner. Tested on Firefox.

A simple browser Port Scanner

The following script will perform a port scanning on localhost target.

Reference

https://portswigger.net/research/exposing-intranets-with-reliable-browser-based-port-scanning

portscan.js

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();

index.html

<!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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment