gpt4 book ai didi

c# - 在 Pcap.NET 中创建有效的 TCP 连接

转载 作者:行者123 更新时间:2023-11-30 14:31:09 25 4
gpt4 key购买 nike

我正在尝试创建到我的测试服务器的有效 TCP 连接以模拟 GET 请求。希望我非常接近解决方案。

使用来自真实请求的 Wireshark 数据,在使用 PacketCommunicator.SendPacket() 发送一个包时,我设法获得了 SYNSYN-ACK :

enter image description here

但是,我似乎无法让 ACK 出现。

第一个数据包是使用 EthernetLayerIpV4Layer 和一个 TcpLayer 构建的,而后者(目前不工作)只使用那些提到 + 一个 HttpRequestLayer

最后一个数据包 TcpLayerControlBits 设置为 TcpControlBits.Acknowledgment。尽管如此,它并没有像我的“真实”GET 请求那样出现在 WireShark 中。

希望我已经发布了代码的相关部分。如果没有 - 请告诉我。

最佳答案

这是一份有效的草稿(我已经检查过)。

请将构造 HttpGetSender 实例时使用的值更改为您需要的任何值。

棘手的部分是获得正确的序列号和确认号。

using System;
using System.Collections.Generic;
using PcapDotNet.Core;
using PcapDotNet.Packets;
using PcapDotNet.Packets.Ethernet;
using PcapDotNet.Packets.Http;
using PcapDotNet.Packets.IpV4;
using PcapDotNet.Packets.Transport;

