Exemple #1
0
 private void SavePackets()
 {
     lock (LockForPacketOnSaveAndOnAddNew)
     {
         foreach (KeyValuePair <ICaptureDevice, ArrayList> dev in DevicesAndPackets)
         {
             if (dev.Key.Started)
             {
                 string dev_friendlyname = dev.Key.ToString();
                 dev_friendlyname = dev_friendlyname.Substring(dev_friendlyname.IndexOf("FriendlyName: "), dev_friendlyname.Length - dev_friendlyname.IndexOf("FriendlyName: ") - "FriendlyName: ".Length);
                 dev_friendlyname = dev_friendlyname.Substring("FriendlyName: ".Length, dev_friendlyname.IndexOf('\n') - "FriendlyName: ".Length);
                 string CapFileName = dev_friendlyname + DateTime.Now.ToString() + ".pcap";
                 CapFileName = CleanFileName(CapFileName);
                 //CapFileName=CapFileName.Replace("/", "-");
                 //CapFileName = CapFileName.Replace(":", "_");
                 SharpPcap.LibPcap.CaptureFileWriterDevice captureFileWriter = new SharpPcap.LibPcap.CaptureFileWriterDevice((SharpPcap.LibPcap.LibPcapLiveDevice)dev.Key, CapFileName);
                 //int count = dev.Value.Count;
                 foreach (RawCapture p in dev.Value)
                 {
                     captureFileWriter.Write(p);
                 }
             }
         }
     }
 }
Exemple #2
0
        //
        // save file
        //
        private void button3_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.SaveFileDialog savefile1 = new SaveFileDialog();
            savefile1.InitialDirectory = Environment.CurrentDirectory;
            savefile1.Filter           = "pcap files (*.pcap)|*.pcap";
            savefile1.AddExtension     = true;
            savefile1.RestoreDirectory = true;

            if (savefile1.ShowDialog() == DialogResult.OK && 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("success!");
                }
                catch (Exception)
                {
                    MessageBox.Show("fail!");
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// 保存为Pcap格式文件
        /// </summary>
        private void button8_Click(object sender, EventArgs e)
        {
            string capFile = "";

            SaveFileDialog sfd = new SaveFileDialog();
            sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Templates);
            sfd.Filter = "PCAP(*.pcap)|*.pcap";
            sfd.OverwritePrompt = true;
            sfd.AddExtension = true;
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    capFile = sfd.FileName;
                    this.device.Open();
                    SharpPcap.LibPcap.CaptureFileWriterDevice captureFileWriter = new SharpPcap.LibPcap.CaptureFileWriterDevice((SharpPcap.LibPcap.LibPcapLiveDevice)this.device, capFile);
                    int count = this.packets.Count;
                    foreach (packet i in this.packets)
                    {
                        captureFileWriter.Write(i.pPacket);
                    }
                    this.device.Close();
                    this.is_saved = true;
                    MessageBox.Show("保存完毕");
                }
                catch (Exception er)
                {
                    MessageBox.Show(er.Message);
                }
            }
        }
Exemple #4
0
        //点击保存文件按钮
        private void saveButton_Click(object sender, EventArgs e)
        {
            if (!stopButton.Enabled)
            {
                string savePath = "";

                //windows保存文件的对话框
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.InitialDirectory = "c:\\";
                saveFileDialog.Filter = "pcap文件(*.pcap)|*.pcap";
                saveFileDialog.DefaultExt = ".pcap";
                saveFileDialog.RestoreDirectory = true;
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    savePath = saveFileDialog.FileName;
                }
                if (savePath != "")
                {
                    SharpPcap.LibPcap.CaptureFileWriterDevice captureFilerWriter = new SharpPcap.LibPcap.CaptureFileWriterDevice(savePath, System.IO.FileMode.OpenOrCreate);
                    foreach (var packet in packet_save)
                    {
                        captureFilerWriter.Write(packet);
                    }
                    MessageBox.Show("文件已保存在" + savePath);
                }

            }
        }
Exemple #5
0
//// /////////////////////////////////////////////////////////////////////////////////////////////////////////
        ///---------------------------------------文件保存部分(需调用Search窗体)-------------------------------------------------------------
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////

        private void button3_Click(object sender, EventArgs e)
        {
            string capFile = "";

            SaveFileDialog sfd = new SaveFileDialog();

            sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Templates);
            sfd.Filter           = "PCAP(*.pcap)|*.pcap";
            sfd.OverwritePrompt  = true;
            sfd.AddExtension     = true;
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    capFile = sfd.FileName;
                    this.device.Open();
                    SharpPcap.LibPcap.CaptureFileWriterDevice captureFileWriter = new SharpPcap.LibPcap.CaptureFileWriterDevice((SharpPcap.LibPcap.LibPcapLiveDevice) this.device, capFile);
                    int count = this.packets.Count;
