Projects

Find all our projects in development below.
All source code is GNU General Public License (GPL)

javaSpy

Browsing frmPacketSpy.vb (12.97 KB)

Option Explicit On

Imports System.Net
Imports System.Text.ASCIIEncoding
Imports javaSpy.clsPacketMonitor

Public Class frmPacketSpy

    Private packetsTotal As Integer = 0
    Private packetsSize As Double = 0

    Private Delegate Sub UpdatePacketList(ByVal p As clsPacket)

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

    End Sub

    Public Sub New(ByVal filterProtocol As String, ByVal filterPort As String, ByVal filterIP As String)

        Me.New()

        cboProtocol.Text = filterProtocol
        txtPort.Text = filterPort
        txtIP.Text = filterIP

    End Sub

    Private Sub _showWaitCursor(ByVal bShow As Boolean)

        ' show/hide the hourglass
        If bShow Then
            Me.Cursor = Cursors.WaitCursor
            Me.UseWaitCursor = True
        Else
            Me.UseWaitCursor = False
            Me.Cursor = Cursors.Arrow
        End If

    End Sub

    Private Sub frmPacketSpy_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        If btnStop.Enabled Then btnStop.PerformClick()

    End Sub

    Private Sub frmPacketSpy_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Try

            Dim hosts() As IPAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList
            If hosts.Length = 0 Then _
                Throw New NotSupportedException("This computer does not have non-loopback interfaces installed!")

            For i As Integer = 0 To hosts.Length - 1

                If hosts(i).AddressFamily = Sockets.AddressFamily.InterNetwork Then

                    Dim newMonitor As New clsPacketMonitor(hosts(i))
                    newMonitor.NewPacket = New NewPacketEventHandler(AddressOf Me.OnNewPacket)

                    Dim newItem As New ToolStripMenuItem(hosts(i).ToString(), Nothing, New EventHandler(AddressOf Me.OnHostsClick))
                    newItem.Tag = newMonitor

                    btnMonitor.DropDown.Items.Add(newItem)

                End If

            Next

        Catch ex As Exception

            btnMonitor.Enabled = False
            btnClear.Enabled = False
            lblStatus.Text = "Error: " + ex.Message

        End Try

    End Sub

    Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click

        btnStop.Enabled = False
        Me.Text = "javaSpy :: Packet Spy"

        For Each thisItem As ToolStripMenuItem In btnMonitor.DropDown.Items

            If thisItem.Checked Then

                Dim thisMonitor As clsPacketMonitor = thisItem.Tag
                thisMonitor.Stop()

                thisItem.Checked = False

            End If

        Next

        If lblStatus.Text = "Monitoring" Then lblStatus.Text = "Ready"

    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

        SyncLock lvPacketSpy

            lvPacketSpy.Items.Clear()

            packetsTotal = 0
            packetsSize = 0

            lblStatus.Text = "Ready"

        End SyncLock

    End Sub

    Private Sub OnHostsClick(ByVal sender As Object, ByVal e As EventArgs)

        ' start or stop listening on the specified interface
        Dim item As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
        Dim monitor As clsPacketMonitor = item.Tag
        Dim activeCount As Integer = 0

        item.Checked = Not item.Checked

        For Each thisItem As ToolStripMenuItem In btnMonitor.DropDown.Items
            If thisItem.Checked Then activeCount += 1
        Next

        If item.Checked Then

            Try

                monitor.Start()
                btnStop.Enabled = True

                If activeCount = 1 Then
                    Me.Text = "javaSpy :: Packet Spy :: Monitoring " + monitor.IP.ToString()
                Else
                    Me.Text = "javaSpy :: Packet Spy :: Monitoring multiple interfaces"
                End If

                lblStatus.Text = "Monitoring"

            Catch ex As Exception

                item.Checked = False

                MessageBox.Show("Error: " + ex.Message, Application.ProductName, _
                    MessageBoxButtons.OK, MessageBoxIcon.Error)

            End Try

        Else

            monitor.Stop()

            If activeCount = 0 Then

                btnStop.Enabled = False
                Me.Text = "javaSpy :: Packet Spy"

                If lblStatus.Text = "Monitoring" Then _
                    lblStatus.Text = "Ready"

            ElseIf activeCount = 1 Then

                Dim monitoringIP As String = ""
                For Each thisItem As ToolStripMenuItem In btnMonitor.DropDown.Items

                    If thisItem.Checked Then

                        Dim thisMonitor As clsPacketMonitor = thisItem.Tag
                        monitoringIP = thisMonitor.IP.ToString()

                        Exit For

                    End If

                Next

                Me.Text = "javaSpy :: Packet Spy :: Monitoring " + monitoringIP

            Else
                Me.Text = "javaSpy :: Packet Spy :: Monitoring multiple interfaces"
            End If

        End If

    End Sub

    Private Sub OnNewPacket(ByVal pm As clsPacketMonitor, ByVal p As clsPacket)

        If Me.InvokeRequired Then
            Me.Invoke(New NewPacketEventHandler(AddressOf OnNewPacket), pm, p)
        Else

            ' check packet against filters
            Dim keepPacket As Boolean = True

            If cboProtocol.Text <> "" AndAlso cboProtocol.Text <> p.Protocol.ToString() Then keepPacket = False

            If keepPacket AndAlso txtPort.Text <> "" Then

                Dim filterMatch As Boolean = False

                If p.Source.Contains(":") Then

                    Dim srcPort As String = p.Source.Split(":")(1)
                    If txtPort.Text = srcPort Then filterMatch = True

                End If

                If Not filterMatch AndAlso p.Destination.Contains(":") Then

                    Dim destPort As String = p.Destination.Split(":")(1)
                    If txtPort.Text = destPort Then filterMatch = True

                End If

                keepPacket = filterMatch

            End If

            If keepPacket AndAlso txtIP.Text <> "" Then

                Dim filterMatch As Boolean = False

                If p.Source.Contains(":") Then

                    Dim srcIP As String = p.Source.Split(":")(0)
                    If txtIP.Text = srcIP Then filterMatch = True

                End If

                If Not filterMatch AndAlso p.Destination.Contains(":") Then

                    Dim destIP As String = p.Destination.Split(":")(0)
                    If txtIP.Text = destIP Then filterMatch = True

                End If

                keepPacket = filterMatch

            End If

            If keepPacket Then

                SyncLock lvPacketSpy

                    ' add the new packet to the list
                    packetsTotal += 1
                    packetsSize += p.TotalLength

                    With lvPacketSpy.Items.Add(New ListViewItem(New String() {p.Time.ToString(), p.Protocol.ToString(), p.Source, p.Destination, p.TotalLength.ToString()}))
                        .Tag = p
                    End With

                    lblStatus.Text = String.Format("Intercepted {0} packet(s) [{1} byte(s)]", packetsTotal.ToString("###,##0"), packetsSize.ToString("###,##0"))
                    Application.DoEvents()

                End SyncLock

            End If

        End If

    End Sub

    Private Sub mnuCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCopy.Click

        If lvPacketSpy.SelectedItems.Count > 0 Then

            Try

                Dim copyData As String = lvPacketSpy.SelectedItems(0).Text + vbTab
                For i As Integer = 1 To lvPacketSpy.SelectedItems(0).SubItems.Count - 1

                    copyData += lvPacketSpy.SelectedItems(0).SubItems(i).Text

                    If i < lvPacketSpy.SelectedItems(0).SubItems.Count - 1 Then _
                        copyData += vbTab

                Next

                Clipboard.SetText(copyData, TextDataFormat.Text)

            Catch
            End Try

        End If

    End Sub

    Private Sub mnuViewPacket_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewPacket.Click

        If lvPacketSpy.SelectedItems.Count > 0 Then

            With New frmPacketDisplay(lvPacketSpy.SelectedItems(0).Tag)
                .Show(Me)
            End With

        End If

    End Sub

    Private Sub lvPacketSpy_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvPacketSpy.DoubleClick

        mnuViewPacket.PerformClick()

    End Sub

    Private Sub lvPacketSpy_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lvPacketSpy.KeyUp

        If e.KeyCode = Keys.F3 Then
            lblFind_Click(sender, Nothing)
        ElseIf e.KeyCode = Keys.Enter Then
            mnuViewPacket.PerformClick()
        End If

    End Sub

    Private Sub lblFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblFind.Click

        Static lastFindText As String = ""
        Dim findText As String = ""

        If e Is Nothing And lastFindText <> "" Then
            findText = lastFindText
        Else
            findText = InputBox("Enter the text to find:", Application.ProductName, lastFindText)
        End If

        If findText <> "" Then

            _showWaitCursor(True)

            Dim startItem As ListViewItem = Nothing
            Dim findItem As ListViewItem = Nothing

            If lvPacketSpy.SelectedItems.Count > 0 Then startItem = lvPacketSpy.SelectedItems(0)

            For Each thisItem As ListViewItem In lvPacketSpy.Items

                If startItem Is Nothing Then

                    Dim thisPacket As clsPacket = thisItem.Tag
                    If ASCII.GetString(thisPacket.Raw).Contains(findText) Then

                        findItem = thisItem
                        Exit For

                    End If

                ElseIf thisItem.Equals(startItem) Then
                    startItem = Nothing
                End If

            Next

            _showWaitCursor(False)

            If Not (findItem Is Nothing) Then

                findItem.EnsureVisible()
                findItem.Selected = True
                findItem.Focused = True

            Else

                MessageBox.Show("Cannot find """ + findText + """", Application.ProductName, _
                    MessageBoxButtons.OK, MessageBoxIcon.Information)

                findText = ""

            End If

        End If

        lastFindText = findText

    End Sub

    Private Sub lblFindUnicode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblFindUnicode.Click

        Static lastFindText As String = ""
        Dim findText As String = ""

        If e Is Nothing And lastFindText <> "" Then
            findText = lastFindText
        Else
            findText = InputBox("Enter the Unicode text to find:", Application.ProductName, lastFindText)
        End If

        If findText <> "" Then

            _showWaitCursor(True)

            Dim startItem As ListViewItem = Nothing
            Dim findItem As ListViewItem = Nothing

            If lvPacketSpy.SelectedItems.Count > 0 Then startItem = lvPacketSpy.SelectedItems(0)

            For Each thisItem As ListViewItem In lvPacketSpy.Items

                If startItem Is Nothing Then

                    Dim thisPacket As clsPacket = thisItem.Tag
                    If ASCII.GetString(thisPacket.Raw).Replace(vbNullChar, "").Contains(findText) Then

                        findItem = thisItem
                        Exit For

                    End If

                ElseIf thisItem.Equals(startItem) Then
                    startItem = Nothing
                End If

            Next

            _showWaitCursor(False)

            If Not (findItem Is Nothing) Then

                findItem.EnsureVisible()
                findItem.Selected = True
                findItem.Focused = True

            Else

                MessageBox.Show("Cannot find """ + findText + """", Application.ProductName, _
                    MessageBoxButtons.OK, MessageBoxIcon.Information)

                findText = ""

            End If

        End If

        lastFindText = findText

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

        If btnStop.Enabled Then btnStop.PerformClick()

    End Sub

End Class

Download frmPacketSpy.vb

Back to file list


Back to project page