Exemple #1
0
        /// <summary>
        /// This is the main detection function called to recover the OS
        /// </summary>
        /// <param name="remoteCredentials"></param>
        /// <returns></returns>
        public int Detect(RemoteCredentials remoteCredentials)
        {
            Layton.Common.Controls.Impersonator impersonator = null;
            String remoteHost = remoteCredentials.RemoteHost;

            try
            {
                // We may want to impersonate a different user so that we can audit remote computers - if so
                // start the impersonation here
                if (remoteCredentials.Username != null && remoteCredentials.Username != "")
                {
                    impersonator = new Impersonator(remoteCredentials.Username, remoteCredentials.Domain, remoteCredentials.Password);
                }

                // Pickup and format the remote host name for WMI
                if (remoteCredentials.IsLocalComputer())
                {
                    remoteHost = @"\\localhost";
                }
                else
                {
                    remoteHost = @"\\" + remoteCredentials.RemoteHost;
                }

                //Connection credentials to the remote computer - not needed if the logged in account has access
                ConnectionOptions oConn = null;

                // Construct the path to the WMI node we are interested in
                String          path = remoteHost + @"\root\cimv2";
                ManagementScope scope;
                if (oConn == null)
                {
                    scope = new ManagementScope(path);
                }
                else
                {
                    scope = new ManagementScope(path, oConn);
                }

                // ...and connect
                scope.Connect();

                // Query the Operating System
                ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
                ManagementObjectSearcher   searcher        = new ManagementObjectSearcher(scope, query);
                ManagementObjectCollection queryCollection = searcher.Get();
                foreach (ManagementObject managementObject in queryCollection)
                {
                    _name             = managementObject["Caption"].ToString();
                    _name             = RationalizeOS(_name);
                    _serial           = new ApplicationSerial();
                    _serial.ProductId = managementObject["SerialNumber"].ToString();
                    managementObject.Dispose();
                    break;
                }

                // The above WMI call works as far as it goes however it cannot recover the CD Key
                // for this we will need to use registry access - we may as well use WMI for this as
                // well as if the above fails we stuck anyway
                DetectOSCdKey(remoteHost);
            }
            catch (Exception)
            {
                return(-1);
            }

            finally
            {
                if (impersonator != null)
                {
                    impersonator.Dispose();
                }
            }

            return(0);
        }