Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,40 @@ internal void WriteNonTerminatingError(
WriteError(new ErrorRecord(exception, errorId, category, targetObject));
}

internal void SetServiceSecurityDescriptor(
ServiceController service,
string securityDescriptorSddl,
NakedWin32Handle hService)
{
var rawSecurityDescriptor = new RawSecurityDescriptor(securityDescriptorSddl);
RawAcl rawDiscretionaryAcl = rawSecurityDescriptor.DiscretionaryAcl;
var discretionaryAcl = new DiscretionaryAcl (false, false, rawDiscretionaryAcl);

byte[] rawDacl = new byte[discretionaryAcl.BinaryLength];
discretionaryAcl.GetBinaryForm(rawDacl, 0);
rawSecurityDescriptor.DiscretionaryAcl = new RawAcl(rawDacl, 0);
byte[] securityDescriptorByte = new byte[rawSecurityDescriptor.BinaryLength];
rawSecurityDescriptor.GetBinaryForm(securityDescriptorByte, 0);

bool status = NativeMethods.SetServiceObjectSecurity(
hService,
SecurityInfos.DiscretionaryAcl,
securityDescriptorByte);

if (!status)
{
int lastError = Marshal.GetLastWin32Error();
Win32Exception exception = new Win32Exception(lastError);
bool accessDenied = exception.NativeErrorCode == NativeMethods.ERROR_ACCESS_DENIED;
WriteNonTerminatingError(
service,
exception,
nameof(ServiceResources.CouldNotSetServiceSecurityDescriptorSddl),
StringUtil.Format(ServiceResources.CouldNotSetServiceSecurityDescriptorSddl, service.ServiceName, exception.Message),
accessDenied ? ErrorCategory.PermissionDenied : ErrorCategory.InvalidOperation);
}

}
#endregion Internal
}
#endregion ServiceBaseCommand
Expand Down Expand Up @@ -1888,33 +1922,7 @@ protected override void ProcessRecord()

if(!string.IsNullOrEmpty(SecurityDescriptorSddl))
{
var rawSecurityDescriptor = new RawSecurityDescriptor(SecurityDescriptorSddl);
RawAcl rawDiscretionaryAcl = rawSecurityDescriptor.DiscretionaryAcl ;
var discretionaryAcl = new DiscretionaryAcl (false, false, rawDiscretionaryAcl );

byte[] rawDacl = new byte[discretionaryAcl.BinaryLength];
discretionaryAcl.GetBinaryForm(rawDacl, 0);
rawSecurityDescriptor.DiscretionaryAcl = new RawAcl(rawDacl, 0);
byte[] securityDescriptorByte = new byte[rawSecurityDescriptor.BinaryLength];
rawSecurityDescriptor.GetBinaryForm(securityDescriptorByte, 0);

status = NativeMethods.SetServiceObjectSecurity(
hService,
SecurityInfos.DiscretionaryAcl,
securityDescriptorByte);

if (!status)
{
int lastError = Marshal.GetLastWin32Error();
Win32Exception exception = new Win32Exception(lastError);
bool accessDenied = exception.NativeErrorCode == NativeMethods.ERROR_ACCESS_DENIED;
WriteNonTerminatingError(
service,
exception,
nameof(ServiceResources.CouldNotSetServiceSecurityDescriptorSddl),
StringUtil.Format(ServiceResources.CouldNotSetServiceSecurityDescriptorSddl, Name, exception.Message),
accessDenied ? ErrorCategory.PermissionDenied : ErrorCategory.InvalidOperation);
}
SetServiceSecurityDescriptor(service, SecurityDescriptorSddl, hService);
}

if (PassThru.IsPresent)
Expand Down Expand Up @@ -2080,6 +2088,18 @@ public PSCredential Credential

internal PSCredential credential = null;

/// <summary>
/// Sets the SecurityDescriptorSddl of the service using a SDDL string.
/// </summary>
[Parameter]
[Alias("sd")]
[ValidateNotNullOrEmpty]
public string SecurityDescriptorSddl
{
get;
set;
}

