我將使用Pcap.Net 0.10.0(67076)發送TCP或HTTP數據包但不工作,只能使用UDP數據包?我可以看到UDP數據包顯示在TCPViewer中,但現在顯示爲TCP或HTTP數據包。C#Pcap.Net發送TCP,HTTP數據包不工作
我使用Windows 7 64位,但我真的構建應用程序的x86平臺。
這是我的代碼。
using System;
using System.Collections.Generic;
using System.Linq;
using PcapDotNet.Base;
using PcapDotNet.Core;
using PcapDotNet.Packets;
using PcapDotNet.Packets.Arp;
using PcapDotNet.Packets.Dns;
using PcapDotNet.Packets.Ethernet;
using PcapDotNet.Packets.Gre;
using PcapDotNet.Packets.Http;
using PcapDotNet.Packets.Icmp;
using PcapDotNet.Packets.Igmp;
using PcapDotNet.Packets.IpV4;
using PcapDotNet.Packets.Transport;
using System.Text;
namespace PcapTest
{
class Program
{
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(String.Format("{0}. {1}", (i + 1), device.Name));
Console.WriteLine(device.Description != null ? String.Format(" ({0})", device.Description) : " (No description available)");
}
int deviceIndex = 0;
do
{
Console.WriteLine(String.Format("Enter the interface number (1-{0}):", 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];
// Open the output device
using (PacketCommunicator communicator = selectedDevice.Open(69559, PacketDeviceOpenAttributes.Promiscuous, 1000)) // read timeout
{
communicator.SendPacket(BuildHttpPacket());
}
Console.ReadKey();
}
private static Packet BuildHttpPacket()
{
EthernetLayer ethernetLayer =
new EthernetLayer
{
Source = new MacAddress("01:01:01:01:01:01"),
Destination = new MacAddress("02:02:02:02:02:02"),
EtherType = EthernetType.None, // Will be filled automatically.
};
IpV4Layer ipV4Layer =
new IpV4Layer
{
Source = new IpV4Address("99.99.99.99"),
CurrentDestination = new IpV4Address("111.90.147.168"),
Fragmentation = IpV4Fragmentation.None,
HeaderChecksum = null, // Will be filled automatically.
Identification = 123,
Options = IpV4Options.None,
Protocol = null, // Will be filled automatically.
Ttl = 100,
TypeOfService = 0,
};
TcpLayer tcpLayer =
new TcpLayer
{
SourcePort = 4050,
DestinationPort = 80,
Checksum = null, // Will be filled automatically.
SequenceNumber = 100,
AcknowledgmentNumber = 50,
ControlBits = TcpControlBits.Acknowledgment,
Window = 100,
UrgentPointer = 0,
Options = TcpOptions.None,
};
HttpRequestLayer httpLayer =
new HttpRequestLayer
{
Version = HttpVersion.Version11,
Header = new HttpHeader(new HttpContentLengthField(11)),
Body = new Datagram(Encoding.ASCII.GetBytes("hello world")),
Method = new HttpRequestMethod(HttpRequestKnownMethod.Get),
Uri = @"http://pcapdot.net/",
};
PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, tcpLayer, httpLayer);
return builder.Build(DateTime.Now);
}
}
}
你能使用Wireshark,看看當你運行該代碼,並認爲你希望得到什麼包你得到了什麼包?請記住,Pcap.Net目前只能用於創建單個數據包,因此如果要創建TCP數據流,則需要處理所有TCP 3次握手以及下列數據包中的正確序列號和確認號。 – brickner
感謝您的回覆@brickner,我使用我的IP地址(真IP)並使用pcapdotnet在端口80上的VPS服務器上創建TCP數據包請求!我可以在wireshark上看到請求,但我在服務器上看不到此請求。 – vietnguyen09