본문 바로가기

C#

C# 개발 - 패킷 단위 자르는 방법 공부해서 Wireshark · Go Deep 유사품 만들기

반응형

IP 패킷은 헤더와 페이로드로 이루어진다. IPv4 패킷 헤더는 다음으로 구성된다:[2]

00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 (비트 위치)
버전 IHL QoS 길이  
식별   0  DF MF 프레그먼트 오프셋(Fragment Offset)  
TTL 프로토콜 체크섬  
원본 IP  
도착 IP  

패킷(packet, 문화어: 파케트, 소포)은 정보 기술에서 패킷 방식 컴퓨터 네트워크가 전달하는 데이터의 형식화된 블록이다. 패킷은 제어 정보와 사용자 데이터로 이루어지며[1], 이는 페이로드라고도 한다. 패킷을 지원하지 않는 컴퓨터 통신 연결은 단순히 바이트, 문자열, 비트를 독립적으로 연속하여 데이터를 전송한다. 데이터가 패킷으로 형식이 바뀔 때, 네트워크는 장문 메시지를 더 효과적이고 신뢰성 있게 보낼 수 있다.

Wireshark · Go Deep

https://www.wireshark.org/

 

Wireshark · Go Deep

SharkFest Find out more about SharkFest, the premiere Wireshark educational conference.

www.wireshark.org

 

비트 (단위) - 위키백과, 우리 모두의 백과사전

위키백과, 우리 모두의 백과사전. 비트(bit, binary digit)[1]는 하나의 비트는 0이나 1의 값을 가질 수 있고,[2] 각각은 참, 거짓 혹은 서로 배타적인 상태를 나타낸다. 바이트는 비트가 여러 개 모인 것

ko.wikipedia.org

Wireshark · Go Deep

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;

using System.Windows.Forms;
using SharpPcap;

namespace _20200826_001
{
    // 1. 설치 - 캡쳐 - 목록 - 선택 - 오픈 - 캡쳐
    // 2. 캡쳐된 것을 요리하는 과정

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        

        private int Del_Button_Device(object sender)
        {
            int DeviceNum = Convert.ToInt32((sender as Button).Name.Substring("Facto".Length));

            for (int iTemp = 0; iTemp < CaptureDeviceList.Instance.Count; iTemp++)
            {
                foreach (Control Temp in Controls) // controls라는 집합안에 버튼이 속해있음
                {
                    if (Temp.Name == ("Facto" + iTemp))
                    {
                        Controls.Remove(Temp);
                    }
                }
            }
            return DeviceNum;
        }

