Creating a Port Scanner in Visual Basic
Results 1 to 10 of 27

Thread: Creating a Port Scanner in Visual Basic

Hybrid View

  1. #1
    Senior Member
    Join Date
    Feb 2004
    Posts
    620

    Creating a Port Scanner in Visual Basic

    Introduction
    I created this because when I read HTRegz’s tutorial about PyScan, I thought I should apply my modest VB skills and create a port scanner of my own.

    This tutorial is aimed at VB programmers who are comfortable with the basics of the language. If there’s a lot of stuff you don’t understand here, you might want to read some more basic tutorials then come back to this one.

    Attached is a ZIP file containing all the VB files. Download it and follow along; it will make it a lot easier. Enjoy!

    Extra Components
    Aside from all the normal frames, text boxes, command buttons, etc. you will need the Winsock control and the (optional) ProgressBar control. To insert these:

    1. On the menu bar click Project -> Components…
    2. Scroll down in the list and insert a check box into “Microsoft Windows Common Controls 6.0” and “Microsoft Winsock Control 6.0”

    Now you can insert the socket control and the progress bar. I’m not going to go into the details of setting up all the objects on the form. I’m assuming you already know how to do that.

    The Code Explained
    Now I’ll explain most of the code to you.

    Code:
    Private Sub cmdScan_Click()
    'This Sub creates the socket objects and initializes everything.
    '---------------------------------------------------------------
    
    Dim I As Integer 'Counter for the For loop.
    
    NextPort = txtMin.Text 'Set the next port to the first port to be scanned.
    
    'Set the progress bar values:
    prgProgress.Min = txtMin.Text
    prgProgress.Max = txtMax.Text
    prgProgress.Value = txtMin.Text
    
    txtOutput.Text = "" 'Clear the text box.
    
    For I = 1 To txtMaxConn.Text 'Loop from 1 to the value in the Maximum Connections textbox.
        Load sckScan(I) 'Load a socket with index of I.
        
        sckScan(I).Connect txtIP.Text, NextPort 'Connect the socket.
        NextPort = NextPort + 1 'Increment next port.
    
    Next I
    
    End Sub
    This is called when the user clicks the Scan button. The code is pretty self-explanatory down to the For loop. Let’s look at the loop:

    Code:
    For I = 1 To txtMaxConn.Text 
        Load sckScan(I)
        
        sckScan(I).Connect txtIP.Text, NextPort
        NextPort = NextPort + 1
    
    Next I
    This is what sets off everything. As you can see, it loops from 1 to the user-specified amount in the maximum connections text box. Each time, it loads the next socket object. It connects then adds 1 NextPort so that another socket can scan the next port.

    When the socket objects are initialized in Sub cmdScan_Click(), they attempt to connect to the remote machine. There are two possibilities: it connects successfully, or it can’t connect. When it connects successfully sckScan_Connect() is called:

    Code:
    Private Sub sckScan_Connect(Index As Integer)
    'This Sub is called when the socket makes a successful connection. It prints the port to
    'the text box and calls TryNext to connect the socket to the next port.
    '---------------------------------------------------------------------------------------
    
    txtOutput.Text = txtOutput.Text & "Port " & sckScan(Index).RemotePort & vbCrLf
    
    TryNext (Index)
    
    End Sub
    This prints “Port #” to the text box (where “#” is the remote port). Then it calls TryNext(), a sub that will connect the socket again to another port.

    Code:
    Sub TryNext(Index)
    'This Sub is called when a socket finishes trying a port. It closes the connection then
    'connects the socket to the next open port.
    '--------------------------------------------------------------------------------------
    
    On Error Resume Next 'I had some errors with the progress bar so I added this statement.
    
    sckScan(Index).Close 'Close the conection.
    
    prgProgress.Value = prgProgress.Value + 1 'Increment the progress bar.
    
    'This If statement unloads the socket if there are no more ports to scan.
    If NextPort > txtMax.Text Then
        Unload sckScan(Index)
        Exit Sub
    End If
    
    sckScan(Index).Connect txtIP.Text, NextPort 'Connect to the target with the next port.
    
    NextPort = NextPort + 1 'Increment the next port.
    
    End Sub
    Let’s walk through this step by step.

    On Error Resume Next – If an error occurs, it will skip to the next line of code. I inserted this because I was having errors with the progress bar. Sloppy programming, no doubt . It works fine now, though.

    sckScan(Index).Close – This closes the connection. If this statement wasn’t there, it wouldn’t allow the socket to connect. It can’t connect when it’s already connected!

    prgProgress.Value = prgProgress.Value + 1 – This adds 1 to the value of the progress bar.

    If NextPort > txtMax.Text Then
    Unload sckScan(Index)
    Exit Sub
    End If

    This determines if there are no more ports to scan. If the next port is higher than the last port to scan, it unloads the socket object since it has no more to do.

    sckScan(Index).Connect txtIP.Text, NextPort – Connects to next port.

    NextPort = NextPort + 1 – adds 1 to NextPort for the use other sockets.

    Last but not least, we have the code for the Stop button:

    Code:
    Private Sub cmdStop_Click()
    'This Sub stops the scan by closing the connection then unloading each socket
    '----------------------------------------------------------------------------
    
    'I is the counter for the For loop
    Dim I As Integer
    
    'Loop from 1 to the last Winsock control:
    For I = 1 To sckScan.UBound
        sckScan(I).Close 'Close the connection
        Unload sckScan(I) 'Unload the control
    Next I
    
    End Sub
    When the user clicks the Stop button, this loops from sckScan(1) to sckScan(UBound), (where UBound is the last object). It disconnects them then unloads them.

    Conclusion
    I think that’s about it. If you have any questions (I know sometimes I explain things in a way that only I understand) just ask and I’ll try to help you out. This is my first security tutorial so if I did something wrong, TELL ME so that I won’t do it in the future. I always welcome constructive criticism.

    Look out for more VB tutorials from mjk in the future

    mjk

    EDIT:
    New zip file uploaded 5/25/04. I fixed all the bugs I could find.
    Share on Google+

  2. #2
    Junior Member
    Join Date
    May 2007
    Posts
    2
    ^^ i tried this but it didnt' work...
    Share on Google+

  3. #3
    Junior Member
    Join Date
    May 2007
    Posts
    2
    ^^ i tried this but it didnt' work...
    Share on Google+

  4. #4
    Super Moderator: GMT Zone nihil's Avatar
    Join Date
    Jul 2003
    Location
    United Kingdom: Bridlington
    Posts
    17,190
    Closed.
    If you cannot do someone any good: don't do them any harm....
    As long as you did this to one of these, the least of my little ones............you did it unto Me.
    What profiteth a man if he gains the entire World at the expense of his immortal soul?
    Share on Google+

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides