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

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

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.


Sign In
Create Account


Back to top