        private void Print_MAC(byte[] EtherData)
        {

            Label aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "Destination MAC";
            aLabel.Name = "LabelDMAC";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 10);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "Source MAC";
            aLabel.Name = "LabelSMAC";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 40);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "Next Protocal";
            aLabel.Name = "LabelProto";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 70);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);

            
            
            string data = string.Format("{0:X2}", EtherData[0]);
            for (int iTemp = 1; iTemp < 6; ++iTemp)
            {
                data += string.Format("-{0:X2}", EtherData[iTemp]);
            }
            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = data;
            aLabel.Name = "LabelDMACData";
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 10);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);

            data = string.Format("{0:X2}", EtherData[6]);
            for (int iTemp = 7; iTemp < 12; ++iTemp)
            {
                data += string.Format("-{0:X2}", EtherData[iTemp]);
            }
            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = data;
            aLabel.Name = "LabelSMACData";
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 40);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            int iNum = BitConverter.ToInt16(EtherData.Skip<byte>(12).Take(2).Reverse<byte>().ToArray(), 0);
            switch (iNum)
            {
                case 0x0800:
                    data = "IP";
                    break;
                case 0x0200:
                    data = "Xerox PUP";
                    break;
                case 0x0500:
                    data = "Sprite";
                    break;
                case 0x0806:
                    data = "Address resolution";
                    break;
                case 0x8035:
                    data = "Reverse ARP";
                    break;
                case 0x809B:
                    data = "AppleTalk protocol";
                    break;
                case 0x80F3:
                    data = "AppleTalk ARP";
                    break;
                case 0x8100:
                    data = "IEEE 802.1Q VLAN tagging";
                    break;
                case 0x8137:
                    data = "IPX";
                    break;
                case 0x86dd:
                    data = "IP protocol version 6";
                    break;
                case 0x9000:
                    data = "used to test interfaces";
                    break;
                default:
                    data = "Unknown";
                    break;
            }
            aLabel.Text = data;
            aLabel.Name = "LabelProtoData";
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 70);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);
        }

        private void Print_IP(byte[] IPData)
        {
            Label aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "IP Version";
            aLabel.Name = "LabelIPV";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 100);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "IP Head Length";
            aLabel.Name = "LabelIPHL";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 130);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "IP TOS";
            aLabel.Name = "LabelIPTOS";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 160);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);
            
            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "IP Total Length";
            aLabel.Name = "LabelIPTL";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 190);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);
            
            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "IP IDentification";
            aLabel.Name = "LabelIPID";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 220);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "Fragment DF";
            aLabel.Name = "LabelFDF";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 250);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);
            
            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "Fragment MF";
            aLabel.Name = "LabelIFMF";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 270);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "Fragment Offset";
            aLabel.Name = "LabelIFOFF";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 290);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);
           
            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "Time To Live";
            aLabel.Name = "LabelIPTTL";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 290);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);


            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "Time To Live";
            aLabel.Name = "LabelPROTO";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 320);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);
            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            byte byteTemp = IPData[14];

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = "IPv"+(byteTemp >> 4).ToString();
            aLabel.Name = "LabelIPVData";
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 100);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = ((byteTemp & 0x0F) * 4).ToString()+" Bytes";
            aLabel.Name = "LabelIPHLData";
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 130);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);
           
            byteTemp = IPData[15];

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = String.Format("0X{0:X2}", byteTemp);
            aLabel.Name = "LabelIPTOSData"; 
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 160);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);
            
            

            int iNum = BitConverter.ToInt16(IPData.Skip<byte>(16).Take(2).Reverse<byte>().ToArray(), 0);
            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = iNum.ToString() + " Bytes";
            aLabel.Name = "LabelIPTLData";
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 190);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);
            
            iNum = BitConverter.ToInt16(IPData.Skip<byte>(18).Take(2).Reverse().ToArray(), 0);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = iNum.ToString();
            aLabel.Name = "LabelIPIDData";
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 220);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);


            iNum = BitConverter.ToInt16(IPData.Skip<byte>(20).Take(2).Reverse().ToArray(), 0);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = (0 != (iNum & (1 << 14))).ToString(); //0이 들어가 있으면 거짓이다.
            aLabel.Name = "LabelFDFData"; //1BITE DONT FRAGMENT
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 250);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);

            iNum = BitConverter.ToInt16(IPData.Skip<byte>(20).Take(2).Reverse().ToArray(), 0);
            
            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = (0 != (iNum & (1 << 13))).ToString(); //0이 들어가 있으면 거짓이다.
            aLabel.Name = "LabelFMFData"; //1BITE MORE FRAGEMENT
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 270);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);

            iNum = BitConverter.ToInt16(IPData.Skip<byte>(20).Take(2).Reverse().ToArray(), 0);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = (iNum & 0X1FFF).ToString();
            aLabel.Name = "LabelFOFFSETData";// 13BITES
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 290);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);



            byteTemp = IPData[22];

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = byteTemp.ToString();
            aLabel.Name = "LabelTTLData";
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 290);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);

            byteTemp = IPData[22];

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = "--------------";
            aLabel.Name = "LabelPROTOData";
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 320);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);

        }

        private void Facto_Click(object sender, EventArgs e)
        {
            int DeviceNum = Del_Button_Device(sender);
            CaptureDeviceList.Instance[DeviceNum].Open(DeviceMode.Normal, 0);
            RawCapture arawCapture = CaptureDeviceList.Instance[DeviceNum].GetNextPacket();// 패킷을 하나가져오는 과정
            Print_MAC(arawCapture.Data); // 1계층 출력
            Print_IP(arawCapture.Data);// 2계층 출력
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            List<string> NicName = new List<string>();
            foreach (ICaptureDevice Temp in CaptureDeviceList.Instance)
            {
                string[] arrTemp = Temp.ToString().Split('\n');
                NicName.Add(arrTemp[1].Substring("FriendlyName: ".Length));
            }

            for (int iTemp = 0; iTemp < NicName.Count; iTemp++)
            {
                Button abutton = new Button();
                abutton.Text = NicName[iTemp];
                abutton.Name = "Facto" + iTemp;
                abutton.Location = new Point(10, 10 + iTemp * 30);
                abutton.Size = new Size(200, 22);
                abutton.BackColor = Color.White;
                abutton.Click += new EventHandler(Facto_Click);
                Controls.Add(abutton);
            }
            (sender as Button).Enabled = false;

        }
    }
}​
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;

