stdex
Additional custom or not Standard C++ covered algorithms
Loading...
Searching...
No Matches
sys_info.hpp
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 2023 Amebis
4*/
5
6#pragma once
7
8#include "compat.hpp"
9#include "system.hpp"
10#ifdef _WIN32
11#include "windows.h"
12#include <stdlib.h>
13#include <tchar.h>
14#else
15#include <sys/utsname.h>
16#endif
17#include <memory>
18
19namespace stdex
20{
24#ifdef _WIN32
25 typedef uint16_t platform_id;
26#else
27 typedef const char* platform_id;
28#endif
29}
30
31#ifndef _WIN32
32constexpr stdex::platform_id IMAGE_FILE_MACHINE_UNKNOWN = nullptr;
33constexpr stdex::platform_id IMAGE_FILE_MACHINE_I386 = "i386";
34constexpr stdex::platform_id IMAGE_FILE_MACHINE_AMD64 = "x86_64";
35constexpr stdex::platform_id IMAGE_FILE_MACHINE_ARMNT = "arm";
36constexpr stdex::platform_id IMAGE_FILE_MACHINE_ARM64 = "aarch64";
37
38inline bool operator ==(_In_ const stdex::platform_id a, _In_ const stdex::platform_id b) { return a == b; }
39inline bool operator !=(_In_ const stdex::platform_id a, _In_ const stdex::platform_id b) { return a != b; }
40inline bool operator <(_In_ const stdex::platform_id a, _In_ const stdex::platform_id b) { return a == IMAGE_FILE_MACHINE_UNKNOWN && b != IMAGE_FILE_MACHINE_UNKNOWN || a != IMAGE_FILE_MACHINE_UNKNOWN && b != IMAGE_FILE_MACHINE_UNKNOWN && strcmp(a, b) < 0; }
41inline bool operator <=(_In_ const stdex::platform_id a, _In_ const stdex::platform_id b) { return a == IMAGE_FILE_MACHINE_UNKNOWN || a != IMAGE_FILE_MACHINE_UNKNOWN && b != IMAGE_FILE_MACHINE_UNKNOWN && strcmp(a, b) <= 0; }
42inline bool operator >(_In_ const stdex::platform_id a, _In_ const stdex::platform_id b) { return a != IMAGE_FILE_MACHINE_UNKNOWN && b == IMAGE_FILE_MACHINE_UNKNOWN || a != IMAGE_FILE_MACHINE_UNKNOWN && b != IMAGE_FILE_MACHINE_UNKNOWN && strcmp(a, b) > 0; }
43inline bool operator >=(_In_ const stdex::platform_id a, _In_ const stdex::platform_id b) { return b == IMAGE_FILE_MACHINE_UNKNOWN || a != IMAGE_FILE_MACHINE_UNKNOWN && b != IMAGE_FILE_MACHINE_UNKNOWN && strcmp(a, b) >= 0; }
44#endif
45
46namespace stdex
47{
51#if _HAS_CXX17
52 inline
53#endif
54 const struct sys_info_t
55 {
59#if _M_IX86
60 static constexpr platform_id process_platform = IMAGE_FILE_MACHINE_I386;
61#elif _M_X64 // _M_ARM64EC is introducing as x64
62 static constexpr platform_id process_platform = IMAGE_FILE_MACHINE_AMD64;
63#elif _M_ARM
64 static constexpr platform_id process_platform = IMAGE_FILE_MACHINE_ARMNT;
65#elif _M_ARM64
66 static constexpr platform_id process_platform = IMAGE_FILE_MACHINE_ARM64;
67#elif __i386__
68 static constexpr platform_id process_platform = "i386";
69#elif __x86_64__
70 static constexpr platform_id process_platform = "x86_64";
71#elif __aarch64__
72 static constexpr platform_id process_platform = "aarch64";
73#else
74 #error Unknown platform
75#endif
76
80 platform_id os_platform;
81
82#ifdef _WIN32
86 bool wow64;
87#endif
88
93
97 bool admin;
98
103
104 sys_info_t() :
105 os_platform(IMAGE_FILE_MACHINE_UNKNOWN),
106#ifdef _WIN32
107 wow64(false),
108#endif
110 admin(false),
111 elevated(false)
112 {
113#ifdef _WIN32
114 HMODULE kernel32_handle;
115 kernel32_handle = LoadLibrary(_T("kernel32.dll"));
116 _Assume_(kernel32_handle);
117 BOOL(WINAPI * IsWow64Process2)(HANDLE hProcess, USHORT * pProcessMachine, USHORT * pNativeMachine);
118 *reinterpret_cast<FARPROC*>(&IsWow64Process2) = GetProcAddress(kernel32_handle, "IsWow64Process2");
119 HANDLE process = GetCurrentProcess();
120 USHORT process_machine;
121#ifndef _WIN64
122 BOOL Wow64Process;
123#endif
124 if (IsWow64Process2 && IsWow64Process2(process, &process_machine, &os_platform)) {
125 wow64 = process_machine != IMAGE_FILE_MACHINE_UNKNOWN;
126 }
127#ifdef _WIN64
128 else {
129 os_platform = process_platform;
130 wow64 = false;
131 }
132#else
133 else if (IsWow64Process(process, &Wow64Process)) {
134 if (Wow64Process) {
135 os_platform = IMAGE_FILE_MACHINE_AMD64;
136 wow64 = true;
137 }
138 else {
139 os_platform = process_platform;
140 wow64 = false;
141 }
142 }
143#endif
144 FreeLibrary(kernel32_handle);
145#else
146 memset(&m_utsn, 0, sizeof(m_utsn));
147 if (uname(&m_utsn) != -1)
148 os_platform = reinterpret_cast<platform_id>(m_utsn.machine);
149#endif
150
151#ifdef _WIN32
152 HWINSTA hWinSta = GetProcessWindowStation();
153 if (hWinSta) {
154 TCHAR sName[MAX_PATH];
155 if (GetUserObjectInformation(hWinSta, UOI_NAME, sName, sizeof(sName), NULL)) {
156 sName[_countof(sName) - 1] = 0;
157 // Only "WinSta0" is interactive (Source: KB171890)
158 interactive_process = _tcsicmp(sName, _T("WinSta0")) == 0;
159 }
160 }
161#else
162 // TODO: Research interactive process vs service/agent/daemon on this platform.
163#endif
164
165#if defined(_WIN32)
166 {
167 HANDLE token_h;
168 if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token_h)) {
169 sys_object token(token_h);
170
171 TOKEN_ELEVATION elevation;
172 DWORD size = sizeof(TOKEN_ELEVATION);
173 if (GetTokenInformation(token_h, TokenElevation, &elevation, sizeof(elevation), &size))
174 elevated = elevation.TokenIsElevated;
175
176 GetTokenInformation(token.get(), TokenGroups, NULL, 0, &size);
177 std::unique_ptr<TOKEN_GROUPS> groups((TOKEN_GROUPS*)new uint8_t[size]);
178 if (GetTokenInformation(token.get(), TokenGroups, (LPVOID)groups.get(), size, &size)) {
179 SID_IDENTIFIER_AUTHORITY authority = SECURITY_NT_AUTHORITY;
180 PSID sid_admins_h = NULL;
181 if (AllocateAndInitializeSid(&authority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &sid_admins_h)) {
182 struct SID_delete { void operator()(_In_ PSID p) const { FreeSid(p); } };
183 std::unique_ptr<void, SID_delete> sid_admins(sid_admins_h);
184 for (DWORD i = 0; i < groups->GroupCount; ++i)
185 if (EqualSid(sid_admins.get(), groups->Groups[i].Sid)) {
186 admin = true;
187 break;
188 }
189 }
190 }
191 }
192 }
193#elif defined(__APPLE__)
194 {
195 gid_t gids[NGROUPS + 1]; // A user cannot be member in more than NGROUPS groups, not counting the default group (hence the + 1)
196 for (int i = 0, n = getgroups(_countof(gids), gids); i < n; ++i) {
197 struct group* group = getgrgid(gids[i]);
198 if (!group) continue;
199 if (strcmp(group->gr_name, "admin") == 0) {
200 admin = true;
201 break;
202 }
203 }
204 }
205
206 elevated = geteuid() == 0;
207#else
208 // TODO: Set admin.
209 elevated = geteuid() == 0;
210#endif
211 }
212
213 protected:
214#ifndef _WIN32
215 struct utsname m_utsn;
216#endif
217 } sys_info;
218}
Operating system object (file, pipe, anything with an OS handle etc.)
Definition system.hpp:93
sys_handle get() const noexcept
Returns object handle.
Definition system.hpp:150
System information.
Definition sys_info.hpp:55
bool admin
Is member of local group Administrators (Windows) or member of group wheel/sudoers (others)?
Definition sys_info.hpp:97
bool elevated
Is elevated process (Windows) or running as root (others)?
Definition sys_info.hpp:102
platform_id os_platform
The platform this process was compiled for.
Definition sys_info.hpp:80
bool interactive_process
Is interactive process?
Definition sys_info.hpp:92