'ETC'에 해당되는 글 1건

  1. 2015.11.19 SharpPcap을 이용한 패킷 캡쳐(C#)

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using SharpPcap;

namespace sharppcap1

{

    class Program

    {

        static void Main(string[] args)

        {

            // Print SharpPcap version

            string ver = SharpPcap.Version.VersionString;

            Console.WriteLine("SharpPcap {0}, Example1.IfList.cs", ver);

            // Retrieve the device list

            CaptureDeviceList devices = CaptureDeviceList.Instance;

            // If no devices were found print an error

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


            int i = 0;

            // Print out the devices

            foreach (var dev in devices)

            {

                /* Description */

                Console.WriteLine("{0}) {1} {2}", i, dev.Name, dev.Description);

                i++;

            }

            Console.WriteLine();

            Console.Write("-- Please choose a device to capture: ");

            i = int.Parse(Console.ReadLine());


            // Extract a device from the list

            ICaptureDevice device = devices[i];

            // Open the device for capturing

            int readTimeoutMilliseconds = 1000;

            device.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds);

            Console.WriteLine();

            Console.WriteLine("-- Listening on {0}...",

                device.Description);

            RawCapture packet = null;

            System.Net.IPAddress srcIP;

            System.Net.IPAddress dstIP;

            

            // Keep capture packets using GetNextPacket()

            while ((packet = device.GetNextPacket()) != null)

            {

                // Prints the time and length of each received packet

                   

               

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

                var Packetinfo = PacketDotNet.TcpPacket.GetEncapsulated(tcpPacket);

                if (Packetinfo != null)

                {

                    var ipPacket = (PacketDotNet.IpPacket)Packetinfo.ParentPacket;

                    srcIP = ipPacket.SourceAddress;

                    dstIP = ipPacket.DestinationAddress;

                    DateTime time = packet.Timeval.Date;

                    var len = packet.Data.Length;

                    Console.WriteLine("{0}:{1}:{2},{3} Len={4}, {5}->{6}",

                        time.Hour, time.Minute, time.Second,

                        time.Millisecond, len, srcIP, dstIP);

                }

            }

            // Close the pcap device

            device.Close();

            Console.WriteLine(" -- Capture stopped, device closed.");

 

        }

     }

}

Posted by OnewayK
,