-
Notifications
You must be signed in to change notification settings - Fork 3
/
token.c
250 lines (194 loc) · 6.35 KB
/
token.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
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
/*
* Copyright (c) 2004 Security Architects Corporation. All rights reserved.
*
* Module Name:
*
* token.c
*
* Abstract:
*
* This module implements various token hooking routines.
* Token objects encapsulate the privileges and access rights of an agent
* (a thread or process).
*
* Author:
*
* Eugene Tsyrklevich 25-Mar-2004
*
* Revision History:
*
* None.
*/
#include "token.h"
#ifdef ALLOC_PRAGMA
#pragma alloc_text (INIT, InitTokenHooks)
#endif
fpZwAdjustPrivilegesToken OriginalNtAdjustPrivilegesToken = NULL;
fpZwSetInformationToken OriginalNtSetInformationToken = NULL;
/*
* HookedNtAdjustPrivilegesToken()
*
* Description:
* This function mediates the NtAdjustPrivilegesToken() system service and XXX.
*
* NOTE: ZwAdjustPrivilegesToken adjusts the attributes of the privileges in a token. [NAR]
*
* Parameters:
* Those of NtAdjustPrivilegesToken().
*
* Returns:
* STATUS_ACCESS_DENIED if the call does not pass the security policy check.
* Otherwise, NTSTATUS returned by NtAdjustPrivilegesToken().
*/
NTSTATUS
NTAPI
HookedNtAdjustPrivilegesToken
(
IN HANDLE TokenHandle,
IN BOOLEAN DisableAllPrivileges,
IN PTOKEN_PRIVILEGES NewState,
IN ULONG BufferLength,
OUT PTOKEN_PRIVILEGES PreviousState OPTIONAL,
OUT PULONG ReturnLength
)
{
PCHAR FunctionName = "HookedNtAdjustPrivilegesToken";
PCHAR TOKENNAME = NULL; /* allow the use of POLICY_CHECK_OPTYPE_NAME() macro */
ULONG i;
HOOK_ROUTINE_ENTER();
/*
if (LearningMode == FALSE && IsTokenModificationAllowed() == FALSE)
{
LOG(LOG_SS_TOKEN, LOG_PRIORITY_DEBUG, ("%d (%S) HookedNtAdjustPrivilegesToken: disallowing token modification\n", (ULONG) PsGetCurrentProcessId(), GetCurrentProcessName()));
LogAlert(ALERT_SS_TOKEN, OP_MODIFY, ALERT_RULE_NONE, ACTION_DENY, ALERT_PRIORITY_MEDIUM, NULL, 0, NULL);
HOOK_ROUTINE_EXIT( STATUS_ACCESS_DENIED );
}
*/
if (LearningMode == FALSE)
{
POLICY_CHECK_OPTYPE_NAME(TOKEN, OP_TOKEN_MODIFY);
}
if (KeGetPreviousMode() != KernelMode && DisableAllPrivileges == FALSE && ARGUMENT_PRESENT(NewState))
{
BOOLEAN CaughtException;
__try
{
// Probe to make sure the first ULONG (PrivilegeCount) is accessible
ProbeForRead(NewState, sizeof(ULONG), sizeof(ULONG));
// Now probe the entire structure
ProbeForRead(NewState, sizeof(TOKEN_PRIVILEGES) +
(NewState->PrivilegeCount - ANYSIZE_ARRAY) * sizeof(LUID_AND_ATTRIBUTES),
sizeof(ULONG));
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
NTSTATUS status = GetExceptionCode();
LOG(LOG_SS_TOKEN, LOG_PRIORITY_DEBUG, ("HookedNtAdjustPrivilegesToken(): caught an exception. status = 0x%x\n", status));
CaughtException = TRUE;
}
LOG(LOG_SS_TOKEN, LOG_PRIORITY_VERBOSE, ("%d HookedNtAdjustPrivilegesToken: %S\n", (ULONG) PsGetCurrentProcessId(), GetCurrentProcessName()));
//XXX replace with PID lookup
/*
if (CaughtException == FALSE &&
wcsstr(GetCurrentProcessName(), L"svchost.exe") == 0 &&
wcsstr(GetCurrentProcessName(), L"services.exe") == 0)
{
LOG(LOG_SS_TOKEN, LOG_PRIORITY_DEBUG, ("%d HookedNtAdjustPrivilegesToken\n", (ULONG) PsGetCurrentProcessId()));
for (i = 0; i < NewState->PrivilegeCount; i++)
{
if (NewState->Privileges[i].Luid.LowPart == SE_AUDIT_PRIVILEGE && NewState->Privileges[i].Luid.HighPart == 0)
;
else
KdPrint(("priv %d: %x %x %x\n", i, NewState->Privileges[i].Attributes, NewState->Privileges[i].Luid.LowPart, NewState->Privileges[i].Luid.HighPart));
}
}
*/
}
ASSERT(OriginalNtAdjustPrivilegesToken);
rc = OriginalNtAdjustPrivilegesToken(TokenHandle, DisableAllPrivileges, NewState, BufferLength,
PreviousState, ReturnLength);
if (LearningMode == TRUE)
{
AddRule(RULE_TOKEN, NULL, OP_TOKEN_MODIFY);
}
HOOK_ROUTINE_EXIT(rc);
}
/*
* HookedNtSetInformationToken()
*
* Description:
* This function mediates the NtSetInformationToken() system service and XXX.
*
* NOTE: ZwSetInformationToken sets information affecting a token object. [NAR]
*
* Parameters:
* Those of NtSetInformationToken().
*
* Returns:
* STATUS_ACCESS_DENIED if the call does not pass the security policy check.
* Otherwise, NTSTATUS returned by NtSetInformationToken().
*/
NTSTATUS
NTAPI
HookedNtSetInformationToken
(
IN HANDLE TokenHandle,
IN TOKEN_INFORMATION_CLASS TokenInformationClass,
IN PVOID TokenInformation,
IN ULONG TokenInformationLength
)
{
PCHAR FunctionName = "HookedNtSetInformationToken";
PCHAR TOKENNAME = NULL; /* allow the use of POLICY_CHECK_OPTYPE_NAME() macro */
HOOK_ROUTINE_ENTER();
LOG(LOG_SS_TOKEN, LOG_PRIORITY_VERBOSE, ("%d HookedNtSetInformationToken %S %x %x %x\n", (ULONG) PsGetCurrentProcessId(), GetCurrentProcessName(), TokenInformationClass, TokenInformation, TokenInformationLength));
/*
if (LearningMode == FALSE && IsTokenModificationAllowed() == FALSE)
{
LOG(LOG_SS_TOKEN, LOG_PRIORITY_DEBUG, ("%d (%S) HookedNtSetInformationToken: disallowing token modification\n", (ULONG) PsGetCurrentProcessId(), GetCurrentProcessName()));
LogAlert(ALERT_SS_TOKEN, OP_MODIFY, ALERT_RULE_NONE, ACTION_DENY, ALERT_PRIORITY_MEDIUM, NULL, 0, NULL);
HOOK_ROUTINE_EXIT( STATUS_ACCESS_DENIED );
}
*/
if (LearningMode == FALSE)
{
POLICY_CHECK_OPTYPE_NAME(TOKEN, OP_TOKEN_MODIFY);
}
ASSERT(OriginalNtSetInformationToken);
rc = OriginalNtSetInformationToken(TokenHandle, TokenInformationClass, TokenInformation, TokenInformationLength);
if (LearningMode == TRUE)
{
AddRule(RULE_TOKEN, NULL, OP_TOKEN_MODIFY);
}
HOOK_ROUTINE_EXIT(rc);
}
/*
* InitTokenHooks()
*
* Description:
* Initializes all the mediated token object operation pointers. The "OriginalFunction" pointers
* are initialized by InstallSyscallsHooks() that must be called prior to this function.
*
* NOTE: Called once during driver initialization (DriverEntry()).
*
* Parameters:
* None.
*
* Returns:
* TRUE to indicate success, FALSE if failed.
*/
BOOLEAN
InitTokenHooks()
{
if ( (OriginalNtAdjustPrivilegesToken = (fpZwAdjustPrivilegesToken) ZwCalls[ZW_ADJUST_TOKEN_INDEX].OriginalFunction) == NULL)
{
LOG(LOG_SS_TOKEN, LOG_PRIORITY_DEBUG, ("InitTokenHooks: OriginalNtAdjustPrivilegesToken is NULL\n"));
return FALSE;
}
if ( (OriginalNtSetInformationToken = (fpZwSetInformationToken) ZwCalls[ZW_SET_INFO_TOKEN_INDEX].OriginalFunction) == NULL)
{
LOG(LOG_SS_TOKEN, LOG_PRIORITY_DEBUG, ("InitTokenHooks: OriginalNtSetInformationToken is NULL\n"));
return FALSE;
}
return TRUE;
}