Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Visual Basic 2010 : Port Scanner (Console)

port scanner console

  • Please log in to reply
21 replies to this topic

#1 Jarryd

Jarryd

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 63 posts
  • Location:Australia
  • Programming Language:C, Java, C++, C#

Posted 17 September 2010 - 05:25 PM

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.

  • 1

#2 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 18 September 2010 - 08:23 PM

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.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#3 Jarryd

Jarryd

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 63 posts
  • Location:Australia
  • Programming Language:C, Java, C++, C#

Posted 19 September 2010 - 04:37 PM

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. :)
  • 0

#4 willemnz

willemnz

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 02 October 2010 - 08:00 AM

Hey, i'm getting an error.

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

what can i do?

Thanks.
  • 0

#5 Jarryd

Jarryd

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 63 posts
  • Location:Australia
  • Programming Language:C, Java, C++, C#

Posted 03 October 2010 - 11:56 PM

'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.

  • 0

#6 willemnz

willemnz

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 04 October 2010 - 05:51 AM

Thanks!
  • 0

#7 Jarryd

Jarryd

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 63 posts
  • Location:Australia
  • Programming Language:C, Java, C++, C#

Posted 05 October 2010 - 02:25 AM

Thanks!


Your welcome, :)
  • 0

#8 Zizooooo

Zizooooo

    CC Newcomer

  • Just Joined
  • PipPip
  • 14 posts

Posted 10 October 2010 - 02:05 PM

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 ,,,,,
  • 0

#9 Jarryd

Jarryd

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 63 posts
  • Location:Australia
  • Programming Language:C, Java, C++, C#

Posted 10 October 2010 - 03:35 PM

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...
  • 0

#10 answar

answar

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 10 October 2010 - 05:24 PM

Yesterday i ever using with winsock and i could with it.
My bye you wrong method your input.
  • 0

#11 knutter539

knutter539

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 15 June 2011 - 09:07 AM

for some reason i get a quick flash of the console when i debug but then it disapears
  • 0

#12 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 15 June 2011 - 08:44 PM

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?
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.






Also tagged with one or more of these keywords: port scanner, console

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download