SharpPcap - 数据包捕获获取消息问题

发布于 2024-08-26 07:06:48 字数 4519 浏览 3 评论 0原文

我尝试使用 SharpPcap 库捕获数据包。 我可以返回数据包详细信息,但无法获取数据包内的消息内容。

使用 .Data 返回消息的数据包,当我使用它时,它正在返回(System.Byte[])。

这是图书馆网站: http://www.codeproject.com/KB/IP/sharppcap.aspx

这是我的代码:

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) { }


                    }


                }
            }

I trying to capture packets using SharpPcap library.
I'm able to return the packets details but I'm having problem to get what the message content inside the packet.

the packet using .Data to return the message and when I use it it is returning (System.Byte[]).

here is the library website:
http://www.codeproject.com/KB/IP/sharppcap.aspx

here is my 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) { }


                    }


                }
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁拥 2024-09-02 07:06:48

我找到了答案...

数据是一个字节数组,所以我需要使用位转换器,而不是使用:

Convert.ToString(tcp.Data);

我应该使用:

BitConverter.ToString(tcp.Data)

I found the answer...

Data is a byte array so I need to use bit converter and instead of using:

Convert.ToString(tcp.Data);

I should use:

BitConverter.ToString(tcp.Data)
把昨日还给我 2024-09-02 07:06:48

解析器并没有那么复杂...

我查看了 Packet.Net 代码(这是 SharpPcap 的解析),所有字段都以常用格式存储。

IP 地址以 System.Net.IPAddress 格式存储,因此您只需对它们调用 .ToString 即可获取正确包含点标记的文本字符串。

端口号存储为 ushort,可以像任何其他整数一样打印。

唯一需要以二进制形式解释的部分是数据字段,因为它会根据下一层使用的协议而变化。 SharpPcap/Packet.Net 已经为您完成了大部分工作,并且字段以最方便或与协议规范中的形式相同的形式存储。只需使用智能感知来检查字段的类型,如果它不是您熟悉的类型(例如 System.Net.IPAddress 或 System.NetworkInformation.PhysicalAddress(对于 MAC 地址)),只需 google 一下即可。

The parser isn't that complex...

I looked at the Packet.Net code (which is the parse for SharpPcap) and all of the fields are stored in commonly used formats.

The IP Addresses are stored in System.Net.IPAddress format so you can just call .ToString on them to get a text string that properly includes the dot marks.

The port numbers are stored as ushort which can be printed the same as any other integer.

The only part that needs to be interpreted in its binary form is the Data field because that changes based on what protocol is being used on the next layer up. SharpPcap/Packet.Net does most of the work for you already and fields are stored in the most convenient or identical forms to those found in the protocol specification. Just use intellisense to check the field's type and if it's not one you're familiar with (such as System.Net.IPAddress or System.NetworkInformation.PhysicalAddress (For MAC addresses)) just google it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文