Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
inject_exclusions.cpp
Go to the documentation of this file.
1#include "inject_exclusions.h"
2#include "pano_events.h"
3
4#define INJECT_EXCLUSIONS_TAG 'xEnP'
5
6// One allocation holding the header, the Names array and the characters the names point at,
7// so a list is replaced by swapping a single pointer
12
13static EX_PUSH_LOCK g_ListLock;
14static PINJECT_EXCLUSION_LIST g_List; // NULL when the list is empty
15
17{
18 ExInitializePushLock(&g_ListLock);
19 g_List = NULL;
20}
21
23{
24 if (g_List != NULL) {
25 ExFreePoolWithTag(g_List, INJECT_EXCLUSIONS_TAG);
26 g_List = NULL;
27 }
28}
29
30NTSTATUS InjectExclusionsSet(PVOID Buffer, ULONG Length)
31{
32 PAGED_CODE();
33
34 const ULONG headerSize = FIELD_OFFSET(PANO_INJECTION_EXCLUSIONS, Names);
35 if (Buffer == NULL || Length < headerSize || (Length - headerSize) % sizeof(WCHAR) != 0) {
36 return STATUS_INVALID_PARAMETER;
37 }
38
40 ULONG count = request->Count;
42 return STATUS_INVALID_PARAMETER;
43 }
44
45 // Check every name lies inside the buffer and is terminated before anything is allocated.
46 // The whole buffer must be used, so a wrong Count is caught
47 const WCHAR* names = request->Names;
48 ULONG remaining = (Length - headerSize) / sizeof(WCHAR);
49 ULONG position = 0;
50 for (ULONG i = 0; i < count; i++) {
51 ULONG nameLength = 0;
52 while (position + nameLength < remaining && names[position + nameLength] != L'\0') {
53 nameLength++;
54 }
55 if (position + nameLength >= remaining || nameLength == 0 ||
57 return STATUS_INVALID_PARAMETER;
58 }
59 position += nameLength + 1;
60 }
61 if (position != remaining) {
62 return STATUS_INVALID_PARAMETER;
63 }
64
65 PINJECT_EXCLUSION_LIST newList = NULL;
66 if (count > 0) {
67 // The characters, terminators included, follow the Names array. position counts them
68 SIZE_T namesOffset = FIELD_OFFSET(INJECT_EXCLUSION_LIST, Names) + (SIZE_T)count * sizeof(UNICODE_STRING);
69 newList = (PINJECT_EXCLUSION_LIST)ExAllocatePool2(POOL_FLAG_PAGED, namesOffset + (SIZE_T)position * sizeof(WCHAR), INJECT_EXCLUSIONS_TAG);
70 if (newList == NULL) {
71 return STATUS_INSUFFICIENT_RESOURCES;
72 }
73
74 PWCHAR characters = (PWCHAR)((PUCHAR)newList + namesOffset);
75 RtlCopyMemory(characters, names, (SIZE_T)position * sizeof(WCHAR));
76
77 newList->Count = count;
78 PWCHAR name = characters;
79 for (ULONG i = 0; i < count; i++) {
80 // Validated above: non-empty, terminated, at most PANO_MAX_INJECTION_EXCLUSION_LENGTH
81 USHORT nameLength = (USHORT)wcslen(name);
82 // Upper case, as FsRtlIsNameInExpression requires of a pattern when ignoring case.
83 // Plain names are compared ignoring case anyway
84 for (USHORT c = 0; c < nameLength; c++) {
85 name[c] = RtlUpcaseUnicodeChar(name[c]);
86 }
87 newList->Names[i].Buffer = name;
88 newList->Names[i].Length = nameLength * sizeof(WCHAR);
89 newList->Names[i].MaximumLength = newList->Names[i].Length + sizeof(WCHAR);
90 name += nameLength + 1;
91 DbgPrintEx(DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL, "[+] Panoptes: Do not inject: %wZ\n", &newList->Names[i]);
92 }
93 }
94
95 KeEnterCriticalRegion();
96 ExAcquirePushLockExclusive(&g_ListLock);
97 PINJECT_EXCLUSION_LIST oldList = g_List;
98 g_List = newList;
99 ExReleasePushLockExclusive(&g_ListLock);
100 KeLeaveCriticalRegion();
101
102 if (oldList != NULL) {
103 ExFreePoolWithTag(oldList, INJECT_EXCLUSIONS_TAG);
104 }
105
106 DbgPrintEx(DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, "[+] Panoptes: Injection exclusion list set (%lu names)\n", count);
107 return STATUS_SUCCESS;
108}
109
110static BOOLEAN HasWildcard(PCUNICODE_STRING Name)
111{
112 for (USHORT i = 0; i < Name->Length / sizeof(WCHAR); i++) {
113 if (Name->Buffer[i] == L'*' || Name->Buffer[i] == L'?') {
114 return TRUE;
115 }
116 }
117 return FALSE;
118}
119
120BOOLEAN IsInjectionExcluded(PCUNICODE_STRING ProcessImagePath)
121{
122 PAGED_CODE();
123
124 if (ProcessImagePath == NULL || ProcessImagePath->Buffer == NULL || ProcessImagePath->Length == 0) {
125 return FALSE;
126 }
127
128 // The file name is everything after the last separator
129 USHORT totalChars = ProcessImagePath->Length / sizeof(WCHAR);
130 USHORT start = totalChars;
131 while (start > 0 && ProcessImagePath->Buffer[start - 1] != L'\\') {
132 start--;
133 }
134
135 UNICODE_STRING fileName;
136 fileName.Buffer = ProcessImagePath->Buffer + start;
137 fileName.Length = (totalChars - start) * sizeof(WCHAR);
138 fileName.MaximumLength = fileName.Length;
139 if (fileName.Length == 0) {
140 return FALSE;
141 }
142
143 BOOLEAN excluded = FALSE;
144 KeEnterCriticalRegion();
145 ExAcquirePushLockShared(&g_ListLock);
146 if (g_List != NULL) {
147 for (ULONG i = 0; i < g_List->Count; i++) {
148 // A name starting with \ is a full NT path and must match the whole image path.
149 // Any other name is a file name and matches the image in any folder
150 PCUNICODE_STRING entry = &g_List->Names[i];
151 PCUNICODE_STRING candidate = (entry->Buffer[0] == L'\\') ? ProcessImagePath : &fileName;
152 // * matches any characters, \ included, so C:\Tools\* covers subfolders too; ? matches
153 // one character. The service only sends * and ? (Configuration rejects < > ", which
154 // FsRtlIsNameInExpression would treat as DOS wildcards)
155 BOOLEAN matched = HasWildcard(entry) ?
156 FsRtlIsNameInExpression((PUNICODE_STRING)entry, (PUNICODE_STRING)candidate, TRUE, NULL) :
157 RtlEqualUnicodeString(candidate, entry, TRUE);
158 if (matched) {
159 excluded = TRUE;
160 break;
161 }
162 }
163 }
164 ExReleasePushLockShared(&g_ListLock);
165 KeLeaveCriticalRegion();
166 return excluded;
167}
void InjectExclusionsInit()
Initializes the (empty) list of image names the hook DLL is not injected into. Call before the device...
struct _INJECT_EXCLUSION_LIST INJECT_EXCLUSION_LIST
BOOLEAN IsInjectionExcluded(PCUNICODE_STRING ProcessImagePath)
NTSTATUS InjectExclusionsSet(PVOID Buffer, ULONG Length)
struct _INJECT_EXCLUSION_LIST * PINJECT_EXCLUSION_LIST
#define INJECT_EXCLUSIONS_TAG
void InjectExclusionsCleanup()
Frees the list. Call once the device and the image load callback are gone.
struct _PANO_INJECTION_EXCLUSIONS PANO_INJECTION_EXCLUSIONS
The input of IOCTL_PANOPTES_SET_INJECTION_EXCLUSIONS. Each name is either:
#define PANO_MAX_INJECTION_EXCLUSION_LENGTH
The longest name, in WCHARs not counting its terminator, a PANO_INJECTION_EXCLUSIONS may hold....
Definition pano_events.h:39
#define PANO_MAX_INJECTION_EXCLUSIONS
The most names a PANO_INJECTION_EXCLUSIONS may hold.
Definition pano_events.h:35
struct _PANO_INJECTION_EXCLUSIONS * PPANO_INJECTION_EXCLUSIONS
WCHAR Names[1]
Count non-empty, null-terminated names, back to back.
Definition pano_events.h:51
UINT32 Count
The number of names in Names.
Definition pano_events.h:49