C# PCAP.NET 如何发送有效的 TCP 请求? [英] C# PCAP.NET how to send a valid TCP request?

查看:34
本文介绍了C# PCAP.NET 如何发送有效的 TCP 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

从我的电脑发送并在此处显示在 Wireshare 中

但是在我的服务器上看不到这个 TCP 请求,请帮忙.

这是我的代码

使用系统;使用 System.Collections.Generic;使用 PcapDotNet.Base;使用 PcapDotNet.Core;使用 PcapDotNet.Packets;使用 PcapDotNet.Packets.Arp;使用 PcapDotNet.Packets.Dns;使用 PcapDotNet.Packets.Ethernet;使用 PcapDotNet.Packets.Gre;使用 PcapDotNet.Packets.Http;使用 PcapDotNet.Packets.Icmp;使用 PcapDotNet.Packets.Igmp;使用 PcapDotNet.Packets.IpV4;使用 PcapDotNet.Packets.Transport;使用 System.Text;命名空间 PcapTest{课程计划{static void Main(string[] args){//从本地机器检索设备列表IListallDevices = LivePacketDevice.AllLocalMachine;如果(allDevices.Count == 0){Console.WriteLine("没有找到接口!确保安装了WinPcap.");返回;}//打印列表for (int i = 0; i != allDevices.Count; ++i){LivePacketDevice 设备 = allDevices[i];Console.Write(String.Format("{0}.{1}", (i + 1), device.Name));if (device.Description != null)Console.WriteLine(String.Format(" ({0})", device.Description));别的Console.WriteLine(" (无描述)");}int deviceIndex = 0;做{Console.WriteLine(String.Format("请输入接口编号(1-{0}):", allDevices.Count));string deviceIndexString = Console.ReadLine();if (!int.TryParse(deviceIndexString, out deviceIndex) ||设备索引<1 ||设备索引>allDevices.Count){设备索引 = 0;}} while (deviceIndex == 0);//获取选定的适配器PacketDevice selectedDevice = allDevices[deviceIndex - 1];//打开输出设备使用 (PacketCommunicator 通信器 = selectedDevice.Open()){通信器.SendPacket(BuildTcpPacket());}Console.ReadKey();}私有静态数据包 BuildTcpPacket(){以太网层 以太网层 =新以太网层{Source = new MacAddress("00:25:22:50:ef:74"),Destination = new MacAddress("02:02:02:02:02:02"),EtherType = EthernetType.None,//会自动填充.};ipV4Layer ipV4Layer =新的 IpV4Layer{Source = new IpV4Address("192.168.1.203"),//<- 这是我的 LAN IP 地址是真的吗??CurrentDestination = new IpV4Address("113.23.x.x"),//<-- 这是我的服务器 IPADDESS??碎片 = IpV4Fragmentation.None,HeaderChecksum = null,//会自动填充.标识 = 64,选项 = IpV4Options.None,Protocol = null,//会自动填充.ttl = 64,服务类型 = 0,};TcpLayer tcpLayer =新的 TCP 层{源端口 = 9509,目的地端口 = 80,Checksum = null,//会自动填充.序号 = 100,确认编号 = 50,ControlBits = TcpControlBits.Acknowledgment,窗口 = 100,紧急指针 = 0,选项 = TcpOptions.None,};有效载荷层有效载荷层 =新的有效载荷层{Data = new Datagram(Encoding.ASCII.GetBytes("hello world")),};PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, tcpLayer);返回 builder.Build(DateTime.Now);}}}

解决方案

我看不到您在 PacketBuilder 中使用了 payloadLayer.

根据来自 Pcat.net 的示例,有效载荷应包含在 PacketBuilder 构造函数中.

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

发送数据包教程

I wil try using PCAP.NET to send TCP Request from my computer to my server but i don't see request into the server?

Send from my PC and showing in Wireshare here

But can't see this TCP Request on my server, please help.

here is my code

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);
        }
    }
}

解决方案

I can't see that you are using your payloadLayer in the PacketBuilder.

According to a sample from Pcat.net, the payload should be included in the PacketBuilder constructor.

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

Tutorial sending packets

这篇关于C# PCAP.NET 如何发送有效的 TCP 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