-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShowSID.cpp
77 lines (60 loc) · 2.3 KB
/
ShowSID.cpp
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
/** ShowSID.cpp (2023.06.15) P. Stuer **/
#include "pch.h"
#include <CppCoreCheck/Warnings.h>
#pragma warning(disable: 4710 4820 ALL_CPPCORECHECK_WARNINGS)
#include "Console.h"
#include "Support.h"
extern Console _Console;
static LPCWSTR GetSidNameUseDescription(SID_NAME_USE sidNameUse);
///<summary>
/// Shows the specified SID.
///</summary>
HRESULT ShowSID(const PSID sid)
{
WCHAR AccountName[256] = { 0 };
DWORD AccountNameSize = ARRAYSIZE(AccountName);
WCHAR DomainName[256] = { 0 };
DWORD DomainNameSize = ARRAYSIZE(DomainName);
SID_NAME_USE SidNameUse;
if (!::LookupAccountSidW(NULL, sid, AccountName, &AccountNameSize, DomainName, &DomainNameSize, &SidNameUse))
{
if (::GetLastError() == ERROR_NONE_MAPPED)
::wcscpy_s(AccountName, AccountNameSize, L"<Not Mapped>");
else
return HRESULT_FROM_WIN32(ShowError(L"LookupAccountSid() failed."));
}
LPWSTR AccountSidString = nullptr;
if (::ConvertSidToStringSidW(sid, &AccountSidString))
{
_Console.SetBullet(true);
_Console.Write(L"SID: \"");
_Console.Write((DomainName[0] ? L"%s\\%s" : L"%s"), DomainName, AccountName);
_Console.Write(L"\", %s, %s\n", AccountSidString, GetSidNameUseDescription(SidNameUse));
_Console.SetBullet(false);
::LocalFree((HLOCAL)AccountSidString);
}
else
return HRESULT_FROM_WIN32(ShowError(L"ConvertSidToStringSid() failed."));
return S_OK;
}
///<summary>
/// Gets the description of the specified SID name use.
///</summary>
static LPCWSTR GetSidNameUseDescription(SID_NAME_USE sidNameUse)
{
switch (sidNameUse)
{
case SidTypeUser: return L"User SID";
case SidTypeGroup: return L"Group SID";
case SidTypeDomain: return L"Domain SID";
case SidTypeAlias: return L"Alias SID";
case SidTypeWellKnownGroup: return L"Well-known Group SID";
case SidTypeDeletedAccount: return L"Deleted Account SID";
case SidTypeInvalid: return L"Invalid SID";
case SidTypeUnknown: return L"Unknown SID Type";
case SidTypeComputer: return L"Computer SID";
case SidTypeLabel: return L"Mandatory Integrity Label SID";
case SidTypeLogonSession: return L"Logon Session SID";
default: return L"Unknown SID Name Use";
}
}