• 大小: 332KB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2021-06-12
  • 语言: C#
  • 标签: C#  SharpPcap  

资源简介

C#利用SharpPcap发送以太网报文(以太网原始帧),软件基本流程:从文件读取要发送的帧内容,然后找到电脑上的以太网卡,接下来利用SharpPcap来循环发送。
vs2008工程,全部源代码,可编译和测试。
以太网帧内容可以自己构造任意的帧类型和内容;也可以使用wireshark抓包,然后利用本软件来重放。
wireshark抓包后取数据方法:在wireshark的收到的帧上点右键,选择 复制 -> 为一个HEX流,然后粘贴到一个空文件内,命名为packet.dat,这样本软件就会读取packet.dat内的内容并循环发送了。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpPcap.LibPcap;
using SharpPcap.WinPcap;
using SharpPcap;
using System.Net.Sockets;
using System.Collections;
using System.Net.NetworkInformation;
using System.Threading;
using System.IO;

namespace LldpSend
{
    class Program
    {
        static string loadDevice()// 获取网卡方法
        {
            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
            string netinterface = null;
            try
            {
                foreach (NetworkInterface adap in adapters)
                {
                    if (adap.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                    {
                        Console.Out.WriteLine(“netport name:“ + adap.Name);
                        netinterface = adap.Name;//保存网口
                        //device = i;//找到的第一个网口
                        //deviceMac = adap.GetPhysicalAddress().ToString();//得到设备mac,后续设置iP地址等信息的时候需要
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine(“错误:初始化没有找到以太网卡“);
                Console.Out.WriteLine(ex.Message + “\n“ + ex.StackTrace);
            }
            return netinterface;
        }

        static NetworkInterface getNetworkDevice(string name)// 获取指定网卡方法
        {
            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
            NetworkInterface netinterface = null;
            try
            {
                foreach (NetworkInterface adap in adapters)
                {
                    if (adap.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                    {
                        if (adap.Name.Equals(name))
                        {
                            Console.Out.WriteLine(“netport name:“ + adap.Name);
                            netinterface = adap;//保存网口
                            break;
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                Console.Out.WriteLine(“错误:初始化没有找到以太网卡,“ + ex.Message);
                Console.Out.WriteLine(ex.Message + “\n“ + ex.StackTrace);
            }
            return netinterface;
        }

        static string read_packet_file(string filename)
        {
            //文件路径
            string filePath = filename;
            try
            {
                if (File.Exists(filePath))
                {
                    string str = File.ReadAllText(filePath);
                    return str;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(“读取帧文件失败:“+ex.Message);
                return null;
      

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

    ..A..H.      7168  2020-01-13 12:29  LldpSend.suo

     文件       8192  2020-01-13 12:50  LldpSend\bin\Debug\LldpSend.exe

     文件      17920  2020-01-13 12:50  LldpSend\bin\Debug\LldpSend.pdb

     文件      14328  2020-01-13 12:51  LldpSend\bin\Debug\LldpSend.vshost.exe

     文件        490  2009-06-11 05:14  LldpSend\bin\Debug\LldpSend.vshost.exe.manifest

     文件        723  2020-01-13 12:50  LldpSend\bin\Debug\packet.dat

     文件     196608  2013-01-14 19:59  LldpSend\bin\Debug\PacketDotNet.dll

     文件     476563  2013-01-14 19:59  LldpSend\bin\Debug\PacketDotNet.xml

     文件      72704  2013-01-14 19:59  LldpSend\bin\Debug\SharpPcap.dll

     文件     186730  2013-01-14 19:59  LldpSend\bin\Debug\SharpPcap.xml

     文件     196608  2013-01-14 19:59  LldpSend\dll\PacketDotNet.dll

     文件     476563  2013-01-14 19:59  LldpSend\dll\PacketDotNet.xml

     文件      72704  2013-01-14 19:59  LldpSend\dll\SharpPcap.dll

     文件        492  2013-01-14 19:59  LldpSend\dll\SharpPcap.dll.config

     文件     186730  2013-01-14 19:59  LldpSend\dll\SharpPcap.xml

     文件       2965  2020-01-13 12:47  LldpSend\LldpSend.csproj

     文件        496  2020-01-13 12:51  LldpSend\obj\Debug\LldpSend.csproj.FileListAbsolute.txt

     文件       8192  2020-01-13 12:50  LldpSend\obj\Debug\LldpSend.exe

     文件      17920  2020-01-13 12:50  LldpSend\obj\Debug\LldpSend.pdb

     文件       6033  2020-01-13 12:47  LldpSend\obj\Debug\ResolveAssemblyReference.cache

     文件       7175  2020-01-13 12:50  LldpSend\Program.cs

     文件       1366  2020-01-13 12:29  LldpSend\Properties\AssemblyInfo.cs

     文件        914  2020-01-13 12:29  LldpSend.sln

     目录          0  2020-01-13 12:29  LldpSend\obj\Debug\TempPE

     目录          0  2020-01-13 12:56  LldpSend\bin\Debug

     目录          0  2020-01-13 12:50  LldpSend\obj\Debug

     目录          0  2020-01-13 12:29  LldpSend\bin

     目录          0  2020-01-13 12:47  LldpSend\dll

     目录          0  2020-01-13 12:29  LldpSend\obj

     目录          0  2020-01-13 12:29  LldpSend\Properties

............此处省略4个文件信息

评论

共有 条评论