コード例 #1
0
ファイル: DhcpCapture.cs プロジェクト: jdehaan/DhcpCheck
 public DhcpCapture(Parameters parameters)
 {
     _parameters = parameters;
     _packets = new Queue<Packet>();
     _pcapFileWriter = new CaptureFileWriterDevice(
         LinkLayers.Ethernet, null, _parameters.PcapFilename, FileMode.OpenOrCreate);
 }
コード例 #2
0
 public void TestFileCreationAndDeletion()
 {
     var wd = new CaptureFileWriterDevice(@"abc.pcap");
     wd.Write(new byte[] {1, 2, 3, 4});
     wd.Close();
     System.IO.File.Delete(@"abc.pcap");
 }
コード例 #3
0
ファイル: DeviceWorker.cs プロジェクト: bmallred/WhiteNoise
        /// <summary>
        /// Initializes a new instance of the <see cref="WhiteNoise.DeviceWorker"/> class.
        /// </summary>
        /// <param name='lazyLoad'>
        /// Bypass loading of device list on loading.
        /// </param>
        public DeviceWorker(bool lazyLoad = false)
        {
            _captureFileWriter = null;
            _file = new FileInfo("results.pcap");

            this.Devices = new List<string>();
            this.Filter = "ip and tcp";
            this.Timeout = 1000;
            this.Version = SharpPcap.Version.VersionString;

            if (!lazyLoad)
            {
                this._devices = CaptureDeviceList.Instance;

                // Make a pretty list of devices.
                for (int i = 0; i < this._devices.Count; i++)
                {
                    this.Devices.Add(
                        string.Format("{1}. {2}{0}",
                            Environment.NewLine,
                            i + 1,
                            this._devices[i].Description)
                        );
                }
            }

            this.WorkerReportsProgress = true;
            this.WorkerSupportsCancellation = true;
        }
コード例 #4
0
        private void SaveFile_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.SaveFileDialog savefile1 = new SaveFileDialog();
            savefile1.InitialDirectory = Environment.CurrentDirectory;
            savefile1.Filter           = "pcap files (*.pcap)|*.pcap";
            savefile1.AddExtension     = true;
            savefile1.RestoreDirectory = true;
            savefile1.ShowDialog();

            if (savefile1.FileName.ToString() != "")
            {
                try{
                    string name = savefile1.FileName;
                    this.device.Open();
                    SharpPcap.LibPcap.CaptureFileWriterDevice captureFileWriter = new SharpPcap.LibPcap.CaptureFileWriterDevice((SharpPcap.LibPcap.LibPcapLiveDevice) this.device, name);
                    foreach (packet pac in this.packets)
                    {
                        captureFileWriter.Write(pac.rawp);
                    }
                    MessageBox.Show("Svae Success!");
                }
                catch (Exception) {
                    MessageBox.Show("Save Fail!");
                }
            }
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: wwhitehead/ProjectFaolan
        private static void WritePacketWrappers(string path, List<PacketWrapper> parsedPackets)
        {
            var writer = new CaptureFileWriterDevice(path, FileMode.CreateNew);
            writer.Open();

            foreach (var p in parsedPackets.SelectMany(pw => pw.GetWriteableCaptures()))
                writer.Write(p);

            writer.Close();
        }
