Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a Windows Service logon credentials

There are numerous examples of how to set the user logon credentials for a Windows Service however I can't discover how you first ascertain what the current credentials are set for that Windows Service.

What I want to do is:

If(WinService.logonCredentials == LocalUser)
   WinService.logonCredentials = new logonCredentials;

Is there a class that I can access that will give me the required data and/or possibly other settings for the Windows Service as well?

like image 537
David Avatar asked Sep 06 '25 09:09

David


2 Answers

Im not sure, but if you cant do it with classes built in .net probably only way is using WMI.

This is Win32Service class:

    class Win32_Service : Win32_BaseService
    {
      boolean  AcceptPause;
      boolean  AcceptStop;
      string   Caption;
      uint32   CheckPoint;
      string   CreationClassName;
      string   Description;
      boolean  DesktopInteract;
      string   DisplayName;
      string   ErrorControl;
      uint32   ExitCode;
      datetime InstallDate;
      string   Name;
      string   PathName;
      uint32   ProcessId;
      uint32   ServiceSpecificExitCode;
      string   ServiceType;
      boolean  Started;
      string   StartMode;
      string   StartName;
      string   State;
      string   Status;
      string   SystemCreationClassName;
      string   SystemName;
      uint32   TagId;
      uint32   WaitHint;
    };

And this is what you are asking:

        string   StartName;

I used PowerShell to get data about "Remote desktop" service on my laptop and i got a more data like this (some of that data are properties from Win32_BaseService, not Win32Service):

    DesktopInteract         : False
    DisconnectedSessions    : 1
    DisplayName             : Remote desktop services
    ErrorControl            : Normal
    ExitCode                : 1077
    InstallDate             :
    Name                    : TermService
    PathName                : C:\Windows\System32\svchost.exe -k NetworkService
    ProcessId               : 0
    ServiceSpecificExitCode : 0
    ServiceType             : Share Process
    Started                 : False
    StartMode               : Manual
    StartName               : NT Authority\NetworkService
    State                   : Stopped
    Status                  : OK
    SystemCreationClassName : Win32_ComputerSystem
    SystemName              : NOTEBOOK
    TagId                   : 0
    TotalSessions           : 2
    WaitHint                : 0

I cant help with WMI in C#. Maybe you will find StartName property somewhere in class which you are using (i dont know what class it is because u didnt wrote).

like image 85
Kamil Avatar answered Sep 07 '25 23:09

Kamil


Here is how to retrieve this information using WMI in C#:

public string GetWindowsServiceLoginCredentials(string serviceName)
    {
        var credentials = (
            from x 
                in new ManagementObjectSearcher($"SELECT StartName FROM Win32_Service WHERE Caption = '{serviceName}'")
                .Get()
                .Cast<ManagementObject>()
            select 
                x.GetPropertyValue("StartName")).FirstOrDefault();

        return credentials != null ? credentials.ToString() : "";
    }

You can use this to get other properties of the service, just change out the 'StartName' for the property you're interested in.

like image 24
Pete Magsig Avatar answered Sep 07 '25 23:09

Pete Magsig