0

I'would like to write about each services this sentence " Service exploitable"

I tried with a lot of things but nothing worked...

this is my code

Get-CimInstance Win32_Service |
  Where-Object { $_.PathName -like '*.exe*'} | 
    Select-Object Name, State, Pathname, StartName |
      ForEach-Object {
        $_.PathName = ($_.PathName -split '(?<=\.exe\b)')[0].Trim('"')
        Add-Member -PassThru -InputObject $_ Acl (Get-Acl -LiteralPath $_.PathName)
      } | 
        Where-Object { 
          $_.Acl.Access.Where({
             $_.IdentityReference -ceq 'BUILTIN\Utilisateurs' -and 
              $_.FileSystemRights -eq 'FullControl' 
          }, 'First').Count -gt 0
        }

The result enter image description here

  • 2
    If you're going to copy code verbatim from [an existing answer](https://stackoverflow.com/a/68692081/45375), please provide proper attribution. Also, allow me to again give you the standard advice to newcomers in the following comment: – mklement0 Aug 18 '21 at 18:14
  • 2
    Also, please update your question to show _what_ you have tried, specifically, and _in what way_ it hasn't worked. – mklement0 Aug 18 '21 at 18:15

1 Answers1

0

If your goal is to simply write out the services which are exploitable due to insecure permissions, you can assign your Get-CimInstance result to a variable (e.g. $insecureServices, and use a subexpression in a string to join the service names together:

Note: Write-Host can be used for informational output that does not need to be processed within the same PowerShell session (the information stream will still be visible on STDOUT from the calling process). You can read my answer here which goes into output streams in more detail.

Write-Output "Exploitable services: $($insecureServices.Name -join ', ')"

This will result in a comma-delimited list of services returned by your existing code (the below uses the names in your screenshot):

Exploitable services: ClickToRunSvc, DeepETPService, DeepMgmtService

Note: within a double-quoted string, the $() operator will evaluate a sub-expression and return the result as a string. The above use of this is essentially shorthand for the following, and both will produce the same output:

$allServicesString = $insecureServices.Name -join ', '
Write-Output "Exploitable services: $allServicesString"

The reason this works is because even though you are returning a collection of services meeting specific criteria, PowerShell lets you operate on common properties of objects within the collection. So if every object in the collection has a Name property, $insecureServices.Name will return a collection of values for the Name property on each object in the collection, even though [System.Array] does not have a property called Name itself.

And since $insecureServices.Name returns a collection of names, we can then use the -join operator to join each element with ', '.


codewario
  • 19,553
  • 20
  • 90
  • 159