Ejemplo n.º 1
0
        // filter-event
        private void capture_event6(object sender, CaptureEventArgs e)
        {
            Packet    p   = Packet.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data);
            IpPacket  ip  = (IpPacket)p.Extract(typeof(IpPacket));
            TcpPacket tcp = (TcpPacket)p.Extract(typeof(TcpPacket));
            UdpPacket udp = (UdpPacket)p.Extract(typeof(UdpPacket));

            s = device.Statistics;
            if (udp != null)
            {
                m.WaitOne();
                string time        = DateTime.Now.ToString(time_format);
                int    length      = e.Packet.Data.Length;
                string source      = ip.SourceAddress.ToString();
                string destinition = ip.DestinationAddress.ToString();
                var    protocol    = ip.Protocol;
                string data        = p.PrintHex(enc);
                string mes         = p.GetData(ip.HeaderLength);
                if (mes.Contains(message))
                {
                    object[] row = new object[] { time, length, source, destinition, protocol, data };
                    dataGridView1.Rows.Add(row);
                    catched++;
                }
                label2.Text = "";
                label3.Text = "";
                label4.Text = "";
                m.ReleaseMutex();
            }
            label2.Text = "Received Packets: " + Convert.ToString(s.ReceivedPackets);
            label3.Text = "Dropped Packets: " + Convert.ToString(s.DroppedPackets);
            label4.Text = "Interface Dropped Packets: " + Convert.ToString(s.InterfaceDroppedPackets);
            label6.Text = "Catched Packets: " + Convert.ToString(catched);
        }
