Remediate unquoted service path vulnerability
I have now been to three different environments that have this vulnerability. In all three environments this was one of the top offenders per total count of vulnerability. This not something that is necessary a problem that will cause outages if not patched, but potential for malicious programs to run.There are already published posts with great explanation of the vulnerability from CommonExploits, and Tennable. The scope of this blog post will be how to identify the vulnerability, how to manually remediate it, and how to auto-remediate it via the compliance item.
cmd /c ‘wmic service get name,displayname,pathname,startmode |findstr /i “auto” |findstr /i /v “c:windows\” |findstr /i /v “””‘
Below we see that we do have 1 item that is vulnerable.
This is what the ACAS Scans are finding however it does not tell you where to correct this within the registry. To remediate this we will have to locate the object within the registry and add quotes to the path. I believe there is a way to configure reporting to be more verbose to include the registry location but I am unfamiliar with the ACAS tool.
ACAS SCAN BEFORE IT IS FIXED
Automated Fix via Compliance Item
We will create a compliance Item to discovery systems with the unquoted service path vulnerability and then remediate it.
Discovery Script
$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}
Remediation Script
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& “rootcimv2”) 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 & “rootdefault: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= “SYSTEMCurrentControlSetServices”& 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
At this point go ahead and deploy the configuration item you have just created. I would recommend only performing this against workstations but only after serious testing.
Unfortunately I forgot to take a screenshot when I fist deployed this compliance item, but I do have one after half the systems checked for policy.
After systems get policy we are now looking at a very high remediation percentage.
The compliance item can be downloaded on Technet here.
I would like to thank Khalid Al Alul, and Ricky Richard for their time in working on this configuration item.
Add comment