利用WPCAP函数,开发局域网ARP数据包监听程序

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpPcap;
using SharpPcap.Protocols;
using SharpPcap.Packets;
using System.IO;

namespace ArpCap
{
    class Program
    {
        static void Main(string[] args)
        {
            List<PcapDevice> devices = SharpPcap.Pcap.GetAllDevices();

            foreach (PcapDevice d in devices)
            {
                Console.WriteLine(d.Name);

                Console.WriteLine(d.Description);

                foreach (Pcap.PcapAddress addr in d.Addresses)
                {
                    Console.WriteLine(addr.ToString());
                }
                Console.WriteLine();
            }

            Console.WriteLine("请选择将要监控的设备:");

            int deviceIndex = Console.Read() - 48;

            PcapDevice device = devices[deviceIndex];

            Console.WriteLine("开始监控设备:" + device.Description);
           
            device.OnPacketArrival += new Pcap.PacketArrivalEvent(device_OnPacketArrival);

            device.OnCaptureStopped += new Pcap.PcapCaptureStoppedEvent(device_OnCaptureStopped);

            device.Open(false);

            device.SetFilter("ip");

            device.StartCapture();
           
            Console.ReadKey();

            device.StopCapture();

            device.Close();
        }

        static void device_OnCaptureStopped(object sender, bool error)
        {
            //throw new NotImplementedException();
            Console.WriteLine("Stop Capture!");
        }

        static void device_OnPacketArrival(object sender, Packet packet)
        {
            ARPPacket p = (ARPPacket)packet;

            if (p.ARPSenderProtoAddress.ToString() != "10.10.11.88")
            {
                Console.WriteLine();

                Console.WriteLine(DateTime.Now.ToString());

                Console.WriteLine(p);

                Console.WriteLine("源地址:");

                Console.WriteLine(p.ARPSenderProtoAddress.ToString());

                Console.WriteLine("目的地址:");

                Console.WriteLine(p.ARPTargetProtoAddress.ToString());
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值