using System.Windows.Forms;
using SharpPcap;

namespace _20200826_001
{
    // 1. 설치 - 캡쳐 - 목록 - 선택 - 오픈 - 캡쳐
    // 2. 캡쳐된 것을 요리하는 과정

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        

        private int Del_Button_Device(object sender)
        {
            int DeviceNum = Convert.ToInt32((sender as Button).Name.Substring("Facto".Length));

            for (int iTemp = 0; iTemp < CaptureDeviceList.Instance.Count; iTemp++)
            {
                foreach (Control Temp in Controls) // controls라는 집합안에 버튼이 속해있음
                {
                    if (Temp.Name == ("Facto" + iTemp))
                    {
                        Controls.Remove(Temp);
                    }
                }
            }
            return DeviceNum;
        }

        private void Print_MAC(byte[] EtherData)
        {

            Label aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "Destination MAC";
            aLabel.Name = "LabelDMAC";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 10);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "Source MAC";
            aLabel.Name = "LabelSMAC";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 40);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "Next Protocal";
            aLabel.Name = "LabelProto";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 70);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);

            
            
            string data = string.Format("{0:X2}", EtherData[0]);
            for (int iTemp = 1; iTemp < 6; ++iTemp)
            {
                data += string.Format("-{0:X2}", EtherData[iTemp]);
            }
            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = data;
            aLabel.Name = "LabelDMACData";
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 10);
            Controls.Add(aLabel);

            data = string.Format("{0:X2}", EtherData[6]);
            for (int iTemp = 7; iTemp < 12; ++iTemp)
            {
                data += string.Format("-{0:X2}", EtherData[iTemp]);
            }
            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = data;
            aLabel.Name = "LabelSMACData";
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 40);
            Controls.Add(aLabel);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            int iNum = BitConverter.ToInt16(EtherData.Skip<byte>(12).Take(2).Reverse<byte>().ToArray(), 0);
            switch (iNum)
            {
                case 0x0800:
                    data = "IP";
                    break;
                case 0x0200:
                    data = "Xerox PUP";
                    break;
                case 0x0500:
                    data = "Sprite";
                    break;
                case 0x0806:
                    data = "Address resolution";
                    break;
                case 0x8035:
                    data = "Reverse ARP";
                    break;
                case 0x809B:
                    data = "AppleTalk protocol";
                    break;
                case 0x80F3:
                    data = "AppleTalk ARP";
                    break;
                case 0x8100:
                    data = "IEEE 802.1Q VLAN tagging";
                    break;
                case 0x8137:
                    data = "IPX";
                    break;
                case 0x86dd:
                    data = "IP protocol version 6";
                    break;
                case 0x9000:
                    data = "used to test interfaces";
                    break;
                default:
                    data = "Unknown";
                    break;
            }
            aLabel.Text = data;
            aLabel.Name = "LabelProtoData";
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 70);
            Controls.Add(aLabel);
        }

        private void Print_IP(byte[] IPData)
        {
            Label aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "IP Version";
            aLabel.Name = "LabelIPV";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 100);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "IP Head Length";
            aLabel.Name = "LabelIPHL";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 130);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "IP TOS";
            aLabel.Name = "LabelIPTOS";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 160);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);
            
            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "IP Total Length";
            aLabel.Name = "LabelIPTL";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 190);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);
            
            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "IP IDentification";
            aLabel.Name = "LabelIPID";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 220);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "Fragment DF";
            aLabel.Name = "LabelFDF";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 250);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);
            
            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "Fragment MF";
            aLabel.Name = "LabelIFMF";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 270);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "Fragment Offset";
            aLabel.Name = "LabelIFOFF";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 290);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);
            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            byte byteTemp = IPData[14];

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = "IPv"+(byteTemp >> 4).ToString();
            aLabel.Name = "LabelIPVData";
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 100);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = ((byteTemp & 0x0F) * 4).ToString()+" Bytes";
            aLabel.Name = "LabelIPHLData";
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 130);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);
           
            byteTemp = IPData[15];

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = String.Format("0X{0:X2}", byteTemp);
            aLabel.Name = "LabelIPTOSData"; 
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 160);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);
            
            

            int iNum = BitConverter.ToInt16(IPData.Skip<byte>(16).Take(2).Reverse<byte>().ToArray(), 0);
            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = iNum.ToString() + " Bytes";
            aLabel.Name = "LabelIPTLData";
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 190);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);
            
            iNum = BitConverter.ToInt16(IPData.Skip<byte>(18).Take(2).Reverse().ToArray(), 0);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = iNum.ToString();
            aLabel.Name = "LabelIPIDData";
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 220);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);


            iNum = BitConverter.ToInt16(IPData.Skip<byte>(20).Take(2).Reverse().ToArray(), 0);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = (0 != (iNum & (1 << 14))).ToString(); //0이 들어가 있으면 거짓이다.
            aLabel.Name = "LabelFDFData"; //1BITE DONT FRAGMENT
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 250);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);

            iNum = BitConverter.ToInt16(IPData.Skip<byte>(20).Take(2).Reverse().ToArray(), 0);
            
            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = (0 != (iNum & (1 << 13))).ToString(); //0이 들어가 있으면 거짓이다.
            aLabel.Name = "LabelFMFData"; //1BITE MORE FRAGEMENT
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 270);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);

            iNum = BitConverter.ToInt16(IPData.Skip<byte>(20).Take(2).Reverse().ToArray(), 0);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = (iNum & 0X1FFF).ToString();
            aLabel.Name = "LabelFOFFSETData";// 13BITES
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 290);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);

        }

        private void Facto_Click(object sender, EventArgs e)
        {
            int DeviceNum = Del_Button_Device(sender);
            CaptureDeviceList.Instance[DeviceNum].Open(DeviceMode.Normal, 0);
            RawCapture arawCapture = CaptureDeviceList.Instance[DeviceNum].GetNextPacket();// 패킷을 하나가져오는 과정
            Print_MAC(arawCapture.Data); // 1계층 출력
            Print_IP(arawCapture.Data);// 2계층 출력
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            List<string> NicName = new List<string>();
            foreach (ICaptureDevice Temp in CaptureDeviceList.Instance)
            {
                string[] arrTemp = Temp.ToString().Split('\n');
                NicName.Add(arrTemp[1].Substring("FriendlyName: ".Length));
            }

            for (int iTemp = 0; iTemp < NicName.Count; iTemp++)
            {
                Button abutton = new Button();
                abutton.Text = NicName[iTemp];
                abutton.Name = "Facto" + iTemp;
                abutton.Location = new Point(10, 10 + iTemp * 30);
                abutton.Size = new Size(200, 22);
                abutton.BackColor = Color.White;
                abutton.Click += new EventHandler(Facto_Click);
                Controls.Add(abutton);
            }
            (sender as Button).Enabled = false;

        }
    }
}

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;

