본문 바로가기
기술자료/C#

C# Packet Capture Programming #4 - 패킷 출력 HexaView

by 와이즈캣 2021. 9. 7.
728x90
반응형
using System;
using SharpPcap;
using HexaViewer;

namespace Net_Packet001
{
    class Program
    {
        static void Main(string[] args)
        {
            string NIC_Name = "Wi-Fi";
            ICaptureDevice PcapDevice = null;

            if (1 > CaptureDeviceList.Instance.Count)
            {
                Console.WriteLine("패킷을 캡쳐할 수 있는 장치가 존재하지 않아 종료합니다...");
                return;
            }

            foreach (ICaptureDevice NIC in CaptureDeviceList.Instance)
            {
                string FriendName = NIC.ToString().Split('\n')[1];
                FriendName = FriendName.Substring(FriendName.IndexOf(' ') + 1);

                if (FriendName == NIC_Name)
                {
                    PcapDevice = NIC;

                    break;
                }
            }

            if (null == PcapDevice)
            {
                Console.WriteLine("캡쳐에 적합한 장치 이름을 검색할 수 없어 종료합니다...");
                return;
            }
            else
            {
                Console.WriteLine("===========================================================================");
                Console.WriteLine("캡쳐에 사용할 적합한 NIC의 검색을 완료하였습니다...");
                Console.WriteLine("===========================================================================");
                Console.WriteLine(PcapDevice);
                Console.WriteLine("===========================================================================");
                Console.WriteLine("패킷을 캡쳐하기 위해 위 장치를 사용합니다...");
                Console.WriteLine("===========================================================================");
            }

            PcapDevice.Open(DeviceMode.Promiscuous, 0);

            RawCapture Pakcet = PcapDevice.GetNextPacket();

            HexaView.HexaViewText(Pakcet.Data);

            PcapDevice.Close();
        }
    }
}

 

using System;

namespace HexaViewer
{
    public class HexaView
    {
        static void PrintLine(char Char, ushort Count)
        {
            for(int Temp = 0; Temp < Count; ++Temp)
            {
                Console.Write(Char);
            }
            Console.WriteLine();
        }
        static void PrintHead(char Line1, char Line2, ushort Count)
        {
            PrintLine(Line1, Count);
            PrintLine(Line2, Count);
            Console.Write(" ADDRESS ");

            for (int iCount = 0; iCount < 16; ++iCount)
            {
                Console.Write("{0:X02} ", iCount);
            }

            for (int iCount = 0; iCount < 16; ++iCount)
            {
                Console.Write("{0:X01}", iCount);
            }
            Console.WriteLine();
            PrintLine(Line1, Count);
        }
        static void PrintHead()
        {
            PrintHead('=', '-',73);
        }
        static void PrintEnd()
        {
            PrintLine('=', 73);
        }
        public static void HexaViewText(byte[] Data)
        {
            ArraySegment<byte> TempDataPos;
            int iLoopTotal = Data.Length / 16;
            int iLoopRemain = Data.Length % 16;

            PrintHead();

            for (int iLoopCount = 0; iLoopCount < iLoopTotal; iLoopCount = iLoopCount + 1)
            {
                Console.Write("{0:X08} ", iLoopCount * 16);

                TempDataPos = new ArraySegment<byte>(Data, iLoopCount * 16, 16);
                foreach (var Temp in TempDataPos)
                {
                    Console.Write("{0:X02} ", Temp);
                }

                foreach (char Temp in TempDataPos)
                {
                    if (' ' > Temp)
                    {
                        Console.Write(".");
                    }
                    else if (127 < Temp)
                    {
                        Console.Write(".");
                    }
                    else
                    {
                        Console.Write("{0}", Temp);
                    }
                }
                Console.WriteLine();
            }
            if (0 != iLoopRemain)
            {
                iLoopTotal = iLoopTotal * 16;
                Console.Write("{0:X08} ", iLoopTotal);

                TempDataPos = new ArraySegment<byte>(Data, iLoopTotal, iLoopRemain);
                foreach (var Temp in TempDataPos)
                {
                    Console.Write("{0:X02} ", Temp);
                }
                for (int Temp = 0; Temp < 16 - iLoopRemain; ++Temp)
                {
                    Console.Write("   ");
                }


                foreach (char Temp in TempDataPos)
                {
                    if (' ' > Temp)
                    {
                        Console.Write(".");
                    }
                    else if (127 < Temp)
                    {
                        Console.Write(".");
                    }
                    else
                    {
                        Console.Write("{0}", Temp);
                    }
                }

                for (int Temp = 0; Temp < 16 - iLoopRemain; ++Temp)
                {
                    Console.Write(" ");
                }
            }
            Console.WriteLine();

            PrintEnd();
        }
    }
}

 

HexaViewer.cs
0.00MB
PrintPacket.cs
0.00MB

 

728x90