예제 #1
0
        /* here we search through all the frag we have collected to see if
         * one fits */
        bool check_fragments(int index, PacketDotNet.UdpPacket udpPacket)
        {
            Udpfrag prev = null;
            Udpfrag current;

            current = frags[index];
            while (current != null)
            {
                /* this fragment fits the stream */
                if (current.data != null)
                {
                    write_packet_data(index, current.data, udpPacket);
                }

                if (prev != null)
                {
                    prev.next = current.next;
                }
                else
                {
                    frags[index] = current.next;
                }
                current.data = null;
                current      = null;
                return(true);

                prev    = current;
                current = current.next;
            }
            return(false);
        }
예제 #2
0
 private static void AnalyzePacket_Udp(PacketDotNet.UdpPacket packet, PacketAnalyzeParam param)
 {
     param.Protocol        = "UDP";
     param.SourcePort      = packet.SourcePort;
     param.DestinationPort = packet.DestinationPort;
     param.PayloadFrame    = packet.PayloadData;
 }
예제 #3
0
        private void reassemble_udp(ulong length, byte[] data,
                                    ulong data_length, long net_src,
                                    long net_dst, uint srcport, uint dstport, PacketDotNet.UdpPacket udpPacket)
        {
            long    srcx, dstx;
            int     src_index, j;
            bool    first = false;
            ulong   newseq;
            Udpfrag tmp_frag;

            src_index = -1;

            /* Now check if the packet is for this connection. */
            srcx = net_src;
            dstx = net_dst;

            // Check to see if we have seen this source IP and port before.
            // Note: We have to check both source IP and port, the connection
            // might be between two different ports on the same machine.
            for (j = 0; j < 2; j++)
            {
                if (src_addr[j] == srcx && src_port[j] == srcport)
                {
                    src_index = j;
                }
            }

            // We didn't find it if src_index == -1.
            if (src_index < 0)
            {
                // Assign it to a src_index and get going.
                for (j = 0; j < 2; j++)
                {
                    if (src_port[j] == 0)
                    {
                        src_addr[j] = srcx;
                        src_port[j] = srcport;
                        src_index   = j;
                        first       = true;
                        break;
                    }
                }
            }

            if (src_index < 0)
            {
                throw new Exception("ERROR in reassemble_udp: Too many addresses!");
            }

            if (data_length < length)
            {
                incomplete_udp_stream = true;
            }

            /* write out the packet data */
            write_packet_data(src_index, data, udpPacket);
        }
예제 #4
0
        /// <summary>
        /// Writes the payload data to the file
        /// </summary>
        /// <param name="index"></param>
        /// <param name="data"></param>
        private void write_packet_data(int index, byte[] i_data, PacketDotNet.UdpPacket udpPacket)
        {
            // Add packet to packets list.
            this.packets.Add(udpPacket);

            // ignore empty packets.
            if (i_data.Length == 0)
            {
                return;
            }

            data = Combine(this.data, i_data);
            bytes_written[index] += (uint)i_data.Length;
            EmptyStream           = false;
        }
예제 #5
0
        public void HandlePacket(PacketDotNet.UdpPacket udpPacket)
        {
            var session = new UdpSession()
            {
                SourceIp        = ((PacketDotNet.IPPacket)udpPacket.ParentPacket).SourceAddress.ToString(),
                SourcePort      = udpPacket.SourcePort,
                DestinationIp   = ((PacketDotNet.IPPacket)udpPacket.ParentPacket).DestinationAddress.ToString(),
                DestinationPort = udpPacket.DestinationPort
            };

            if (!_sessions.ContainsKey(session))
            {
                UdpRecon recon = new UdpRecon();
                _sessions.Add(session, recon);
            }

            _sessions[session].ReassemblePacket(udpPacket);
        }
예제 #6
0
        /// <summary>
        /// The main function of the class receives a tcp packet and reconstructs the stream
        /// </summary>
        /// <param name="tcpPacket"></param>
        public void ReassemblePacket(PacketDotNet.UdpPacket udpPacket)
        {
            // if the paylod length is zero bail out
            ulong length = (ulong)(udpPacket.Bytes.Length - udpPacket.HeaderData.Length);

            if (length == 0)
            {
                return;
            }

            reassemble_udp(
                length,
                udpPacket.PayloadData,
                (ulong)udpPacket.PayloadData.Length,
                IpAddressToLong(udpPacket.ParentPacket.Extract <PacketDotNet.IPPacket>().SourceAddress.ToString()),
                IpAddressToLong(udpPacket.ParentPacket.Extract <PacketDotNet.IPPacket>().DestinationAddress.ToString()),
                (uint)udpPacket.SourcePort,
                (uint)udpPacket.DestinationPort,
                udpPacket);
        }
