Solved

Listening to Port 80 (with Winsock?)

Posted on 2002-04-06
8
1,124 Views
Last Modified: 2013-11-13
I want to set up a VB program that can monitor all incoming data / messages on a port (particularly port 80).
This needs to include messages that did not originate from the same VB program - i.e. if I have an instance of Internet Explorer open and I am downloading a web page, I want my VB program to take a copy of the incoming HTML.
I have tried to set up Winsock to do this, but am unable to find many guidelines as to whether this is possible and if so how to do it.

0
Comment
Question by:stuart_edwards
8 Comments
 
LVL 22

Expert Comment

by:cookre
Comment Utility
Have you looked at the Winsock object (component Microsoft Winsock)?
0
 
LVL 38

Expert Comment

by:PaulHews
Comment Utility
Here is a simple project that monitors port 80, determines if the request is coming from my subnet, or an external subnet (you would have to enter your own subnet in there) and logs the request and passed data appropriately.  Before logging, it checks to see if the request is from a unique IP address, but you could change that if you wish.

-----

Form code with Winsock control and picture box control to view incoming requests on port 80.

Option Explicit

Private mcolIPAddresses As Collection
Private mlngCountISP As Long
Private mlngCountOther As Long
Const strFileISP = "P80_MyISP.txt"
Const strFileOther = "P80_Other.txt"

Private Sub Form_Load()
    Dim hFile As Integer
    With Winsock1
    .LocalPort = 80
'    Debug.Print .LocalIP
    .Listen
    End With
    Set mcolIPAddresses = New Collection
   
   
    sPrintHeaders "Starting " & Format(Now, "dd-mmm-yyyy hh:nn")
   
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    sPrintHeaders "Finishing " & Format(Now, "dd-mmm-yyyy hh:nn")
End Sub

Private Sub Form_Resize()
    picPrint.Move 300, 300, Me.ScaleWidth - 600, Me.ScaleHeight - 600
End Sub



Private Sub Winsock1_Close()
    Debug.Print "Closed"
End Sub

Private Sub Winsock1_Connect()
    Debug.Print "Connect"
End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
    If Winsock1.State <> sckClosed Then Winsock1.Close
        Winsock1.Accept requestID

End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    Dim strFile As String
    Dim strHost As String
    Dim strIP As String
    Dim hFile As Integer
    Dim vData As Variant
    Dim lngCount As Long
   
   
   
   
    strFile = "C:\Windows\Desktop\"
   
    'increment count
   
   
    strIP = Winsock1.RemoteHostIP
    If fIsUniqueIP(strIP) Then
        picPrint.Cls
        'Change your subnet info here.  Just an example.
        If Left$(strIP, 6) = "64.230" Then
            'This is on my subnet and goes in the file for my subnet.
            strFile = strFile & strFileISP
            mlngCountISP = mlngCountISP + 1
            lngCount = mlngCountISP
        Else
            strFile = strFile & strFileOther
            mlngCountOther = mlngCountOther + 1
            lngCount = mlngCountOther
        End If
        hFile = FreeFile
        Open strFile For Append As #hFile
       
        sPrint String(65, 42), hFile
        sPrint "Record: " & lngCount, hFile
        sPrint "Date: " & Format(Now, "dd-mmm-yyyy hh:nn"), hFile
        sPrint "From IP: " & Winsock1.RemoteHostIP, hFile
        sPrint "HostName: " & strHost, hFile
       
       
        Winsock1.GetData vData, vbString, bytesTotal
        sPrint "Data Received:", hFile
        sPrint vData, hFile
       
        Close hFile
   End If
   DoEvents
   
   Winsock1.Close
   Winsock1.Listen
End Sub


Public Sub sPrint(ByVal strText As String, hFile As Integer)
    Debug.Print strText
    Print #hFile, strText
   
    picPrint.Print strText
End Sub

Public Function fIsUniqueIP(strIP As String) As Boolean
    On Error Resume Next
    mcolIPAddresses.Add strIP, strIP
    fIsUniqueIP = (Err = 0)

End Function

Public Sub sPrintHeaders(strText As String)

    Dim hFile As Integer
    Const strPath = "C:\Windows\Desktop\"
    hFile = FreeFile
    Open strPath & strFileISP For Append As hFile
    Print #hFile, String(65, "-")
    Print #hFile, strText
    Close #hFile
   
    hFile = FreeFile
    Open strPath & strFileOther For Append As hFile
    Print #hFile, String(65, "-")
    Print #hFile, strText
    Close #hFile
   
   


End Sub
0
 
LVL 1

Expert Comment

by:plasmatek
Comment Utility
If you just want to monitor HTTP traffic, the easiest method would be to write an http proxy, this would also let you modify incoming/outgoing data.

You can find example code here:
http://www.pscode.com/vb/scripts/ShowCode.asp?txtCodeId=11700&lngWId=1

Other options are more complex, such as packet sniffing.

Regards,
James.
0
 

Author Comment

by:stuart_edwards
Comment Utility
Paul - tried out your code and it does not do what I am after.  Having looked at your comments again, I realised that I probably need to clarify what I want to do.

I want my program to simply listen to any HTML coming back to my PC via port 80 after I have made a request as a client (not a server).  Your solution seems to monitor incoming requests only (rather than the returned data from a request I have previously made as a client); could it be adapted to perform this task?
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:stuart_edwards
Comment Utility
Paul - tried out your code and it does not do what I am after.  Having looked at your comments again, I realised that I probably need to clarify what I want to do.

I want my program to simply listen to any HTML coming back to my PC via port 80 after I have made a request as a client (not a server).  Your solution seems to monitor incoming requests only (rather than the returned data from a request I have previously made as a client); could it be adapted to perform this task?
0
 

Author Comment

by:stuart_edwards
Comment Utility
James

Have downloaded the code you suggested - looks a very impressive program, but I can't seem to get it to monitor HTTP traffic!  Any tips on what I need to set up for it to work?
0
 
LVL 1

Accepted Solution

by:
M.E.Pierce earned 200 total points
Comment Utility
First off, HTTP uses port 80 on the SERVER side,
ie: your browser talks to the server on port 80,
    But recieves data from the server on a random
    port(usually in the 1000 range).


You'll HAVE to make a Proxy Server as packet sniffing in VB if buggy and usually involves an outside DLL.

For a Proxy Server:

1. Create a project and add 2 winsock controls to it,
   TCPIN and TCPOUT. Set the protocol to TCP.

2. set the port for TCPOUT to 80 and the port for TCPIN to 8080.

3. In the DataArrival sub of each control, call the senddata
   of the other control and pass the recieved data to it.

4. Youll have to parse the data of TCPOUT to find the server, port number, etc to set TCPIN to.

5. Configure your Proxy settings in your web browser to 127.0.0.1:8080 for HTTP only.

if you need an actual code example, I can throw something together, just let me know.

michael.e.pierce@<REMOVETHIS>unforgettable.com
0
 

Author Comment

by:stuart_edwards
Comment Utility
Haven't had time to try this and the project that would have incorporated this is for now on ice.

However, your approach (provided it works!) does seem to show a good understanding of what I am trying to achieve, so the points are yours.  Maybe will try it out someday soon!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now