using System.Windows.Forms;
using SharpPcap;

namespace _20200826_001
{
    // 1. 설치 - 캡쳐 - 목록 - 선택 - 오픈 - 캡쳐
    // 2. 캡쳐된 것을 요리하는 과정

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        

        private int Del_Button_Device(object sender)
        {
            int DeviceNum = Convert.ToInt32((sender as Button).Name.Substring("Facto".Length));

            for (int iTemp = 0; iTemp < CaptureDeviceList.Instance.Count; iTemp++)
            {
                foreach (Control Temp in Controls) // controls라는 집합안에 버튼이 속해있음
                {
                    if (Temp.Name == ("Facto" + iTemp))
                    {
                        Controls.Remove(Temp);
                    }
                }
            }
            return DeviceNum;
        }

        private void Print_MAC(byte[] EtherData)
        {

            Label aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "Destination MAC";
            aLabel.Name = "LabelDMAC";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 10);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "Source MAC";
            aLabel.Name = "LabelSMAC";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 40);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "Next Protocal";
            aLabel.Name = "LabelProto";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 70);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);

            
            
            string data = string.Format("{0:X2}", EtherData[0]);
            for (int iTemp = 1; iTemp < 6; ++iTemp)
            {
                data += string.Format("-{0:X2}", EtherData[iTemp]);
            }
            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = data;
            aLabel.Name = "LabelDMACData";
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 10);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);

            data = string.Format("{0:X2}", EtherData[6]);
            for (int iTemp = 7; iTemp < 12; ++iTemp)
            {
                data += string.Format("-{0:X2}", EtherData[iTemp]);
            }
            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = data;
            aLabel.Name = "LabelSMACData";
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 40);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            int iNum = BitConverter.ToInt16(EtherData.Skip<byte>(12).Take(2).Reverse<byte>().ToArray(), 0);
            switch (iNum)
            {
                case 0x0800:
                    data = "IP";
                    break;
                case 0x0200:
                    data = "Xerox PUP";
                    break;
                case 0x0500:
                    data = "Sprite";
                    break;
                case 0x0806:
                    data = "Address resolution";
                    break;
                case 0x8035:
                    data = "Reverse ARP";
                    break;
                case 0x809B:
                    data = "AppleTalk protocol";
                    break;
                case 0x80F3:
                    data = "AppleTalk ARP";
                    break;
                case 0x8100:
                    data = "IEEE 802.1Q VLAN tagging";
                    break;
                case 0x8137:
                    data = "IPX";
                    break;
                case 0x86dd:
                    data = "IP protocol version 6";
                    break;
                case 0x9000:
                    data = "used to test interfaces";
                    break;
                default:
                    data = "Unknown";
                    break;
            }
            aLabel.Text = data;
            aLabel.Name = "LabelProtoData";
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 70);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);
        }

        private void Print_IP(byte[] IPData)
        {
            Label aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "IP Version";
            aLabel.Name = "LabelIPV";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 100);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "IP Head Length";
            aLabel.Name = "LabelIPHL";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 130);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "IP TOS";
            aLabel.Name = "LabelIPTOS";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 160);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);
            
            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "IP Total Length";
            aLabel.Name = "LabelIPTL";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 190);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);
            
            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "IP IDentification";
            aLabel.Name = "LabelIPID";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 220);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "Fragment DF";
            aLabel.Name = "LabelFDF";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 250);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);
            
            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "Fragment MF";
            aLabel.Name = "LabelIFMF";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 270);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "Fragment Offset";
            aLabel.Name = "LabelIFOFF";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 290);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);
           
            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleCenter;
            aLabel.Text = "Time To Live";
            aLabel.Name = "LabelIPTTL";
            aLabel.Size = new Size(120, 20);
            aLabel.Location = new Point(240, 290);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            aLabel.BackColor = Color.LightGray;
            Controls.Add(aLabel);
            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            byte byteTemp = IPData[14];

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = "IPv"+(byteTemp >> 4).ToString();
            aLabel.Name = "LabelIPVData";
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 100);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = ((byteTemp & 0x0F) * 4).ToString()+" Bytes";
            aLabel.Name = "LabelIPHLData";
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 130);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);
           
            byteTemp = IPData[15];

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = String.Format("0X{0:X2}", byteTemp);
            aLabel.Name = "LabelIPTOSData"; 
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 160);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);
            
            

            int iNum = BitConverter.ToInt16(IPData.Skip<byte>(16).Take(2).Reverse<byte>().ToArray(), 0);
            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = iNum.ToString() + " Bytes";
            aLabel.Name = "LabelIPTLData";
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 190);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);
            
            iNum = BitConverter.ToInt16(IPData.Skip<byte>(18).Take(2).Reverse().ToArray(), 0);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = iNum.ToString();
            aLabel.Name = "LabelIPIDData";
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 220);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);


            iNum = BitConverter.ToInt16(IPData.Skip<byte>(20).Take(2).Reverse().ToArray(), 0);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = (0 != (iNum & (1 << 14))).ToString(); //0이 들어가 있으면 거짓이다.
            aLabel.Name = "LabelFDFData"; //1BITE DONT FRAGMENT
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 250);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);

            iNum = BitConverter.ToInt16(IPData.Skip<byte>(20).Take(2).Reverse().ToArray(), 0);
            
            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = (0 != (iNum & (1 << 13))).ToString(); //0이 들어가 있으면 거짓이다.
            aLabel.Name = "LabelFMFData"; //1BITE MORE FRAGEMENT
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 270);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);

            iNum = BitConverter.ToInt16(IPData.Skip<byte>(20).Take(2).Reverse().ToArray(), 0);

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = (iNum & 0X1FFF).ToString();
            aLabel.Name = "LabelFOFFSETData";// 13BITES
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 290);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);



            byteTemp = IPData[22];

            aLabel = new Label();
            aLabel.TextAlign = ContentAlignment.MiddleLeft;
            aLabel.Text = byteTemp.ToString();
            aLabel.Name = "LabelTTLData";
            aLabel.Size = new Size(140, 20);
            aLabel.Location = new Point(370, 290);
            aLabel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(aLabel);

        }

        private void Facto_Click(object sender, EventArgs e)
        {
            int DeviceNum = Del_Button_Device(sender);
            CaptureDeviceList.Instance[DeviceNum].Open(DeviceMode.Normal, 0);
            RawCapture arawCapture = CaptureDeviceList.Instance[DeviceNum].GetNextPacket();// 패킷을 하나가져오는 과정
            Print_MAC(arawCapture.Data); // 1계층 출력
            Print_IP(arawCapture.Data);// 2계층 출력
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            List<string> NicName = new List<string>();
            foreach (ICaptureDevice Temp in CaptureDeviceList.Instance)
            {
                string[] arrTemp = Temp.ToString().Split('\n');
                NicName.Add(arrTemp[1].Substring("FriendlyName: ".Length));
            }

            for (int iTemp = 0; iTemp < NicName.Count; iTemp++)
            {
                Button abutton = new Button();
                abutton.Text = NicName[iTemp];
                abutton.Name = "Facto" + iTemp;
                abutton.Location = new Point(10, 10 + iTemp * 30);
                abutton.Size = new Size(200, 22);
                abutton.BackColor = Color.White;
                abutton.Click += new EventHandler(Facto_Click);
                Controls.Add(abutton);
            }
            (sender as Button).Enabled = false;

        }
    }
}

반응형