예제 #1
0
        /// <summary>
        /// Creates a HardwareDevice object based on its SharpPcap representation.
        /// </summary>
        /// <param name="dev"></param>
        public HardwareDevice(ICaptureDevice dev)
        {
            this.Device = dev;
            string input = dev.ToString();

            IpAddresses = new List <String>();

            List <String> detectedAddrs = new List <String>();

            IPAddress[] localIps = Dns.GetHostAddresses(Dns.GetHostName());

            foreach (IPAddress ip in localIps)
            {
                string addr = ip.ToString();

                // scrub out the %xxx at the end (if present)
                int percentSignPos = addr.IndexOf("%");
                if (percentSignPos > 0)
                {
                    addr = addr.Substring(0, percentSignPos);
                }

                detectedAddrs.Add(addr);
            }

            // break everything up so we can parse line-by-line
            string[] deviceFields = input.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string line in deviceFields)
            {
                if (line.StartsWith("FriendlyName"))
                {
                    FriendlyName = line.Replace("FriendlyName: ", "");
                }
                else if (line.StartsWith("Description"))
                {
                    Description = line.Replace("Description: ", "");
                }

                /*	IP or MAC address
                 *      Only add if the IP address here exists in the detected list
                 *      (that is, the list returned by Dns.GetHostAddresses() )
                 */
                else if (line.StartsWith("Addr:"))
                {
                    string detectedIp = line.Replace("Addr:      ", "");

                    // MAC address
                    if (detectedIp.Contains("HW addr: "))
                    {
                        MacAddress = detectedIp.Replace("HW addr: ", "");
                    }

                    // IP address
                    if (detectedAddrs.Contains(detectedIp))
                    {
                        IpAddresses.Add(detectedIp);
                    }
                }
            }
        }
예제 #2
0
 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
 {
     try
     {
         Action action = () =>
         {
             path = comboBox1.Text;
         };
         Invoke(action);
     }
     catch { }
     device = devices[comboBox1.SelectedIndex];
     while (dataGridView1.Rows.Count != 0)
     {
         dataGridView1.Rows.Remove(dataGridView1.Rows[0]);
     }
     toolStripStatusLabel1.Text = "Packets: capture is not started";
     toolStripStatusLabel2.Text = "";
     device.Open(DeviceMode.Promiscuous, 3000);
     string info;
     info = device.ToString().Substring(11);
     info = info.Substring(0, info.Length - 2);
     label1.Text = info;
     label2.Text = "";
     panel1.Focus();
 }
예제 #3
0
        public override string ToString()
        {
            string msg = "";

            msg += string.Format("网络地址:{0}\n", _net.ToString());
            msg += string.Format("子网掩码:{0}\n", _mask.ToString());
            msg += string.Format("下一跳:{0}\n", _nextHop.ToString());
            msg += string.Format("优先级:{0}\n", _level);
            msg += _outInterface.ToString();
            return(msg);
        }
예제 #4
0
 //parses interface informations and return string containing interface name
 public static String getFriendlyName(ICaptureDevice device)
 {
     String[] splitStr = device.ToString().Split(':', '\n');
     for (int i = 0; i < splitStr.Length; i++)
     {
         if (splitStr[i].Equals("FriendlyName"))
         {
             return(splitStr[i + 1].Trim());
         }
     }
     return("");
 }
        public static string GetName(this ICaptureDevice captureDevice)
        {
            foreach (var line in captureDevice.ToString().Split('\n'))
            {
                if (line.Contains("FriendlyName"))
                {
                    var firstColonIndex = line.IndexOf(':');
                    return(line.Substring(firstColonIndex + 1, line.Length - firstColonIndex - 1).Trim());
                }
            }

            throw new Exception("Could not get name of device");
        }
예제 #6
0
        //ToolTip toolTip1 = new ToolTip();

        public PeerMapStart()
        {
            InitializeComponent();
            lblMessage.Visibility = Visibility.Collapsed;
            try
            {
                devices = CaptureDeviceList.Instance;
            }
            catch
            {
                lblMessage.Text       = "没有发现可用的网络适配器,请检查网卡设置以及是否安装WinPcap";
                lblMessage.Visibility = Visibility.Visible;
                return;
            }
            for (int i = 0; i < devices.Count; i++)
            {
                ICaptureDevice dev      = devices[i];
                RadioButton    radioBtn = new RadioButton();

                radioBtn.Content = dev.Description;
                //radioBtn.AutoSize = true;
                radioBtn.HorizontalContentAlignment = HorizontalAlignment.Left;
                //radioBtn.TextAlign = ContentAlignment.MiddleLeft;
                radioBtn.ToolTip = dev.ToString();
                //toolTip1.SetToolTip(radioBtn, dev.ToString());
                radioBtns.Add(radioBtn);
                canvas.Children.Add(radioBtn);
                Canvas.SetLeft(radioBtn, 30d);
                Canvas.SetTop(radioBtn, 20.0 + 20.0 * i);

//                 radioBtn.Location = new Point(30, 20 + 20 * i);
//                 this.gBoxPeerMap.Controls.Add(radioBtns[i]);
            }
            if (devices.Count == 1)
            {
                radioBtns[0].IsChecked = true;
            }
        }
