How to implement a port scanner using some scripting language?

5.33K viewsProgramminghacking nodejs port programming scanner
0

How to implement a port scanner using some scripting language?

Answered question
0

Below is a port scanner in PERL:-

Below is a small perl script to achieve the same
 #!/usr/bin/perl
 use IO::Socket;
 $port = 1;
$ip   = "192.168";  // should be the IP address prefix of the IPs in your  network or localhost
$sub1 = 0;
$sub2 = 0;
$limit = 255;
 $output = "/home/L098867/Desktop/OpenPorts.txt"; // output to be redirected to this txt file
 open (LIST, " >>$output");
 while($sub1 <= $limit){
     while($sub2 <= $limit){
// scan ports from 0 until 65535
        while ($port <= 65535){
             $fip = $ip.".$sub1".".$sub2";
     $sock = new IO::Socket::INET (PeerAddr => $fip,
             PeerPort => $port,
             Proto => 'tcp');
     if ($sock){
         close $sock;
         print "$fip : " + "$port -open\n";
         print LIST "$fip : " + "$port -open\n";
    }
     else{
         print "$fip : " + "$port -closed\n";
    }
             $port = $port + 1;
            print "$fip\n\n";
        }
         $sub2 = $sub2 + 1;
        $port = 0;
        print "\n\n";
    }
     $sub1 = $sub1 + 1;
    $port = 0;
    print "\n\n";
}
 close(LIST);

Answered question
0

We can use any scripting language like python,perl,php etc. to achieve this. The idea is to create a socket on a port range and try to connect it. If connection allowed, means that particular port is open. If not, means the port is closed. Below is a nodeJS version of a port scanner trying to scan ports from number 1 to 10000.

(function() {
 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 <code>port</code> 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 <code>port</code> 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++;
 }
 })();

Install node.js from node website here . Save the above code in a js file and run the above from command prompt using
node
Below is a sample output of the above script:

OPEN: 445
OPEN: 443
OPEN: 1026
OPEN: 1027
OPEN: 1031
OPEN: 1025
OPEN: 1032
OPEN: 1034
OPEN: 1338
OPEN: 1339
OPEN: 2701
2701: ” ? S T A R T _ H A N D S H A K E
OPEN: 3389
OPEN: 5357

Answered question
Write your answer.

Categories