在 pcap.net C# 中更改 ip
change ip in pcap.net C#
我想用 Pcap.net 更改 IP 地址
我找到了这段代码
我在 C# 中尝试此代码:
private Packet ChangePacketIp(Packet packet, string oldIpAddress, string newIpAddress)
{
try
{
EthernetLayer ethernet = (EthernetLayer)packet.Ethernet.ExtractLayer();
IpV4Layer ipV4Layer = (IpV4Layer)packet.Ethernet.IpV4.ExtractLayer();
DateTime packetTimestamp = packet.Timestamp;
if (packet.Ethernet.IpV4.Source.ToString() == oldIpAddress)
{
ipV4Layer.Source = new IpV4Address(newIpAddress);
ipV4Layer.HeaderChecksum = null;
}
else if (packet.Ethernet.IpV4.Destination.ToString() == oldIpAddress)
{
ipV4Layer.CurrentDestination = new IpV4Address(newIpAddress);
ipV4Layer.HeaderChecksum = null;
}
if (packet.Ethernet.IpV4.Protocol == IpV4Protocol.Tcp)
{
TcpLayer tcpLayer = (TcpLayer)packet.Ethernet.IpV4.Tcp.ExtractLayer();
tcpLayer.Checksum = null;
ILayer payload = packet.Ethernet.IpV4.Tcp.Payload.ExtractLayer();
return PacketBuilder.Build(packetTimestamp, ethernet, ipV4Layer, tcpLayer, payload);
}
else if (packet.Ethernet.IpV4.Protocol == IpV4Protocol.Udp)
{
UdpLayer udpLayer = (UdpLayer)packet.Ethernet.IpV4.Udp.ExtractLayer();
udpLayer.Checksum = null;
ILayer payload = packet.Ethernet.IpV4.Udp.Payload.ExtractLayer();
return PacketBuilder.Build(packetTimestamp, ethernet, ipV4Layer, udpLayer, payload);
}
else
{
return null;
}
}
catch
{
return null;
}
如果我想在按钮事件上使用它,我试试这个:
ChangePacketIp( xxxxx ,"example","example");
但是我应该用 xxxxx 替换什么???
它想要数据包,但我现在不知道
请帮忙
试试这个:
private Packet ChangePacketIp(Packet packet)
{
EthernetLayer ethernetLayer = ...
// Check whether the packet is ipv4 packet.
if (ethernetLayer.EtherType == EthernetType.IpV4)
{
DateTime packetTimestamp = ...
TransportLayer transportlayer = ...
ILayer datagramLayer = ...
IpV4Layer ipV4Layer = ...
if (packet.Ethernet.IpV4.Source.ToString() == OriginalIpAddress)
ipV4Layer.Source = new IpV4Address(NewIpAddress);
else if (packet.Ethernet.IpV4.Destination.ToString() == OriginalIpAddress)
ipV4Layer.CurrentDestination = new IpV4Address(NewIpAddress);
ipV4Layer.HeaderChecksum = null;
if (transportlayer == null)
return PacketBuilder.Build(packetTimestamp, ethernetLayer, ipV4Layer, datagramLayer);
else
{
transportlayer.Checksum = null;
ILayer payload = packet.Ethernet.IpV4.Transport.Payload.ExtractLayer();
return PacketBuilder.Build(packetTimestamp, ethernetLayer, ipV4Layer, transportlayer, payload);
}
}
else
return packet;
}
用法:
ChangePacketIp(packet, "1.1.1.1", "2.2.2.2")
我想用 Pcap.net 更改 IP 地址
我找到了这段代码
我在 C# 中尝试此代码:
private Packet ChangePacketIp(Packet packet, string oldIpAddress, string newIpAddress)
{
try
{
EthernetLayer ethernet = (EthernetLayer)packet.Ethernet.ExtractLayer();
IpV4Layer ipV4Layer = (IpV4Layer)packet.Ethernet.IpV4.ExtractLayer();
DateTime packetTimestamp = packet.Timestamp;
if (packet.Ethernet.IpV4.Source.ToString() == oldIpAddress)
{
ipV4Layer.Source = new IpV4Address(newIpAddress);
ipV4Layer.HeaderChecksum = null;
}
else if (packet.Ethernet.IpV4.Destination.ToString() == oldIpAddress)
{
ipV4Layer.CurrentDestination = new IpV4Address(newIpAddress);
ipV4Layer.HeaderChecksum = null;
}
if (packet.Ethernet.IpV4.Protocol == IpV4Protocol.Tcp)
{
TcpLayer tcpLayer = (TcpLayer)packet.Ethernet.IpV4.Tcp.ExtractLayer();
tcpLayer.Checksum = null;
ILayer payload = packet.Ethernet.IpV4.Tcp.Payload.ExtractLayer();
return PacketBuilder.Build(packetTimestamp, ethernet, ipV4Layer, tcpLayer, payload);
}
else if (packet.Ethernet.IpV4.Protocol == IpV4Protocol.Udp)
{
UdpLayer udpLayer = (UdpLayer)packet.Ethernet.IpV4.Udp.ExtractLayer();
udpLayer.Checksum = null;
ILayer payload = packet.Ethernet.IpV4.Udp.Payload.ExtractLayer();
return PacketBuilder.Build(packetTimestamp, ethernet, ipV4Layer, udpLayer, payload);
}
else
{
return null;
}
}
catch
{
return null;
}
如果我想在按钮事件上使用它,我试试这个:
ChangePacketIp( xxxxx ,"example","example");
但是我应该用 xxxxx 替换什么???
它想要数据包,但我现在不知道
请帮忙
试试这个:
private Packet ChangePacketIp(Packet packet)
{
EthernetLayer ethernetLayer = ...
// Check whether the packet is ipv4 packet.
if (ethernetLayer.EtherType == EthernetType.IpV4)
{
DateTime packetTimestamp = ...
TransportLayer transportlayer = ...
ILayer datagramLayer = ...
IpV4Layer ipV4Layer = ...
if (packet.Ethernet.IpV4.Source.ToString() == OriginalIpAddress)
ipV4Layer.Source = new IpV4Address(NewIpAddress);
else if (packet.Ethernet.IpV4.Destination.ToString() == OriginalIpAddress)
ipV4Layer.CurrentDestination = new IpV4Address(NewIpAddress);
ipV4Layer.HeaderChecksum = null;
if (transportlayer == null)
return PacketBuilder.Build(packetTimestamp, ethernetLayer, ipV4Layer, datagramLayer);
else
{
transportlayer.Checksum = null;
ILayer payload = packet.Ethernet.IpV4.Transport.Payload.ExtractLayer();
return PacketBuilder.Build(packetTimestamp, ethernetLayer, ipV4Layer, transportlayer, payload);
}
}
else
return packet;
}
用法:
ChangePacketIp(packet, "1.1.1.1", "2.2.2.2")