Пример #1
0
        public void BenchmarkICaptureDevice()
        {
            int packetsRead = 0;
            var startTime   = DateTime.Now;

            while (packetsRead < packetsToRead)
            {
                ICaptureDevice captureDevice = new SharpPcap.LibPcap.CaptureFileReaderDevice(TestHelper.GetFile("10k_packets.pcap"));
                captureDevice.Open();

                RawCapture rawCapture = null;
                do
                {
                    rawCapture = captureDevice.GetNextPacket();

                    // Parse the packet using PacketDotNet
                    if (rawCapture != null)
                    {
                        Packet.ParsePacket(rawCapture.LinkLayerType, rawCapture.Data);
                    }

                    packetsRead++;
                }while (rawCapture != null);

                captureDevice.Close();
            }

            var endTime = DateTime.Now;

            var rate = new Rate(startTime, endTime, packetsRead, "packets parsed");

            Console.WriteLine("{0}", rate.ToString());
        }
Пример #2
0
        void openfile(string capFile)
        {
            try
            {
                this.device.StopCapture();
                this.device.Close();
            }
            catch (Exception)
            {
                ;
            }
            this.packets = new ArrayList();
            this.dataGridView1.Rows.Clear();

            SharpPcap.LibPcap.CaptureFileReaderDevice captureFileReader = new SharpPcap.LibPcap.CaptureFileReaderDevice(capFile);

            SharpPcap.RawCapture pPacket;

            int indx = 0;

            // Go through all packets in the file
            while ((pPacket = captureFileReader.GetNextPacket()) != null)
            {
                try
                {
                    packet temp = new packet(pPacket);
                    temp.index = indx;
                    indx++;
                    this.packets.Add(temp);

//                            if (filter_check(temp))
//                            {
                    if (this.dataGridView1.InvokeRequired)
                    {
                        this.dataGridView1.BeginInvoke(new setDataGridViewDelegate(setDataGridView), new object[] { temp });
                    }
                    else
                    {
                        int index = this.dataGridView1.Rows.Add();
                        this.dataGridView1.Rows[index].DefaultCellStyle.BackColor = Color.FromName(temp.color);
                        this.dataGridView1.Rows[index].Cells[0].Value             = temp.time;
                        this.dataGridView1.Rows[index].Cells[1].Value             = temp.srcIp;
                        this.dataGridView1.Rows[index].Cells[2].Value             = temp.destIp;
                        this.dataGridView1.Rows[index].Cells[3].Value             = temp.protocol;
                        this.dataGridView1.Rows[index].Cells[4].Value             = temp.info;
                        this.dataGridView1.Rows[index].Cells[5].Value             = temp.index;

                        this.dataGridView1.FirstDisplayedScrollingRowIndex = this.dataGridView1.Rows.Count - 1;
                    }
//                            }
                }
                catch (Exception)
                {
                    ;
                }
            }
            //this.is_saved = true;
            captureFileReader.Close();
            MessageBox.Show("读取完毕");
        }
Пример #3
0
        public unsafe void BenchmarkICaptureDeviceUnsafe()
        {
            int packetsRead = 0;
            var startTime   = DateTime.Now;

            while (packetsRead < packetsToRead)
            {
                ICaptureDevice captureDevice = new SharpPcap.LibPcap.CaptureFileReaderDevice(TestHelper.GetFile("10k_packets.pcap"));
                captureDevice.Open();

                RawCapture rawCapture = null;
                do
                {
                    rawCapture = captureDevice.GetNextPacket();
                    packetsRead++;
                }while (rawCapture != null);

                captureDevice.Close();
            }

            var endTime = DateTime.Now;

            var rate = new Rate(startTime, endTime, packetsRead, "packets captured");

            Console.WriteLine("{0}", rate.ToString());
        }
Пример #4
0
        public void BenchmarkICaptureDevice()
        {
            int packetsRead = 0;
            var startTime = DateTime.Now;
            while(packetsRead < packetsToRead)
            {
                ICaptureDevice captureDevice = new SharpPcap.LibPcap.CaptureFileReaderDevice("../../capture_files/10k_packets.pcap");
                captureDevice.Open();

                RawCapture rawCapture = null;
                do
                {
                    rawCapture = captureDevice.GetNextPacket();

                    // Parse the packet using PacketDotNet
                    if(rawCapture != null)
                        Packet.ParsePacket(rawCapture.LinkLayerType, rawCapture.Data);

                    packetsRead++;
                }
                while(rawCapture != null);

                captureDevice.Close();
            }

            var endTime = DateTime.Now;

            var rate = new Rate(startTime, endTime, packetsRead, "packets parsed");

            Console.WriteLine("{0}", rate.ToString());
        }
