cmd /c 'wmic service get name,displayname,pathname,startmode |findstr /i "auto" |findstr /i /v "c:\windows\\" |findstr /i /v """'
$value = cmd /c 'wmic service get name,displayname,pathname,startmode |findstr /i "auto" |findstr /i /v "c:\windows\\" |findstr /i /v """' if ($value -like "* *")
{
1
}
else
{
0}
Option ExplicitConst HKLM = &H80000002Dim objWMIServiceDim colServices, objServiceDim strComputerdim quote: quote=chr(34)Dim strPathNameDim strCommand,strArgs,messageDim bBatch: bBatch = Falsedim wshShell:Set wshShell = WScript.CreateObject("WScript.Shell") Dim i, iCount, objRegDim bDebug:bDebug = False'If not CScript, re-run with cscript...If (Not IsCScript()) ThenFor i = WScript.Arguments.Count -1 to 0 Step -1strArgs = WScript.Arguments(i) & Space(1) & strArgs NextWshShell.Run "CScript.exe " & quote & WScript.ScriptFullName & quote & space(1) & strArgs, 1, trueWScript.Quit '...and stop running as WScriptEnd IfIf WScript.Arguments.Count = 1 ThenbBatch = TruestrComputer = WScript.Arguments(0)ElsestrComputer = wshShell.ExpandEnvironmentStrings("% COMPUTERNAME%") End IfIf strComputer = "" Then WScript.QuitstrComputer = UCase(strComputer)WMICX()Main()If Not bDebug ThenWScript.Echo iCount & " Unquoted Service Path(s) were fixed on " & strComputerEnd If''''''''''' Functions and Subs ''''''''''''''''''Sub WMICX()'WMI connectionOn Error Resume NextSet objWMIService = GetObject("winmgmts:{impersonationLevel= impersonate}!\\" & strComputer& "\root\cimv2") If Err.Number <> 0 Thenmessage = "Error reaching or connecting to " & strComputer If not bBatch Then MsgBox message, vbcritical + vbinformation,"Failure" Else WScript.Echo message End If WScript.Quit(100) End IfOn Error GoTo 0End SubSub Main()On Error Resume NextSet objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")Set colServices = objWMIService.ExecQuery ("SELECT pathname,displayname FROM Win32_Service")iCount = 0For Each objService in colServicesstrCommand = "" strArgs = "" strPathName = objService.PathName 'Parse the Pathname, which is the command 'This is a little complicated. Dim iLastSlash, iProgEnd 'find the last \ character iLastSlash = InStrRev(strPathName,"\") 'look for a space beginning at last \, put location in iProgEnd iProgEnd = InStr(iLastSlash,strPathName, Space(1)) 'iProgEnd will be zero if no arguments If iProgEnd > 1 Then strArgs= trim(Mid(strPathName,iProgEnd) ) Else strCommand = strPathName End If If Left(strPathName,1) <> quote And InStr(strCommand,Space(1)) Then wscript.echo "Found " & objService.DisplayName & " Service with command line:" & _ VbCrLf & vbtab & objService.PathName FixService objService.Name, strCommand, strArgs End If NextEnd SubSub FixService (strSvsName, strCommand,strArgs)If bDebug Then Exit Sub'add a space only if there are argumentsIf Len(strArgs) > 0 Then strArgs = strArgs & Space(1) & strArgsDim strValueDim strRegPath,strImagePathstrRegPath= "SYSTEM\CurrentControlSet\Services\"& strSvsName strImagePath = quote & strCommand & quote & strArgsWScript.Echo "Setting Command line to " & strImagePathobjReg.SetExpandedStringValue HKLM,strRegPath,"ImagePath",strImagePath iCount = iCount +1End SubFunction IsCScript()If (InStr(UCase(WScript.FullName), "CSCRIPT") <> 0) Then IsCScript = TrueElseIsCScript = FalseEnd IfEnd Function
Labels: Compliance Item, Configuration Item, Powershell, SCCM, Unquoted Service Path Enumeration, VBScript, Vulnerability