示例#1
0
        public bool IsWanted(UdpDatagram packet)
        {
            bool flag  = false;
            bool flag2 = false;
            bool flag3 = false;

            foreach (IAllowFilter filter in this.m_AllowList)
            {
                if (filter.AllowUdpPacket(packet))
                {
                    flag2 = true;
                    break;
                }
            }
            if (flag2)
            {
                return(true);
            }
            foreach (IDenyFilter filter2 in this.m_DenyList)
            {
                if (filter2.DenyUdpPacket(packet))
                {
                    flag3 = true;
                    break;
                }
            }
            if (!flag3)
            {
                flag = true;
            }
            return(flag);
        }
示例#2
0
 private void FireUdpDatagramReceived(UdpDatagram packet)
 {
     if (this.FilterSet.IsWanted(packet) && (this.UdpDatagramReceived != null))
     {
         this.UdpDatagramReceived(packet);
     }
 }
示例#3
0
        /// <summary>
        /// Handle IPV4 packets, including TCP and UDP packets
        /// </summary>
        /// <param name="packet">The IpV4Datagram to parse</param>
        public static void HandleIPV4(Packet packet, IpV4Datagram ip,
                                      ref UInt64 frame_id, object[] ctrl)
        {
            ListViewItem item = new ListViewItem(frame_id.ToString());

            frame_id++;
            List <string>    packet_info = new List <string>();
            ListView         frames      = (ListView)ctrl[0];
            EthernetDatagram ethernet    = packet.Ethernet;

            switch (ip.Protocol)
            {
            case IpV4Protocol.Udp: {
                UdpDatagram udp = ip.Udp;
                packet_info.Add(packet.Timestamp.ToString("hh:mm:ss.fff tt"));
                packet_info.Add(ip.Source + ":" + udp.SourcePort);
                packet_info.Add(ip.Destination + ":" + udp.DestinationPort);
                packet_info.Add(ethernet.Source.ToString());
                packet_info.Add(ethernet.Destination.ToString());
                packet_info.Add("UDP");
                packet_info.Add(udp.Length.ToString());

                break;
            }

            case IpV4Protocol.Tcp: {
                TcpDatagram tcp = ip.Tcp;
                packet_info.Add(packet.Timestamp.ToString("hh:mm:ss.fff tt"));
                packet_info.Add(ip.Source + ":" + tcp.SourcePort);
                packet_info.Add(ip.Destination + ":" + tcp.DestinationPort);
                packet_info.Add(ethernet.Source.ToString());
                packet_info.Add(ethernet.Destination.ToString());
                packet_info.Add("TCP");
                packet_info.Add(tcp.Length.ToString());

                break;
            }

            default: {
                item = null;
                break;
            }
            }

            // update UI
            if (item != null)
            {
                item.SubItems.AddRange(packet_info.ToArray());
                object[] param = new object[2];
                param[0] = frames;
                object[] o = new object[3];
                o[0]     = item;
                o[1]     = ctrl[1];
                o[2]     = packet;
                param[1] = o;
                frames.BeginInvoke(new ParserHelper.UIHandlerEx(ParserHelper.UpdateFrameUI), param);
            }
        }
示例#4
0
        private void HandleUdpDatagram(byte[] data, IPAddress source, IPAddress destination)
        {
            UdpDatagram packet = new UdpDatagram();
            int         port   = Sniffer.HeaderParser.ToInt(data, 0, 0x10);
            int         num2   = Sniffer.HeaderParser.ToInt(data, 0x10, 0x10);
            int         length = Sniffer.HeaderParser.ToInt(data, 0x20, 0x10) - 8;

            packet.Source      = new IPEndPoint(source, port);
            packet.Destination = new IPEndPoint(destination, num2);
            packet.SetData(data, 8, length);
            this.FireUdpDatagramReceived(packet);
        }
示例#5
0
        public UdpDatagram HandleUdpDatagram()
        {
            UdpDatagram packet      = new UdpDatagram();
            int         source_port = HeaderParser.ToInt(this.Data, 0, 16);
            int         dest_port   = HeaderParser.ToInt(this.Data, 16, 16);
            int         length      = HeaderParser.ToInt(this.Data, 32, 16) - 8;

            packet.Source      = new IPEndPoint(this.Source, source_port);
            packet.Destination = new IPEndPoint(this.Destination, dest_port);
            packet.SetData(this.Data, 8, length);
            return(packet);
        }
