I am Hack Sparrow
Captain of the Internets.

A Port Scanner in Node.js

In this tutorial, I will show you how to write a port scanner in Node.js! Hope this tutorial helps remove the misconception that Node is only for serverside programming and creating websites.

First off, what is a port scanner? A port scanner is a software that automates the process of checking if a port is open on a machine. There are various levels of sophistication when it comes to scanning ports, and there are numerous tools that are hailed as industry standard, but in this tutorial we will create a very basic port scanner that does the job and also teaches you some stuff about Node and JavaScript.

Following is the code for the port scanner. There is nothing much left for me to write here, as I have put everything in the code comments. It is highly recommended you read the comments in the code.

var net = require('net');

// the machine to scan
var host = 'localhost';
// starting from port number
var start = 1;
// to port number
var end = 10000;
// sockets should timeout asap to ensure no resources are wasted
// but too low a timeout value increases the likelyhood of missing open sockets, so be careful
var timeout = 2000;

// the port scanning loop
while (start <= end) {
    
    // it is always good to give meaningful names to your variables
    // since the context is changing, we use `port` to refer to current port to scan
    var port = start;
    
    // we create an anonynous function, pass the current port, and operate on it
    // the reason we encapsulate the socket creation process is because we want to preseve the value of `port` for the callbacks
    (function(port) {
        // console.log('CHECK: ' + port);
        var s = new net.Socket();
        
        s.setTimeout(timeout, function() { s.destroy(); });
        s.connect(port, host, function() {
            console.log('OPEN: ' + port);
            // we don't destroy the socket cos we want to listen to data event
            // the socket will self-destruct in 2 secs cos of the timeout we set, so no worries
        });
        
        // if any data is written to the client on connection, show it
        s.on('data', function(data) {
            console.log(port +': '+ data);
            s.destroy();
        });
        
        s.on('error', function(e) {
            // silently catch all errors - assume the port is closed
            s.destroy();
        });
    })(port);
    
    start++;
}

Save the files as portscanner.js and run it this way: $ node portscanner.

Since the target machine is localhost, you may not see any open ports if none are open on your machine. Run some services on a remote machine and point the scanner to it, to see the open ports.

Warning: Repeatedly port scanning a machine that does not belong to you is not recommended. It could be illegal and get you in legal trouble.

References

1. More about Port Scanning
2. What is a Port?
3. The best Port Scanner - Netcat
4. TCP and UP Ports

3 Responses to “A Port Scanner in Node.js”

  1. Michel says:

    new massive tcp ip/ports scanner here https://github.com/eviltik/evilscan ;)

  2. Captain says:

    Cool!

  3. JJ says:

    Thanks for all the great tutorials – learnt a lot about node.js
    This is the only tutorial that has not worked for me.
    I scan localhost or a pc on my local lan and no open ports are display. In addition there are not errors displayed either. Has something changed in newer versions of node as I’m currently using node v0.10.29

Make a Comment