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