資源簡(jiǎn)介
C#利用SharpPcap發(fā)送以太網(wǎng)報(bào)文(以太網(wǎng)原始幀),軟件基本流程:從文件讀取要發(fā)送的幀內(nèi)容,然后找到電腦上的以太網(wǎng)卡,接下來(lái)利用SharpPcap來(lái)循環(huán)發(fā)送。
vs2008工程,全部源代碼,可編譯和測(cè)試。
以太網(wǎng)幀內(nèi)容可以自己構(gòu)造任意的幀類(lèi)型和內(nèi)容;也可以使用wireshark抓包,然后利用本軟件來(lái)重放。
wireshark抓包后取數(shù)據(jù)方法:在wireshark的收到的幀上點(diǎn)右鍵,選擇 復(fù)制 -> 為一個(gè)HEX流,然后粘貼到一個(gè)空文件內(nèi),命名為packet.dat,這樣本軟件就會(huì)讀取packet.dat內(nèi)的內(nèi)容并循環(huán)發(fā)送了。
vs2008工程,全部源代碼,可編譯和測(cè)試。
以太網(wǎng)幀內(nèi)容可以自己構(gòu)造任意的幀類(lèi)型和內(nèi)容;也可以使用wireshark抓包,然后利用本軟件來(lái)重放。
wireshark抓包后取數(shù)據(jù)方法:在wireshark的收到的幀上點(diǎn)右鍵,選擇 復(fù)制 -> 為一個(gè)HEX流,然后粘貼到一個(gè)空文件內(nèi),命名為packet.dat,這樣本軟件就會(huì)讀取packet.dat內(nèi)的內(nèi)容并循環(huán)發(fā)送了。

代碼片段和文件信息
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()//?獲取網(wǎng)卡方法
????????{
????????????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;//保存網(wǎng)口
????????????????????????//device?=?i;//找到的第一個(gè)網(wǎng)口
????????????????????????//deviceMac?=?adap.GetPhysicalAddress().ToString();//得到設(shè)備mac,后續(xù)設(shè)置iP地址等信息的時(shí)候需要
????????????????????????break;
????????????????????}
????????????????}
????????????}
????????????catch?(Exception?ex)
????????????{
????????????????Console.Out.WriteLine(“錯(cuò)誤:初始化沒(méi)有找到以太網(wǎng)卡“);
????????????????Console.Out.WriteLine(ex.Message?+?“\n“?+?ex.StackTrace);
????????????}
????????????return?netinterface;
????????}
????????static?NetworkInterface?getNetworkDevice(string?name)//?獲取指定網(wǎng)卡方法
????????{
????????????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;//保存網(wǎng)口
????????????????????????????break;
????????????????????????}
????????????????????}
????????????????}
????????????}
????????????catch?(Exception?ex)
????????????{
????????????????Console.Out.WriteLine(“錯(cuò)誤:初始化沒(méi)有找到以太網(wǎng)卡,“?+?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;
??????
?屬性????????????大小?????日期????時(shí)間???名稱(chēng)
-----------?---------??----------?-----??----
????..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.xm
?????文件??????72704??2013-01-14?19:59??LldpSend\bin\Debug\SharpPcap.dll
?????文件?????186730??2013-01-14?19:59??LldpSend\bin\Debug\SharpPcap.xm
?????文件?????196608??2013-01-14?19:59??LldpSend\dll\PacketDotNet.dll
?????文件?????476563??2013-01-14?19:59??LldpSend\dll\PacketDotNet.xm
?????文件??????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.xm
?????文件???????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個(gè)文件信息
評(píng)論
共有 條評(píng)論