//                    foreach (packet i in this.packets)
//                    {
//                      if(i.index==)captureFileWriter.Write(i.pPacket);
//                    }
                    foreach (DataGridViewRow r in dataGridView1.Rows)
                    {
                        if (r.Selected || r.Cells[0].Selected || r.Cells[1].Selected || r.Cells[2].Selected || r.Cells[3].Selected || r.Cells[4].Selected || r.Cells[5].Selected)
                        {
                            int n = int.Parse(r.Cells[5].Value.ToString());
                            captureFileWriter.Write(((packet)packets[n]).pPacket);
                        }
                    }
                    this.device.Close();

                    MessageBox.Show("保存完毕");
                }
                catch (Exception er)
                {
                    MessageBox.Show(er.Message);
                }
            }

            /*string filepath="";
             * SaveFileDialog fd = new SaveFileDialog();
             * fd.FileName = Application.StartupPath;
             * fd.ShowDialog();
             * filepath = fd.FileName;
             * FileStream fs = new FileStream(filepath, FileMode.Create);
             * byte[] bytes = Encoding.Default.GetBytes("本文件为系统保存包时记录的可读文件记录\r\n");
             * int length = bytes.Length;
             * fs.Write(bytes, 0, length);*/

            /*
             * this.dataGridView1.Rows[index].Cells[0].Value = Packet.protocol;
             * this.dataGridView1.Rows[index].Cells[1].Value = Packet.srcIp;
             * this.dataGridView1.Rows[index].Cells[2].Value = Packet.destIp;
             * this.dataGridView1.Rows[index].Cells[3].Value = Packet.time;
             * this.dataGridView1.Rows[index].Cells[4].Value = Packet.info;
             * this.dataGridView1.Rows[index].Cells[5].Value = packet_index;
             */
            /*
             * foreach (packet p in packets)
             * {
             *  bytes = Encoding.Default.GetBytes("=================================\r\n简易信息:\r\nProtocol:  " + p.protocol + "\r\nSource IP:  " + p.srcIp + "\r\nDestination IP:  " + p.destIp + "\r\nTime:  " + p.time + "\r\nInfo:  " + p.info + "\r\n---------------------------------\r\n详细信息: \r\n");
             *  length = bytes.Length;//Encoding.Default.GetByteCount("简易信息:\r\nProtocol:  " + p.protocol + "\r\nSource IP:  " + p.srcIp + "\r\nDestination IP:  " + p.destIp + "\r\nTime:  " + p.time + "\r\nInfo:  " + p.info + "\r\n详细信息: \r\n");
             *  fs.Write(bytes, 0, length);
             *  foreach (KeyValuePair<string, string> kv in p.frame_info)
             *  {
             *      bytes = Encoding.Default.GetBytes(kv.Key + " : " + kv.Value + "\r\n");
             *      length = bytes.Length;//Encoding.Default.GetByteCount(kv.Key + " : " + kv.Value + "\r\n");
             *      fs.Write(bytes, 0, length);
             *  }
             *  foreach (KeyValuePair<string, string> kv in p.ethernet_info)
             *  {
             *      bytes = Encoding.Default.GetBytes(kv.Key + " : " + kv.Value + "\r\n");
             *      length = bytes.Length;//Encoding.Default.GetByteCount(kv.Key + " : " + kv.Value + "\r\n");
             *      fs.Write(bytes, 0, length);
             *  }
             *  foreach (KeyValuePair<string, string> kv in p.ip_info)
             *  {
             *      bytes = Encoding.Default.GetBytes(kv.Key + " : " + kv.Value + "\r\n");
             *      length = bytes.Length;//Encoding.Default.GetByteCount(kv.Key + " : " + kv.Value + "\r\n");
             *      fs.Write(bytes, 0, length);
             *  }
             *  foreach (KeyValuePair<string, string> kv in p.arp_info)
             *  {
             *      bytes = Encoding.Default.GetBytes(kv.Key + " : " + kv.Value + "\r\n");
             *      length = bytes.Length;//Encoding.Default.GetByteCount(kv.Key + " : " + kv.Value + "\r\n");
             *      fs.Write(bytes, 0, length);
             *  }
             *  foreach (KeyValuePair<string, string> kv in p.icmp_info)
             *  {
             *      bytes = Encoding.Default.GetBytes(kv.Key + " : " + kv.Value + "\r\n");
             *      length = bytes.Length;//Encoding.Default.GetByteCount(kv.Key + " : " + kv.Value + "\r\n");
             *      fs.Write(bytes, 0, length);
             *  }
             *  foreach (KeyValuePair<string, string> kv in p.igmp_info)
             *  {
             *      bytes = Encoding.Default.GetBytes(kv.Key + " : " + kv.Value + "\r\n");
             *      length = bytes.Length;//Encoding.Default.GetByteCount(kv.Key + " : " + kv.Value + "\r\n");
             *      fs.Write(bytes, 0, length);
             *  }
             *  foreach (KeyValuePair<string, string> kv in p.tcp_info)
             *  {
             *      bytes = Encoding.Default.GetBytes(kv.Key + " : " + kv.Value + "\r\n");
             *      length = bytes.Length;//Encoding.Default.GetByteCount(kv.Key + " : " + kv.Value + "\r\n");
             *      fs.Write(bytes, 0, length);
             *  }
             *  foreach (KeyValuePair<string, string> kv in p.udp_info)
             *  {
             *      bytes = Encoding.Default.GetBytes(kv.Key + " : " + kv.Value + "\r\n");
             *      length = bytes.Length;//Encoding.Default.GetByteCount(kv.Key + " : " + kv.Value + "\r\n");
             *      fs.Write(bytes, 0, length);
             *  }
             *  foreach (KeyValuePair<string, string> kv in p.application_info)
             *  {
             *      bytes = Encoding.Default.GetBytes(kv.Key + " : " + kv.Value + "\r\n");
             *      length = bytes.Length;//Encoding.Default.GetByteCount(kv.Key + " : " + kv.Value + "\r\n");
             *      fs.Write(bytes, 0, length);
             *  }
             *  if (p.application_byte != null)
             *  {
             *      length = p.application_byte.Length;
             *      fs.Write(p.application_byte, 0, length);
             *  }
             * }
             * fs.Close();
             * MessageBox.Show("保存路径:" + filepath, "文件已保存");
             */
        }