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

C# Packet Capture Programming #3 - 패킷 캡쳐 GetNextPacket

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();

            PcapDevice.Close();
        }
    }
}

 

GetNextPacket.cs
0.00MB

728x90