-
Notifications
You must be signed in to change notification settings - Fork 3
/
sysinfo.c
197 lines (158 loc) · 5.53 KB
/
sysinfo.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
/*
* Copyright (c) 2004 Security Architects Corporation. All rights reserved.
*
* Module Name:
*
* sysinfo.c
*
* Abstract:
*
* This module defines various routines used for hooking ZwSetSystemInformation() routine.
* ZwSetSystemInformation's SystemLoadAndCallImage and SystemLoadImage parameters can be used
* to load code into kernel address space.
*
* Author:
*
* Eugene Tsyrklevich 01-Mar-2004
*
* Revision History:
*
* None.
*/
#include <NTDDK.h>
#include "sysinfo.h"
#include "hookproc.h"
#include "procname.h"
#include "learn.h"
#include "time.h"
#include "log.h"
#ifdef ALLOC_PRAGMA
#pragma alloc_text (INIT, InitSysInfoHooks)
#endif
fpZwSetSystemInformation OriginalNtSetSystemInformation = NULL;
/*
* HookedNtSetSystemInformation()
*
* Description:
* This function mediates the NtSetSystemInformation() system service and disallows access to
* Information Classes 26 (SystemLoadImage) and 38 (SystemLoadAndCallImage) which allow
* applications to load code into kernel memory.
*
* NOTE: ZwSetSystemInformation sets information that affects the operation of the system. [NAR]
*
* Parameters:
* Those of NtSetSystemInformation().
*
* Returns:
* STATUS_ACCESS_DENIED if Information Class 26 or 38 is used.
* Otherwise, NTSTATUS returned by NtSetSystemInformation().
*/
NTSTATUS
NTAPI
HookedNtSetSystemInformation
(
IN SYSTEM_INFORMATION_CLASS SystemInformationClass,
IN OUT PVOID SystemInformation,
IN ULONG SystemInformationLength
)
{
PCHAR FunctionName = "HookedNtSetSystemInformation";
HOOK_ROUTINE_ENTER();
if (SystemInformationClass == SystemLoadImage ||
SystemInformationClass == SystemLoadAndCallImage)
{
UNICODE_STRING usImageName;
ANSI_STRING asImageName;
CHAR DriverNameUnresolved[MAX_PATH];
if (!VerifyUnicodeString(SystemInformation, &usImageName))
{
LOG(LOG_SS_PORT, LOG_PRIORITY_DEBUG, ("%s: VerifyUnicodeString failed\n", FunctionName));
HOOK_ROUTINE_EXIT( STATUS_ACCESS_DENIED );
}
if (_snprintf(DriverNameUnresolved, MAX_PATH, "%S", usImageName.Buffer) < 0)
{
LOG(LOG_SS_DRIVER, LOG_PRIORITY_DEBUG, ("%s: Driver name '%S' is too long\n", FunctionName, usImageName.Buffer));
HOOK_ROUTINE_EXIT( STATUS_ACCESS_DENIED );
}
LOG(LOG_SS_SYSINFO, LOG_PRIORITY_VERBOSE, ("%d %s: SystemLoad %d %s\n", (ULONG) PsGetCurrentProcessId(), FunctionName, SystemInformationClass, DriverNameUnresolved));
/*
* Verify the image name against the security policy
*/
if (LearningMode == FALSE)
{
CHAR DRIVERNAME[MAX_PATH];
FixupFilename(DriverNameUnresolved, DRIVERNAME, MAX_PATH);
LOG(LOG_SS_SYSINFO, LOG_PRIORITY_VERBOSE, ("%d %s: SystemLoad %d %s\n", (ULONG) PsGetCurrentProcessId(), FunctionName, SystemInformationClass, DRIVERNAME));
POLICY_CHECK_OPTYPE_NAME(DRIVER, OP_LOAD);
}
else
{
AddRule(RULE_DRIVER, DriverNameUnresolved, OP_LOAD);
}
}
else if (SystemInformationClass == SystemUnloadImage)
{
LOG(LOG_SS_SYSINFO, LOG_PRIORITY_VERBOSE, ("%d HookedNtSetSystemInformation: SystemUnloadImage %x\n", (ULONG) PsGetCurrentProcessId(), SystemInformation));
}
else if (SystemInformationClass == SystemTimeAdjustment)
{
LOG(LOG_SS_SYSINFO, LOG_PRIORITY_VERBOSE, ("%d HookedNtSetSystemInformation: SystemTimeAdjustment\n", (ULONG) PsGetCurrentProcessId()));
if (LearningMode == FALSE)
{
PCHAR TIMENAME = NULL; /* allow the use of POLICY_CHECK_OPTYPE_NAME() macro */
POLICY_CHECK_OPTYPE_NAME(TIME, OP_TIME_CHANGE);
}
else if (LearningMode == TRUE)
{
AddRule(RULE_TIME, NULL, OP_TIME_CHANGE);
}
}
else if (SystemInformationClass == SystemProcessesAndThreadsInformation)
{
LOG(LOG_SS_SYSINFO, LOG_PRIORITY_DEBUG, ("%d HookedNtSetSystemInformation: SystemProcessesAndThreadsInformation\n", (ULONG) PsGetCurrentProcessId()));
}
else if (SystemInformationClass == SystemModuleInformation)
{
LOG(LOG_SS_SYSINFO, LOG_PRIORITY_DEBUG, ("%d HookedNtSetSystemInformation: SystemModuleInformation\n", (ULONG) PsGetCurrentProcessId()));
}
else if (SystemInformationClass == SystemCreateSession)
{
LOG(LOG_SS_SYSINFO, LOG_PRIORITY_VERBOSE, ("%d HookedNtSetSystemInformation: SystemCreateSession %x %x\n", (ULONG) PsGetCurrentProcessId(), SystemInformation, *(PULONG) SystemInformation));
}
else if (SystemInformationClass == SystemDeleteSession)
{
LOG(LOG_SS_SYSINFO, LOG_PRIORITY_VERBOSE, ("%d HookedNtSetSystemInformation: SystemDeleteSession %x %x\n", (ULONG) PsGetCurrentProcessId(), SystemInformation, *(PULONG) SystemInformation));
}
else if (SystemInformationClass == SystemSessionProcessesInformation)
{
LOG(LOG_SS_SYSINFO, LOG_PRIORITY_DEBUG, ("%d HookedNtSetSystemInformation: SystemSessionProcessesInformation\n", (ULONG) PsGetCurrentProcessId()));
}
ASSERT(OriginalNtSetSystemInformation);
rc = OriginalNtSetSystemInformation(SystemInformationClass, SystemInformation, SystemInformationLength);
HOOK_ROUTINE_EXIT(rc);
}
/*
* InitSysInfoHooks()
*
* Description:
* Initializes all the mediated system information 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
InitSysInfoHooks()
{
if ((OriginalNtSetSystemInformation = (fpZwSetSystemInformation) ZwCalls[ZW_SET_SYSTEM_INFORMATION_INDEX].OriginalFunction) == NULL)
{
LOG(LOG_SS_SYSINFO, LOG_PRIORITY_DEBUG, ("InitSysInfoHooks: OriginalNtSetSystemInformation is NULL\n"));
return FALSE;
}
return TRUE;
}