{"id":7219,"date":"2023-08-15T06:18:21","date_gmt":"2023-08-15T06:18:21","guid":{"rendered":"https:\/\/pariswells.com\/blog\/?p=7219"},"modified":"2023-08-15T06:18:22","modified_gmt":"2023-08-15T06:18:22","slug":"windows-unquoted-service-path-remediation","status":"publish","type":"post","link":"https:\/\/pariswells.com\/blog\/research\/windows-unquoted-service-path-remediation","title":{"rendered":"Windows Unquoted Service Path Remediation"},"content":{"rendered":"\n<p>Detection<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">$i = 0\r\n\r\n$BaseKeys = \"HKLM:\\System\\CurrentControlSet\\Services\",                                  #Services\r\n\r\n            \"HKLM:\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\",                #32bit Uninstalls\r\n\r\n            \"HKLM:\\Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\"     #64bit Uninstalls\r\n\r\n#Blacklist for keys to ignore\r\n\r\n$BlackList = $Null\r\n\r\n#Create an ArrayList to store results in\r\n\r\n$Values = New-Object System.Collections.ArrayList\r\n\r\n#Discovers all registry keys under the base keys\r\n\r\n$DiscKeys = Get-ChildItem -Recurse -Directory $BaseKeys -Exclude $BlackList -ErrorAction SilentlyContinue |\r\n\r\n            Select-Object -ExpandProperty Name | %{($_.ToString().Split('\\') | Select-Object -Skip 1) -join '\\'}\r\n\r\n#Open the local registry\r\n\r\n$Registry = [Microsoft.Win32.RegistryKey]::OpenBaseKey('LocalMachine', 'Default')\r\n\r\nForEach ($RegKey in $DiscKeys)\r\n\r\n{\r\n\r\n    #Open each key with write permissions\r\n\r\n    Try { $ParentKey = $Registry.OpenSubKey($RegKey, $True) }\r\n\r\n    Catch { Write-Debug \"Unable to open $RegKey\" }\r\n\r\n    #Test if registry key has values\r\n\r\n    If ($ParentKey.ValueCount -gt 0)\r\n\r\n    {\r\n\r\n        $MatchedValues = $ParentKey.GetValueNames() | ?{ $_ -eq \"ImagePath\" -or $_ -eq \"UninstallString\" }\r\n\r\n        ForEach ($Match in $MatchedValues)\r\n\r\n        {\r\n\r\n            #RegEx that matches values containing .exe with a space in the exe path and no double quote encapsulation\r\n\r\n            $ValueRegEx = '(^(?!\\u0022).*\\s.*\\.[Ee][Xx][Ee](?&lt;!\\u0022))(.*$)'\r\n\r\n            $Value = $ParentKey.GetValue($Match)\r\n\r\n            #Test if value matches RegEx\r\n\r\n            If ($Value -match $ValueRegEx)\r\n\r\n            {\r\n\r\n                $RegType = $ParentKey.GetValueKind($Match)\r\n\r\n                #Uses the matches from the RegEx to build a new entry encapsulating the exe path with double quotes\r\n\r\n                $Correction = \"$([char]34)$($Matches[1])$([char]34)$($Matches[2])\"\r\n\r\n\r\n\r\n                #Attempt to correct the entry\r\n\r\n                $i++\r\n\r\n            }\r\n\r\n        }\r\n\r\n    }\r\n\r\n    $ParentKey.Close()\r\n\r\n}\r\n\r\n$Registry.Close()\r\n\r\n\r\n\r\nIF($i -eq 0){ Exit 0 }else{ Exit 1 }\r\n<\/code><\/pre>\n\n\n\n<p>Remediation<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">$BaseKeys = \"HKLM:\\System\\CurrentControlSet\\Services\",                                  #Services\r\n\r\n            \"HKLM:\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\",                #32bit Uninstalls\r\n\r\n            \"HKLM:\\Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\"     #64bit Uninstalls\r\n\r\n#Blacklist for keys to ignore\r\n\r\n$BlackList = $Null\r\n\r\n#Create an ArrayList to store results in\r\n\r\n$Values = New-Object System.Collections.ArrayList\r\n\r\n#Discovers all registry keys under the base keys\r\n\r\n$DiscKeys = Get-ChildItem -Recurse -Directory $BaseKeys -Exclude $BlackList -ErrorAction SilentlyContinue |\r\n\r\n            Select-Object -ExpandProperty Name | %{($_.ToString().Split('\\') | Select-Object -Skip 1) -join '\\'}\r\n\r\n#Open the local registry\r\n\r\n$Registry = [Microsoft.Win32.RegistryKey]::OpenBaseKey('LocalMachine', 'Default')\r\n\r\nForEach ($RegKey in $DiscKeys)\r\n\r\n{\r\n\r\n    #Open each key with write permissions\r\n\r\n    Try { $ParentKey = $Registry.OpenSubKey($RegKey, $True) }\r\n\r\n    Catch { Write-Debug \"Unable to open $RegKey\" }\r\n\r\n    #Test if registry key has values\r\n\r\n    If ($ParentKey.ValueCount -gt 0)\r\n\r\n    {\r\n\r\n        $MatchedValues = $ParentKey.GetValueNames() | ?{ $_ -eq \"ImagePath\" -or $_ -eq \"UninstallString\" }\r\n\r\n        ForEach ($Match in $MatchedValues)\r\n\r\n        {\r\n\r\n            #RegEx that matches values containing .exe with a space in the exe path and no double quote encapsulation\r\n\r\n            $ValueRegEx = '(^(?!\\u0022).*\\s.*\\.[Ee][Xx][Ee](?&lt;!\\u0022))(.*$)'\r\n\r\n            $Value = $ParentKey.GetValue($Match)\r\n\r\n            #Test if value matches RegEx\r\n\r\n            If ($Value -match $ValueRegEx)\r\n\r\n            {\r\n\r\n                $RegType = $ParentKey.GetValueKind($Match)\r\n\r\n                #Uses the matches from the RegEx to build a new entry encapsulating the exe path with double quotes\r\n\r\n                $Correction = \"$([char]34)$($Matches[1])$([char]34)$($Matches[2])\"\r\n\r\n                #Attempt to correct the entry\r\n\r\n                Try { $ParentKey.SetValue(\"$Match\", \"$Correction\", [Microsoft.Win32.RegistryValueKind]::$RegType) }\r\n\r\n                Catch { Write-Debug \"Unable to write to $ParentKey\" }\r\n\r\n                #Add a hashtable containing details of corrected key to ArrayList\r\n\r\n                $Values.Add((New-Object PSObject -Property @{\r\n\r\n                \"Name\" = $Match\r\n\r\n                \"Type\" = $RegType\r\n\r\n                \"Value\" = $Value\r\n\r\n                \"Correction\" = $Correction\r\n\r\n                \"ParentKey\" = \"HKEY_LOCAL_MACHINE\\$RegKey\"\r\n\r\n                })) | Out-Null\r\n\r\n            }\r\n\r\n        }\r\n\r\n    }\r\n\r\n    $ParentKey.Close()\r\n\r\n}\r\n\r\n$Registry.Close()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Detection Remediation<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7219","post","type-post","status-publish","format-standard","hentry","category-research"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/7219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/comments?post=7219"}],"version-history":[{"count":2,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/7219\/revisions"}],"predecessor-version":[{"id":7221,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/7219\/revisions\/7221"}],"wp:attachment":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/media?parent=7219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/categories?post=7219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/tags?post=7219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}