Пример #5
0
        public unsafe void BenchmarkICaptureDeviceUnsafe()
        {
            int packetsRead = 0;
            var startTime = DateTime.Now;
            while(packetsRead < packetsToRead)
            {
                ICaptureDevice captureDevice = new SharpPcap.LibPcap.CaptureFileReaderDevice("../../capture_files/10k_packets.pcap");
                captureDevice.Open();

                RawCapture rawCapture = null;
                do
                {
                    rawCapture = captureDevice.GetNextPacket();
                    packetsRead++;
                }
                while(rawCapture != null);

                captureDevice.Close();
            }

            var endTime = DateTime.Now;

            var rate = new Rate(startTime, endTime, packetsRead, "packets captured");

            Console.WriteLine("{0}", rate.ToString());
        }
Пример #6
0
        public static void Main(string[] args)
        {
            Console.WriteLine("XeretaNET " + VERSION);

            SharpPcap.LibPcap.CaptureFileReaderDevice dev = new SharpPcap.LibPcap.CaptureFileReaderDevice ("/tmp/br1");

            dev.OnPacketArrival += new PacketArrivalEventHandler (onPacketArrival);

            dev.Capture ();
        }
Пример #7
0
        private static void StartPacketStream(string pcapFile)
        {
            var device = new SharpPcap.LibPcap.CaptureFileReaderDevice(pcapFile);

            // Register our handler function to the 'packet arrival' event
            device.OnPacketArrival += new PacketArrivalEventHandler(deviceStream_OnPacketArrival);
            // Start capture 'All Bytes in File' number of packets
            // This method will return when EOF reached.
            device.Capture();
            concurrentReadDoneFlags.Add("reading done");
        }
Пример #8
0
        public void PrintString()
        {
            Console.WriteLine("Loading the sample capture file");
            var dev = new SharpPcap.LibPcap.CaptureFileReaderDevice(NUnitSetupClass.CaptureDirectory + "arp_request_response.pcap");

            dev.Open();
            Console.WriteLine("Reading packet data");
            dev.GetNextPacket();
            var rawPacket = dev.GetNextPacket();

            dev.Close();

            Console.WriteLine("Printing human readable string");
            Console.WriteLine(rawPacket.ToString());
        }
Пример #9
0
        private void ResetPacketPosition()
        {
            fileAndPacketIndex = 0;
            currentPacketIndex = 0;
            packetFileName     = null;

            indexIntoPacketFile = 0;

            totalPacketsReturned = 0;

            if (captureFileReader != null)
            {
                captureFileReader.Close();
                captureFileReader = null;
            }
        }
Пример #10
0
        private void ResetPacketPosition()
        {
            _fileAndPacketIndex = 0;
            _currentPacketIndex = 0;
            _packetFileName     = null;

            _indexIntoPacketFile = 0;

            _totalPacketsReturned = 0;

            if (_captureFileReader != null)
            {
                _captureFileReader.Close();
                _captureFileReader = null;
            }
        }
Пример #11
0
        //
        //open file
        //
        private void open_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.OpenFileDialog openfile = new OpenFileDialog();
            openfile.InitialDirectory = Environment.CurrentDirectory;
            openfile.Filter           = "pcap files (*.pcap)|*.pcap";
            openfile.CheckFileExists  = true;
            openfile.RestoreDirectory = true;

            if (openfile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string name = openfile.FileName;

                listView1.Items.Clear();
                this.packets = new ArrayList();

                SharpPcap.LibPcap.CaptureFileReaderDevice reader = new SharpPcap.LibPcap.CaptureFileReaderDevice(name);
                RawCapture rawp;

                try
                {
                    rawp = reader.GetNextPacket();
                    while (rawp != null)
                    {
                        packet temp = new packet(rawp);
                        packets.Add(temp);

                        temp.index = (listView1.Items.Count + 1);

                        ListViewItem item = new ListViewItem(new string[] { temp.index.ToString(), temp.time, temp.source, temp.destination, temp.protocol, temp.information });
                        item.BackColor = Color.FromName(temp.color);
                        listView1.Items.Add(item);


                        rawp = reader.GetNextPacket();
                    }
                    MessageBox.Show("success!");
                }
                catch (Exception)
                {
                    MessageBox.Show("fail!");
                }
            }
        }
