-
Notifications
You must be signed in to change notification settings - Fork 36
/
NtUtils.WinSafer.pas
169 lines (136 loc) · 3.96 KB
/
NtUtils.WinSafer.pas
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
unit NtUtils.WinSafer;
{
This module provides functions for restricting tokens via Safer API.
}
interface
uses
Ntapi.ntseapi, Ntapi.WinSafer, NtUtils, NtUtils.Objects;
type
ISaferHandle = IHandle;
// Open a Safer level
function SafexOpenLevel(
out hxLevel: ISaferHandle;
ScopeId: TSaferScopeId;
LevelId: TSaferLevelId
): TNtxStatus;
// Query Safer level information
function SafexQueryLevel(
hLevel: TSaferHandle;
InfoClass: TSaferObjectInfoClass;
out xMemory: IMemory;
InitialBuffer: Cardinal = 0;
[opt] GrowthMethod: TBufferGrowthMethod = nil
): TNtxStatus;
// Query Safer level name
function SafexQueryNameLevel(
hLevel: TSaferHandle;
out Name: String
): TNtxStatus;
// Query Safer level description
function SafexQueryDescriptionLevel(
hLevel: TSaferHandle;
out Description: String
): TNtxStatus;
// Restricts a token using Safer API level
function SafexComputeSaferToken(
out hxNewToken: IHandle;
[Access(TOKEN_DUPLICATE or TOKEN_QUERY)] hxExistingToken: IHandle;
hLevel: TSaferHandle;
MakeSandboxInert: Boolean = False
): TNtxStatus;
// Restricts a token using Safer API level identified by its IDs
function SafexComputeSaferTokenById(
out hxNewToken: IHandle;
[Access(TOKEN_DUPLICATE or TOKEN_QUERY)] const hxExistingToken: IHandle;
ScopeId: TSaferScopeId;
LevelId: TSaferLevelId;
MakeSandboxInert: Boolean = False
): TNtxStatus;
implementation
uses
NtUtils.Tokens, DelphiUtils.AutoObjects, NtUtils.SysUtils;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
type
TSaferAutoHandle = class(TCustomAutoHandle, ISaferHandle, IAutoReleasable)
procedure Release; override;
end;
procedure TSaferAutoHandle.Release;
begin
if FHandle <> 0 then
SaferCloseLevel(FHandle);
FHandle := 0;
inherited;
end;
function SafexOpenLevel;
var
hLevel: TSaferHandle;
begin
Result.Location := 'SaferCreateLevel';
Result.Win32Result := SaferCreateLevel(ScopeId, LevelId, SAFER_LEVEL_OPEN,
hLevel);
if Result.IsSuccess then
hxLevel := TSaferAutoHandle.Capture(hLevel);
end;
function SafexQueryLevel;
var
Required: Cardinal;
begin
Result.Location := 'SaferGetLevelInformation';
Result.LastCall.UsesInfoClass(InfoClass, icQuery);
xMemory := Auto.AllocateDynamic(InitialBuffer);
repeat
Required := 0;
Result.Win32Result := SaferGetLevelInformation(hLevel, InfoClass,
xMemory.Data, xMemory.Size, Required);
until not NtxExpandBufferEx(Result, xMemory, Required, GrowthMethod);
end;
function SafexQueryNameLevel;
var
xMemory: IMemory<PWideChar>;
begin
Result := SafexQueryLevel(hLevel, SaferObjectFriendlyName, IMemory(xMemory),
SizeOf(WideChar));
if Result.IsSuccess then
Name := RtlxCaptureString(xMemory.Data, xMemory.Size div SizeOf(WideChar));
end;
function SafexQueryDescriptionLevel;
var
xMemory: IMemory<PWideChar>;
begin
Result := SafexQueryLevel(hLevel, SaferObjectDescription, IMemory(xMemory),
SizeOf(WideChar));
if Result.IsSuccess then
Description := RtlxCaptureString(xMemory.Data,
xMemory.Size div SizeOf(WideChar));
end;
function SafexComputeSaferToken;
var
hNewToken: THandle;
Flags: TSaferComputeOptions;
begin
// Add support for pseudo-handles on input
Result := NtxExpandToken(hxExistingToken, TOKEN_DUPLICATE or TOKEN_QUERY);
if not Result.IsSuccess then
Exit;
Flags := 0;
if MakeSandboxInert then
Flags := Flags or SAFER_TOKEN_MAKE_INERT;
Result.Location := 'SaferComputeTokenFromLevel';
Result.LastCall.Expects<TTokenAccessMask>(TOKEN_DUPLICATE or TOKEN_QUERY);
Result.Win32Result := SaferComputeTokenFromLevel(hLevel,
hxExistingToken.Handle, hNewToken, Flags);
if Result.IsSuccess then
hxNewToken := Auto.CaptureHandle(hNewToken);
end;
function SafexComputeSaferTokenById;
var
hxLevel: ISaferHandle;
begin
Result := SafexOpenLevel(hxLevel, ScopeId, LevelId);
if Result.IsSuccess then
Result := SafexComputeSaferToken(hxNewToken, hxExistingToken,
hxLevel.Handle, MakeSandboxInert);
end;
end.