Advertisement
Guest User

Untitled

a guest
May 4th, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.82 KB | None | 0 0
  1. ' DpcCap ( DPOCDERS CAP ) a .net framework vb.net language small and simple winpcap wrapper .
  2. ' Coded by unCoder form dpcoders team
  3. ' What dose it do ? : it can send raw packets simply and capture network traffic with winpcap fillter options
  4. ' in a fast and easy way to use .
  5. ' there's examples of using it which you can find in this code .
  6. '                                                                                                                            
  7. ' Note    : don't forget to import the wrapper ( DpcCap.DPCODERS_CAP ) , and put it beside the project and the c++ dll too .
  8. ' Coder   : unCoder ( uncodersc@gmail.com ) ( Team : dpcoders@gmail.com ) ( Twitter : @DPCoders )
  9. ' Website : wwww.dpcoders.com
  10. ' DpcCap  : (.Net Refrences - c++ dll) wwww.dpcoders.com/projects/DpcCap.rar
  11. ' Winpcap : www.winpcap.org/install/bin/WinPcap_4_1_3.exe
  12.  
  13. Imports DpcCap.DPCODERS_CAP
  14. Imports System.Runtime.InteropServices
  15.  
  16. '' Some packets protocols structers
  17. Imports DpcCap.PacketsStructers
  18.  
  19. Imports System.Windows.Forms
  20.  
  21. Module Module1
  22.  
  23.  
  24.    Dim frm As Form1 = New Form1()
  25.    Public Function RunTheForm()
  26.        Application.Run(frm)
  27.    End Function
  28.  
  29.    Sub Main()
  30.  
  31.        Dim formThread As New System.Threading.Thread(AddressOf RunTheForm)
  32.        formThread.Start()
  33.  
  34.  
  35.        '' Getting device adapter to send using it ( getting it by number ) , number used here = 2
  36.        Dim Adapter As AdapterDevice = GetDeviceAdapter(2)
  37.  
  38.        '' checking no errors
  39.        If Adapter.Err = NO_ERROR Then
  40.  
  41.            '' Open adpater live handle
  42.            '' OpenAdapterLive(Adapter name,capturing size,flags, read time out)
  43.            Dim AdHandleLive As IntPtr = OpenAdapterLive(Adapter.Name, 65534, OPENFLAG_PROMISCUOUS, 1000)
  44.  
  45.            '' checking handle
  46.            If AdHandleLive <> IntPtr.Zero Then
  47.  
  48.  
  49.                '' Checking if the adpater is an ethernet adapter
  50.                If (DataLink(AdHandleLive) = DLT_EN10MB) Then
  51.  
  52.                    '' Getting adapter netmask
  53.                    Dim NetMask As UInteger = GetAdapterNetMask(Adapter.Name)
  54.  
  55.                    '' Making Filter ( " tcp - udp - .... " ) and check if no errors
  56.                    If MakeFilter(AdHandleLive, "udp", NetMask) = NO_ERROR Then
  57.  
  58.                        '' Getting NewPacket function address , to callback it when new packet captured
  59.                        Dim AdDg As New AdapterDelg(AddressOf NewPacket)
  60.  
  61.                        '' Capturing traffic
  62.                        '' CaptureLoop(Adapter handle live, count to loop (0 if infinity),CallBack func address)
  63.                        CaptureLoop(AdHandleLive, 0, Marshal.GetFunctionPointerForDelegate(AdDg))
  64.  
  65.                    End If
  66.  
  67.  
  68.                End If
  69.  
  70.            End If
  71.  
  72.        End If
  73.  
  74.  
  75.  
  76.    End Sub
  77.  
  78.    Declare Function ntohs Lib "ws2_32.dll" (x As UShort) As Integer
  79.  
  80.    Public Structure Ethernet
  81.        Dim SrcByte1 As Byte
  82.        Dim SrcByte2 As Byte
  83.        Dim SrcByte3 As Byte
  84.        Dim SrcByte4 As Byte
  85.        Dim SrcByte5 As Byte
  86.        Dim SrcByte6 As Byte
  87.  
  88.        Dim DstByte1 As Byte
  89.        Dim DstByte2 As Byte
  90.        Dim DstByte3 As Byte
  91.        Dim DstByte4 As Byte
  92.        Dim DstByte5 As Byte
  93.        Dim DstByte6 As Byte
  94.  
  95.        Dim TypeWord As UInt16
  96.    End Structure
  97.  
  98.    Dim buff() As Byte
  99.    Dim type As String
  100.    Dim proto As String
  101.    Dim sport, dport As Integer
  102.    '' ^ NewPacket fucntion ( called back when new packet captured using the preivous method )
  103.    Public Function NewPacket(PacketData As IntPtr, Size As Integer)
  104.        '' sizing & saving packet
  105.        Array.Resize(buff, Size)
  106.        Marshal.Copy(PacketData, buff, 0, Size)
  107.  
  108.        '' Headring the pakcet , the first of the packet ( IP Header )
  109.        Dim eh As Ethernet
  110.        Dim ih As ip_header
  111.        Dim uh As udp_header
  112.  
  113.        '' Calculating the ip header postion in the packet = ( The packet begining + 14 )
  114.        eh = Marshal.PtrToStructure(PacketData, GetType(Ethernet))
  115.        Dim calc As IntPtr = PacketData.ToInt32() + 14
  116.  
  117.        '' point the structer to the ip header address
  118.        ih = Marshal.PtrToStructure(calc, GetType(ip_header))
  119.  
  120.        calc = calc.ToInt32 + 20
  121.        uh = Marshal.PtrToStructure(calc, GetType(udp_header))
  122.  
  123.        If ih.proto = 17 Then proto = "UDP"
  124.        If ih.proto = 6 Then proto = "TCP"
  125.  
  126.        If proto = "UDP" Then
  127.  
  128.            If ntohs(uh.dport) = 53 Or ntohs(uh.sport) = 53 Then
  129.                type = "DNS"
  130.            Else
  131.                type = "N/A"
  132.            End If
  133.  
  134.        End If
  135.  
  136.        frm.Invoke(Sub() frm.AddItem(ih.saddr.byte1 & "." & ih.saddr.byte2 & "." & ih.saddr.byte3 & "." & ih.saddr.byte4, ih.daddr.byte1 & "." & ih.daddr.byte2 & "." & ih.daddr.byte3 & "." & ih.daddr.byte4, buff, proto, type))
  137.    End Function
  138.  
  139. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement