Skip to content

Commit 7b49a3a

Browse files
authored
Merge pull request #112 from david-risney/user/peiche/60-il-field
#60: FIx IL field in Host app details
2 parents 0c22def + bbdb113 commit 7b49a3a

File tree

1 file changed

+82
-2
lines changed

1 file changed

+82
-2
lines changed

wv2util/ProcessUtil.cs

+82-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,41 @@
1010

1111
namespace wv2util
1212
{
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+
1326
public static class ProcessUtil
1427
{
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+
1548
private readonly static string[] InterestingDllFileNames = new string[]
1649
{
1750
"embeddedbrowserwebview.dll",
@@ -155,7 +188,7 @@ public static string GetPackageFullName(int processId)
155188

156189
public static string GetIntegrityLevelOfProcess(int pid)
157190
{
158-
// Determine if this is admin
191+
// Determine if this is admin with TokenElevationType
159192
var processSafeHandle = PInvoke.Kernel32.OpenProcess(
160193
PInvoke.Kernel32.ProcessAccess.PROCESS_QUERY_INFORMATION,
161194
false,
@@ -196,6 +229,7 @@ public static string GetIntegrityLevelOfProcess(int pid)
196229
}
197230
}
198231

232+
// Determine if this is AppContainer with TokenIsAppContainer
199233
UInt32[] isAppContainer = new UInt32[] { 0 };
200234
unsafe
201235
{
@@ -219,7 +253,53 @@ public static string GetIntegrityLevelOfProcess(int pid)
219253
}
220254
}
221255

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;
223303
}
224304

225305
public static Process GetParentProcess(this Process process)

0 commit comments

Comments
 (0)