예제 #7
0
        private void Send()
        {
            while (true)
            {
                string input = TXData.Take();
                if (input == "")
                {
                    continue;
                }
                var Timestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
                var MD5Sum    = BitConverter.ToString(sha256Ctx.ComputeHash(System.Text.Encoding.Default.GetBytes(input + rand.Next(0, 65536).ToString()))).Replace("-", "");

                Stack <string> PiecedMsg = new Stack <string>();
                while (input.Length > 0)
                {
                    if (input.Length <= 5)
                    {
                        PiecedMsg.Push(input);
                        input = "";
                    }
                    else
                    {
                        var cutlen = rand.Next(0, input.Length);
                        PiecedMsg.Push(input.Substring(0, cutlen));
                        input = input.Substring(cutlen);
                    }
                }
                var PacketTotal = PiecedMsg.Count;

                while (PiecedMsg.Count > 0)
                {
                    IPAddress SrcIP, DstIP;
                    switch (rand.Next(0, 2))
                    {
                    case 0:
                        if (IPv6SrcList.Count != 0 && IPv6DstList.Count != 0)
                        {
                            DstIP = IPv6DstList[rand.Next(0, IPv6DstList.Count)];
                            SrcIP = IPv6SrcList[rand.Next(0, IPv6SrcList.Count)];
                        }
                        else
                        {
                            DstIP = IPv4DstList[rand.Next(0, IPv4DstList.Count)];
                            SrcIP = IPv4SrcList[rand.Next(0, IPv4SrcList.Count)];
                        }
                        break;

                    default:
                        if (IPv4SrcList.Count != 0 && IPv4DstList.Count != 0)
                        {
                            DstIP = IPv4DstList[rand.Next(0, IPv4DstList.Count)];
                            SrcIP = IPv4SrcList[rand.Next(0, IPv4SrcList.Count)];
                        }
                        else
                        {
                            DstIP = IPv6DstList[rand.Next(0, IPv6DstList.Count)];
                            SrcIP = IPv6SrcList[rand.Next(0, IPv6SrcList.Count)];
                        }
                        break;
                    }
                    var TXProtocolPacket = new ProtocolPacket {
                        PacketTimestamp = Timestamp, PacketTotal = PacketTotal, PacketMD5Sum = MD5Sum, PacketCount = PiecedMsg.Count, SrcIP = SrcIP.ToString(), DstIP = DstIP.ToString()
                    };
                    TXProtocolPacket.PiecedMsg = PiecedMsg.Pop();
                    var    udpPacket = new PacketDotNet.UdpPacket((ushort)rand.Next(1, 65536), (ushort)rand.Next(1, 65536));
                    long   nonceTime = (DateTimeOffset.Now.ToUnixTimeSeconds() / 300) * 300;
                    byte[] nonce     = sha256Ctx.ComputeHash(System.Text.Encoding.Default.GetBytes(nonceTime.ToString())).Take(8).ToArray();
                    var    NSecKey   = Key.Import(AeadAlgorithm.ChaCha20Poly1305, key, KeyBlobFormat.RawSymmetricKey);
                    var    NSecNonce = new Nonce(nonce, 4);
                    udpPacket.PayloadData = AeadAlgorithm.ChaCha20Poly1305.Encrypt(NSecKey, NSecNonce, null, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(TXProtocolPacket)));
                    if (DstIP.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                    {
                        var ipv4Packet = new PacketDotNet.IPv4Packet(SrcIP, DstIP);
                        ipv4Packet.PayloadPacket = udpPacket;
                        udpPacket.UpdateCalculatedValues();
                        udpPacket.UpdateUDPChecksum();
                        ipv4Packet.UpdateCalculatedValues();
                        txBuffer.Add(new DivertPacket {
                            Addr = new WINDIVERT_ADDRESS {
                                Direction = 0
                            }, Data = ipv4Packet.Bytes
                        });
                    }
                    if (DstIP.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
                    {
                        var ipv6Packet = new PacketDotNet.IPv6Packet(SrcIP, DstIP);
                        ipv6Packet.NextHeader    = PacketDotNet.IPProtocolType.UDP;
                        ipv6Packet.PayloadPacket = udpPacket;
                        udpPacket.UpdateCalculatedValues();
                        udpPacket.UpdateUDPChecksum();
                        ipv6Packet.UpdateCalculatedValues();
                        txBuffer.Add(new DivertPacket {
                            Addr = new WINDIVERT_ADDRESS {
                                Direction = 0
                            }, Data = ipv6Packet.Bytes
                        });
                    }
                }
            }
        }
예제 #8
0
        private void device_OnPacketArrival(object sender, CaptureEventArgs e)
        {
            var time = e.Packet.Timeval.Date;
            var len  = e.Packet.Data.Length;

            var pack = PacketDotNet.Packet.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data);

            if (pack is PacketDotNet.EthernetPacket)
            {
                PacketDotNet.EthernetPacket ethPack = pack as PacketDotNet.EthernetPacket;
                if (ethPack.PayloadPacket is PacketDotNet.IPv4Packet)
                {
                    PacketDotNet.IPv4Packet ipPack = ethPack.PayloadPacket as PacketDotNet.IPv4Packet;
                    //TCP
                    if (ipPack.PayloadPacket is PacketDotNet.TcpPacket)
                    {
                        PacketDotNet.TcpPacket tcpPack = ipPack.PayloadPacket as PacketDotNet.TcpPacket;
                        if ((ipPack.DestinationAddress.ToString().Equals("172.25.25.50") && tcpPack.DestinationPort == 8001) ||
                            (ipPack.SourceAddress.ToString().Equals("172.25.25.50") && tcpPack.SourcePort == 8001))
                        {
                            //Console.WriteLine("TCP:{0}:{1}-{2}:{3}",ipPack.SourceAddress, tcpPack.SourcePort,ipPack.DestinationAddress,tcpPack.DestinationPort);
                            if (tcpPack.PayloadData != null && tcpPack.PayloadData.Length > 0)
                            {
                                Console.WriteLine("读取数据:{0}", System.Text.Encoding.UTF8.GetString(tcpPack.PayloadData));
                            }
                        }
                    }
                    //UDP
                    else if (ipPack.PayloadPacket is PacketDotNet.UdpPacket)
                    {
                        PacketDotNet.UdpPacket udp = ipPack.PayloadPacket as PacketDotNet.UdpPacket;
                        if (ipPack.DestinationAddress.ToString().Equals("172.25.25.69") && udp.DestinationPort == 5060)
                        {
                            if (udp.PayloadData != null && udp.PayloadData.Length > 0)
                            {
                                Console.WriteLine("读取数据:{0}", System.Text.Encoding.UTF8.GetString(udp.PayloadData));
                            }
                        }
                        //if (ipPack.DestinationAddress.ToString().Equals("172.25.25.69") && udp.DestinationPort == 18038)
                        if (ipPack.DestinationAddress.ToString().Equals("172.25.25.66") && udp.DestinationPort == 18132)
                        {
                            if (udp.PayloadData != null && udp.PayloadData.Length > 100)
                            {
                                RtpPacket rtpPacket  = new RtpPacket(udp.PayloadData);
                                RtpHeader rtpHeader  = new RtpHeader(udp.PayloadData);
                                int       packetRate = RTPPayloadTypes.GetSamplingFrequency((RTPPayloadTypesEnum)Enum.ToObject(typeof(RTPPayloadTypesEnum), rtpHeader.PayloadType)); //8000

                                int minSec = ((int)rtpHeader.Timestamp - _preTimestamp - _prePacketLength) / (packetRate / 1000);
                                _preTimestamp    = (int)rtpHeader.Timestamp;
                                _prePacketLength = rtpPacket.Payload.Length;

                                //写文件
                                string fileName = "F:\\" + "testRtp9" + ".wav";
                                PCMU   m_PCMU   = new PCMU();
                                //语音包
                                using (System.IO.FileStream fs = new System.IO.FileStream(fileName,
                                                                                          System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.None))
                                {
                                    byte[] temp = null; //空白语音
                                    byte[] dec  = null; //payload荷载数据
                                    byte[] data = null; //完整数据
                                    //空白
                                    if (minSec > 0)
                                    {
                                        //temp = new byte[16 * minSec];
                                        //dec = m_PCMU.Decode_pcm8(rtpPacket.Payload, 0, rtpPacket.Payload.Length);

                                        //data = new byte[temp.Length + dec.Length];
                                        //Array.Copy(temp, 0, data, 0, temp.Length);
                                        //Array.Copy(dec, 0, data, temp.Length, dec.Length);

                                        temp = new byte[8 * minSec];
                                        for (int i = 0; i < temp.Length; i++)
                                        {
                                            temp[i] = 0xFE;
                                        }
                                        dec = rtpPacket.Payload;

                                        data = new byte[temp.Length + dec.Length];

                                        Array.Copy(temp, 0, data, 0, temp.Length);
                                        Array.Copy(dec, 0, data, temp.Length, dec.Length);
                                        data = m_PCMU.Decode_ulaw_pcm8(data, 0, data.Length);
                                    }
                                    else
                                    {
                                        data = m_PCMU.Decode_ulaw_pcm8(rtpPacket.Payload, 0, rtpPacket.Payload.Length);
                                    }

                                    fs.Position = fs.Length;

                                    fs.Write(data, 0, data.Length);
                                }
                            }
                        }
                    }
                }
            }
        }
