Skip to content

Commit 38aabbc

Browse files
committed
ToolStatus: Add service FileName and BinaryPath search filtering
1 parent a13706e commit 38aabbc

File tree

2 files changed

+184
-1
lines changed

2 files changed

+184
-1
lines changed

plugins/ToolStatus/filter.c

Lines changed: 178 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,9 @@ BOOLEAN ProcessTreeFilterCallback(
297297

298298
for (i = 0; i < serviceList->Count; i++)
299299
{
300+
PPH_STRING serviceFileName = NULL;
301+
PPH_STRING serviceBinaryPath = NULL;
302+
300303
serviceItem = serviceList->Items[i];
301304

302305
if (!PhIsNullOrEmptyString(serviceItem->Name))
@@ -329,6 +332,36 @@ BOOLEAN ProcessTreeFilterCallback(
329332
break;
330333
}
331334
}
335+
336+
if (NT_SUCCESS(QueryServiceFileName(
337+
&serviceItem->Name->sr,
338+
&serviceFileName,
339+
&serviceBinaryPath
340+
)))
341+
{
342+
if (serviceFileName)
343+
{
344+
if (WordMatchStringRef(&serviceFileName->sr))
345+
{
346+
matched = TRUE;
347+
}
348+
349+
PhDereferenceObject(serviceFileName);
350+
}
351+
352+
if (serviceBinaryPath)
353+
{
354+
if (WordMatchStringRef(&serviceBinaryPath->sr))
355+
{
356+
matched = TRUE;
357+
}
358+
359+
PhDereferenceObject(serviceBinaryPath);
360+
}
361+
362+
if (matched)
363+
break;
364+
}
332365
}
333366

334367
PhDereferenceObjects(serviceList->Items, serviceList->Count);
@@ -347,6 +380,8 @@ BOOLEAN ServiceTreeFilterCallback(
347380
)
348381
{
349382
PPH_SERVICE_NODE serviceNode = (PPH_SERVICE_NODE)Node;
383+
PPH_STRING serviceFileName = NULL;
384+
PPH_STRING serviceBinaryPath = NULL;
350385

351386
if (PhIsNullOrEmptyString(SearchboxText))
352387
return TRUE;
@@ -385,6 +420,38 @@ BOOLEAN ServiceTreeFilterCallback(
385420
return TRUE;
386421
}
387422

423+
if (NT_SUCCESS(QueryServiceFileName(
424+
&serviceNode->ServiceItem->Name->sr,
425+
&serviceFileName,
426+
&serviceBinaryPath
427+
)))
428+
{
429+
BOOLEAN matched = FALSE;
430+
431+
if (serviceFileName)
432+
{
433+
if (WordMatchStringRef(&serviceFileName->sr))
434+
{
435+
matched = TRUE;
436+
}
437+
438+
PhDereferenceObject(serviceFileName);
439+
}
440+
441+
if (serviceBinaryPath)
442+
{
443+
if (WordMatchStringRef(&serviceBinaryPath->sr))
444+
{
445+
matched = TRUE;
446+
}
447+
448+
PhDereferenceObject(serviceBinaryPath);
449+
}
450+
451+
if (matched)
452+
return TRUE;
453+
}
454+
388455
return FALSE;
389456
}
390457

@@ -464,4 +531,114 @@ BOOLEAN NetworkTreeFilterCallback(
464531
}
465532

466533
return FALSE;
467-
}
534+
}
535+
536+
// NOTE: This function does not use the SCM due to major performance issues.
537+
// For now we just query this information from the registry but it might be out-of-sync
538+
// until the SCM flushes its cache.
539+
NTSTATUS QueryServiceFileName(
540+
_In_ PPH_STRINGREF ServiceName,
541+
_Out_ PPH_STRING *ServiceFileName,
542+
_Out_ PPH_STRING *ServiceBinaryPath
543+
)
544+
{
545+
static PH_STRINGREF servicesKeyName = PH_STRINGREF_INIT(L"System\\CurrentControlSet\\Services\\");
546+
static PH_STRINGREF typeKeyName = PH_STRINGREF_INIT(L"Type");
547+
548+
NTSTATUS status;
549+
HANDLE keyHandle;
550+
ULONG serviceType;
551+
PPH_STRING keyName;
552+
PPH_STRING binaryPath;
553+
PPH_STRING fileName;
554+
555+
keyName = PhConcatStringRef2(&servicesKeyName, ServiceName);
556+
binaryPath = NULL;
557+
fileName = NULL;
558+
559+
if (NT_SUCCESS(status = PhOpenKey(
560+
&keyHandle,
561+
KEY_READ,
562+
PH_KEY_LOCAL_MACHINE,
563+
&keyName->sr,
564+
0
565+
)))
566+
{
567+
PPH_STRING serviceImagePath;
568+
PKEY_VALUE_PARTIAL_INFORMATION buffer;
569+
570+
if (NT_SUCCESS(status = PhQueryValueKey(
571+
keyHandle,
572+
&typeKeyName,
573+
KeyValuePartialInformation,
574+
&buffer
575+
)))
576+
{
577+
if (
578+
buffer->Type == REG_DWORD &&
579+
buffer->DataLength == sizeof(ULONG)
580+
)
581+
{
582+
serviceType = *(PULONG)buffer->Data;
583+
}
584+
585+
PhFree(buffer);
586+
}
587+
588+
if (serviceImagePath = PhQueryRegistryString(keyHandle, L"ImagePath"))
589+
{
590+
PPH_STRING expandedString;
591+
592+
if (expandedString = PhExpandEnvironmentStrings(&serviceImagePath->sr))
593+
{
594+
binaryPath = expandedString;
595+
PhDereferenceObject(serviceImagePath);
596+
}
597+
else
598+
{
599+
binaryPath = serviceImagePath;
600+
}
601+
}
602+
else
603+
{
604+
status = STATUS_NOT_FOUND;
605+
}
606+
607+
NtClose(keyHandle);
608+
}
609+
610+
if (NT_SUCCESS(status))
611+
{
612+
PhGetServiceDllParameter(ServiceName, &fileName);
613+
614+
if (!fileName)
615+
{
616+
if (serviceType & SERVICE_WIN32)
617+
{
618+
PH_STRINGREF dummyFileName;
619+
PH_STRINGREF dummyArguments;
620+
621+
PhParseCommandLineFuzzy(&binaryPath->sr, &dummyFileName, &dummyArguments, &fileName);
622+
623+
if (!fileName)
624+
PhSwapReference(&fileName, binaryPath);
625+
}
626+
else
627+
{
628+
fileName = PhGetFileName(binaryPath);
629+
}
630+
}
631+
632+
*ServiceFileName = fileName;
633+
*ServiceBinaryPath = binaryPath;
634+
}
635+
else
636+
{
637+
if (binaryPath)
638+
PhDereferenceObject(binaryPath);
639+
}
640+
641+
PhDereferenceObject(keyName);
642+
643+
return status;
644+
}

plugins/ToolStatus/toolstatus.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,12 @@ BOOLEAN NetworkTreeFilterCallback(
252252
_In_opt_ PVOID Context
253253
);
254254

255+
NTSTATUS QueryServiceFileName(
256+
_In_ PPH_STRINGREF ServiceName,
257+
_Out_ PPH_STRING *ServiceFileName,
258+
_Out_ PPH_STRING *ServiceBinaryPath
259+
);
260+
255261
// searchbox.c
256262

257263
HWND CreateSearchControl(

0 commit comments

Comments
 (0)