示例#6
0
        /// <summary>
        /// Parse an IP packet
        /// Dispatch TCP and UDP packets
        /// </summary>
        /// <param name="ip">The IP packet to be parsed</param>
        /// <param name="ctrl"></param>
        public static void ParseIPPacket(IpV4Datagram ip, object[] ctrl)
        {
            if (ip == null || ctrl == null ||
                ctrl[0] == null || ctrl[1] == null)
            {
                return;
            }

            List <ListViewItem> items = new List <ListViewItem>();
            ListViewItem        item  = new ListViewItem("IPV4");
            string estr = "Destination = " + ip.Destination;

            estr += ", Source = " + ip.Source;
            estr += ", Next Protocol = " + ip.Protocol;
            estr += ", Packet ID = " + ip.Identification;
            estr += ", Total IP length = " + ip.Length;
            item.SubItems.Add(estr);
            items.Add(item);

            ListView f_details = (ListView)ctrl[0];

            object[] param = new object[2];
            param[0] = f_details;
            param[1] = items;
            f_details.BeginInvoke(new ParserHelper.UIHandler(ParserHelper.UpdateDetailsUI), param);

            switch (ip.Protocol)
            {
            case IpV4Protocol.Tcp:
            {
                TcpDatagram tcp = ip.Tcp;
                SnifferMain.datagram = tcp;
                SnifferMain.payload  = tcp.Payload;
                ParseTcpPacket(tcp, ctrl[0]);
                PacketParser.DumpPacket(tcp, ctrl[1]);
                break;
            }

            case IpV4Protocol.Udp:
            {
                UdpDatagram udp = ip.Udp;
                SnifferMain.datagram = udp;
                SnifferMain.payload  = udp.Payload;
                ParseUdpPacket(udp, ctrl[0]);
                PacketParser.DumpPacket(udp, ctrl[1]);
                break;
            }

            default: break;
            }
        }
        private void PacketHandler2(Packet packet)
        {
            // print timestamp and length of the packet
            richTextBox1.Text += (packet.Timestamp.ToString("yyyy-MM-dd hh:mm:ss.fff") + " length:" + packet.Length + Environment.NewLine);

            IpV4Datagram ip   = packet.Ethernet.IpV4;
            IcmpDatagram icmp = ip.Icmp;
            UdpDatagram  udp  = ip.Udp;

            // print ip addresses and udp ports
            richTextBox1.Text += (ip.Source + ":" + packet.Ethernet.IpV6.Source + " -> " + ip.Destination + ":" + packet.Ethernet.IpV6.Source + Environment.NewLine);
            richTextBox1.Text += ("************************************************" + packet.Timestamp.Millisecond.ToString() + Environment.NewLine);
            richTextBox1.Text += ("************************************************" + icmp.MessageType + Environment.NewLine);

            richTextBox1.Text += ("************************************************" + Environment.NewLine);
        }
示例#8
0
        private static void ParseUdpPacket(UdpDatagram udp, object ctrl)
        {
            if (udp == null || ctrl == null)
            {
                return;
            }
            ListView f_details = (ListView)ctrl;

            List <ListViewItem> items = new List <ListViewItem>();

            ListViewItem item = new ListViewItem("Packet Type");

            item.SubItems.Add("UDP");
            items.Add(item);

            item = new ListViewItem("SrcPort");
            item.SubItems.Add(udp.SourcePort.ToString());
            items.Add(item);

            item = new ListViewItem("DstPort");
            item.SubItems.Add(udp.DestinationPort.ToString());
            items.Add(item);

            item = new ListViewItem("Checksum");
            string checksum = string.Format("0x{0:X}", udp.Checksum);

            item.SubItems.Add(checksum);
            items.Add(item);

            item = new ListViewItem("Payload Length");
            item.SubItems.Add(string.Format("{0} ({1:x})", udp.Payload.Length, udp.Payload.Length));
            items.Add(item);

            item = new ListViewItem("TotalLength");
            ushort len     = udp.TotalLength;
            string len_str = string.Format("{0} (0x{1:X})", len, len);

            item.SubItems.Add(len_str);
            items.Add(item);

            object[] param = new object[2];
            param[0] = f_details;
            param[1] = items;
            f_details.BeginInvoke(new ParserHelper.UIHandler(ParserHelper.UpdateDetailsUI), param);
        }
示例#9
0
        private void PacketHandler(Packet packet)
        {
            this.count  = String.Empty; this.time = String.Empty; this.source = String.Empty; this.destination = String.Empty; this.s_port = String.Empty; this.d_port = String.Empty; this.protocol = String.Empty; this.length = String.Empty;
            this.tcpack = String.Empty; this.tcpsec = String.Empty; this.tcpnsec = String.Empty; this.tcpsrc = String.Empty; this.tcpdes = String.Empty; this.udpscr = String.Empty;
            this.udpdes = String.Empty; this.httpheader = String.Empty; this.httpver = String.Empty; this.httpayload = String.Empty; this.reqres = String.Empty; this.httpbody = String.Empty; payload = String.Empty;

            IpV4Datagram ip   = packet.Ethernet.IpV4;
            TcpDatagram  tcp  = ip.Tcp;
            UdpDatagram  udp  = ip.Udp;
            HttpDatagram http = null;

            try
            {
                if (ip.Protocol.ToString().Equals("Tcp"))
                {
                    http = tcp.Http;

                    if (http.Header != null)
                    {
                        protocol   = "Http";
                        httpheader = http.Header.ToString();
                        httpver    = http.Version.ToString();
                        httpayload = http.Length.ToString();
                        httpbody   = http.Body.ToString();

                        if (http.IsRequest)
                        {
                            reqres = "Request";
                        }
                        else
                        {
                            reqres = "Response";
                        }
                    }

                    else
                    {
                        protocol = ip.Protocol.ToString();
                        s_port   = tcp.SourcePort.ToString();
                        d_port   = tcp.DestinationPort.ToString();
                        tcpack   = tcp.AcknowledgmentNumber.ToString();
                        tcpsec   = tcp.SequenceNumber.ToString();
                        tcpnsec  = tcp.NextSequenceNumber.ToString();
                        payload  = ", data:" + tcp.Payload.Length.ToString();
                    }
                }
                else if ((ip.Protocol.ToString().Equals("Udp")))
                {
                    protocol = ip.Protocol.ToString();
                    s_port   = udp.SourcePort.ToString();
                    d_port   = udp.DestinationPort.ToString();
                    payload  = ", data:" + udp.Payload.Length.ToString();
                }
                else
                {
                    protocol = ip.Protocol.ToString();
                }
                count            = packet.Count.ToString();
                time             = packet.Timestamp.ToString();
                this.source      = ip.Source.ToString();
                this.destination = ip.Destination.ToString();
                length           = ip.Length.ToString();
                payload          = ", data:" + ip.Payload.Length.ToString();
            }
            catch { }
        }