Jump to content

Visual Basic 2010 : Port Scanner (Console)

- - - - -

  • Please log in to reply
17 replies to this topic

#1
Jarryd

Jarryd

    Learning Programmer

  • Members
  • PipPipPip
  • 63 posts

Port Scanner (Console Application)


Difficulty: 3 / 10 : Depending on your knowledge of the Visual Basic language.
Assumed Knowledge: Basic knowledge of Visual Basic, Basic knowledge of TCP (Transmission Control Protocol)
Information: This application connects to a TCP server and scans an IP showing all its ports whether there open or closed.

There are in total 65,535 ports but not every port is used. Below is a few known ports:

Port 20: FTP | Data port
Port 21: FTP | Control (Command) port
Port 23: Telnet | Unencrypted text communications
Port 25: SMTP | Used for e-mail routing between mailservers
Port 80: HTTP | HyperText Transfer Protocol


Step 1: Creating the Console

To start this project, you will need to create a new Visual Basic console


Posted Image

Step 2: Adding imports and Variables

Before we get carried away into the more interesting part, first we will need to add imports and other variables.


Imports:
Imports System.Net.Sockets

Imports System.Net

Imports System.Threading

Imports System.Text

These imports will allow us to extend our code, Making us able to use TcpClient() method, and to connect to it.

Class:
Public Class psAPP

    Public Shared Sub Main()


    End Sub

End Class

Variables:
Dim portStart As Integer

        Dim portEnd As Integer

        Dim lngPort As Long


        Dim txtHost As String

        Dim openPorts As Integer

        Dim closedPorts As Integer

We have declared a start and End variable to hold the From and To port numbers, When the port scanner is running, it will scan the ports in between the From and To numbers.

Step 3: Starting the main process


Starting the main part of the console,


        Console.Write("Host: ")

        txtHost = Console.ReadLine()


        Console.Write("From: ")

        portStart = Console.ReadLine()


        Console.Write("To: ")

        portEnd = Console.ReadLine()

Doing this allows the user to key in input for the console to read,

If the user keys in these feilds correctly, When scanning it will connect to the Host, and scan From one port to the Other.

Step 4: Programming the TCP connections


This is were most of the work is done. Next, we are going to try to connect to the port, using a try/catch statement. If it connects, the port will come back saying it is open, If it does not connect, You will get the port number saying its closed.

        For lngPort = portStart To portEnd

            Dim myTcpClient As New TcpClient()

            Try

                myTcpClient.Connect(txtHost, lngPort)

                Console.WriteLine("Host: " + txtHost + " : ")

                Console.WriteLine("     Port " + lngPort.ToString() + " : Open :")

                openPorts += 1

                myTcpClient.Close()

            Catch ex As SocketException

                Console.WriteLine("Host: " + txtHost + " : ")

                Console.WriteLine("     Port " + lngPort.ToString() + " : Closed :")

                ' Console.WriteLine(ex.Message)

                closedPorts += 1

            End Try

        Next

Now, in doing this, It will connect to the TCP server, Scan for the ports, And give the user an output wether it is open or closed.

You would of noticed myTcpClient.Close(), If it detects a port that is open, it will lcose the TCP connection to the current port using the Close() method.

Step 5: Finishing touches


This is optional, It just makes the scanner easier to use.


        Console.Write("                                                              " & openPorts.ToString & " open port(s) : ")

        Console.Write("                                                           " & closedPorts.ToString & " closed port(s) : ")


        Console.Beep()

        Console.Write(txtHost + " : " + portStart.ToString + " - " + portEnd.ToString + " : Scanned Sucessfully")


Finished Source Code:
Imports System.Net.Sockets

Imports System.Net

Imports System.Threading

Imports System.Text


