Sandboxie/SandboxiePlus/SandMan/Helpers/WinAdmin.cpp

167 lines
4.9 KiB
C++
Raw Normal View History

2021-10-16 16:19:51 +01:00
#include "stdafx.h"
#include "WinAdmin.h"
#include <windows.h>
#include <codecvt>
#include <Shlwapi.h>
bool IsElevated()
{
bool fRet = false;
HANDLE hToken = NULL;
if(OpenProcessToken(GetCurrentProcess(),TOKEN_QUERY,&hToken))
{
TOKEN_ELEVATION Elevation;
DWORD cbSize = sizeof(TOKEN_ELEVATION);
if(GetTokenInformation(hToken, TokenElevation, &Elevation, sizeof( Elevation ), &cbSize))
fRet = Elevation.TokenIsElevated;
}
if(hToken)
CloseHandle(hToken);
return fRet;
}
2022-09-29 17:28:48 +01:00
int RunElevated(const std::wstring& Params, bool bGetCode)
2021-10-16 16:19:51 +01:00
{
2022-11-20 16:23:15 +00:00
// Launch itself as admin
2021-10-16 16:19:51 +01:00
wchar_t szPath[MAX_PATH];
if (!GetModuleFileName(NULL, szPath, ARRAYSIZE(szPath)))
2022-11-20 16:23:15 +00:00
return -104;
return RunElevated(std::wstring(szPath), Params, bGetCode ? 10000 : 0);
2021-10-16 16:19:51 +01:00
}
2022-11-20 16:23:15 +00:00
int RunElevated(const std::wstring& binaryPath, const std::wstring& Params, quint32 uTimeOut)
2021-10-16 16:19:51 +01:00
{
SHELLEXECUTEINFO sei = { sizeof(sei) };
sei.fMask = SEE_MASK_NOCLOSEPROCESS;
sei.lpVerb = L"runas";
sei.lpFile = binaryPath.c_str();
sei.lpParameters = Params.c_str();
sei.hwnd = NULL;
sei.nShow = SW_NORMAL;
2022-11-20 16:23:15 +00:00
2021-10-16 16:19:51 +01:00
if (!ShellExecuteEx(&sei))
{
DWORD dwError = GetLastError();
if (dwError == ERROR_CANCELLED)
2022-11-20 16:23:15 +00:00
return -102; // The user refused to allow privileges elevation.
return -101;
2021-10-16 16:19:51 +01:00
}
else
{
2022-11-20 16:23:15 +00:00
DWORD ExitCode = 0;
BOOL success = TRUE;
if (uTimeOut)
2021-10-16 16:19:51 +01:00
{
2022-11-20 16:23:15 +00:00
WaitForSingleObject(sei.hProcess, uTimeOut);
success = GetExitCodeProcess(sei.hProcess, &ExitCode);
2021-10-16 16:19:51 +01:00
}
2022-11-20 16:23:15 +00:00
CloseHandle(sei.hProcess);
2023-07-01 17:54:53 +01:00
return success ? ExitCode : STATUS_PENDING;
2021-10-16 16:19:51 +01:00
}
}
int RestartElevated(int &argc, char **argv)
{
2022-09-29 17:28:48 +01:00
std::wstring Params;
2021-10-16 16:19:51 +01:00
for (int i = 1; i < argc; i++)
{
if (i > 1)
Params.append(L" ");
2022-09-29 17:28:48 +01:00
Params.append(L"\"" + std::wstring_convert<std::codecvt_utf8<wchar_t>>().from_bytes(argv[i]) + L"\"");
2021-10-16 16:19:51 +01:00
}
return RunElevated(Params);
}
2022-09-02 10:04:49 +01:00
bool IsAdminUser(bool OnlyFull)
{
HANDLE hToken;
if (!OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED, &hToken))
return false;
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
PSID AdministratorsGroup;
BOOL bRet = AllocateAndInitializeSid(&NtAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdministratorsGroup);
if (bRet) {
if (!CheckTokenMembership(NULL, AdministratorsGroup, &bRet))
bRet = FALSE;
FreeSid(AdministratorsGroup);
if (!bRet || OnlyFull) {
OSVERSIONINFO osvi;
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if (GetVersionEx(&osvi) && osvi.dwMajorVersion >= 6) {
ULONG elevationType, len;
bRet = GetTokenInformation(hToken, (TOKEN_INFORMATION_CLASS)TokenElevationType, &elevationType, sizeof(elevationType), &len);
if (bRet && (elevationType != TokenElevationTypeFull && (OnlyFull || elevationType != TokenElevationTypeLimited)))
bRet = FALSE;
}
}
}
CloseHandle(hToken);
return !!bRet;
}
2021-10-16 16:19:51 +01:00
//////////////////////////////////////////////////////////////////////////////////
// AutoRun
#define APP_NAME L"SandboxiePlus"
#define AUTO_RUN_KEY_NAME APP_NAME L"_AutoRun"
bool IsAutorunEnabled()
{
2020-07-19 21:09:02 +01:00
bool result = false;
HKEY hkey = nullptr;
if (ERROR_SUCCESS == RegOpenKeyEx (HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_ALL_ACCESS, &hkey))
2020-07-19 21:09:02 +01:00
{
2023-03-26 17:39:05 +01:00
// First, determine the required buffer size, including NUL terminator (in bytes). RegGetValue() always adds
// an extra NUL terminator to size, even if one already exists, in case the stored value doesn't have one.
DWORD size {0};
if (ERROR_SUCCESS == RegGetValue(hkey, nullptr, AUTO_RUN_KEY_NAME, RRF_RT_REG_SZ, nullptr, nullptr, &size))
2020-07-19 21:09:02 +01:00
{
// Then, allocate the buffer (in WCHARs) and retrieve the auto-run value. If successful, the size
// variable will be set to the actual size, without the extra NUL terminator.
auto buffer = std::make_unique< WCHAR[] >(size / sizeof(WCHAR));
if (ERROR_SUCCESS == RegGetValue(hkey, nullptr, AUTO_RUN_KEY_NAME, RRF_RT_REG_SZ, nullptr, reinterpret_cast<LPBYTE>(buffer.get()), &size))
{
result = true; // todo: check path
}
2020-07-19 21:09:02 +01:00
}
RegCloseKey (hkey);
}
2021-10-16 16:19:51 +01:00
return result;
}
2020-07-19 21:09:02 +01:00
bool AutorunEnable (bool is_enable)
{
bool result = false;
HKEY hkey = nullptr;
if (ERROR_SUCCESS == RegOpenKeyEx (HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_ALL_ACCESS, &hkey))
2020-07-19 21:09:02 +01:00
{
if (is_enable)
{
constexpr size_t MAX_PATH_EX = 32767; // Long file path max length, in characters
auto szPath = std::make_unique< WCHAR[] >(MAX_PATH_EX);
if (GetModuleFileName(NULL, szPath.get(), MAX_PATH_EX))
2020-07-19 21:09:02 +01:00
{
const std::wstring path = L"\"" + std::wstring(szPath.get()) + L"\" -autorun";
2020-07-19 21:09:02 +01:00
result = (ERROR_SUCCESS == RegSetValueEx(hkey, AUTO_RUN_KEY_NAME, 0, REG_SZ, reinterpret_cast<const BYTE*>(path.c_str()), static_cast<DWORD>((path.length() + 1) * sizeof(WCHAR))));
2020-07-19 21:09:02 +01:00
}
}
else
{
result = (ERROR_SUCCESS == RegDeleteValue (hkey, AUTO_RUN_KEY_NAME));
2020-07-19 21:09:02 +01:00
}
RegCloseKey (hkey);
}
return false;
}