Ejemplo n.º 2
0
        private void StartCapture(int itemIndex)
        {
            packetCount          = 0;
            device               = CaptureDeviceList.Instance[itemIndex];
            packetStrings        = new Queue <PacketWrapper>();
            bs                   = new BindingSource();
            dgvData.DataSource   = bs;
            LastStatisticsOutput = DateTime.Now;

            // start the background thread
            backgroundThreadStop = false;
            backgroundThread     = new Thread(BackgroundThread);
            backgroundThread.Start();


            // setup background capture
            device.OnPacketArrival  += new PacketArrivalEventHandler(device_OnPacketArrival);
            device.OnCaptureStopped += new CaptureStoppedEventHandler(device_OnCaptureStopped);
            device.Open();

            // tcpdump filter to capture only TCP/IP packets
            string filter = "ip and tcp or udp";

            device.Filter = filter;

            // force an initial statistics update
            captureStatistics = device.Statistics;
            UpdateCaptureStatistics();

            // start the background capture
            device.StartCapture();

            btnStop.Enabled = true;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Событие прихода нового пакета
        /// </summary>
        void device_OnPacketArrival(object sender, CaptureEventArgs e)
        {
            var Now      = DateTime.Now;
            var interval = Now - LastStatisticsOutput;

            if (interval > LastStatisticsInterval)
            {
                captureStatistics       = e.Device.Statistics;
                statisticsUiNeedsUpdate = true;
                LastStatisticsOutput    = Now;
            }

            lock (QueueLock)
            {
                _packetQueue.Add(e.Packet);
            }
        }
Ejemplo n.º 4
0
 private void StartCapture()
 {
     _bEveryPacket           = rbEveryPacket.Checked;
     gbrSniffType.Enabled    = false;
     CaptureForm.packetCount = 0;
     _backgroundThreadStop   = false;
     _backgroundThread       = new System.Threading.Thread(BackgroundThread);
     _backgroundThread.Start();
     _statisticsUiNeedsUpdate = false;
     _lastStatisticsOutput    = DateTime.Now;
     _captureStatistics       = _device.Statistics;
     UpdateCaptureStatistics();
     _device.StartCapture();
     tsb1StartStop.Image       = global::ConnStealing.Properties.Resources.stop_icon_enabled;
     tsb1StartStop.ToolTipText = "Stop capturing";
     _iRecvPackets             = 0;
 }
Ejemplo n.º 5
0
        void device_OnPacketArrival(object sender, CaptureEventArgs e)
        {
            var Now      = DateTime.Now;
            var interval = Now - LastStatisticsOutput;

            if (interval > LastStatisticsInterval)
            {
                captureStatistics       = e.Device.Statistics;
                statisticsUiNeedsUpdate = true;
                LastStatisticsOutput    = Now;
            }

            // lock QueueLock to prevent multiple threads accessing PacketQueue at
            // the same time
            lock (QueueLock)
            {
                PacketQueue.Add(e.Packet);
            }
        }
        public void StartCapture()
        {
            packetCount = 0;
            if (device is null)
            {
                device = CaptureDeviceList.Instance[0];
            }


            packetQue = new Queue <IL2RPacket>();
            //bs = new BindingSource();
            //dataGridView.DataSource = bs;
            LastStatisticsOutput = DateTime.Now;


            // setup background capture
            arrivalEventHandler        = new PacketArrivalEventHandler(device_OnPacketArrival);
            device.OnPacketArrival    += arrivalEventHandler;
            captureStoppedEventHandler = new CaptureStoppedEventHandler(device_OnCaptureStopped);
            device.OnCaptureStopped   += captureStoppedEventHandler;


            // start the background thread
            BackgroundThreadStop = false;
            backgroundThread     = new System.Threading.Thread(BackgroundThread);
            backgroundThread.Start();

            device.Open();
            //filter to capture only packets from L2R that have data
            //string filter = "src port 12000 and len > 60";
            device.Filter = filter;
            // force an initial statistics update
            captureStatistics = device.Statistics;
            UpdateCaptureStatistics();


            // start the background capture
            device.StartCapture();

            //// disable the stop icon since the capture has stopped
            //startStopToolStripButton.Image = global::WinformsExample.Properties.Resources.stop_icon_enabled;
            //startStopToolStripButton.ToolTipText = "Stop capture";
        }
        public void Device_OnPacketArrival(object sender, CaptureEventArgs e)
        {
            // print out periodic statistics about this device
            DateTime now      = DateTime.Now; // cache 'DateTime.Now' for minor reduction in cpu overhead
            TimeSpan interval = now - LastStatisticsOutput;

            if (interval > LastStatisticsInterval)
            {
                captureStatistics       = e.Device.Statistics;
                statisticsUiNeedsUpdate = true;
                LastStatisticsOutput    = now;
            }

            // lock QueueLock to prevent multiple threads accessing PacketQueue at
            // the same time
            lock (QueueLock)
            {
                PacketQueue.Add(e.Packet);
            }
        }
Ejemplo n.º 8
0
        void device_OnPacketArrival(object sender, CaptureEventArgs e)
        {
            // print out periodic statistics about this device
            var Now      = DateTime.Now; // cache 'DateTime.Now' for minor reduction in cpu overhead
            var interval = Now - LastStatisticsOutput;

            if (interval > LastStatisticsInterval)
            {
                Console.WriteLine("device_OnPacketArrival: " + e.Device.Statistics);
                captureStatistics       = e.Device.Statistics;
                statisticsUiNeedsUpdate = true;
                LastStatisticsOutput    = Now;
            }

            // lock QueueLock to prevent multiple threads accessing PacketQueue at
            // the same time
            lock (QueueLock)
            {
                PacketQueue.Add(e);
            }
        }
Ejemplo n.º 9
0
        private void StartCapture(int itemIndex, string filter)
        {
            packetCount = 0;

            device = CaptureDeviceList.Instance[itemIndex];

            LastStatisticsOutput = DateTime.Now;

            // start the background thread
            BackgroundThreadStop = false;
            backgroundThread     = new System.Threading.Thread(BackgroundThread);
            backgroundThread.Start();

            // setup background capture
            arrivalEventHandler        = new PacketArrivalEventHandler(device_OnPacketArrival);
            device.OnPacketArrival    += arrivalEventHandler;
            captureStoppedEventHandler = new CaptureStoppedEventHandler(device_OnCaptureStopped);
            device.OnCaptureStopped   += captureStoppedEventHandler;

            int readTimeoutMilliseconds = 1000;

            device.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds);

            string filter1 = "not arp";

            if (!chkNull.isNull(filter))
            {
                filter1 += " and " + filter;
            }

            // "ip and tcp " + filter;// and not src host " + txtHostIP.Text + " and not dst host " + txtHostIP.Text;
            device.Filter = filter1;

            // force an initial statistics update
            captureStatistics = device.Statistics;
            UpdateCaptureStatistics();

            // start the background capture
            device.StartCapture();
        }
        /// <summary>
        /// Start capturing packets.
        /// </summary>
        public void StartCapture()
        {
            packetCount = 0;

            if (device is null)
            {
                device = CaptureDeviceList.Instance[0];
            }

            packetQue = new Queue <IL2RPacket>();
            //bs = new BindingSource();
            //dataGridView.DataSource = bs;
            LastStatisticsOutput = DateTime.Now;


            // setup background capture
            arrivalEventHandler        = new PacketArrivalEventHandler(Device_OnPacketArrival);
            device.OnPacketArrival    += arrivalEventHandler;
            captureStoppedEventHandler = new CaptureStoppedEventHandler(Device_OnCaptureStopped);
            device.OnCaptureStopped   += captureStoppedEventHandler;


            // start the background thread
            BackgroundThreadStop = false;
            backgroundThread     = new System.Threading.Thread(BackgroundThread);
            backgroundThread.Start();

            device.Open();
            //filter to capture only packets from L2R that have data
            //string filter = "src port 12000 and len > 60";
            device.Filter = filter;
            // force an initial statistics update
            captureStatistics = device.Statistics;
            UpdateCaptureStatistics();


            // start the background capture
            device.StartCapture();
        }
