0
 private Packet BuildTcpPacket(string SourceIP, string DestIP, int sourceport, int destport,
        string payload, bool syn, bool ack, bool fin, bool rst, bool urg, bool psh, bool cwr, bool enc, bool ns, bool none)
    {
        EthernetLayer ethernetLayer = new EthernetLayer
        {
            Source = new MacAddress(tbMacVs.Text),  //doublechecked its OK
            Destination = new MacAddress(tbMacVy.Text), //OK
            EtherType = EthernetType.None, // Will be filled automatically.
        };

        IpV4Layer ipV4Layer = new IpV4Layer
        {
            Source = new IpV4Address(SourceIP), //OK
            CurrentDestination = new IpV4Address(DestIP),  //OK
            Fragmentation = IpV4Fragmentation.None,
            HeaderChecksum = null, // Will be filled automatically.
            Identification = (ushort)Convert.ToInt16("1000"), //1000
            Options = IpV4Options.None,
            Protocol = null, // Will be filled automatically.
            Ttl = (byte)Convert.ToInt16("64"), //64
            TypeOfService = 0,
        };

        TcpLayer tcpLayer = new TcpLayer();
        tcpLayer.SourcePort = (ushort)sourceport; //OK
        tcpLayer.DestinationPort = (ushort)destport; //OK
        tcpLayer.Checksum = null;// Will be filled automatically.
        tcpLayer.SequenceNumber = Convert.ToUInt16("124");
        tcpLayer.AcknowledgmentNumber = (ushort)Convert.ToUInt16("0");
        if (syn)
            tcpLayer.ControlBits = TcpControlBits.Synchronize;
        if (ack)
            tcpLayer.ControlBits = TcpControlBits.Acknowledgment;
        //if (fin)
        //    tcpLayer.ControlBits = TcpControlBits.Fin;
        //if (rst)
        //    tcpLayer.ControlBits = TcpControlBits.Reset;
        //if (urg)
        //    tcpLayer.ControlBits = TcpControlBits.Urgent;
        //if (psh)
        //    tcpLayer.ControlBits = TcpControlBits.Push;
        //if (cwr)
        //    tcpLayer.ControlBits = TcpControlBits.CongestionWindowReduced;
        //if (enc)
        //    tcpLayer.ControlBits = TcpControlBits.ExplicitCongestionNotificationEcho;
        //if (ns)
        //    tcpLayer.ControlBits = TcpControlBits.NonceSum; // not a flag, rather a tcp header bit set
        //if (none)
        //    tcpLayer.ControlBits = TcpControlBits.None;


        tcpLayer.Window = (ushort)Convert.ToUInt16("0");
        tcpLayer.UrgentPointer = 0;
        tcpLayer.Options = TcpOptions.None;

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

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

        return builder.Build(DateTime.Now);
    }

您好,我正在使用 Pcap.Net 库创建我自己的 TCP 数据包,如 Syn/Fin/Ack,没有其他信息,我需要在不建立 TCP 连接的情况下打磨这个数据包。我只需要使用这个数据包访问我的服务器端口。

但是通过这种 TCP 数据包的构造,它永远不会到达我的服务器。但我确定数据包已发送(由wireshark测试)。所以请帮助我构建这些数据包,我不确定所有参数都正确使用。

4

0 回答 0