Beispiel #1
0
        public static void DumpServices(MemoryFileSystem mfs)
        {
            using (var services = mfs.RootObject.GetChild("Services"))
            {
                foreach (var service in Windows.GetServices().Values)
                {
                    using (var serviceChild = services.CreateChild(service.ServiceName))
                    {
                        BinaryWriter bw = new BinaryWriter(serviceChild.GetWriteStream());

                        bw.Write("Name", service.ServiceName);
                        bw.Write("DisplayName", service.DisplayName);
                        bw.Write("Type", (int)service.ServiceStatusProcess.ServiceType);
                        bw.Write("State", (int)service.ServiceStatusProcess.CurrentState);
                        bw.Write("ProcessId", service.ServiceStatusProcess.ProcessID);
                        bw.Write("ControlsAccepted", (int)service.ServiceStatusProcess.ControlsAccepted);
                        bw.Write("Flags", (int)service.ServiceStatusProcess.ServiceFlags);

                        try
                        {
                            QueryServiceConfig config;

                            using (var shandle = new ServiceHandle(service.ServiceName, ServiceAccess.QueryConfig))
                            {
                                config = shandle.GetConfig();

                                bw.Write("StartType", (int)config.StartType);
                                bw.Write("ErrorControl", (int)config.ErrorControl);
                                bw.Write("BinaryPath", FileUtils.GetFileName(config.BinaryPathName));
                                bw.Write("Group", config.LoadOrderGroup);
                                bw.Write("UserName", config.ServiceStartName);

                                bw.Write("Description", shandle.GetDescription());
                            }

                            if (config.ServiceType == ServiceType.Win32ShareProcess)
                            {
                                try
                                {
                                    using (var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(
                                               "SYSTEM\\CurrentControlSet\\Services\\" + service.ServiceName + "\\Parameters"))
                                    {
                                        bw.Write(
                                            "ServiceDll",
                                            Environment.ExpandEnvironmentVariables((string)key.GetValue("ServiceDll"))
                                            );
                                    }
                                }
                                catch
                                { }
                            }
                        }
                        catch
                        { }

                        bw.Close();
                    }
                }
            }
        }
        private void UpdateInformation()
        {
            checkChangePassword.Checked = false;

            if (listServices.SelectedItems.Count == 0)
            {
                buttonApply.Enabled        = false;
                buttonStart.Enabled        = false;
                buttonStop.Enabled         = false;
                buttonDependents.Enabled   = false;
                buttonDependencies.Enabled = false;
                buttonPermissions.Enabled  = false;
                comboType.Enabled          = false;
                comboStartType.Enabled     = false;
                comboErrorControl.Enabled  = false;
                _oldConfig = new QueryServiceConfig();
                this.ClearControls();
            }
            else
            {
                try
                {
                    buttonApply.Enabled        = true;
                    buttonStart.Enabled        = true;
                    buttonStop.Enabled         = true;
                    buttonDependents.Enabled   = true;
                    buttonDependencies.Enabled = true;
                    buttonPermissions.Enabled  = true;
                    comboType.Enabled          = true;
                    comboStartType.Enabled     = true;
                    comboErrorControl.Enabled  = true;

                    try
                    {
                        using (var shandle =
                                   new ServiceHandle(listServices.SelectedItems[0].Name, ServiceAccess.QueryConfig))
                            _provider.UpdateServiceConfig(listServices.SelectedItems[0].Name, shandle.GetConfig());
                    }
                    catch
                    { }

                    ServiceItem item = _provider.Dictionary[listServices.SelectedItems[0].Name];

                    _oldConfig = item.Config;
                    _oldConfig.BinaryPathName = FileUtils.GetFileName(_oldConfig.BinaryPathName);

                    buttonStart.Enabled = true;
                    buttonStop.Enabled  = true;

                    if (item.Status.ServiceStatusProcess.CurrentState == ServiceState.Running)
                    {
                        buttonStart.Enabled = false;
                    }
                    else if (item.Status.ServiceStatusProcess.CurrentState == ServiceState.Stopped)
                    {
                        buttonStop.Enabled = false;
                    }

                    if ((item.Status.ServiceStatusProcess.ControlsAccepted & ServiceAccept.Stop) == 0)
                    {
                        buttonStop.Enabled = false;
                    }

                    labelServiceName.Text        = item.Status.ServiceName;
                    labelServiceDisplayName.Text = item.Status.DisplayName;
                    comboType.SelectedItem       = item.Config.ServiceType.ToString();

                    if (item.Config.ServiceType ==
                        (ProcessHacker.Native.Api.ServiceType.Win32OwnProcess |
                         ProcessHacker.Native.Api.ServiceType.InteractiveProcess))
                    {
                        comboType.SelectedItem = "Win32OwnProcess, InteractiveProcess";
                    }
                    else if (item.Config.ServiceType ==
                             (ProcessHacker.Native.Api.ServiceType.Win32ShareProcess |
                              ProcessHacker.Native.Api.ServiceType.InteractiveProcess))
                    {
                        comboType.SelectedItem = "Win32ShareProcess, InteractiveProcess";
                    }

                    comboStartType.SelectedItem    = item.Config.StartType.ToString();
                    comboErrorControl.SelectedItem = item.Config.ErrorControl.ToString();
                    textServiceBinaryPath.Text     = FileUtils.GetFileName(item.Config.BinaryPathName);
                    textUserAccount.Text           = item.Config.ServiceStartName;
                    textLoadOrderGroup.Text        = item.Config.LoadOrderGroup;

                    try
                    {
                        using (var shandle
                                   = new ServiceHandle(item.Status.ServiceName, ServiceAccess.QueryConfig))
                            textDescription.Text = shandle.GetDescription();
                    }
                    catch
                    {
                        textDescription.Text = "";
                    }

                    textServiceDll.Text = "";

                    if (item.Config.ServiceType == ProcessHacker.Native.Api.ServiceType.Win32ShareProcess)
                    {
                        try
                        {
                            using (var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(
                                       "SYSTEM\\CurrentControlSet\\Services\\" + item.Status.ServiceName + "\\Parameters"))
                                textServiceDll.Text = Environment.ExpandEnvironmentVariables((string)key.GetValue("ServiceDll"));
                        }
                        catch
                        { }
                    }

                    try
                    {
                        using (ServiceController controller = new ServiceController(
                                   listServices.SelectedItems[0].Name))
                        {
                            if (controller.DependentServices.Length == 0)
                            {
                                buttonDependents.Enabled = false;
                            }
                            if (controller.ServicesDependedOn.Length == 0)
                            {
                                buttonDependencies.Enabled = false;
                            }
                        }
                    }
                    catch
                    {
                        buttonDependents.Enabled   = false;
                        buttonDependencies.Enabled = false;
                    }
                }
                catch (Exception ex)
                {
                    labelServiceName.Text = ex.Message;
                    _oldConfig            = new QueryServiceConfig();
                    this.ClearControls();
                }
            }
        }
        private void UpdateInformation()
        {
            checkChangePassword.Checked = false;

            if (listServices.SelectedItems.Count == 0)
            {
                buttonApply.Enabled = false;
                buttonStart.Enabled = false;
                buttonStop.Enabled = false;
                buttonDependents.Enabled = false;
                buttonDependencies.Enabled = false;
                buttonPermissions.Enabled = false;
                comboType.Enabled = false;
                comboStartType.Enabled = false;
                comboErrorControl.Enabled = false;         
                _oldConfig = new QueryServiceConfig();
                this.ClearControls();
            }
            else
            {
                try
                {
                    buttonApply.Enabled = true;
                    buttonStart.Enabled = true;
                    buttonStop.Enabled = true;
                    buttonDependents.Enabled = true;
                    buttonDependencies.Enabled = true;
                    buttonPermissions.Enabled = true;
                    comboType.Enabled = true;
                    comboStartType.Enabled = true;
                    comboErrorControl.Enabled = true;

                    try
                    {
                        using (var shandle = 
                            new ServiceHandle(listServices.SelectedItems[0].Name, ServiceAccess.QueryConfig))
                            _provider.UpdateServiceConfig(listServices.SelectedItems[0].Name, shandle.GetConfig());
                    }
                    catch
                    { }

                    ServiceItem item = _provider.Dictionary[listServices.SelectedItems[0].Name];

                    _oldConfig = item.Config;
                    _oldConfig.BinaryPathName = FileUtils.GetFileName(_oldConfig.BinaryPathName);

                    buttonStart.Enabled = true;
                    buttonStop.Enabled = true;

                    if (item.Status.ServiceStatusProcess.CurrentState == ServiceState.Running)
                        buttonStart.Enabled = false;
                    else if (item.Status.ServiceStatusProcess.CurrentState == ServiceState.Stopped)
                        buttonStop.Enabled = false;

                    if ((item.Status.ServiceStatusProcess.ControlsAccepted & ServiceAccept.Stop) == 0)
                        buttonStop.Enabled = false;

                    labelServiceName.Text = item.Status.ServiceName;
                    labelServiceDisplayName.Text = item.Status.DisplayName;
                    comboType.SelectedItem = item.Config.ServiceType.ToString();

                    if (item.Config.ServiceType ==
                        (ProcessHacker.Native.Api.ServiceType.Win32OwnProcess |
                        ProcessHacker.Native.Api.ServiceType.InteractiveProcess))
                        comboType.SelectedItem = "Win32OwnProcess, InteractiveProcess";
                    else if (item.Config.ServiceType ==
                        (ProcessHacker.Native.Api.ServiceType.Win32ShareProcess |
                        ProcessHacker.Native.Api.ServiceType.InteractiveProcess))
                        comboType.SelectedItem = "Win32ShareProcess, InteractiveProcess";

                    comboStartType.SelectedItem = item.Config.StartType.ToString();
                    comboErrorControl.SelectedItem = item.Config.ErrorControl.ToString();
                    textServiceBinaryPath.Text = FileUtils.GetFileName(item.Config.BinaryPathName);
                    textUserAccount.Text = item.Config.ServiceStartName;
                    textLoadOrderGroup.Text = item.Config.LoadOrderGroup;

                    try
                    {
                        using (var shandle
                            = new ServiceHandle(item.Status.ServiceName, ServiceAccess.QueryConfig))
                            textDescription.Text = shandle.GetDescription();
                    }
                    catch
                    {
                        textDescription.Text = string.Empty;
                    }

                    textServiceDll.Text = string.Empty;

                    if (item.Config.ServiceType == ProcessHacker.Native.Api.ServiceType.Win32ShareProcess)
                    {
                        try
                        {
                            using (var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(
                                "SYSTEM\\CurrentControlSet\\Services\\" + item.Status.ServiceName + "\\Parameters"))
                                textServiceDll.Text = Environment.ExpandEnvironmentVariables((string)key.GetValue("ServiceDll"));
                        }
                        catch
                        { }
                    }

                    try
                    {
                        using (ServiceController controller = new ServiceController(
                            listServices.SelectedItems[0].Name))
                        {
                            if (controller.DependentServices.Length == 0)
                                buttonDependents.Enabled = false;
                            if (controller.ServicesDependedOn.Length == 0)
                                buttonDependencies.Enabled = false;
                        }
                    }
                    catch
                    {
                        buttonDependents.Enabled = false;
                        buttonDependencies.Enabled = false;
                    }
                }
                catch (Exception ex)
                {
                    labelServiceName.Text = ex.Message;
                    _oldConfig = new QueryServiceConfig();
                    this.ClearControls();
                }
            }
        }