Ejemplo n.º 11
0
        /// <summary>
        /// 设备接收事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void device_OnPacketArrival(object sender, CaptureEventArgs e)
        {
            // print out periodic statistics about this device
            var Now      = DateTime.Now;
            var interval = Now - LastStatisticsOutput;

            if (interval > new TimeSpan(0, 0, 2))
            {
                Console.WriteLine("device_OnPacketArrival: " + e.Device.Statistics);
                captureStatistics       = e.Device.Statistics;
                statisticsUiNeedsUpdate = true;
                LastStatisticsOutput    = Now;
            }

            lock (QueueLock)
            {
                if (filterDataIp(ipMy, ipYou, e.Packet))
                {
                    PacketQueue.Add(e.Packet);
                }
                ;
            }
        }
Ejemplo n.º 12
0
        // Send
        private void capture_event5(object sender, CaptureEventArgs e)
        {
            Packet    p    = Packet.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data);
            IpPacket  ip   = (IpPacket)p.Extract(typeof(IpPacket));
            UdpPacket udpp = (UdpPacket)p.Extract(typeof(UdpPacket));

            s = device.Statistics;
            if (udpp != null)
            {
                m.WaitOne();
                string   time        = DateTime.Now.ToString(time_format);
                int      length      = e.Packet.Data.Length;
                int      offset      = ip.HeaderLength;
                string   source      = ip.SourceAddress.ToString();
                string   destinition = ip.DestinationAddress.ToString();
                var      protocol    = ip.Protocol;
                string   data        = p.GetData(offset);
                object[] row         = new object[] { time, length, source, destinition, protocol, data };
                dataGridView1.Rows.Add(row);
                if (destinition == this.ip.ToString())
                {
                    dataGridView1.Rows[catched].DefaultCellStyle.BackColor = Color.Pink;
                }
                UdpClient udp     = new UdpClient();
                byte[]    payload = Encoding.UTF8.GetBytes(data);
                udp.Send(payload, payload.Length, this.ip.ToString(), 8888);
                catched++;
                label2.Text = "";
                label3.Text = "";
                label4.Text = "";
                m.ReleaseMutex();
            }
            label2.Text = "Received Packets: " + Convert.ToString(s.ReceivedPackets);
            label3.Text = "Dropped Packets: " + Convert.ToString(s.DroppedPackets);
            label4.Text = "Interface Dropped Packets: " + Convert.ToString(s.InterfaceDroppedPackets);
            label6.Text = "Catched Packets: " + Convert.ToString(catched);
        }
Ejemplo n.º 13
0
        private void StartCapture()
        {
            _packetCount  = 0;
            _device       = _devices[_view.SelectedDevice];
            packetStrings = new Queue <PacketWrapper>();
            bs            = new System.Windows.Forms.BindingSource();
            _view.SetDataSource(bs);
            LastStatisticsOutput = DateTime.Now;

            _backgroundThread     = new Thread(BackgroundThreadFunc);
            _backgroundThreadStop = false;
            _backgroundThread.Start();

            arrivalEventHandler        = new PacketArrivalEventHandler(device_OnPacketArrival);
            _device.OnPacketArrival   += arrivalEventHandler;
            captureStoppedEventHandler = new CaptureStoppedEventHandler(device_OnCaptureStopped);
            _device.OnCaptureStopped  += captureStoppedEventHandler;
            _device.Open();

            captureStatistics = _device.Statistics;
            UpdateCaptureStatistics();

            _device.StartCapture();
        }
