-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPsNotify.c
165 lines (141 loc) · 3.93 KB
/
PsNotify.c
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
#include <ntddk.h>
#include "PsNotify.h"
#include "Global.h"
#include "Utils.h"
UINT64 FindPspLoadImageNotifyRoutine()
{
UNICODE_STRING usFunc = { 0 };
RtlInitUnicodeString(&usFunc, L"PsSetLoadImageNotifyRoutine");
UINT64 Function = (UINT64)MmGetSystemRoutineAddress(&usFunc);
if (!Function)
return 0;
char Pattern[3] = { 0x48,0x8D,0x0D };
Function = FindPattern(Function, 0xFF, Pattern, sizeof(Pattern));
if (Function)
{
LONG Offset = 0;
memcpy(&Offset, (UCHAR*)(Function + 3), 4);
Function = Function + 7 + Offset;
}
return Function;
}
NTSTATUS DisablePsImageCallback()
{
UINT64 PspLoadImageFunc = FindPspLoadImageNotifyRoutine(), pRoutineAddress;
if (PspLoadImageFunc)
{
ListCallbacks(PspLoadImageFunc, DriverInfo.Base, DriverInfo.Size, &pRoutineAddress, TRUE);
return pRoutineAddress ? PsRemoveLoadImageNotifyRoutine(pRoutineAddress) : STATUS_UNSUCCESSFUL;
}
return STATUS_UNSUCCESSFUL;
}
UINT64 FindPspCreateProcessNotifyRoutine()
{
UNICODE_STRING usFunc = { 0 };
RtlInitUnicodeString(&usFunc, L"PsSetCreateProcessNotifyRoutine");
UINT64 Function = (UINT64)MmGetSystemRoutineAddress(&usFunc);
if (!Function)
return 0;
char Pattern1[2] = { 0xC2,0xE9 };
Function = FindPattern(Function, 20, Pattern1, sizeof(Pattern1));
if (Function)
{
LONG Offset = 0;
memcpy(&Offset, (UCHAR*)(Function + 2), 4);
Function = Function + 5 + Offset + 9;
}
char Pattern2[3] = { 0x4C,0x8D,0x25 };
Function = FindPattern(Function, 0xFF, Pattern2, sizeof(Pattern2));
if (Function)
{
LONG Offset = 0;
memcpy(&Offset, (UCHAR*)(Function + 3), 4);
UINT64 Addy = Function + 7 + Offset;
return Addy;
}
return 0;
}
NTSTATUS DisablePsProcessCallback()
{
UINT64 PspCreateProcessFunc = FindPspLoadImageNotifyRoutine(), pRoutineAddress;
if (PspCreateProcessFunc)
{
ListCallbacks(PspCreateProcessFunc, DriverInfo.Base, DriverInfo.Size, &pRoutineAddress, TRUE);
return pRoutineAddress ? PsSetCreateProcessNotifyRoutine(pRoutineAddress, TRUE) : STATUS_UNSUCCESSFUL;
}
return STATUS_UNSUCCESSFUL;
}
UINT64 FindPspCreateThreadNotifyRoutine()
{
UNICODE_STRING usFunc = { 0 };
RtlInitUnicodeString(&usFunc, L"PsSetCreateThreadNotifyRoutine");
UINT64 Function = (UINT64)MmGetSystemRoutineAddress(&usFunc);
if (!Function)
return 0;
char Pattern1[3] = { 0x33,0xD2,0xE9 };
Function = FindPattern(Function, 20, Pattern1, sizeof(Pattern1));
if (Function)
{
LONG Offset = 0;
memcpy(&Offset, (UCHAR*)(Function + 3), 4);
Function = Function + 5 + Offset;
}
char Pattern2[3] = { 0x48,0x8D,0x0D };
Function = FindPattern(Function, 0xFF, Pattern2, sizeof(Pattern2));
if (Function)
{
LONG Offset = 0;
memcpy(&Offset, (UCHAR*)(Function + 3), 4);
UINT64 Addy = Function + 7 + Offset;
return Addy;
}
return 0;
}
NTSTATUS DisablePsThreadCallback()
{
UINT64 PspCreateThreadFunc = FindPspCreateThreadNotifyRoutine(), pRoutineAddress;
if (PspCreateThreadFunc)
{
ListCallbacks(PspCreateThreadFunc, DriverInfo.Base, DriverInfo.Size, &pRoutineAddress, TRUE);
return pRoutineAddress ? PsRemoveCreateThreadNotifyRoutine(pRoutineAddress) : STATUS_UNSUCCESSFUL;
}
return STATUS_UNSUCCESSFUL;
}
VOID ListCallbacks(
_In_ UINT64* Psp,
_In_ UINT64 Base, _In_ UINT64 Size,
_Out_ UINT64* pCallbackAddress,
_In_ BOOLEAN bSaveCallback
)
{
if (Psp)
{
__try
{
for (int i = 0; i < 0x10; ++i)
{
if (!MmIsAddressValid((PVOID)Psp[i]))
continue;
PPS_CALLBACK_ENTRY monCallBack = (PPS_CALLBACK_ENTRY)Mask3Bits(Psp[i]);
if (MmIsAddressValid(monCallBack->Callback))
{
if ((UINT64)monCallBack->Callback > Base && (UINT64)monCallBack->Callback < (Base + Size))
{
kprintf("%s: Found Callback Address at: %p\n", __FUNCTION__, monCallBack->Callback);
if (bSaveCallback)
*pCallbackAddress = (UINT64)monCallBack->Callback;
else
monCallBack->Callback = (PVOID*)*pCallbackAddress;
}
}
}
}
__except (EXCEPTION_EXECUTE_HANDLER) {
return;
}
}
}
VOID DummyNotifyRoutine()
{
return;
}