예제 #9
0
        private void ui_populate_packet_view()
        {
            // There's a race condition here, but imo not worth changing the architecture to fix yet.
            if (cFileUtilities.get_size(_backend_file) == _backend_size)
            {
                return;
            }

            _backend_size = cFileUtilities.get_size(_backend_file);

            ICaptureDevice pcap_device = null;

            bool has_temp_file_open = false;

            string temp_file = "";

            _packet_bytes.Clear();

            try
            {
                temp_file = cFileUtilities.get_temp_copy(_backend_file);

                has_temp_file_open = true;

                pcap_device = new SharpPcap.LibPcap.CaptureFileReaderDevice(temp_file);
                pcap_device.Open();

                RawCapture capture;
                int        current_packet = 0;

                while ((capture = pcap_device.GetNextPacket()) != null)
                {
                    var eth_packet = PacketDotNet.Packet.ParsePacket(capture.LinkLayerType, capture.Data);

                    PacketDotNet.IPPacket ip_packet = eth_packet.Extract <PacketDotNet.IPPacket>();
                    if (_stream_type == "tcp")
                    {
                        PacketDotNet.TcpPacket tcp_packet = eth_packet.Extract <PacketDotNet.TcpPacket>();
                        _packet_bytes.Add(tcp_packet.PayloadData.ToList());

                        object packet_view_item = new
                        {
                            PacketNumber  = current_packet.ToString(),
                            Source        = ip_packet.SourceAddress.ToString() + ":" + tcp_packet.SourcePort.ToString(),
                            Destination   = ip_packet.DestinationAddress.ToString() + ":" + tcp_packet.DestinationPort.ToString(),
                            PayloadLength = tcp_packet.PayloadData.Length.ToString(),
                        };

                        if (packet_stream_view.Items.Contains(packet_view_item) == false)
                        {
                            packet_stream_view.Items.Add(packet_view_item);
                        }
                    }
                    else
                    {
                        PacketDotNet.UdpPacket udp_packet = eth_packet.Extract <PacketDotNet.UdpPacket>();
                        _packet_bytes.Add(udp_packet.PayloadData.ToList());

                        object packet_view_item = new
                        {
                            PacketNumber  = current_packet.ToString(),
                            Source        = ip_packet.SourceAddress.ToString() + ":" + udp_packet.SourcePort.ToString(),
                            Destination   = ip_packet.DestinationAddress.ToString() + ":" + udp_packet.DestinationPort.ToString(),
                            PayloadLength = udp_packet.PayloadData.Length.ToString(),
                        };

                        if (packet_stream_view.Items.Contains(packet_view_item) == false)
                        {
                            packet_stream_view.Items.Add(packet_view_item);
                        }
                    }

                    current_packet++;
                }
            }
            catch (Exception e)
            {
                if (has_temp_file_open == true)
                {
                    cFileUtilities.remove_temp_copy(temp_file);
                    has_temp_file_open = false;
                }

                MessageBox.Show("Error: " + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            if (pcap_device != null)
            {
                pcap_device.Close();
            }

            if (has_temp_file_open == true)
            {
                cFileUtilities.remove_temp_copy(temp_file);
                has_temp_file_open = false;
            }
        }