コード例 #6
0
ファイル: MainForm.cs プロジェクト: Charming199/UserWatch
        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveFileDialog sd = new SaveFileDialog();

            sd.Filter = "Pcap文件|*.pcap";
            if (sd.ShowDialog() == DialogResult.OK)
            {
                var offDev = new SharpPcap.LibPcap.CaptureFileWriterDevice(sd.FileName);
                foreach (var i in packetList)
                {
                    offDev.Write(i);
                }
                MessageBox.Show("文件保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
コード例 #7
0
ファイル: MainWindow.xaml.cs プロジェクト: 2hanson/sniffer
        private void CaptureBegin()
        {
            // Open the device for capturing
            int readTimeoutMilliseconds = 1000;
            if (captureMode == 1)
            {
                device.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds);
            }
            else
            {
                device.Open(DeviceMode.Normal, readTimeoutMilliseconds);
            }
            string pcapFile = System.AppDomain.CurrentDomain.BaseDirectory;
            pcapFile += "\\pcapFile.Sniffer";
            captureFileWriter = new CaptureFileWriterDevice((LibPcapLiveDevice)this.device, pcapFile);

            this.canStopListening = true;
            //filter = "ip and tcp"
            if (this.filterRule != "")
            {
                device.Filter = this.filterRule;
            }

            this.continueGetPacket = true;
            this.PacketCount = 0;

            if (packets != null)
            {
                packets.Clear();
            }

            if (rawCapturesList != null)
            {
                rawCapturesList.Clear();
            }

            GetNextPacket();
        }
コード例 #8
0
ファイル: ExpSniffer.cs プロジェクト: CullenGao/ExpSniffer
 private void export_Click(object sender, EventArgs e)
 {
     string path = "";
     SaveFileDialog save = new SaveFileDialog();
     save.Filter = " pcap files(*.pcap)|*.pcap";
     save.FilterIndex = 2;
     save.RestoreDirectory = true;
     if (save.ShowDialog() == DialogResult.OK)
     {
         path = save.FileName.ToString();
     }
     if (path != "")
     {
         CaptureFileWriterDevice captureFileWriter = new CaptureFileWriterDevice(path, System.IO.FileMode.Create);
         foreach (RawCapture RawPacket in RawCaptureList)
         {
             captureFileWriter.Write(RawPacket);
         }
         MessageBox.Show("Saved.");
     }
 }
コード例 #9
0
ファイル: Main.cs プロジェクト: Praymundo/sharppcap
        public static void Main (string[] args)
        {
            // Print SharpPcap version
            string ver = SharpPcap.Version.VersionString;
            Console.WriteLine("SharpPcap {0}, CreatingCaptureFile", ver);

            // Retrieve the device list
            var devices = LibPcapLiveDeviceList.Instance;

            // If no devices were found print an error
            if(devices.Count < 1)
            {
                Console.WriteLine("No devices were found on this machine");
                return;
            }

            Console.WriteLine();
            Console.WriteLine("The following devices are available on this machine:");
            Console.WriteLine("----------------------------------------------------");
            Console.WriteLine();

            int i = 0;

            // Print out the devices
            foreach(var dev in devices)
            {
                /* Description */
                Console.WriteLine("{0}) {1} {2}", i, dev.Name, dev.Description);
                i++;
            }

            Console.WriteLine();
            Console.Write("-- Please choose a device to capture on: ");
            i = int.Parse( Console.ReadLine() );
            Console.Write("-- Please enter the output file name: ");
            string capFile = Console.ReadLine();

            var device = devices[i];

            // Register our handler function to the 'packet arrival' event
            device.OnPacketArrival +=
                new PacketArrivalEventHandler( device_OnPacketArrival );

            // Open the device for capturing
            int readTimeoutMilliseconds = 1000;
            if (device is AirPcapDevice)
            {
                // NOTE: AirPcap devices cannot disable local capture
                var airPcap = device as AirPcapDevice;
                airPcap.Open(SharpPcap.WinPcap.OpenFlags.DataTransferUdp, readTimeoutMilliseconds);
            }
            else if(device is WinPcapDevice)
            {
                var winPcap = device as WinPcapDevice;
                winPcap.Open(SharpPcap.WinPcap.OpenFlags.DataTransferUdp | SharpPcap.WinPcap.OpenFlags.NoCaptureLocal, readTimeoutMilliseconds);
            }
            else if (device is LibPcapLiveDevice)
            {
                var livePcapDevice = device as LibPcapLiveDevice;
                livePcapDevice.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds);
            }
            else
            {
                throw new System.InvalidOperationException("unknown device type of " + device.GetType().ToString());
            }

            Console.WriteLine();
            Console.WriteLine("-- Listening on {0} {1}, writing to {2}, hit 'Enter' to stop...",
                              device.Name, device.Description,
                              capFile);

            // open the output file
            captureFileWriter = new CaptureFileWriterDevice(device, capFile);

            // Start the capturing process
            device.StartCapture();

            // Wait for 'Enter' from the user.
            Console.ReadLine();

            // Stop the capturing process
            device.StopCapture();

            Console.WriteLine("-- Capture stopped.");

            // Print out the device statistics
            Console.WriteLine(device.Statistics.ToString());

            // Close the pcap device
            device.Close();
        }
コード例 #10
0
ファイル: DeviceWorker.cs プロジェクト: bmallred/WhiteNoise
        /// <summary>
        /// Captures the device.
        /// </summary>
        /// <param name='deviceNumber'>
        /// Device number.
        /// </param>
        public void CaptureDevice(int deviceNumber)
        {
            var device = this._devices[deviceNumber - 1];

            // Add event handler(s).
            device.OnPacketArrival += new PacketArrivalEventHandler(Device_OnPacketArrival);

            // Open each device requested for scan.
            if (device is AirPcapDevice)
            {
                // NOTE: AirPcap devices cannot disable local capture.
                var airDevice = device as AirPcapDevice;
                airDevice.Open(OpenFlags.DataTransferUdp, this.Timeout);
            }
            else if (device is WinPcapDevice)
            {
                var winDevice = device as WinPcapDevice;
                winDevice.Open(OpenFlags.DataTransferUdp | OpenFlags.NoCaptureLocal, this.Timeout);
            }
            else
            {
                device.Open(DeviceMode.Promiscuous, this.Timeout);
            }

            // Apply the filter *only* after the device is open.
            device.Filter = this.Filter;

            // Create the capture file writer.
            _captureFileWriter = new CaptureFileWriterDevice((LibPcapLiveDevice)device, _file.FullName);

            // Start capturing.
            device.StartCapture();
        }