Skip to content

Commit 1ec51ea

Browse files
committed
Added extra property which represents which generic access an access is included in.
1 parent 66a1249 commit 1ec51ea

5 files changed

Lines changed: 117 additions & 5 deletions

File tree

NtApiDotNet/AccessMaskEntry.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,34 @@
1616

1717
namespace NtApiDotNet
1818
{
19+
/// <summary>
20+
/// Flags representing what generic access the entry maps to.
21+
/// </summary>
22+
[Flags]
23+
public enum GenericAccessType
24+
{
25+
/// <summary>
26+
/// Not mapped to any access.
27+
/// </summary>
28+
None = 0,
29+
/// <summary>
30+
/// Mapped to read.
31+
/// </summary>
32+
Read = 1,
33+
/// <summary>
34+
/// Mapped to write.
35+
/// </summary>
36+
Write = 2,
37+
/// <summary>
38+
/// Mapped to execute.
39+
/// </summary>
40+
Execute = 4,
41+
/// <summary>
42+
/// Mapped to All.
43+
/// </summary>
44+
All = 8
45+
}
46+
1947
/// <summary>
2048
/// A structure to hold an access mask to enum mapping.
2149
/// </summary>
@@ -29,6 +57,10 @@ public struct AccessMaskEntry
2957
/// The value of the access mask entry enumeration.
3058
/// </summary>
3159
public Enum Value { get; }
60+
/// <summary>
61+
/// The generic access this maps to.
62+
/// </summary>
63+
public GenericAccessType GenericAccess { get; }
3264

3365
/// <summary>
3466
/// Overridden ToString method.
@@ -39,10 +71,11 @@ public override string ToString()
3971
return $"{Mask:X08} - {Value}";
4072
}
4173

42-
internal AccessMaskEntry(AccessMask mask, Enum value)
74+
internal AccessMaskEntry(AccessMask mask, Enum value, GenericAccessType generic_access)
4375
{
4476
Mask = mask;
4577
Value = value;
78+
GenericAccess = generic_access;
4679
}
4780
}
4881
}

NtApiDotNet/NtSecurity.cs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,25 @@ public static CachedSigningLevel GetCachedSigningLevel(SafeKernelObjectHandle ha
902902
return new CachedSigningLevel(flags, signing_level, thumb_print, thumb_print_algo);
903903
}
904904

905+
/// <summary>
906+
/// Get the cached signing level for a file.
907+
/// </summary>
908+
/// <param name="handle">The handle to the file to query.</param>
909+
/// <param name="throw_on_error">True to throw on error.</param>
910+
/// <returns>The cached signing level.</returns>
911+
public static NtResult<CachedSigningLevel> GetCachedSigningLevel(SafeKernelObjectHandle handle, bool throw_on_error)
912+
{
913+
byte[] thumb_print = new byte[0x68];
914+
int thumb_print_size = thumb_print.Length;
915+
916+
return NtSystemCalls.NtGetCachedSigningLevel(handle, out int flags,
917+
out SigningLevel signing_level, thumb_print, ref thumb_print_size, out HashAlgorithm thumb_print_algo).CreateResult(throw_on_error, () =>
918+
{
919+
Array.Resize(ref thumb_print, thumb_print_size);
920+
return new CachedSigningLevel(flags, signing_level, thumb_print, thumb_print_algo);
921+
});
922+
}
923+
905924
/// <summary>
906925
/// Get the cached singing level from the raw EA buffer.
907926
/// </summary>
@@ -944,20 +963,50 @@ public static void SetCachedSigningLevel(SafeKernelObjectHandle handle,
944963
int flags, SigningLevel signing_level,
945964
IEnumerable<SafeKernelObjectHandle> source_files,
946965
string catalog_path)
966+
{
967+
SetCachedSigningLevel(handle, flags, signing_level, source_files, catalog_path, true);
968+
}
969+
970+
/// <summary>
971+
/// Set the cached signing level for a file.
972+
/// </summary>
973+
/// <param name="handle">The handle to the file to set the cache on.</param>
974+
/// <param name="flags">Flags to set for the cache.</param>
975+
/// <param name="signing_level">The signing level to cache</param>
976+
/// <param name="source_files">A list of source file for the cache.</param>
977+
/// <param name="catalog_path">Optional directory path to look for catalog files.</param>
978+
/// <param name="throw_on_error">True to throw on error.</param>
979+
public static NtStatus SetCachedSigningLevel(SafeKernelObjectHandle handle,
980+
int flags, SigningLevel signing_level,
981+
IEnumerable<SafeKernelObjectHandle> source_files,
982+
string catalog_path, bool throw_on_error)
947983
{
948984
IntPtr[] handles = source_files?.Select(f => f.DangerousGetHandle()).ToArray();
949985
int handles_count = handles == null ? 0 : handles.Length;
950986
if (catalog_path != null)
951987
{
952988
CachedSigningLevelInformation info = new CachedSigningLevelInformation(catalog_path);
953-
NtSystemCalls.NtSetCachedSigningLevel2(flags, signing_level, handles, handles_count, handle, info).ToNtException();
989+
return NtSystemCalls.NtSetCachedSigningLevel2(flags, signing_level, handles,
990+
handles_count, handle, info).ToNtException(throw_on_error);
954991
}
955992
else
956993
{
957-
NtSystemCalls.NtSetCachedSigningLevel(flags, signing_level, handles, handles_count, handle).ToNtException();
994+
return NtSystemCalls.NtSetCachedSigningLevel(flags, signing_level, handles,
995+
handles_count, handle).ToNtException(throw_on_error);
958996
}
959997
}
960998

