Creating my own TCP packet using Pcap.Net - packet is sent but never reach destination

918 Views Asked by At
 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);
    }

Hello Im using Pcap.Net library to create my own TCP packet like Syn/Fin/Ack with no additional information and I need to sand this packet without establishing of TCP connection. I just need to reach to my server port with this packet.

But with this construction of TCP packet it never reach to my server. But Im sure packet is send (tested by wireshark). So please help me with construction of these packets, Im not sure all parameters are right used.

0

There are 0 best solutions below