Пример #12
0
        private static Packet GetNextPacket()
        {
            if(currentFAPI != null)
            {
                log.DebugFormat("currentFAPI.PacketIndexes.Count {0}," +
                                "currentPacketIndex {1}",
                                currentFAPI.PacketIndexes.Count,
                                currentPacketIndex);
            } else
            {
                log.Debug("currentFAPI is null");
            }
            // do we need to open a file up or are we done with the current file?
            if((packetFileName == null) ||
               (currentFAPI == null) ||
               (currentFAPI.PacketIndexes.Count == currentPacketIndex))
            {
                log.Debug("opening a new file up");

                // close the open device if there was one
                if(captureFileReader != null)
                {
                    captureFileReader.Close();
                    captureFileReader = null;
                }

                // do we have any more files to process?
                if(fileAndPacketIndex >= fileAndPacketIndexes.Count)
                {
                    log.DebugFormat("totalPacketsReturned {0}, expectedTotalPackets {1}",
                                    totalPacketsReturned,
                                    expectedTotalPackets);

                    Assert.AreEqual(expectedTotalPackets, totalPacketsReturned,
                                   "expectedTotalPackets does not match totalPacketsReturned");

                    return null;
                } else
                {
                    currentFAPI = fileAndPacketIndexes[fileAndPacketIndex];
                    currentPacketIndex = 0;
                    packetFileName = currentFAPI.Filename;

                    // opening a new file, we are at the first index into the new file
                    indexIntoPacketFile = 0;

                    try
                    {
                        log.DebugFormat("Opening {0}", currentFAPI.Filename);

                        captureFileReader = new SharpPcap.LibPcap.CaptureFileReaderDevice(currentFAPI.Filename);
                        captureFileReader.Open();

                        fileAndPacketIndex++;
                    } catch(System.Exception e)
                    {
                        log.Error("caught exception",e);
                        throw;
                    }
                }
            }

            Packet p = null;

            do
            {
                log.DebugFormat("currentPacketIndex {0}", currentPacketIndex);
                log.DebugFormat("indexIntoPacketFile {0}, currentFAPI.PacketIndexes[currentPacketIndex] {1}",
                                indexIntoPacketFile,
                                currentFAPI.PacketIndexes[currentPacketIndex]);

                log.Debug("retrieving packet");

                // read the next packet
                var packet = captureFileReader.GetNextPacket();
                Assert.IsNotNull(packet, "Expected a valid packet but it was null");

                p = Packet.ParsePacket(packet.LinkLayerType, packet.Data);

                currentPacketDescription = currentFAPI.PacketDescription[currentPacketIndex];

                // advance our index into the current packet file
                indexIntoPacketFile++;
            } while((indexIntoPacketFile -1) != currentFAPI.PacketIndexes[currentPacketIndex]);
            // does the current index match the index of the packet we want?

            // and because we got a packet we advance our index into the FileAndPacketIndex class
            currentPacketIndex++;

            log.Debug("returning packet");
            totalPacketsReturned++;
            return p;
        }
Пример #13
0
        private void button5_Click(object sender, EventArgs e)
        {
            if (this.is_saved == true || MessageBox.Show("不保存并读取文件?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                try
                {
                    this.device.StopCapture();
                    this.device.Close();
                }
                catch (Exception)
                {
                    ;
                }
                string capFile = "";
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Templates);
                ofd.Filter = "PCAP(*.pcap)|*.pcap";
                ofd.ValidateNames = true;
                ofd.CheckFileExists = true;
                ofd.CheckPathExists = true;
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    this.packets = new ArrayList();
                    this.dataGridView1.Rows.Clear();
                    capFile = ofd.FileName;
                    SharpPcap.LibPcap.CaptureFileReaderDevice captureFileReader = new SharpPcap.LibPcap.CaptureFileReaderDevice(capFile);

                    SharpPcap.RawCapture pPacket;

                    // Go through all packets in the file
                    while ((pPacket = captureFileReader.GetNextPacket()) != null)
                    {
                        try
                        {
                            packet temp = new packet(pPacket);
                            this.packets.Add(temp);

                            if (filter_check(temp))
                            {
                                if (this.dataGridView1.InvokeRequired)
                                {
                                    this.dataGridView1.BeginInvoke(new setDataGridViewDelegate(setDataGridView), new object[] { temp, this.packets.Count - 1 });
                                }
                                else
                                {
                                    int index = this.dataGridView1.Rows.Add();
                                    this.dataGridView1.Rows[index].DefaultCellStyle.BackColor = Color.FromName(temp.color);
                                    this.dataGridView1.Rows[index].Cells[0].Value = temp.time;
                                    this.dataGridView1.Rows[index].Cells[1].Value = temp.srcIp;
                                    this.dataGridView1.Rows[index].Cells[2].Value = temp.destIp;
                                    this.dataGridView1.Rows[index].Cells[3].Value = temp.protocol;
                                    this.dataGridView1.Rows[index].Cells[4].Value = temp.info;
                                    this.dataGridView1.Rows[index].Cells[5].Value = packets.Count - 1;

                                    this.dataGridView1.FirstDisplayedScrollingRowIndex = this.dataGridView1.Rows.Count - 1;
                                }
                            }
                        }
                        catch (Exception)
                        {
                            ;
                        }
                    }
                    this.is_saved = true;
                    captureFileReader.Close();
                    MessageBox.Show("读取完毕");
                }
            }
        }
