Sharppacp Packet senden Problem.

  • VB.NET

Es gibt 10 Antworten in diesem Thema. Der letzte Beitrag () ist von nikeee13.

    Sharppacp Packet senden Problem.

    Hallo,

    Im moment versuche ich mit sharppcap TCPPackete zusenden. Leider schaffe ich es nicht ich habe schon gegoogelt aber Leider nix aktuelles gefunden.
    Hier erstmal das der Code um Packete zuempfangen das Funktioniert auch.

    VB.NET-Quellcode

    1. Private Sub Device_OnPacketArrival(ByVal sender As Object, ByVal e As SharpPcap.CaptureEventArgs) Handles
    2. Device.OnPacketArrival
    3. CheckForIllegalCrossThreadCalls = False '
    4. Dim retPacket As Packet = Packet.ParsePacket(CType(1, LinkLayers), e.Packet.Data)
    5. Dim contcpPacket As TcpPacket = TcpPacket.GetEncapsulated(retPacket)
    6. If contcpPacket.SourcePort = 8101 Then
    7. Dim retdata As String = ASCII.GetString(contcpPacket.PayloadData)
    8. 'String Verarbeiten.
    9. 'Neues Packet Senden.
    10. SendPacket("L|1|0|0|0&S|0|0|2&", contcpPacket.DestinationPort, contcpPacket.SourcePort)
    11. MsgBox("gesendet")
    12. End If
    13. End Sub






    Hier die Funk zum Senden:

    VB.NET-Quellcode

    1. Public Function SendPacket(ByVal msg As String, ByVal sport As UShort, ByVal dport As UShort) As Boolean
    2. Dim tpack As New TcpPacket(sport, dport)
    3. tpack.PayloadData = ASCII.GetBytes(msg)
    4. Device.SendPacket(tpack)
    5. End Function

    Und Hier noch Form_Load:

    VB.NET-Quellcode

    1. TopMost = True
    2. 'Selecting Device
    3. Try
    4. Devices = CaptureDeviceList.Instance
    5. Catch ex As Exception
    6. MessageBox.Show("Please install Winpcap and restart the Application!", "SFCatcher Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
    7. End
    8. End Try
    9. If Devices.Count < 1 Then
    10. MessageBox.Show("No devices found!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
    11. End
    12. End If
    13. If Devices.Count > 1 Then
    14. For Each dev In Devices
    15. Using devdialog As New DeviceDialog
    16. devdialog.list_devices.Items.Add(dev.Name)
    17. If devdialog.ShowDialog = Windows.Forms.DialogResult.OK Then
    18. iDeviceID = devdialog.list_devices.SelectedIndex
    19. Device = Devices(iDeviceID)
    20. End If
    21. End Using
    22. Next
    23. ElseIf Devices.Count = 1 Then
    24. iDeviceID = 0
    25. Device = Devices(iDeviceID)
    26. End If
    27. '//Open Device
    28. Device.Open(DeviceMode.Promiscuous, 1000)
    29. Device.Filter = "tcp"
    30. Device.StartCapture()

    Dieser Beitrag wurde bereits 3 mal editiert, zuletzt von „Tantra“ ()

    Würdest Du demjenigen helfen, der solch gequirlten Quelltext abliefert :?:
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Nutze den Quellcodeeditor. Alles andere macht nur Probleme.

    Weiterhin sehe ich die Zeile zum Aktivieren des Net1.1 Verhaltens (control.checkforilligalblabla).
    Egal, was du gemacht hast, das ist das Problem. Mit deaktivierter Überprüfung gibt es die merkwürdigsten Fehler aller Zeiten...

    Gruß,
    Manawyrm

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Manawyrm“ ()

    Wenn du dich ein wenig auf der Seite umgesehen hättest, hättest du bemerkt, dass beim Sourcecode Beispiele dabei sind.

    Das musst du nur mehr nach vb.net übersetzen:
    Spoiler anzeigen

    Brainfuck-Quellcode

    1. using System;
    2. using System.Collections.Generic;
    3. using SharpPcap;
    4. namespace Example9
    5. {
    6. public class DumpTCP
    7. {
    8. public static void Main(string[] args)
    9. {
    10. // Print SharpPcap version
    11. string ver = SharpPcap.Version.VersionString;
    12. Console.WriteLine("SharpPcap {0}, Example9.SendPacket.cs\n", ver);
    13. // Retrieve the device list
    14. var devices = CaptureDeviceList.Instance;
    15. // If no devices were found print an error
    16. if(devices.Count < 1)
    17. {
    18. Console.WriteLine("No devices were found on this machine");
    19. return;
    20. }
    21. Console.WriteLine("The following devices are available on this machine:");
    22. Console.WriteLine("----------------------------------------------------");
    23. Console.WriteLine();
    24. int i = 0;
    25. // Print out the available devices
    26. foreach(var dev in devices)
    27. {
    28. Console.WriteLine("{0}) {1}",i,dev.Description);
    29. i++;
    30. }
    31. Console.WriteLine();
    32. Console.Write("-- Please choose a device to send a packet on: ");
    33. i = int.Parse( Console.ReadLine() );
    34. var device = devices[i];
    35. Console.Write("-- This will send a random packet out this interface, "+
    36. "continue? [YES|no]");
    37. string resp = Console.ReadLine().ToLower();
    38. // If user refused, exit program
    39. if((resp!="") && ( !resp.StartsWith("y")))
    40. {
    41. Console.WriteLine("Cancelled by user!");
    42. return;
    43. }
    44. //Open the device
    45. device.Open();
    46. //Generate a random packet
    47. byte[] bytes = GetRandomPacket();
    48. try
    49. {
    50. //Send the packet out the network device
    51. device.SendPacket( bytes );
    52. Console.WriteLine("-- Packet sent successfuly.");
    53. }
    54. catch(Exception e)
    55. {
    56. Console.WriteLine("-- "+ e.Message );
    57. }
    58. //Close the pcap device
    59. device.Close();
    60. Console.WriteLine("-- Device closed.");
    61. Console.Write("Hit 'Enter' to exit...");
    62. Console.ReadLine();
    63. }
    64. /// <summary>
    65. /// Generates a random packet of size 200
    66. /// </summary>
    67. private static byte[] GetRandomPacket()
    68. {
    69. byte[] packet = new byte[200];
    70. Random rand = new Random();
    71. rand.NextBytes( packet );
    72. return packet;
    73. }
    74. }
    75. }



    EDIT:
    Es muss die Neueste Version von WinPCAP installiert sein, und du brauchst Verweise auf die SharpPcap.dll, sowie die PacketDotNet.dll

    Weils mich auch interessiert hat, hier die Übersetzung:
    Spoiler anzeigen
    Übersetzung in VB.NET

    VB.NET-Quellcode

    1. Imports System
    2. Imports System.Collections.Generic
    3. Imports SharpPcap
    4. Imports PacketDotNet
    5. Namespace Example9
    6. Public Class DumpTCP
    7. Public Sub Main(args() As String)
    8. ' Print SharpPcap version
    9. Dim ver As String = SharpPcap.Version.VersionString
    10. Console.WriteLine("SharpPcap {0}, Example9.SendPacket.cs\n", ver)
    11. ' Retrieve the device list
    12. Dim devices As SharpPcap.CaptureDeviceList = CaptureDeviceList.Instance
    13. ' If no devices were found print an error
    14. If devices.Count < 1 Then
    15. Console.WriteLine("No devices were found on this machine")
    16. End
    17. End If
    18. Console.WriteLine("The following devices are available on this machine:")
    19. Console.WriteLine("----------------------------------------------------")
    20. Console.WriteLine()
    21. Dim i As Integer = 0
    22. ' Print out the available devices
    23. For Each dev As SharpPcap.ICaptureDevice In devices
    24. Console.WriteLine("{0}) {1}", i, dev.Description)
    25. i += 1
    26. Next
    27. Console.WriteLine()
    28. Console.Write("-- Please choose a device to send a packet on: ")
    29. i = Integer.Parse(Console.ReadLine())
    30. Dim device As ICaptureDevice = devices(i)
    31. Console.Write("-- This will send a random packet out this interface, " &
    32. "continue? [YES|no]")
    33. Dim resp As String = Console.ReadLine().ToLower()
    34. ' If user refused, exit program
    35. If resp <> "" AndAlso Not resp.StartsWith("y") Then
    36. Console.WriteLine("Cancelled by user!")
    37. End
    38. End If
    39. 'Open the device
    40. device.Open()
    41. 'Generate a random packet
    42. Dim bytes As Byte() = GetRandomPacket()
    43. Try
    44. 'Send the packet out the network device
    45. device.SendPacket(bytes)
    46. Console.WriteLine("-- Packet sent successfuly.")
    47. Catch e As Exception
    48. Console.WriteLine("-- " + e.Message)
    49. Finally
    50. 'Close the pcap device
    51. device.Close()
    52. Console.WriteLine("-- Device closed.")
    53. End Try
    54. Console.Write("Hit 'Enter' to exit...")
    55. Console.ReadLine()
    56. End Sub
    57. '/ <summary>
    58. '/ Generates a random packet of size 200
    59. '/ </summary>
    60. Private Function GetRandomPacket() As Byte()
    61. Dim packet(200) As Byte
    62. Dim rand As New Random
    63. rand.NextBytes(packet)
    64. Return packet
    65. End Function
    66. End Class
    67. End Namespace
    SWYgeW91IGNhbiByZWFkIHRoaXMsIHlvdSdyZSBhIGdlZWsgOkQ=

    Weil einfach, einfach zu einfach ist! :D

    Dieser Beitrag wurde bereits 3 mal editiert, zuletzt von „BiedermannS“ ()

    Was dir daran helfen soll? Damit du weißt wie du ein Network-Packet sendest.

    Bau dir das TCP-Packet das du brauchst und schick es weg. Wenn du nicht weißt wie ein TCP-Packet gebaut wir, hier mal was wo du anfangen kannst Wiki-Link
    SWYgeW91IGNhbiByZWFkIHRoaXMsIHlvdSdyZSBhIGdlZWsgOkQ=

    Weil einfach, einfach zu einfach ist! :D