Printing Packet Data

Feb 23, 2011 at 4:14 AM

Hi All,

Probably a pretty straight forward question but can someone lead me to an answer of how to display the packet data in the output? Preferably displayed in ASCII. 

Any help would be appreciated!

J

Coordinator
Feb 23, 2011 at 1:13 PM

Hi jbfasulo,

 

If you just want the packet bytes, you can iterate over the bytes (using foreach) and print each byte.

 

Boaz.

Feb 23, 2011 at 4:08 PM
Edited Feb 23, 2011 at 4:11 PM

Hi J

what do you mean by "Packet Data" ?? do you mean the packet payload or the fields values ??

for the fields values you have to take every one of them individually

for the packet payload you have to make an object (for instance lets call it packetObj) that represent the packet type (TCP or UDP) you are handling and then use (packetObj.Payload) to get the data you want

oh and for the ASCII thingy , just use (System.Encoding) Class , it has everything you need from ASCII to Unicode

 

i hope this is enough , if you want more then just ask :)

Feb 23, 2011 at 4:11 PM

I'll give it a go!

I owe you so much thanks for Pcap.NET!

J

Feb 23, 2011 at 4:13 PM

Shamilqais,

Yes, exactly what I was looking for with the packet payload! I'll give it a go!

J

Feb 23, 2011 at 5:49 PM
shamilqais wrote:

Hi J

what do you mean by "Packet Data" ?? do you mean the packet payload or the fields values ??

for the fields values you have to take every one of them individually

for the packet payload you have to make an object (for instance lets call it packetObj) that represent the packet type (TCP or UDP) you are handling and then use (packetObj.Payload) to get the data you want

oh and for the ASCII thingy , just use (System.Encoding) Class , it has everything you need from ASCII to Unicode

 

i hope this is enough , if you want more then just ask :)

Shamilqais,

I'm trying something like this..

 

 IpV4Datagram ip = packet.Ethernet.IpV4;
                            //UdpDatagram udp = ip.Udp;
                            TcpDatagram tcp = ip.Tcp;
                            //Filter HTTP traffic
                            communicator.SetFilter("port 80");
                            
                            //trying to set up the acii encoded string
                            Encoding ascii = Encoding.ASCII;
                            String output = ascii.GetString(ip.Payload); //<---yes error


                            // print ip addresses and udp ports
                            // textbox.AppendText(ip.Source + ":" + udp.SourcePort.Equals(80) + " -> " + ip.Destination + ":" + udp.DestinationPort);
                            textbox.AppendText(ip.Source + ":" + tcp.SourcePort + "->" + ip.Destination + ":" +
                                tcp.DestinationPort + "---" + "[I'M TRYING TO OUTPUT THE PAYLOAD HERE]");

 

I'd appreciate any advice!

J

Feb 23, 2011 at 6:23 PM
Edited Feb 23, 2011 at 6:57 PM

Made a little more progress..

 

 

  IpV4Datagram ip = packet.Ethernet.IpV4;
                            //UdpDatagram udp = ip.Udp;
                            TcpDatagram tcp = ip.Tcp;

                            //Filter HTTP traffic
                            communicator.SetFilter("port 80");

                            //trying to set up the acii encoded string
                            Encoding ascii = Encoding.ASCII;
                            //  String output = ascii.GetString(ip.Payload); //<---yes error

                            //Get bytes from packet
                            byte[] packetBytes = new byte[10000];
                            short count =0;
                            foreach (byte i in ip.Payload)
                            {
                                packetBytes[count] = i;
                                count++;
                            }

                            //convert byte array to string
                            string stringPacketBytes = System.Text.ASCIIEncoding.ASCII.GetString(packetBytes);

                            // print ip addresses and udp ports
                            // textbox.AppendText(ip.Source + ":" + udp.SourcePort.Equals(80) + " -> " + ip.Destination + ":" + udp.DestinationPort);
                            textbox.AppendText(ip.Source + ":" + tcp.SourcePort + "->" + ip.Destination + ":" +
                                tcp.DestinationPort + "---" + stringPacketBytes);

 

 

I think what I'm trying to access is the Packet.Payload.Buffer to get its contents, in the foreach loop, but am not able to access it. Ideas?

 

J

Coordinator
Feb 26, 2011 at 8:07 AM

I don't understand what problem are you having.

To get the IP payload bytes as an ASCII string the simplest way (not the most efficient one though) would be to do

string stringPacketBytes = Encoding.ASCII.GetString(ip.Payload.ToArray());