namespace SendingHttpGet
{
internal class HttpGetSender
{
public HttpGetSender()
{
}

public MacAddress SourceMac { get; set; }
public MacAddress DestinationMac { get; set; }
public IpV4Address SourceIpV4 { get; set; }
public IpV4Address DestinationIpV4 { get; set; }
public string Host { get; set; }

public void Run(PacketDevice device)
{
using (PacketCommunicator communicator = device.Open(100, // name of the device
PacketDeviceOpenAttributes.Promiscuous,
// promiscuous mode
100)) // read timeout
{
SendSyn(communicator);
WaitForAck(communicator);
}
}

private void WaitForAck(PacketCommunicator communicator)
{
communicator.SetFilter("tcp and src " + DestinationIpV4 + " and dst " + SourceIpV4 + " and src port " + _destinationPort + " and dst port " + _sourcePort);
Packet packet;
while (true)
{
if (communicator.ReceivePacket(out packet) == PacketCommunicatorReceiveResult.Ok)
{
if (packet.Ethernet.IpV4.Tcp.AcknowledgmentNumber == _expectedAckNumber)
{
_seqNumber = _expectedAckNumber;
_ackNumber = packet.Ethernet.IpV4.Tcp.SequenceNumber + 1;
SendGet(communicator);
break;
}

}
SendSyn(communicator);
}
WaitForResponse(communicator);
}

private void WaitForResponse(PacketCommunicator communicator)
{
communicator.SetFilter("tcp and src " + DestinationIpV4 + " and dst " + SourceIpV4 + " and src port " + _destinationPort + " and dst port " + _sourcePort);
Packet packet;
while (true)
{
if (communicator.ReceivePacket(out packet) == PacketCommunicatorReceiveResult.Ok)
{
Console.WriteLine("Expected ack number: " + _expectedAckNumber);
Console.WriteLine("Received ack number: " + packet.Ethernet.IpV4.Tcp.AcknowledgmentNumber);
if (packet.Ethernet.IpV4.Tcp.AcknowledgmentNumber == _expectedAckNumber)
{
break;
}

}
SendGet(communicator);
}
}

private void SendSyn(PacketCommunicator communicator)
{
// Ethernet Layer
EthernetLayer ethernetLayer = new EthernetLayer
{
Source = SourceMac,
Destination = DestinationMac,
};

// IPv4 Layer
IpV4Layer ipV4Layer = new IpV4Layer
{
Source = SourceIpV4,
CurrentDestination = DestinationIpV4,
Ttl = 128,
Fragmentation =
new IpV4Fragmentation(IpV4FragmentationOptions.DoNotFragment, 0),
Identification = 1234,
};

// TCP Layer
TcpLayer tcpLayer = new TcpLayer
{
SourcePort = _sourcePort,
DestinationPort = _destinationPort,
SequenceNumber = _seqNumber,
ControlBits = TcpControlBits.Synchronize,
Window = _windowSize,
};

communicator.SendPacket(PacketBuilder.Build(DateTime.Now, ethernetLayer, ipV4Layer, tcpLayer));
_expectedAckNumber = _seqNumber + 1;
}

private void SendGet(PacketCommunicator communicator)
{
// Ethernet Layer
EthernetLayer ethernetLayer = new EthernetLayer
{
Source = SourceMac,
Destination = DestinationMac,
};

// IPv4 Layer
IpV4Layer ipV4Layer = new IpV4Layer
{
Source = SourceIpV4,
CurrentDestination = DestinationIpV4,
Ttl = 128,
Fragmentation =
new IpV4Fragmentation(IpV4FragmentationOptions.DoNotFragment, 0),
Identification = 1235,
};

// TCP Layer
TcpLayer tcpLayer = new TcpLayer
{
SourcePort = _sourcePort,
DestinationPort = _destinationPort,
SequenceNumber = _seqNumber,
AcknowledgmentNumber = _ackNumber,
ControlBits = TcpControlBits.Acknowledgment,
Window = _windowSize,
};

// HTTP Layer
HttpLayer httpLayer = new HttpRequestLayer
{
Uri = "/",
Header = new HttpHeader(HttpField.CreateField("Host", Host)),
Method = new HttpRequestMethod(HttpRequestKnownMethod.Get),
Version = HttpVersion.Version11,
};

Packet packet = PacketBuilder.Build(DateTime.Now, ethernetLayer, ipV4Layer, tcpLayer, httpLayer);
communicator.SendPacket(packet);
_expectedAckNumber = (uint) (_seqNumber + packet.Ethernet.IpV4.Tcp.PayloadLength);
}

private ushort _sourcePort = (ushort) (4123 + new Random().Next() % 1000);
private ushort _destinationPort = 80;
private uint _seqNumber = (uint) new Random().Next();
private uint _expectedAckNumber;
private ushort _windowSize = 8192;
private uint _ackNumber;
}

internal class Program
{
private static void Main(string[] args)
{
// Retrieve the device list from the local machine
IList<LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine;

if (allDevices.Count == 0)
{
Console.WriteLine("No interfaces found! Make sure WinPcap is installed.");
return;
}

// Print the list
for (int i = 0; i != allDevices.Count; ++i)
{
LivePacketDevice device = allDevices[i];
Console.Write((i + 1) + ". " + device.Name);
if (device.Description != null)
Console.WriteLine(" (" + device.Description + ")");
else
Console.WriteLine(" (No description available)");
}

int deviceIndex = 0;
do
{
Console.WriteLine("Enter the interface number (1-" + allDevices.Count + "):");
string deviceIndexString = Console.ReadLine();
if (!int.TryParse(deviceIndexString, out deviceIndex) ||
deviceIndex < 1 || deviceIndex > allDevices.Count)
{
deviceIndex = 0;
}
} while (deviceIndex == 0);

// Take the selected adapter
PacketDevice selectedDevice = allDevices[deviceIndex - 1];

HttpGetSender sender = new HttpGetSender
{
SourceMac = new MacAddress("your:host:mac:address:1:2"),
DestinationMac = new MacAddress("gateway:mac:address:1:2:3"),
SourceIpV4 = new IpV4Address("your.host.ip.address"),
DestinationIpV4 = new IpV4Address("target.host.ip.address"),
Host = "targethost.com",
};

sender.Run(selectedDevice);
}
}
}

关于c# - 在 Pcap.NET 中创建有效的 TCP 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21102104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com