Frage

ich versuche, Pakete zu erfassen SharpPcap Bibliothek. Ich bin in der Lage, die Pakete Details zurück, aber ich habe Probleme zu bekommen, was der Inhalt der Nachricht innerhalb des Pakets.

das Paket .Data mit der Nachricht zurück, und wenn ich es verwenden, es ist die Rückkehr (System.Byte []).

Hier ist die Bibliothek Website: http://www.codeproject.com/KB/IP/sharppcap.aspx

Hier ist mein Code:

string packetData;
        private void packetCapturingThreadMethod()
            {

            Packet packet = null;
           int countOfPacketCaptures = 0;

            while ((packet = device.GetNextPacket()) != null)
                {

                packet = device.GetNextPacket();
                if (packet is TCPPacket)
                    {
                    TCPPacket tcp = (TCPPacket)packet;
                    myPacket tempPacket = new myPacket();

                    tempPacket.packetType = "TCP";
                    tempPacket.sourceAddress = Convert.ToString(tcp.SourceAddress);
                    tempPacket.destinationAddress = Convert.ToString(tcp.DestinationAddress);
                    tempPacket.sourcePort = Convert.ToString(tcp.SourcePort);
                    tempPacket.destinationPort = Convert.ToString(tcp.DestinationPort);
                    tempPacket.packetMessage = Convert.ToString(tcp.Data);
                    packetsList.Add(tempPacket);

                     packetData = 
                        "Type= TCP" +
                        "   Source Address = "+  Convert.ToString(tcp.SourceAddress)+
                       "   Destination Address =" +Convert.ToString(tcp.DestinationAddress)+
                       "   SourcePort =" +    Convert.ToString(tcp.SourcePort)+
                       "   SourcePort =" +Convert.ToString(tcp.DestinationPort)+
                       "   Messeage =" + Convert.ToString(tcp.Data);
                    txtpackets.Invoke(new UpdatetxtpacketsCallback(this.Updatetxtpackets),
            new object[] { packetData });


                    string[] row = { packetsList[countOfPacketCaptures].packetType, packetsList[countOfPacketCaptures].sourceAddress, packetsList[countOfPacketCaptures].destinationAddress, packetsList[countOfPacketCaptures].sourcePort, packetsList[countOfPacketCaptures].destinationPort, packetsList[countOfPacketCaptures].packetMessage };
                    try { //dgwPacketInfo.Rows.Add(row); countOfPacketCaptures++;
                    //lblCapturesLabels.Text = Convert.ToString(countOfPacketCaptures);
                    }
                    catch (Exception e) { }

                    }
                else if (packet is UDPPacket)
                    {

                    UDPPacket udp = (UDPPacket)packet;


                    myPacket tempPacket = new myPacket();

                    tempPacket.packetType = "UDP";
                    tempPacket.sourceAddress = Convert.ToString(udp.SourceAddress);
                    tempPacket.destinationAddress = Convert.ToString(udp.DestinationAddress);
                    tempPacket.sourcePort = Convert.ToString(udp.SourcePort);
                    tempPacket.destinationPort = Convert.ToString(udp.DestinationPort);
                    tempPacket.packetMessage = udp.Data.ToArray() + "\n";
                    packetsList.Add(tempPacket);

                    packetData = 
                        "Type= UDP" +
                        "   Source Address = "+  Convert.ToString(udp.SourceAddress)+
                       "   Destination Address =" +Convert.ToString(udp.DestinationAddress)+
                       "   SourcePort =" +    Convert.ToString(udp.SourcePort)+
                       "   SourcePort =" +Convert.ToString(udp.DestinationPort)+
                       "   Messeage =" + udp.Data.ToArray() + "\n";
                    string[] row = { packetsList[countOfPacketCaptures].packetType, packetsList[countOfPacketCaptures].sourceAddress, packetsList[countOfPacketCaptures].destinationAddress, packetsList[countOfPacketCaptures].sourcePort, packetsList[countOfPacketCaptures].destinationPort, packetsList[countOfPacketCaptures].packetMessage };
                    try {
                        //dgwPacketInfo.Rows.Add(row);
                    //countOfPacketCaptures++;
                    //lblCapturesLabels.Text = Convert.ToString(countOfPacketCaptures);
                        txtpackets.Invoke(new UpdatetxtpacketsCallback(this.Updatetxtpackets),
               new object[] { packetData });

                    }
                    catch (Exception e) { }


                    }


                }
            }
War es hilfreich?

Lösung

Ich fand die Antwort ...

Data ist ein Byte-Array so ich Verwendung Bit-Wandler müssen und statt mit:

Convert.ToString(tcp.Data);

Ich sollte verwenden:

BitConverter.ToString(tcp.Data)

Andere Tipps

Der Parser ist nicht so komplex ...

schaute ich auf dem Packet.Net Code (das ist der Parsing für SharpPcap) und alle Felder werden in gängigen Formaten gespeichert werden.

Die IP-Adressen werden in System.Net.IPAddress Format gespeichert, so dass Sie nur .ToString auf sich nennen können eine Textzeichenfolge zu erhalten, die richtig die Punktmarkierungen enthält.

Die Portnummern werden als ushort gespeichert, die das gleiche wie jede andere ganze Zahl gedruckt werden können.

Der einzige Teil, dass der Bedarf in seiner binären Form ist das Datenfeld, da die auf der Grundlage Veränderungen interpretiert werden, welches Protokoll wird auf der nächsten Ebene nach oben verwendet wird. SharpPcap / Packet.Net macht die meiste Arbeit für Sie bereits und Felder in den bequemsten oder identischen Formen annehmen, die in der Protokollspezifikation gefunden gespeichert werden. Verwenden Sie einfach intellisense Feld des Typs zu überprüfen und, wenn es nicht ein du bist vertraut mit (wie System.Net.IPAddress oder System.NetworkInformation.PhysicalAddress (für MAC-Adressen)) Googles es einfach.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top