-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-invalidimagepaths.ps1
364 lines (263 loc) · 9.77 KB
/
get-invalidimagepaths.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
###########################################################################
#
# NAME: GetInvalidImagePathsV2.ps1
#
# AUTHOR: Suhas Rao
# REVISED BY: Mark Stanfill
#
# COMMENT: This script was created to help minimize the time of parsing through the registry searching for invalid ImagePaths
# under the HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServices keys.
#
# For more details, search for “Enumeration of the files failed” in a blog posted on:
# http://blogs.technet.com/b/askcore/
#
# Disclaimer:
#
# The sample scripts are not supported under any Microsoft standard support program or service.
# The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including,
# without limitation, any implied warranties of merchantability or of fitness for a particular purpose.
# The entire risk arising out of the use or performance of the sample scripts and documentation remains with you.
# In no event shall Microsoft, its authors, or anyone else involved in the creation, production,
# or delivery of the scripts be liable for any damages whatsoever (including, without limitation,
# damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss)
# arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.
#
#
# VERSION HISTORY:
# 1.0 – Initial release
# 2.0 – Included searching for spaces in the path
#
###########################################################################
$Verbose = $true;
if (($ARGS.Count -gt 0) -and ($ARGS[0] -ieq “-verbose”))
{
$Verbose = $true;
}
#
# The list of possible reasons for failure
#
$FailureReasons = @{
“INVALID_CHARS” = “The service path contains invalid characters. ” +
“Characters < > : `” | ? cannot be used in a file path.”;
“INVALID_FORMAT” = “The service path does not have a proper path format. ” +
“Only paths beginning with [<Drive>]: format are supported.”;
“DOUBLE_SLASH” = “The service path contains double inverted slashes. ” +
“UNC Network paths or paths containing double inverted slashes are not supported.”;
“RELATIVE_PATH” = “The service path is relative. ” +
“Only absolute paths are supported.”;
“FOWARD_SLASH” = “The service path contains a foward slash. ” +
“Only paths containing an inverted slash are supported.”;
“REPARSE_POINT” = “The service path contains a reparse point. ” +
“Paths containing a reparse point are not supported.”;
“UNRECOGNIZED_PATH” = “Unable to check the path. ” +
“Expecting the ImagePath for the service to be a .dll or .exe”;
“SPACE_IN_PATH” = “The service path contains spaces, ” +
“the whole path needs to be enclosed using double quotes”;
}
#
# The failure INVALID_CHARS can occur due to the following type of characters
#
$InvalidChars = @{
“*\*” = “DOUBLE_SLASH”;
“*..*” = “RELATIVE_PATH”;
“*.*” = “RELATIVE_PATH”;
“*/*” = “FOWARD_SLASH”
}
#
# Display the service info
#
function PrintServiceInfo([System.Management.ManagementObject] $Service, [string] $Header,
[string] $Reason, [string] $Color='white')
{
$Name = $Service.Name
$Caption = $Service.Caption
$Path = $Service.PathName
$Info = $FailureReasons.Item($Reason)
Write-Host -ForegroundColor $Color “$Header`n” `
” Service Name : $Name`n” `
” Service Caption : $Caption`n” `
” Registry key : HKEY_LOCAL_MACHINESYSTEM\CurrentControlSet\services\$NameImagePath`n” `
” Value : $Path`n” `
” Reason : $Info`n” `
“`n” `
}
#
# For verbose mode, print extra info for every path
#
function PrintStatus([Boolean] $IsBadPath)
{
if ($Verbose -eq $true)
{
if ($IsBadPath -eq $true)
{
Write-Host “ERROR” -ForegroundColor Red;
}
else
{
Write-Host “OK” -ForeGroundColor Green;
}
}
}
#
# This is a core function that fetches the service path given the input from registry
# It expects the service path to be a .dll or .exe
#
function GetActualPathFromServiceImagePath([string] $ServiceImagePath)
{
$ActualPathName = $null
$IndexForPath = $null
$ExeIndex = $ServiceImagePath.ToLower().IndexOf(“.exe”);
$DllIndex = $ServiceImagePath.ToLower().IndexOf(“.dll”);
##
## NOTE: Assumption is that the Service Path Always ends in dll or exe
##
if(($ExeIndex -eq -1) -and ($DllIndex -eq -1))
{
return $null
}
##
## If the path contains both Dll And Exe then we should use the One that Comes First
##
if(($ExeIndex -ne -1) -and ($DllIndex -ne -1))
{
if($ExeIndex -gt $DllIndex)
{
$IndexForPath = $DllIndex +4;
}
else
{
$IndexForPath = $ExeIndex +4;
}
}
else
{
if($ExeIndex -eq -1)
{
$IndexForPath = $DllIndex +4;
}
else
{
$IndexForPath = $ExeIndex +4;
}
}
$ActualPathName = $ServiceImagePath.Substring(0,$IndexForPath)
$Quote = “`””
if($ActualPathName.StartsWith($Quote))
{
$ActualPathName = $ActualPathName.Remove(0,1);
}
if ($ActualPathName.StartsWith(“\?”) -or $ActualPathName.StartsWith(“\.”))
{
$ActualPathName = $ActualPathName.Substring(4);
}
return $ActualPathName
}
##################################################################################
## Main ##
##################################################################################
$Services = Get-WmiObject Win32_Service
$BadServices = $null
$Reasons = $null
$UnrecognizedPath = 0
for ($i = 0; $i -lt $Services.Count; $i++)
{
##
## Get the actual Exe Path
##
$ActualPathName = GetActualPathFromServiceImagePath $Services[$i].PathName
if($ActualPathName -eq $null)
{
$Path = $Services[$i].PathName
$Name = $Services[$i].Name
$UnrecognizedPath = 1;
PrintServiceInfo $Services[$i] “WARNING:” “UNRECOGNIZED_PATH” “Yellow”
continue;
}
########new
#######make sure all paths with spaces have double quotes around them
if ( ($Services[$i].PathName -match “^[^x22].*s.*.exe.*” ) -eq $true)
{
$BadServices += ,$Services[$i];
$Reasons += ,”SPACE_IN_PATH”;
PrintStatus($true);
continue;
}
if ($Verbose -eq $true)
{
Write-Host -nonewline “Analyzing path ‘$ActualPathName’ …”
}
##
## Check for Attributes
##
if((Test-Path -IsValid $ActualPathName) -eq $False)
{
$BadServices += ,$Services[$i] ;
$Reasons += ,”INVALID_CHARS”;
PrintStatus($true);
continue;
}
##
## Check for invalid chars
##
foreach ($Key in $InvalidChars.Keys)
{
$Value = $InvalidChars.Item($Key);
if ($ActualPathName -like $Key)
{
$temp = $Key.Replace(“*”,””)
$BadServices += ,$Services[$i]
$Reasons += ,$Value;
}
}
##
## The Start string must be in the below specified format
##
if((($ActualPathName -match “^[a-z]:\”) -ne $true))
{
$BadServices += ,$Services[$i]
$Reasons += ,”INVALID_FORMAT”;
PrintStatus($true);
continue;
}
##
## Check for Reparse points
##
$RootPath = [System.IO.Path]::GetPathRoot($ActualPathName)
$Path = $ActualPathName
$DoesPathExist = Test-Path -Path $ActualPathName
while(($DoesPathExist -eq $true) -and ($Path -ne $RootPath))
{
$h = [System.IO.File]::GetAttributes($Path);
if ($h.CompareTo([System.IO.FileAttributes]::ReparsePoint) -ge 0)
{
$BadServices += ,$Services[$i]
$Reasons += ,”REPARSE_POINT”;
PrintStatus($true);
break;
}
if ($Path.Contains(“”) -ne $true)
{
break;
}
$strPath = $Path.Substring(0,$Path.LastIndexOf(“”));
$Path = $strPath
}
PrintStatus($false);
}
if ($BadServices.Count -gt 0)
{
echo “”
echo “Following are the service(s) found to be reporting invalid paths.”
echo “”
for ($i=0; $i-lt$BadServices.Count; $i++)
{
$Count = $i + 1
PrintServiceInfo $BadServices[$i] “$Count.” $Reasons[$i]
}
}
elseif ($UnrecognizedPath -ne 1)
{
echo “”
Write-Host “No invalid service paths found.” -ForeGroundColor Green
echo “”
}