Пример #14
0
        private static Packet GetNextPacket()
        {
            if (_currentFapi != null)
            {
                Log.DebugFormat("currentFAPI.PacketIndexes.Count {0}," +
                                "currentPacketIndex {1}",
                                _currentFapi.PacketIndexes.Count,
                                _currentPacketIndex);
            }
            else
            {
                Log.Debug("currentFAPI is null");
            }

            // do we need to open a file up or are we done with the current file?
            if ((_packetFileName == null) ||
                (_currentFapi == null) ||
                (_currentFapi.PacketIndexes.Count == _currentPacketIndex))
            {
                Log.Debug("opening a new file up");

                // close the open device if there was one
                if (_captureFileReader != null)
                {
                    _captureFileReader.Close();
                    _captureFileReader = null;
                }

                // do we have any more files to process?
                if (_fileAndPacketIndex >= FilePacketIndexes.Count)
                {
                    Log.DebugFormat("totalPacketsReturned {0}, expectedTotalPackets {1}",
                                    _totalPacketsReturned,
                                    ExpectedTotalPackets);

                    Assert.AreEqual(ExpectedTotalPackets,
                                    _totalPacketsReturned,
                                    "expectedTotalPackets does not match totalPacketsReturned");

                    return(null);
                }

                _currentFapi        = FilePacketIndexes[_fileAndPacketIndex];
                _currentPacketIndex = 0;
                _packetFileName     = _currentFapi.Filename;

                // opening a new file, we are at the first index into the new file
                _indexIntoPacketFile = 0;

                try
                {
                    Log.DebugFormat("Opening {0}", _currentFapi.Filename);

                    _captureFileReader = new SharpPcap.LibPcap.CaptureFileReaderDevice(_currentFapi.Filename);
                    _captureFileReader.Open();

                    _fileAndPacketIndex++;
                }
                catch (Exception e)
                {
                    Log.Error("caught exception", e);
                    throw;
                }
            }

            Packet p;

            do
            {
                Log.DebugFormat("currentPacketIndex {0}", _currentPacketIndex);
                Log.DebugFormat("indexIntoPacketFile {0}, currentFAPI.PacketIndexes[currentPacketIndex] {1}",
                                _indexIntoPacketFile,
                                _currentFapi.PacketIndexes[_currentPacketIndex]);

                Log.Debug("retrieving packet");

                // read the next packet
                var packet = _captureFileReader.GetNextPacket();
                Assert.IsNotNull(packet, "Expected a valid packet but it was null");

                p = Packet.ParsePacket(packet.LinkLayerType, packet.Data);

                _currentPacketDescription = _currentFapi.PacketDescription[_currentPacketIndex];

                // advance our index into the current packet file
                _indexIntoPacketFile++;
            }while ((_indexIntoPacketFile - 1) != _currentFapi.PacketIndexes[_currentPacketIndex]);
            // does the current index match the index of the packet we want?

            // and because we got a packet we advance our index into the FileAndPacketIndex class
            _currentPacketIndex++;

            Log.Debug("returning packet");
            _totalPacketsReturned++;
            return(p);
        }
Пример #15
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;
            }
        }
Пример #16
0
        //点击打开文件按钮
        private void openButton_Click(object sender, EventArgs e)
        {
            string loadPath = "";

            //windows打开文件的对话框
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = "c:\\";
            openFileDialog.Filter = "pcap文件(*.pcap)|*.pcap";
            openFileDialog.RestoreDirectory = true;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                loadPath = openFileDialog.FileName;
                startButton.Enabled = true;
                stopButton.Enabled = false;
                restartButton.Enabled = false;

                if (this.device != null)
                {
                    this.device.StopCapture();
                    this.device.Close();
                }

                listView.Items.Clear();
                treeView.Nodes.Clear();
                showDataBox.Clear();
            }
            if (loadPath != "")
            {
                ICaptureDevice captureReaderDevice = new SharpPcap.LibPcap.CaptureFileReaderDevice(loadPath);
                captureReaderDevice.OnPacketArrival += new PacketArrivalEventHandler(device_OnPacketArrival);
                this.is_loadFlie = true;
                deviceComboBox.Enabled = true;
                resetView();
                FilterForm.init_changed_device();
                captureReaderDevice.Capture();      //开始读取文件
            }
        }
Пример #17
0
        private void ResetPacketPosition()
        {
            fileAndPacketIndex = 0;
            currentPacketIndex = 0;
            packetFileName = null;

            indexIntoPacketFile = 0;

            totalPacketsReturned = 0;

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