Learn How to write a port scanner

What is Port Scanner ?

A port scanner is a piece of software designed to search a network host for open ports. This is often used by administrators to check the security of their networks and by hackers to compromise it.
Understand and Write a Port Scanner in PERL

Port scanner script in PERL

#!/usr/bin/perl

use IO::Socket;

$port = 1;

$output = “/home/sakuramboo/perl/OpenPorts.txt”;

open (LIST, ” >>$output”);

while ($port <= 65535){

$sock = new IO::Socket::INET (PeerAddr => ‘127.0.0.1′,

PeerPort => $port,

Proto => ‘tcp’);

if ($sock){

close $sock;

print “$port -open\n”;

print LIST “$port -open\n”;

$port = $port + 1;

}

else{

print “$port -closed\n”;;

$port = $port + 1;

}

}

close(LIST);
now, lets take a look at how all of this works, shall we?

#!/usr/bin/perl

start off the perl script with this line.

use IO::Socket;
you are saying that you are going to be using the perl modules named IO::Socket. This allows you to utilize the commands for socket programming.

$port = 1;
you are declaring $port to equal 1.

$output = “/home/sakuramboo/perl/OpenPorts.txt”;
you are declaring that $output will be a file, and it is given then exact location of the file and file name.

open (LIST, ” >>$output”);
this opens the file from $output to allow the script to write to it.

while ($port <= 65535){
while $port (which is 1 at the start) is less than or equal to 65535 (the total number of ports a computer can have) if will do what is in the brackets.

$sock = new IO::Socket::INET
this declares that $sock will be a new socket connection.

(PeerAddr => ‘127.0.0.1′,PeerPort => $port, Proto => ‘tcp’);
this is the details of $sock. PeerAddr points to the IP address you want to scan. For this script, i used the localhost. PeerPort points to $port (which starts at 1). this will be increasing as the script is run. Proto points to the protocol that is being used. If you wanted to scan ports with udp, you could. Just replace ‘tcp’ with ‘udp’.

if ($sock){
this is where the script uses the socket and attempts to make a connection with what you have in $sock. It is basically saying, “if $sock makes a connection to the settings in $sock do what is in the brackets.”

close $sock;
this closes the connection.

print “$port -open\n”;
this will print what port just got scanned and tell you that it is open, as well as return a line so everything doesnt get printed right next to each other.

print LIST “$port -open\n”;
this will print the same things that it did in the command prompt, into the file in $output.

$port = $port + 1;
this is where $port gets increased by 1.

else{

print “$port -closed\n”;

$port = $port + 1;

}
here is what happens if the port is closed. Does the same as if it was open exept that it doesnt print it to a file. I did this just so i know where it is in the port scan.

close(LIST);




Leave a Comment

Name: (Required)

E-mail: (Required)

Website:

Comment:

Google