C#PCAP.NET如何发送一个有效的TCP请求?(C# PCAP.NET how to send

2019-09-26 06:26发布

我西港岛线尝试使用PCAP.NET从我的计算机发送TCP请求到我的服务器,但我没有看到请求到服务器?

从我的PC发送和Wireshare在这里展示

但不能看到我的服务器上的此TCP请求,请帮助。

这里是我的代码

using System;
using System.Collections.Generic;
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));
                if (device.Description != null)
                    Console.WriteLine(String.Format(" ({0})", device.Description));
                else
                    Console.WriteLine(" (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())
            {
                communicator.SendPacket(BuildTcpPacket());
            }
            Console.ReadKey();
        }

        private static Packet BuildTcpPacket()
        {
            EthernetLayer ethernetLayer =
                new EthernetLayer
                {
                    Source = new MacAddress("00:25:22:50:ef:74"),
                    Destination = new MacAddress("02:02:02:02:02:02"),
                    EtherType = EthernetType.None, // Will be filled automatically.
                };

            IpV4Layer ipV4Layer =
                new IpV4Layer
                {
                    Source = new IpV4Address("192.168.1.203"), // <- this is my LAN IP ADDRESS IS TRUE??
                    CurrentDestination = new IpV4Address("113.23.x.x"), // <-- THIS IS MY SERVER IPADDESS??
                    Fragmentation = IpV4Fragmentation.None,
                    HeaderChecksum = null, // Will be filled automatically.
                    Identification = 64,
                    Options = IpV4Options.None,
                    Protocol = null, // Will be filled automatically.
                    Ttl = 64,
                    TypeOfService = 0,
                };

            TcpLayer tcpLayer =
                new TcpLayer
                {
                    SourcePort = 9509,
                    DestinationPort = 80,
                    Checksum = null, // Will be filled automatically.
                    SequenceNumber = 100,
                    AcknowledgmentNumber = 50,
                    ControlBits = TcpControlBits.Acknowledgment,
                    Window = 100,
                    UrgentPointer = 0,
                    Options = TcpOptions.None,
                };

            PayloadLayer payloadLayer =
                new PayloadLayer
                {
                    Data = new Datagram(Encoding.ASCII.GetBytes("hello world")),
                };

            PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, tcpLayer);

            return builder.Build(DateTime.Now);
        }
    }
}

Answer 1:

我看不到你正在使用您的payloadLayer在PacketBuilder。

根据从Pcat.net样本,有效负载应被包括在PacketBuilder构造。

PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, tcpLayer, payloadLayer);

教程发送数据包



文章来源: C# PCAP.NET how to send a valid TCP request?
标签: c# tcp pcap.net