Public Class Tester

    Public Shared Sub Main()

        Dim portStart As Integer

        Dim portEnd As Integer

        Dim lngPort As Long


        Dim txtHost As String

        Dim openPorts As Integer

        Dim closedPorts As Integer


        Console.Title = CurDir() + " - Jarryd Hoffman"


        Console.ForegroundColor = ConsoleColor.DarkGreen


        Console.WriteLine("                                                            :: Jarryd Hoffman ::")


        Console.WriteLine("")


        Console.ForegroundColor = ConsoleColor.DarkGreen

        Console.Write("Host: ")

        Console.ForegroundColor = ConsoleColor.Gray

        Console.Write("")

        txtHost = Console.ReadLine()


        Console.ForegroundColor = ConsoleColor.DarkGreen

        Console.Write("From: ")

        Console.ForegroundColor = ConsoleColor.Gray

        Console.Write("")

        portStart = Console.ReadLine()


        Console.ForegroundColor = ConsoleColor.DarkGreen

        Console.Write("To: ")

        Console.ForegroundColor = ConsoleColor.Gray

        Console.Write("")

        portEnd = Console.ReadLine()


        Console.WriteLine("")


        For lngPort = portStart To portEnd

            Dim myTcpClient As New TcpClient()

            Try

                myTcpClient.Connect(txtHost, lngPort)

                Console.ForegroundColor = ConsoleColor.Gray

                Console.WriteLine("Host: " + txtHost + " : ")

                Console.WriteLine("     Port " + lngPort.ToString() + " : Open :")

                openPorts += 1

                myTcpClient.Close()

            Catch ex As SocketException

                Console.ForegroundColor = ConsoleColor.DarkGreen

                Console.WriteLine("Host: " + txtHost + " : ")

                Console.WriteLine("     Port " + lngPort.ToString() + " : Closed :")

                ' Console.WriteLine(ex.Message)

                closedPorts += 1

            End Try

        Next


        Console.WriteLine("")

        Console.ForegroundColor = ConsoleColor.Gray

        Console.Write("                                                              " & openPorts.ToString & " open port(s) : ")

        Console.ForegroundColor = ConsoleColor.DarkGreen

        Console.Write("                                                           " & closedPorts.ToString & " closed port(s) : ")

        Console.WriteLine("" & vbCrLf)

        Console.Beep()

        Console.Write(txtHost + " : " + portStart.ToString + " - " + portEnd.ToString + " : Scanned Sucessfully")


        Console.ReadLine()

    End Sub

End Class

Posted Image

If you have any information about this tutorial, Please don't hesitate to ask...


Images Uploaded with ImageShack.us


Edited by Jarryd, 24 September 2010 - 06:46 PM.


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
The way you check for socket exception to verify it is closed is the best method (I had seen many others do some crazy things), good tutorial. Only thing I am not so partial to is the alignment and colour.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
Jarryd

Jarryd

    Learning Programmer

  • Members
  • PipPipPip
  • 63 posts

Nullw0rm said:

The way you check for socket exception to verify it is closed is the best method (I had seen many others do some crazy things), good tutorial. Only thing I am not so partial to is the alignment and colour.

Thanks man, glad you liked it.

The colour and the text alignment are setup like that only because, if your scanning a massive amount of ports, it can get a bit hard to read. :)

#4
willemnz

willemnz

    Newbie

  • Members
  • Pip
  • 4 posts
Hey, i'm getting an error.

'Sub Main' was not found in 'PortScanner.Module1'.

what can i do?

Thanks.

#5
Jarryd

Jarryd

    Learning Programmer

  • Members
  • PipPipPip
  • 63 posts

willemnz said:

'Sub Main' was not found in 'PortScanner.Module1'.

This error can is caused by an error in your class name, This can easily be fixed by just changing the name of the declaration,

Posted Image


Posted Image

Edited by Jarryd, 25 May 2011 - 11:34 PM.


#6
willemnz

willemnz

    Newbie

  • Members
  • Pip
  • 4 posts
Thanks!

#7
Jarryd

Jarryd

    Learning Programmer

  • Members
  • PipPipPip
  • 63 posts

willemnz said:

Thanks!

Your welcome, :)

#8
Zizooooo

Zizooooo

    Newbie

  • Members
  • PipPip
  • 15 posts
wonderful idea
in vb6 this was easily done with winsock
however u made it much easier using sockets in vb.net
nice tut..
keep on ,,,,,

#9
Jarryd

Jarryd

    Learning Programmer

  • Members
  • PipPipPip
  • 63 posts

Zizooooo said:

wonderful idea
in vb6 this was easily done with winsock
however u made it much easier using sockets in vb.net
nice tut..
keep on ,,,,,

Thanks mate,

Personally i didn't ever like using the winsock control... I've seen alot of weird things happen when you run a portscanner using winsock...

#10
answar

answar

    Newbie

  • Members
  • Pip
  • 2 posts
Yesterday i ever using with winsock and i could with it.
My bye you wrong method your input.

#11
knutter539

knutter539

    Newbie

  • Members
  • Pip
  • 2 posts
for some reason i get a quick flash of the console when i debug but then it disapears

#12
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200

knutter539 said:

for some reason i get a quick flash of the console when i debug but then it disapears
Can you find out what this says?
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users