10
10
11
11
namespace wv2util
12
12
{
13
+ [ StructLayout ( LayoutKind . Sequential ) ]
14
+ public struct SID_AND_ATTRIBUTES
15
+ {
16
+ public IntPtr Sid ;
17
+ public UInt32 Attributes ;
18
+ }
19
+
20
+ [ StructLayout ( LayoutKind . Sequential ) ]
21
+ public struct TOKEN_MANDATORY_LABEL
22
+ {
23
+ public SID_AND_ATTRIBUTES Label ;
24
+ }
25
+
13
26
public static class ProcessUtil
14
27
{
28
+
29
+ #region Integrity Levels Utilities
30
+ [ DllImport ( "advapi32" , CharSet = CharSet . Auto , SetLastError = true ) ]
31
+ static extern bool ConvertSidToStringSid ( IntPtr securityIdentifier , out string securityIdentifierName ) ;
32
+
33
+ [ DllImport ( "advapi32.dll" , SetLastError = true ) ]
34
+ static extern IntPtr GetSidSubAuthority ( IntPtr sid , UInt32 subAuthorityIndex ) ;
35
+
36
+ [ DllImport ( "advapi32.dll" , SetLastError = true ) ]
37
+ static extern IntPtr GetSidSubAuthorityCount ( IntPtr sid ) ;
38
+
39
+ const int SECURITY_MANDATORY_UNTRUSTED_RID = ( 0x00000000 ) ;
40
+ const int SECURITY_MANDATORY_LOW_RID = ( 0x00001000 ) ;
41
+ const int SECURITY_MANDATORY_MEDIUM_RID = ( 0x00002000 ) ;
42
+ const int SECURITY_MANDATORY_MEDIUM_PLUS_RID = ( 0x00002100 ) ;
43
+ const int SECURITY_MANDATORY_HIGH_RID = ( 0x00003000 ) ;
44
+ const int SECURITY_MANDATORY_SYSTEM_RID = ( 0x00004000 ) ;
45
+ const int SECURITY_MANDATORY_PROTECTED_PROCESS_RID = ( 0x00005000 ) ;
46
+ #endregion Integrity Levels Utilities
47
+
15
48
private readonly static string [ ] InterestingDllFileNames = new string [ ]
16
49
{
17
50
"embeddedbrowserwebview.dll" ,
@@ -155,7 +188,7 @@ public static string GetPackageFullName(int processId)
155
188
156
189
public static string GetIntegrityLevelOfProcess ( int pid )
157
190
{
158
- // Determine if this is admin
191
+ // Determine if this is admin with TokenElevationType
159
192
var processSafeHandle = PInvoke . Kernel32 . OpenProcess (
160
193
PInvoke . Kernel32 . ProcessAccess . PROCESS_QUERY_INFORMATION ,
161
194
false ,
@@ -196,6 +229,7 @@ public static string GetIntegrityLevelOfProcess(int pid)
196
229
}
197
230
}
198
231
232
+ // Determine if this is AppContainer with TokenIsAppContainer
199
233
UInt32 [ ] isAppContainer = new UInt32 [ ] { 0 } ;
200
234
unsafe
201
235
{
@@ -219,7 +253,53 @@ public static string GetIntegrityLevelOfProcess(int pid)
219
253
}
220
254
}
221
255
222
- return "Normal" ;
256
+ // Determine the IntegrityLevel with TokenIntegrityLevel
257
+ // https://devblogs.microsoft.com/oldnewthing/20221017-00/?p=107291
258
+ string ilAsString = "Unknown" ;
259
+ unsafe
260
+ {
261
+ // Calling GetTokenInformation first to get the token information length
262
+ PInvoke . AdvApi32 . GetTokenInformation (
263
+ tokenHandle ,
264
+ PInvoke . AdvApi32 . TOKEN_INFORMATION_CLASS . TokenIntegrityLevel ,
265
+ IntPtr . Zero ,
266
+ 0 ,
267
+ out int integrityLevelInfoLength ) ;
268
+ IntPtr integrityLevel = Marshal . AllocHGlobal ( integrityLevelInfoLength ) ;
269
+ if ( ! PInvoke . AdvApi32 . GetTokenInformation (
270
+ tokenHandle ,
271
+ PInvoke . AdvApi32 . TOKEN_INFORMATION_CLASS . TokenIntegrityLevel ,
272
+ integrityLevel ,
273
+ integrityLevelInfoLength ,
274
+ out int _ ) )
275
+ {
276
+ PInvoke . Win32ErrorCode errorCode = PInvoke . Kernel32 . GetLastError ( ) ;
277
+ Marshal . FreeHGlobal ( integrityLevel ) ;
278
+ if ( errorCode != PInvoke . Win32ErrorCode . ERROR_SUCCESS )
279
+ {
280
+ throw new PInvoke . Win32Exception ( errorCode , "Error calling GetTokenInformation" ) ;
281
+ }
282
+ }
283
+
284
+ TOKEN_MANDATORY_LABEL mandatoryLabel = ( TOKEN_MANDATORY_LABEL ) Marshal . PtrToStructure (
285
+ integrityLevel , typeof ( TOKEN_MANDATORY_LABEL ) ) ;
286
+ IntPtr pSid = mandatoryLabel . Label . Sid ;
287
+ // The integrity level is encoded in the SID as the relative identifier (the final subauthority).
288
+ uint subAuthorityCount = Marshal . ReadByte ( GetSidSubAuthorityCount ( pSid ) ) ;
289
+ int subAuthority = Marshal . ReadInt32 ( GetSidSubAuthority ( pSid , subAuthorityCount - 1 ) ) ;
290
+ switch ( subAuthority )
291
+ {
292
+ case SECURITY_MANDATORY_UNTRUSTED_RID : ilAsString = "Untrusted" ; break ; // 0x00000000 Untrusted.
293
+ case SECURITY_MANDATORY_LOW_RID : ilAsString = "Low" ; break ; // 0x00001000 Low integrity.
294
+ case SECURITY_MANDATORY_MEDIUM_RID : ilAsString = "Medium" ; break ; // 0x00002000 Medium integrity.
295
+ case SECURITY_MANDATORY_MEDIUM_PLUS_RID : ilAsString = "MediumPlus" ; break ; // SECURITY_MANDATORY_MEDIUM_RID + 0x100 Medium high integrity.
296
+ case SECURITY_MANDATORY_HIGH_RID : ilAsString = "High" ; break ; // 0X00003000 High integrity.
297
+ case SECURITY_MANDATORY_SYSTEM_RID : ilAsString = "System" ; break ; // 0x00004000 System integrity.
298
+ case SECURITY_MANDATORY_PROTECTED_PROCESS_RID : ilAsString = "ProtectedProcess" ; break ;
299
+ }
300
+ Marshal . FreeHGlobal ( integrityLevel ) ;
301
+ }
302
+ return ilAsString ;
223
303
}
224
304
225
305
public static Process GetParentProcess ( this Process process )
0 commit comments