/// <summary>
/// Other services on which the new service depends.
/// </summary>
Expand All @@ -2102,6 +2122,7 @@ public string[] DependsOn
[ArchitectureSensitive]
protected override void BeginProcessing()
{
ServiceController service = null;
Diagnostics.Assert(!string.IsNullOrEmpty(Name),
"null ServiceName");
Diagnostics.Assert(!string.IsNullOrEmpty(BinaryPathName),
Expand Down Expand Up @@ -2192,7 +2213,7 @@ protected override void BeginProcessing()
hScManager,
Name,
DisplayName,
NativeMethods.SERVICE_CHANGE_CONFIG,
NativeMethods.SERVICE_CHANGE_CONFIG | NativeMethods.WRITE_DAC | NativeMethods.WRITE_OWNER,
NativeMethods.SERVICE_WIN32_OWN_PROCESS,
dwStartType,
NativeMethods.SERVICE_ERROR_NORMAL,
Expand Down Expand Up @@ -2274,7 +2295,13 @@ protected override void BeginProcessing()
}

// write the ServiceController for the new service
ServiceController service = new ServiceController(Name);
service = new ServiceController(Name);

if (!string.IsNullOrEmpty(SecurityDescriptorSddl))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would this work?

service = new ServiceController(Name);

if (!string.IsNullOrEmpty(SecurityDescriptorSddl))
{
    SetServiceSecurityDescriptor(service, SecurityDescriptorSddl, hService);
}

@vexx32 Rain Sallow (/u/ta11ow) (vexx32) Sep 5, 2019 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a parameter, right? Shouldn't we be using if (MyInvocation.BoundParameters.ContainsKey(nameof(SecurityDescriptorSddl))) { ... } for parameters?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aditya Patwardhan (@adityapatwardhan) Rain Sallow (/u/ta11ow) (@vexx32) yes, I've used the same way how its implemented for Set-Service. I referred

L1883 if (!string.IsNullOrEmpty(Status)) which is used for -Status parameter in Set-Service.

{
SetServiceSecurityDescriptor(service, SecurityDescriptorSddl, hService);
}

WriteObject(service);
}
finally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,29 @@ Describe "Set/New/Remove-Service cmdlet tests" -Tags "Feature", "RequireAdminOnW
$testservice2 = New-Service -BinaryPathName $svcfullpath -Name $testservicename2 -DependsOn $testservicename1
$testservice2 | Should -Not -BeNullOrEmpty
}

