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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
use once_cell::unsync::OnceCell;
use vmi_core::{Va, VmiError, VmiState, VmiVa, driver::VmiRead};
use crate::{ArchAdapter, WindowsError, WindowsOs, offset};
/// A Windows security identifier.
///
/// A SID names a security principal. The Object Manager attaches one
/// to every securable kernel object, the Security Reference Monitor
/// uses it for access checks, and tokens carry a user SID plus a list
/// of group SIDs.
///
/// # Implementation Details
///
/// Corresponds to `_SID`.
pub struct WindowsSid<'a, Driver>
where
Driver: VmiRead,
Driver::Architecture: ArchAdapter<Driver>,
{
/// The VMI state.
vmi: VmiState<'a, WindowsOs<Driver>>,
/// Address of the `_SID` structure.
va: Va,
/// Cached sub-authority array.
///
/// # Implementation Details
///
/// Corresponds to `_SID.SubAuthority`.
sub_authorities: OnceCell<Vec<u32>>,
}
impl<Driver> VmiVa for WindowsSid<'_, Driver>
where
Driver: VmiRead,
Driver::Architecture: ArchAdapter<Driver>,
{
fn va(&self) -> Va {
self.va
}
}
impl<'a, Driver> WindowsSid<'a, Driver>
where
Driver: VmiRead,
Driver::Architecture: ArchAdapter<Driver>,
{
/// Maximum allowed value of `_SID.SubAuthorityCount`.
///
/// # Implementation Details
///
/// Corresponds to `SID_MAX_SUB_AUTHORITIES`.
pub const MAX_SUB_AUTHORITIES: u8 = 15;
/// Creates a new Windows security identifier accessor.
pub fn new(vmi: VmiState<'a, WindowsOs<Driver>>, va: Va) -> Self {
Self {
vmi,
va,
sub_authorities: OnceCell::new(),
}
}
/// Returns the SID revision byte. Always 1 for valid SIDs.
///
/// # Implementation Details
///
/// Corresponds to `_SID.Revision`.
pub fn revision(&self) -> Result<u8, VmiError> {
let SID = offset!(self.vmi, _SID);
self.vmi.read_u8(self.va + SID.Revision.offset())
}
/// Returns the number of sub-authority entries.
///
/// # Implementation Details
///
/// Corresponds to `_SID.SubAuthorityCount`.
pub fn sub_authority_count(&self) -> Result<u8, VmiError> {
let SID = offset!(self.vmi, _SID);
let count = self.vmi.read_u8(self.va + SID.SubAuthorityCount.offset())?;
Ok(count)
}
/// Returns the 48-bit identifier authority value.
///
/// # Implementation Details
///
/// Corresponds to `_SID.IdentifierAuthority`, decoded as a 48-bit
/// big-endian value.
pub fn authority(&self) -> Result<u64, VmiError> {
let SID = offset!(self.vmi, _SID);
let mut bytes = [0; 8];
self.vmi
.read(self.va + SID.IdentifierAuthority.offset(), &mut bytes[2..])?;
Ok(u64::from_be_bytes(bytes))
}
/// Returns the sub-authority array.
///
/// # Implementation Details
///
/// Corresponds to `_SID.SubAuthority`. The array length is
/// determined by [`sub_authority_count`].
///
/// [`sub_authority_count`]: Self::sub_authority_count
pub fn sub_authorities(&self) -> Result<&[u32], VmiError> {
self.sub_authorities
.get_or_try_init(|| {
let SID = offset!(self.vmi, _SID);
let count = self.sub_authority_count()?;
if count > Self::MAX_SUB_AUTHORITIES {
return Err(WindowsError::CorruptedStruct("SID.SubAuthorityCount").into());
}
let base = self.va + SID.SubAuthority.offset();
let mut out = Vec::with_capacity(count as usize);
for index in 0..u64::from(count) {
out.push(self.vmi.read_u32(base + index * 4)?);
}
Ok(out)
})
.map(Vec::as_slice)
}
}
impl<Driver> std::fmt::Debug for WindowsSid<'_, Driver>
where
Driver: VmiRead,
Driver::Architecture: ArchAdapter<Driver>,
{
/// Renders the SID in the standard `S-R-A-S-S...` form.
///
/// Authority is decimal when it fits in 32 bits, hex otherwise,
/// matching `RtlConvertSidToUnicodeString`. A read failure on any
/// field aborts rendering and writes `?` instead.
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let revision = match self.revision() {
Ok(revision) => revision,
Err(_) => return write!(f, "?"),
};
let authority = match self.authority() {
Ok(authority) => authority,
Err(_) => return write!(f, "?"),
};
let sub_authorities = match self.sub_authorities() {
Ok(sub_authorities) => sub_authorities,
Err(_) => return write!(f, "?"),
};
write!(f, "S-{revision}-")?;
match authority {
authority if authority < (1 << 32) => write!(f, "{authority}")?,
authority => write!(f, "0x{authority:012X}")?,
}
for sub_authority in sub_authorities {
write!(f, "-{sub_authority}")?;
}
Ok(())
}
}
bitflags::bitflags! {
/// Attribute bitmask stored in `_SID_AND_ATTRIBUTES.Attributes`.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct WindowsSidAttributes: u32 {
/// `SE_GROUP_MANDATORY`.
const MANDATORY = 0x0000_0001;
/// `SE_GROUP_ENABLED_BY_DEFAULT`.
const ENABLED_BY_DEFAULT = 0x0000_0002;
/// `SE_GROUP_ENABLED`.
const ENABLED = 0x0000_0004;
/// `SE_GROUP_OWNER`.
const OWNER = 0x0000_0008;
/// `SE_GROUP_USE_FOR_DENY_ONLY`.
const USE_FOR_DENY_ONLY = 0x0000_0010;
/// `SE_GROUP_INTEGRITY`.
const INTEGRITY = 0x0000_0020;
/// `SE_GROUP_INTEGRITY_ENABLED`.
const INTEGRITY_ENABLED = 0x0000_0040;
/// `SE_GROUP_RESOURCE`.
const RESOURCE = 0x2000_0000;
/// `SE_GROUP_LOGON_ID`.
const LOGON_ID = 0xC000_0000;
}
}
/// A Windows `_SID_AND_ATTRIBUTES` element.
///
/// Pairs a SID pointer with a 32-bit attribute bitmask. Appears as the
/// element type of every inline SID list in the kernel.
///
/// # Implementation Details
///
/// Corresponds to `_SID_AND_ATTRIBUTES`.
pub struct WindowsSidAndAttributes<'a, Driver>
where
Driver: VmiRead,
Driver::Architecture: ArchAdapter<Driver>,
{
/// The VMI state.
vmi: VmiState<'a, WindowsOs<Driver>>,
/// Address of the `_SID_AND_ATTRIBUTES` structure.
va: Va,
}
impl<Driver> VmiVa for WindowsSidAndAttributes<'_, Driver>
where
Driver: VmiRead,
Driver::Architecture: ArchAdapter<Driver>,
{
fn va(&self) -> Va {
self.va
}
}
impl<'a, Driver> WindowsSidAndAttributes<'a, Driver>
where
Driver: VmiRead,
Driver::Architecture: ArchAdapter<Driver>,
{
/// Creates a new Windows `_SID_AND_ATTRIBUTES` accessor.
pub fn new(vmi: VmiState<'a, WindowsOs<Driver>>, va: Va) -> Self {
Self { vmi, va }
}
/// Returns the address of the SID.
///
/// # Implementation Details
///
/// Corresponds to `_SID_AND_ATTRIBUTES.Sid`.
pub fn sid_va(&self) -> Result<Va, VmiError> {
let SID_AND_ATTRIBUTES = offset!(self.vmi, _SID_AND_ATTRIBUTES);
self.vmi
.read_va_native(self.va + SID_AND_ATTRIBUTES.Sid.offset())
}
/// Returns an accessor for the SID itself.
///
/// Shortcut for [`WindowsSid::new`] over [`Self::sid_va`].
pub fn sid(&self) -> Result<WindowsSid<'a, Driver>, VmiError> {
Ok(WindowsSid::new(self.vmi, self.sid_va()?))
}
/// Returns the attribute bitmask.
///
/// # Implementation Details
///
/// Corresponds to `_SID_AND_ATTRIBUTES.Attributes`.
pub fn attributes(&self) -> Result<WindowsSidAttributes, VmiError> {
let SID_AND_ATTRIBUTES = offset!(self.vmi, _SID_AND_ATTRIBUTES);
let raw = self
.vmi
.read_u32(self.va + SID_AND_ATTRIBUTES.Attributes.offset())?;
Ok(WindowsSidAttributes::from_bits_retain(raw))
}
}