c# 使用sharppcap實現 網路抓包的詳情

來源:互聯網
上載者:User
sharppcap的dll下載地址:

sourceforge.net/directory/os:windows/?q=sharppcap

詳細用法:

www.codeproject.com/KB/IP/sharppcap.aspx

為了進一步說明使用方式,在此分享一個我寫的wrapper類。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;using System.Threading;using SharpPcap;using PacketDotNet;using SharpPcap.LibPcap;namespace ServerToolV0._1.Capture{    public class WinCapHelper    {        private static object syncObj = new object();        private static WinCapHelper _capInstance;        public static WinCapHelper WinCapInstance        {            get            {                if (null == _capInstance)                {                    lock (syncObj)                    {                        if (null == _capInstance)                        {                            _capInstance = new WinCapHelper();                        }                    }                }                return _capInstance;            }        }        private Thread _thread;        /// <summary>        /// when get pocket,callback        /// </summary>        public Action<string> _logAction;        /// <summary>        /// 過濾條件關鍵字        /// </summary>        public string filter;        private WinCapHelper()        {        }        public void Listen()        {            if (_thread != null && _thread.IsAlive)            {                return;            }            _thread = new Thread(new ThreadStart(() =>            {                ////遍曆網卡                foreach (PcapDevice device in SharpPcap.CaptureDeviceList.Instance)                {                    ////分別啟動監聽,指定包的處理函數                    device.OnPacketArrival +=                        new PacketArrivalEventHandler(device_OnPacketArrival);                    device.Open(DeviceMode.Normal, 1000);                    device.Capture(500);                    //device.StartCapture();                }            }));            _thread.Start();        }        /// <summary>        /// 列印包資訊,組合包太複雜了,所以直接把hex字串打出來了        /// </summary>        /// <param name="str"></param>        /// <param name="p"></param>        private void PrintPacket(ref string str, Packet p)        {            if (p != null)            {                string s = p.ToString();                if (!string.IsNullOrEmpty(filter) && !s.Contains(filter))                {                    return;                }                str += "\r\n" + s + "\r\n";                ////嘗試建立新的TCP/IP資料包對象,                ////第一個參數為以太頭長度,第二個為資料包資料區塊                str += p.PrintHex() + "\r\n";            }        }        /// <summary>        /// 接收到包的處理函數        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void device_OnPacketArrival(object sender, CaptureEventArgs e)        {            ////解析出基本包            var packet = PacketDotNet.Packet.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data);            ////協議類別           // var dlPacket = PacketDotNet.DataLinkPacket.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data);             //var ethernetPacket = PacketDotNet.EthernetPacket.GetEncapsulated(packet);            //var internetLinkPacket = PacketDotNet.InternetLinkLayerPacket.Parse(packet.BytesHighPerformance.Bytes);            //var internetPacket = PacketDotNet.InternetPacket.Parse(packet.BytesHighPerformance.Bytes);            //var sessionPacket = PacketDotNet.SessionPacket.Parse(packet.BytesHighPerformance.Bytes);            //var appPacket = PacketDotNet.ApplicationPacket.Parse(packet.BytesHighPerformance.Bytes);            //var pppoePacket = PacketDotNet.PPPoEPacket.Parse(packet.BytesHighPerformance.Bytes);            //var arpPacket = PacketDotNet.ARPPacket.GetEncapsulated(packet);            //var ipPacket = PacketDotNet.IpPacket.GetEncapsulated(packet); //ip包            //var udpPacket = PacketDotNet.UdpPacket.GetEncapsulated(packet);            //var tcpPacket = PacketDotNet.TcpPacket.GetEncapsulated(packet);            string ret = "";            PrintPacket(ref ret, packet);            //ParsePacket(ref ret, ethernetPacket);            //ParsePacket(ref ret, internetLinkPacket);            //ParsePacket(ref ret, internetPacket);            //ParsePacket(ref ret, sessionPacket);            //ParsePacket(ref ret, appPacket);            //ParsePacket(ref ret, pppoePacket);            //ParsePacket(ref ret, arpPacket);            //ParsePacket(ref ret, ipPacket);            //ParsePacket(ref ret, udpPacket);            //ParsePacket(ref ret, tcpPacket);            if (!string.IsNullOrEmpty(ret))            {                string rlt = "\r\n時間 : " +                    DateTime.Now.ToLongTimeString() +                    "\r\n資料包: \r\n" + ret;                _logAction(rlt);            }        }        public void StopAll()        {            foreach (PcapDevice device in SharpPcap.CaptureDeviceList.Instance)            {                if (device.Opened)                {                    Thread.Sleep(500);                    device.StopCapture();                }                _logAction("device : " + device.Description + " stoped.\r\n");            }            _thread.Abort();        }    }}

以上就是c# 使用sharppcap實現 網路抓包的詳情的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.