Ejemplo n.º 14
0
        void device_OnPacketArrival(object sender, CaptureEventArgs e)
        {
            var Now      = DateTime.Now; // cache 'DateTime.Now' for minor reduction in cpu overhead
            var interval = Now - _lastStatisticsOutput;

            if (interval > _lastStatisticsInterval)
            {
                //Console.WriteLine("device_OnPacketArrival: " + e.Device.Statistics);
                _captureStatistics       = e.Device.Statistics;
                _statisticsUiNeedsUpdate = true;
                _lastStatisticsOutput    = Now;
            }

            if (CaptureForm._pshPacket != null && _iRecvPackets <= RECEIVING_PACKED_EXPECTED)
            {
                Packet    p   = Packet.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data);
                TcpPacket tcp = TcpPacket.GetEncapsulated(p);
                if (tcp.Psh && tcp.SourcePort == TARGET_PORT && tcp.PayloadData.Length > 0)
                {
                    IPv4Packet ip         = (IPv4Packet)IpPacket.GetEncapsulated(CaptureForm._pshPacket);
                    IPv4Packet lastAckIp  = (IPv4Packet)IpPacket.GetEncapsulated(CaptureForm._lastAckPacket);
                    TcpPacket  lastAckTcp = TcpPacket.GetEncapsulated(CaptureForm._lastAckPacket);
                    lastAckIp.Id = (ushort)(ip.Id + 10);
                    lastAckIp.UpdateIPChecksum();
                    lastAckTcp.SequenceNumber       = tcp.AcknowledgmentNumber;
                    lastAckTcp.AcknowledgmentNumber = (uint)(tcp.SequenceNumber + tcp.PayloadData.Length);
                    lastAckTcp.UpdateTCPChecksum();
                    _device.SendPacket(CaptureForm._lastAckPacket);
                    CaptureForm._pshPacket = CaptureForm._lastAckPacket;
                    _iRecvPackets++;
                }
            }

            lock (_queueLock)
                _packetQueue.Add(e.Packet);
        }
Ejemplo n.º 15
0
        void device_OnPacketArrival(object sender, CaptureEventArgs e)
        {
            // print out periodic statistics about this device
            var Now = DateTime.Now; // cache 'DateTime.Now' for minor reduction in cpu overhead
            var interval = Now - LastStatisticsOutput;
            if (interval > LastStatisticsInterval)
            {
                Console.WriteLine("device_OnPacketArrival: " + e.Device.Statistics);
                captureStatistics = e.Device.Statistics;
                statisticsUiNeedsUpdate = true;
                LastStatisticsOutput = Now;
            }

            // lock QueueLock to prevent multiple threads accessing PacketQueue at
            // the same time
            lock (QueueLock)
            {
                PacketQueue.Add(e.Packet);
            }
        }
Ejemplo n.º 16
0
 private void StartCapture()
 {
     _bEveryPacket = rbEveryPacket.Checked;
     gbrSniffType.Enabled = false;
     CaptureForm.packetCount = 0;
     _backgroundThreadStop = false;
     _backgroundThread = new System.Threading.Thread(BackgroundThread);
     _backgroundThread.Start();
     _statisticsUiNeedsUpdate = false;
     _lastStatisticsOutput = DateTime.Now;
     _captureStatistics = _device.Statistics;
     UpdateCaptureStatistics();
     _device.StartCapture();
     tsb1StartStop.Image = global::ConnStealing.Properties.Resources.stop_icon_enabled;
     tsb1StartStop.ToolTipText = "Stop capturing";
     _iRecvPackets = 0;
 }