예제 #7
0
        public BugSnifferService Init()
        {
            _logger.LogInformation($"Bugs! Sniffer Service using SharpPcap {SharpPcap.Version.VersionString}");
            _logger.LogInformation("Reading computer's network devices");

            CaptureDeviceList devices = CaptureDeviceList.Instance;

            if (devices.Count < 1)
            {
                _logger.LogError("COULD NOT FIND ANY DEVICES!!!!");
                throw new ArgumentNullException();
            }

            string localIPAddress = LocalIPAddress()?.ToString() ?? string.Empty;

            Console.WriteLine($"Your Local IP Address is {localIPAddress}");

            if (devices.Count(device => device.ToString().Contains(localIPAddress)) == 1)
            {
                _device = devices.Single(device => device.ToString().Contains(localIPAddress));
                _logger.LogInformation("Automatically Selected Device:");
                _logger.LogInformation(_device.ToString());
            }
            else
            {
                Console.WriteLine();
                Console.WriteLine("The following devices are available on this machine:");
                Console.WriteLine("----------------------------------------------------");
                Console.WriteLine();

                for (int i = 0; i < devices.Count; i++)
                {
                    Console.WriteLine($"-----------Device {i}-----------");
                    Console.WriteLine(devices[i].ToString());
                }

                Console.WriteLine("Usually device with local ip address is the correct one.");

                int deviceIndex = -1;

                do
                {
                    Console.Write("Please select which device you would like to use: ");

                    string input = Console.ReadLine();

                    deviceIndex = int.TryParse(input, out int result) ? result : -1;
                } while (deviceIndex < 0 || deviceIndex >= devices.Count);

                _device = devices[deviceIndex];

                _logger.LogInformation("Chosen Device:");
                _logger.LogInformation(_device.ToString());
            }

            _logger.LogInformation("Opening Device");
            _logger.LogInformation($"Using Timeout: {Timeout}");
            _device.Open(DeviceMode.Promiscuous, Timeout);

            return(this);
        }
예제 #8
0
        // Util to parse ICaptureDevice.ToString() into the fields of interest,
        // an array of 3 strings containing Name, GW address, and IP address
        public static string[] parseDeviceDescription(ICaptureDevice dev)
        {
            char[] separators = new char[] { '\n', ':' };
            string name = "";
            string gwAddress = "";
            string ipAddress = "";

            //            Console.WriteLine("Parsing Device: {0}", dev);
            string[] splitStr = dev.ToString().Split(separators);
            for (int i = 0; i < splitStr.Length; i++)
            {
                if (splitStr[i].Equals("FriendlyName"))
                {
                    name = splitStr[i + 1].Trim();
                }
                else if (splitStr[i].Equals("GatewayAddress"))
                {
                    gwAddress = splitStr[i + 1].Trim();
                }
                else if (splitStr[i].Equals("Addr"))
                {
                    // could be IPv6, IPv4 or MAC addr entry
                    if (splitStr[i + 1].Contains('.'))
                    {
                        // The IPv4 addr
                        ipAddress = splitStr[i + 1].Trim();
                    }
                }
            }
            // Fallback, could be needed if constant strings used above
            // are EN-US locale dependent (I haven't tested this in other locales)
            if (name.Length == 0)
            {
                // non english output?
                if (splitStr.Length > 7)
                {
                    name = splitStr[4].Trim();
                    gwAddress = splitStr[6].Trim();
                }
            }
            // one last fallback
            if (name.Length == 0)
            {
                name = dev.ToString();
            }
            return new string[] { ipAddress, name };
        }
예제 #9
0
 public static string GetFriendlyName(ICaptureDevice dev)
 {
     string ret = null;
     string toString = dev.ToString();
     string[] separators = { "\n" };
     string[] lines = toString.Split(separators, StringSplitOptions.RemoveEmptyEntries);
     foreach (string line in lines)
     {
         //Console.WriteLine(line);
         if (line.Contains("FriendlyName:"))
         {
             ret = line.Substring(13);
         }
     }
     return ret;
 }