我寫了一個UDP端口掃描器。我發送一個數據包到同一網絡中的另一臺設備,但在這裏有一點問題,爲什麼wireshark只收到一個數據包,如果我在不同的端口上發送10個數據包(每個端口都關閉)。我應該收到10個類型爲ICMP的packects:3.我應該在我的代碼中更改哪些內容以獲取這些內容?端口掃描器 - icmp數據包
using PcapDotNet.Core;
using PcapDotNet.Packets;
using PcapDotNet.Packets.Ethernet;
using PcapDotNet.Packets.IpV4;
using PcapDotNet.Packets.Transport;
using System;
using System.Collections.Generic;
namespace SendingASinglePacketWithSendPacket
{
class Program
{
static void Main(string[] args)
{
IList<LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine;
PacketDevice selectedDevice = allDevices[2];
for (int i = 1; i < 10; i++)
{
new Sender().SendUDPandGetStatus(selectedDevice, (ushort)i);
}
System.Console.ReadKey();
}
}
class Sender
{
public void SendUDPandGetStatus(PacketDevice selectedDevice, ushort port)
{
using (PacketCommunicator communicator = selectedDevice.Open(100, PacketDeviceOpenAttributes.Promiscuous, -1))
{
communicator.SendPacket(BuildUdpPacket(port));
}
}
private static Packet BuildUdpPacket(ushort destinationPort)
{
EthernetLayer ethernetLayer = new EthernetLayer
{
Destination = new MacAddress("14:cc:20:2c:7e:36"),
Source = new MacAddress("9C:4E:36:17:86:48"),
EtherType = EthernetType.None,
};
IpV4Layer ipV4Layer =new IpV4Layer
{
Source = new IpV4Address("192.168.0.104"),
CurrentDestination = new IpV4Address("192.168.0.105"),
Fragmentation = IpV4Fragmentation.None,
HeaderChecksum = null,
Identification = 123,
Options = IpV4Options.None,
Ttl = 30,
TypeOfService = 0,
};
UdpLayer udpLayer = new UdpLayer
{
SourcePort = 4050,
DestinationPort = destinationPort,
Checksum = null,
CalculateChecksumValue = true,
};
PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, udpLayer);
return builder.Build(DateTime.Now);
}
}
}
如果我把斷點行我只想補充:
new Sender().SendUDPandGetStatus(selectedDevice, (ushort)i);
,推動F5鍵(間隔5秒),得到正確的結果。
ICMP是一個ping消息,如果你有一個路由循環將被封鎖。嘗試從cmd.exe> PING 192.168.0.104使用並查看響應。檢查生存時間以指示有多少路由器消息通過。 – jdweng
你可能是對的,但在我的情況下,我有兩臺電腦連接到同一個路由器。我檢查了tracert命令和更高的TTL值 - 相同。我認爲問題在於:「Linux 2.4.20內核將目標不可達消息限制爲每秒一次」。如果我添加「System.Threading.Thread.Sleep(1000);」在for循環中,問題消失。我可以推斷在Windows上的限制是相同的在Linux上。 –
獲取IP地址和掩碼並確保您有路由。您可能需要ping每個子網中的設備,以查看可以訪問哪些設備。還要轉到目標設備和ping源設備。我懷疑你有一條路向一個方向,而不是返回路線。 – jdweng