Function CheckSecurityDescriptorSddl {
Comment thread
kvprasoon marked this conversation as resolved.
Resolved
Param(
[Parameter(Mandatory)]
$SecurityDescriptorSddl,

[Parameter(Mandatory)]
$ServiceName
)
$Counter = 0
$ExpectedSDDL = ConvertFrom-SddlString -Sddl $SecurityDescriptorSddl

# Selecting the first item in the output array as below command gives plain text output from the native sc.exe.
$UpdatedSDDL = ConvertFrom-SddlString -Sddl (sc sdshow $ServiceName)[1]

$UpdatedSDDL.Owner | Should -Be $ExpectedSDDL.Owner
$UpdatedSDDL.Group | Should -Be $ExpectedSDDL.Group
$UpdatedSDDL.DiscretionaryAcl.Count | Should -Be $ExpectedSDDL.DiscretionaryAcl.Count
$UpdatedSDDL.DiscretionaryAcl | ForEach-Object -Process {
$_ | Should -Be $ExpectedSDDL.DiscretionaryAcl[$Counter]
$Counter++
}
}
}
AfterAll {
$global:PSDefaultParameterValues = $originalDefaultParameterValues
Expand Down Expand Up @@ -87,20 +110,8 @@ Describe "Set/New/Remove-Service cmdlet tests" -Tags "Feature", "RequireAdminOnW


It "Sets securitydescriptor of service using Set-Service " {
Set-Service -Name $TestServiceName1 -SecurityDescriptor $SecurityDescriptorSddl
$Counter = 0
$ExpectedSDDL = ConvertFrom-SddlString -Sddl $SecurityDescriptorSddl

# Selecting the first item in the output array as below command gives plain text output from the native sc.exe.
$UpdatedSDDL = ConvertFrom-SddlString -Sddl (sc sdshow $TestServiceName1)[1]

$UpdatedSDDL.Owner | Should -Be $ExpectedSDDL.Owner
$UpdatedSDDL.Group | Should -Be $ExpectedSDDL.Group
$UpdatedSDDL.DiscretionaryAcl.Count | Should -Be $ExpectedSDDL.DiscretionaryAcl.Count
$UpdatedSDDL.DiscretionaryAcl | ForEach-Object -Process {
$_ | Should -Be $ExpectedSDDL.DiscretionaryAcl[$Counter]
$Counter++
}
Set-Service -Name $TestServiceName1 -SecurityDescriptorSddl $SecurityDescriptorSddl
CheckSecurityDescriptorSddl -SecurityDescriptor $SecurityDescriptorSddl -ServiceName $TestServiceName1
}

It "Set-Service can change '<parameter>' to '<value>'" -TestCases @(
Expand Down Expand Up @@ -142,14 +153,15 @@ Describe "Set/New/Remove-Service cmdlet tests" -Tags "Feature", "RequireAdminOnW
}

It "NewServiceCommand can be used as API for '<parameter>' with '<value>'" -TestCases @(
@{parameter = "Name" ; value = "bar"},
@{parameter = "BinaryPathName" ; value = "hello"},
@{parameter = "DisplayName" ; value = "hello world"},
@{parameter = "Description" ; value = "this is a test"},
@{parameter = "StartupType" ; value = "Automatic"},
@{parameter = "StartupType" ; value = "Disabled"},
@{parameter = "StartupType" ; value = "Manual"},
@{parameter = "Credential" ; value = (
@{parameter = "Name" ; value = "bar"},
@{parameter = "BinaryPathName" ; value = "hello"},
@{parameter = "DisplayName" ; value = "hello world"},
@{parameter = "Description" ; value = "this is a test"},
@{parameter = "StartupType" ; value = "Automatic"},
@{parameter = "StartupType" ; value = "Disabled"},
@{parameter = "StartupType" ; value = "Manual"},
@{parameter = "SecurityDescriptorSddl" ; value = $SecurityDescriptorSddl},
@{parameter = "Credential" ; value = (
[System.Management.Automation.PSCredential]::new("username",
#[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Demo/doc/test secret.")]
(ConvertTo-SecureString "PlainTextPassword" -AsPlainText -Force)))
Expand Down Expand Up @@ -195,11 +207,12 @@ Describe "Set/New/Remove-Service cmdlet tests" -Tags "Feature", "RequireAdminOnW
}

It "New-Service can create a new service called '<name>'" -TestCases @(
@{name = "testautomatic"; startupType = "Automatic"; description = "foo" ; displayname = "one"},
@{name = "testmanual" ; startupType = "Manual" ; description = "bar" ; displayname = "two"},
@{name = "testdisabled" ; startupType = "Disabled" ; description = $null ; displayname = $null}
@{name = "testautomatic"; startupType = "Automatic"; description = "foo" ; displayname = "one" ; securityDescriptorSddl = $null},
@{name = "testmanual" ; startupType = "Manual" ; description = "bar" ; displayname = "two" ; securityDescriptorSddl = $SecurityDescriptorSddl},
@{name = "testdisabled" ; startupType = "Disabled" ; description = $null ; displayname = $null ; securityDescriptorSddl = $null},
@{name = "testsddl" ; startupType = "Disabled" ; description = "foo" ; displayname = $null ; securityDescriptorSddl = $SecurityDescriptorSddl}
) {
param($name, $startupType, $description, $displayname)
param($name, $startupType, $description, $displayname, $securityDescriptorSddl)
try {
$parameters = @{
Name = $name;
Expand All @@ -212,6 +225,10 @@ Describe "Set/New/Remove-Service cmdlet tests" -Tags "Feature", "RequireAdminOnW
if ($displayname) {
$parameters += @{displayname = $displayname}
}
if ($securityDescriptorSddl) {
$parameters += @{SecurityDescriptorSddl = $securityDescriptorSddl}
}

$service = New-Service @parameters
$service | Should -Not -BeNullOrEmpty
$service.displayname | Should -Be $(if($displayname){$displayname}else{$name})
Expand All @@ -236,6 +253,9 @@ Describe "Set/New/Remove-Service cmdlet tests" -Tags "Feature", "RequireAdminOnW
else {
$service.DisplayName | Should -Be $displayname
}
if ($securityDescriptorSddl) {
CheckSecurityDescriptorSddl -SecurityDescriptorSddl $SecurityDescriptorSddl -ServiceName $name
}
}
finally {
$service = Get-CimInstance Win32_Service -Filter "name='$name'"
Expand Down