Декодирование байтового массива из TCP-пакета

Мне удалось захватить пакеты в сети для определенного устройства, но во время захвата пакетов я получаю массив байтов. Я пытался расшифровать их в ASCII, но это все еще бред. Мне нужна небольшая помощь, чтобы помочь мне получить данные из пакетов.

Я использую библиотеки SharpPCap и PacketDotNet.

using PacketDotNet;
using SharpPcap;
using SharpPcap.LibPcap;
using System;
using System.Collections.Generic;

namespace InterpretingThePackets
{
    class Program
    {
        public static List<RawCapture> rawcaptures = new List<RawCapture>();
        static void Main(string[] args)
        {
            string ver = SharpPcap.Version.VersionString;
            Console.WriteLine("SharpPcap {0}, Example1.IfList.cs", ver);

            CaptureDeviceList devices = CaptureDeviceList.Instance;

            if (devices.Count < 1)
            {
                Console.WriteLine("No devices were found on this machine");
                return;
            }

            Console.WriteLine("\nThe following devices are available on this machine:");
            Console.WriteLine("----------------------------------------------------\n");

            foreach (ICaptureDevice dev in devices)
            {
                Console.WriteLine("[" + devices.IndexOf(dev) + "]{0}\n", GetNameFromDevice(dev));
            }

            Console.Write("Choose adapter to sniff...\n");
            string choice = Console.ReadLine();
            ICaptureDevice device = devices[int.Parse(choice)];
            Console.WriteLine("selected " + device.ToString() + "\n\n");

            device.OnPacketArrival +=
                new SharpPcap.PacketArrivalEventHandler(device_OnPacketArrival);

            int readTimeoutMilliseconds = 1000000;
            device.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds);

            Console.WriteLine("-- Listening on {0}, hit 'Enter' to stop...",
                device.Description);

            device.StartCapture();
            Console.ReadLine();
            device.StopCapture();
            device.Close();

            Console.ReadLine();
        }

        private static void device_OnPacketArrival(object sender, CaptureEventArgs packet)
        {
            rawcaptures.Add(packet.Packet);

            var packy = PacketDotNet.Packet.ParsePacket(packet.Packet.LinkLayerType, packet.Packet.Data);

            Console.WriteLine("\n \n new packet:\n");
            Console.WriteLine("RAW PACKET: " + ByteArrayToByteArrayString(packet.Packet.Data));
            if (packy.PayloadData == null)
            {
                Console.WriteLine("TCP PACKET: " + ByteArrayToByteArrayString(packy.PayloadPacket.PayloadData));
            }
            else
            {
                Console.WriteLine("TCP PACKET: " + ByteArrayToByteArrayString(packy.PayloadData));
            }
        }

        public static string GetNameFromDevice( ICaptureDevice device)
        {
            //split device description by newline, then return the second line
            string[] slist = device.ToString().Replace("\n", "~").Split('~');
            return slist[1];
        }

        public static string ByteArrayToByteArrayString(byte[] byteArray)
        {
            string returnString = "";
            foreach (Byte singleByte in byteArray)
            {
                returnString = returnString + "[" + singleByte.ToString() + "]";
            }
            return returnString;
        }
    }
}

Это все, что есть в программе, потому что я пытаюсь научить себя, как это сделать.

0 ответов

Другие вопросы по тегам