999+
/// <summary>
1000+
/// Compare two signing levels.
1001+
/// </summary>
1002+
/// <param name="current_level">The current level.</param>
1003+
/// <param name="signing_level">The signing level to compare against.</param>
1004+
/// <returns>True if the current level is above or equal to the signing level.</returns>
1005+
public static bool CompareSigningLevel(SigningLevel current_level, SigningLevel signing_level)
1006+
{
1007+
return NtSystemCalls.NtCompareSigningLevel(current_level, signing_level).IsSuccess();
1008+
}
1009+
9611010
/// <summary>
9621011
/// Get readable name for a SID, if known. This covers sources of names such as LSASS lookup, capability names and package names.
9631012
/// </summary>

NtApiDotNet/NtSecurityNative.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,10 @@ public static extern NtStatus NtGetCachedSigningLevel(
813813
ref int ThumbprintSize,
814814
out HashAlgorithm ThumbprintAlgorithm
815815
);
816+
817+
[DllImport("ntdll.dll")]
818+
public static extern NtStatus NtCompareSigningLevel(
819+
SigningLevel CurrentLevel, SigningLevel CheckLevel);
816820
}
817821

818822
[StructLayout(LayoutKind.Sequential)]

NtApiDotNet/NtType.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,26 @@ public IEnumerable<AccessMaskEntry> AccessRights
196196
{
197197
if (Enum.IsDefined(AccessRightsType, mask))
198198
{
199+
GenericAccessType generic_access = GenericAccessType.None;
200+
if (GenericMapping.GenericRead.IsAccessGranted(mask))
201+
{
202+
generic_access |= GenericAccessType.Read;
203+
}
204+
if (GenericMapping.GenericWrite.IsAccessGranted(mask))
205+
{
206+
generic_access |= GenericAccessType.Write;
207+
}
208+
if (GenericMapping.GenericExecute.IsAccessGranted(mask))
209+
{
210+
generic_access |= GenericAccessType.Execute;
211+
}
212+
if (GenericMapping.GenericAll.IsAccessGranted(mask))
213+
{
214+
generic_access |= GenericAccessType.All;
215+
}
216+
199217
access_rights.Add(new AccessMaskEntry(mask,
200-
(Enum)Enum.ToObject(AccessRightsType, mask)));
218+
(Enum)Enum.ToObject(AccessRightsType, mask), generic_access));
201219
}
202220
mask <<= 1;
203221
}

NtObjectManager/Formatters.ps1xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,12 @@
13141314
</TableColumnHeader>
13151315
<TableColumnHeader>
13161316
<Label>Value</Label>
1317-
<Width>40</Width>
1317+
<Width>20</Width>
1318+
<Alignment>left</Alignment>
1319+
</TableColumnHeader>
1320+
<TableColumnHeader>
1321+
<Label>GenericAccess</Label>
1322+
<Width>30</Width>
13181323
<Alignment>left</Alignment>
13191324
</TableColumnHeader>
13201325
</TableHeaders>
@@ -1328,6 +1333,9 @@
13281333
<TableColumnItem>
13291334
<PropertyName>Value</PropertyName>
13301335
</TableColumnItem>
1336+
<TableColumnItem>
1337+
<PropertyName>GenericAccess</PropertyName>
1338+
</TableColumnItem>
13311339
</TableColumnItems>
13321340
</TableRowEntry>
13331341
</TableRowEntries>

0 commit comments

Comments
 (0)