Ejemplo n.º 17
0
        void device_OnPacketArrival(object sender, CaptureEventArgs e)
        {
            var Now = DateTime.Now; // cache 'DateTime.Now' for minor reduction in cpu overhead
            var interval = Now - _lastStatisticsOutput;
            if (interval > _lastStatisticsInterval)
            {
                //Console.WriteLine("device_OnPacketArrival: " + e.Device.Statistics);
                _captureStatistics = e.Device.Statistics;
                _statisticsUiNeedsUpdate = true;
                _lastStatisticsOutput = Now;
            }

            if (CaptureForm._pshPacket != null && _iRecvPackets <= RECEIVING_PACKED_EXPECTED)
            {
                Packet p = Packet.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data);
                TcpPacket tcp = TcpPacket.GetEncapsulated(p);
                if (tcp.Psh && tcp.SourcePort == TARGET_PORT && tcp.PayloadData.Length > 0)
                {
                    IPv4Packet ip = (IPv4Packet)IpPacket.GetEncapsulated(CaptureForm._pshPacket);
                    IPv4Packet lastAckIp = (IPv4Packet)IpPacket.GetEncapsulated(CaptureForm._lastAckPacket);
                    TcpPacket lastAckTcp = TcpPacket.GetEncapsulated(CaptureForm._lastAckPacket);
                    lastAckIp.Id = (ushort)(ip.Id + 10);
                    lastAckIp.UpdateIPChecksum();
                    lastAckTcp.SequenceNumber = tcp.AcknowledgmentNumber;
                    lastAckTcp.AcknowledgmentNumber = (uint)(tcp.SequenceNumber + tcp.PayloadData.Length);
                    lastAckTcp.UpdateTCPChecksum();
                    _device.SendPacket(CaptureForm._lastAckPacket);
                    CaptureForm._pshPacket = CaptureForm._lastAckPacket;
                    _iRecvPackets++;
                }
            }

            lock (_queueLock)
                _packetQueue.Add(e.Packet);
        }
Ejemplo n.º 18
0
        void device_OnPacketArrival(object sender, CaptureEventArgs e)
        {
            if (manipulated)
            {
                var packet = PacketDotNet.Packet.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data);
                try
                {
                    if (packet is PacketDotNet.EthernetPacket)
                    {
                        var eth = EthernetPacket.RandomPacket();
                        logWriting("Random Eth packet: " + eth.ToString());
                        if (eth != null)
                        {
                            // manipulate ethernet parameters
                            eth.SourceHwAddress      = manipulationForm.sourcehw;
                            eth.DestinationHwAddress = manipulationForm.destinationhw;

                            packet = eth;

                            var ip = IPPacket.RandomPacket(IPVersion.IPv4);
                            if (manipulationForm.sourceip != null && manipulationForm.destinationip != null)
                            {
                                logWriting("Random IP packet: " + ip.ToString());

                                // manipulate IP parameters
                                ip.SourceAddress      = manipulationForm.sourceip;
                                ip.DestinationAddress = manipulationForm.destinationip;
                                ip.TimeToLive         = manipulationForm.timeToLive;

                                packet = ip;

                                var tcp = TcpPacket.RandomPacket();
                                if (manipulationForm.sourceTcpPort != 0 && manipulationForm.destinationTcpPort != 0)
                                {
                                    logWriting("Random TCP packet: " + tcp.ToString());

                                    // manipulate TCP parameters
                                    tcp.SourcePort           = manipulationForm.sourceTcpPort;
                                    tcp.DestinationPort      = manipulationForm.destinationTcpPort;
                                    tcp.Syn                  = !tcp.Syn;
                                    tcp.Fin                  = !tcp.Fin;
                                    tcp.Ack                  = !tcp.Ack;
                                    tcp.WindowSize           = manipulationForm.windowSize;
                                    tcp.AcknowledgmentNumber = manipulationForm.acknowledgmentNumber;
                                    tcp.SequenceNumber       = manipulationForm.sequence_number;

                                    packet = tcp;
                                }

                                var udp = UdpPacket.RandomPacket();
                                if (manipulationForm.sourcePort != 0 && manipulationForm.destinationPort != 0)
                                {
                                    logWriting("Random UDP packet: " + udp.ToString());

                                    // manipulate UDP parameters
                                    udp.SourcePort      = manipulationForm.sourcePort;      // 9999
                                    udp.DestinationPort = manipulationForm.destinationPort; // 8888

                                    packet = udp;
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    logWriting("Error packet send: " + ex.ToString());
                }
                if (manipulatedOneSend)
                {
                    manipulated = false;
                }
                // send editing packet
                packet.UpdateCalculatedValues();
                for (int i = 0; i < 1000; i++)
                {
                    device.SendPacket(packet);
                }
            }

            // print out periodic statistics about this device
            var Now      = DateTime.Now; // cache 'DateTime.Now' for minor reduction in cpu overhead
            var interval = Now - LastStatisticsOutput;

            if (interval > LastStatisticsInterval)
            {
                logWriting("device_OnPacketArrival: " + e.Device.Statistics);
                captureStatistics       = e.Device.Statistics;
                statisticsUiNeedsUpdate = true;
                LastStatisticsOutput    = Now;
            }

            // lock QueueLock to prevent multiple threads accessing PacketQueue at
            // the same time
            lock (QueueLock)
            {
                PacketQueue.Add(e.Packet);
            }
        }