mirror of
https://github.com/decompals/wibo.git
synced 2025-10-16 15:15:10 +00:00
Remove advapi32/internal.cpp; move to relevant source files
This commit is contained in:
parent
3249ebf4bf
commit
694eb85deb
@ -33,7 +33,6 @@ FetchContent_MakeAvailable(mimalloc)
|
|||||||
include_directories(.)
|
include_directories(.)
|
||||||
add_executable(wibo
|
add_executable(wibo
|
||||||
dll/advapi32.cpp
|
dll/advapi32.cpp
|
||||||
dll/advapi32/internal.cpp
|
|
||||||
dll/advapi32/processthreadsapi.cpp
|
dll/advapi32/processthreadsapi.cpp
|
||||||
dll/advapi32/securitybaseapi.cpp
|
dll/advapi32/securitybaseapi.cpp
|
||||||
dll/advapi32/winbase.cpp
|
dll/advapi32/winbase.cpp
|
||||||
|
@ -1,98 +0,0 @@
|
|||||||
#include "internal.h"
|
|
||||||
|
|
||||||
#include <cctype>
|
|
||||||
#include <mutex>
|
|
||||||
#include <unordered_map>
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
constexpr DWORD SECURITY_LOCAL_SYSTEM_RID = 18;
|
|
||||||
|
|
||||||
constexpr BYTE kNtAuthority[6] = {0, 0, 0, 0, 0, 5};
|
|
||||||
|
|
||||||
std::mutex privilegeMapMutex;
|
|
||||||
std::unordered_map<std::string, LUID> privilegeLuidCache;
|
|
||||||
|
|
||||||
LUID generateDeterministicLuid(const std::string &normalizedName) {
|
|
||||||
uint32_t hash = 2166136261u;
|
|
||||||
for (unsigned char ch : normalizedName) {
|
|
||||||
hash ^= ch;
|
|
||||||
hash *= 16777619u;
|
|
||||||
}
|
|
||||||
if (hash == 0) {
|
|
||||||
hash = 1;
|
|
||||||
}
|
|
||||||
LUID luid{};
|
|
||||||
luid.LowPart = hash;
|
|
||||||
luid.HighPart = 0;
|
|
||||||
return luid;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
namespace advapi32 {
|
|
||||||
|
|
||||||
bool isLocalSystemSid(const Sid *sid) {
|
|
||||||
if (!sid) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (sid->Revision != 1 || sid->SubAuthorityCount != 1) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
for (size_t i = 0; i < std::size(kNtAuthority); ++i) {
|
|
||||||
if (sid->IdentifierAuthority.Value[i] != kNtAuthority[i]) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return sid->SubAuthority[0] == SECURITY_LOCAL_SYSTEM_RID;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool writeLocalSystemSid(Sid *sid) {
|
|
||||||
if (!sid) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
sid->Revision = 1;
|
|
||||||
sid->SubAuthorityCount = 1;
|
|
||||||
SidIdentifierAuthority authority{};
|
|
||||||
for (size_t i = 0; i < std::size(kNtAuthority); ++i) {
|
|
||||||
authority.Value[i] = kNtAuthority[i];
|
|
||||||
}
|
|
||||||
sid->IdentifierAuthority = authority;
|
|
||||||
sid->SubAuthority[0] = SECURITY_LOCAL_SYSTEM_RID;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string normalizePrivilegeName(const std::string &name) {
|
|
||||||
std::string normalized;
|
|
||||||
normalized.reserve(name.size());
|
|
||||||
for (unsigned char ch : name) {
|
|
||||||
normalized.push_back(static_cast<char>(std::tolower(ch)));
|
|
||||||
}
|
|
||||||
return normalized;
|
|
||||||
}
|
|
||||||
|
|
||||||
LUID lookupOrGeneratePrivilegeLuid(const std::string &normalizedName) {
|
|
||||||
std::lock_guard<std::mutex> lock(privilegeMapMutex);
|
|
||||||
static const std::unordered_map<std::string, uint32_t> predefined = {
|
|
||||||
{"se_debug_name", 0x14},
|
|
||||||
{"se_shutdown_name", 0x13},
|
|
||||||
};
|
|
||||||
auto it = privilegeLuidCache.find(normalizedName);
|
|
||||||
if (it != privilegeLuidCache.end()) {
|
|
||||||
return it->second;
|
|
||||||
}
|
|
||||||
LUID luid{};
|
|
||||||
auto predefinedIt = predefined.find(normalizedName);
|
|
||||||
if (predefinedIt != predefined.end()) {
|
|
||||||
luid.LowPart = predefinedIt->second;
|
|
||||||
luid.HighPart = 0;
|
|
||||||
} else {
|
|
||||||
luid = generateDeterministicLuid(normalizedName);
|
|
||||||
}
|
|
||||||
privilegeLuidCache[normalizedName] = luid;
|
|
||||||
return luid;
|
|
||||||
}
|
|
||||||
|
|
||||||
void releaseToken(void *tokenPtr) { delete reinterpret_cast<TokenObject *>(tokenPtr); }
|
|
||||||
|
|
||||||
} // namespace advapi32
|
|
@ -3,9 +3,9 @@
|
|||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "securitybaseapi.h"
|
#include "securitybaseapi.h"
|
||||||
|
|
||||||
#include <string>
|
constexpr DWORD SECURITY_LOCAL_SYSTEM_RID = 18;
|
||||||
|
|
||||||
namespace advapi32 {
|
constexpr BYTE kNtAuthority[6] = {0, 0, 0, 0, 0, 5};
|
||||||
|
|
||||||
struct TokenObject {
|
struct TokenObject {
|
||||||
HANDLE processHandle;
|
HANDLE processHandle;
|
||||||
@ -20,11 +20,3 @@ struct Sid {
|
|||||||
SidIdentifierAuthority IdentifierAuthority;
|
SidIdentifierAuthority IdentifierAuthority;
|
||||||
DWORD SubAuthority[1];
|
DWORD SubAuthority[1];
|
||||||
};
|
};
|
||||||
|
|
||||||
bool isLocalSystemSid(const Sid *sid);
|
|
||||||
bool writeLocalSystemSid(Sid *sid);
|
|
||||||
std::string normalizePrivilegeName(const std::string &name);
|
|
||||||
LUID lookupOrGeneratePrivilegeLuid(const std::string &normalizedName);
|
|
||||||
void releaseToken(void *tokenPtr);
|
|
||||||
|
|
||||||
} // namespace advapi32
|
|
||||||
|
@ -10,8 +10,6 @@
|
|||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
using advapi32::Sid;
|
|
||||||
|
|
||||||
constexpr size_t kAceAlignment = 4;
|
constexpr size_t kAceAlignment = 4;
|
||||||
constexpr DWORD ERROR_REVISION_MISMATCH = 1306;
|
constexpr DWORD ERROR_REVISION_MISMATCH = 1306;
|
||||||
constexpr DWORD ERROR_INVALID_ACL = 1336;
|
constexpr DWORD ERROR_INVALID_ACL = 1336;
|
||||||
@ -19,6 +17,32 @@ constexpr DWORD ERROR_INVALID_SID = 1337;
|
|||||||
constexpr DWORD ERROR_ALLOTTED_SPACE_EXCEEDED = 1344;
|
constexpr DWORD ERROR_ALLOTTED_SPACE_EXCEEDED = 1344;
|
||||||
constexpr DWORD ERROR_INVALID_SECURITY_DESCR = 1338;
|
constexpr DWORD ERROR_INVALID_SECURITY_DESCR = 1338;
|
||||||
|
|
||||||
|
struct SidAndAttributes {
|
||||||
|
Sid *SidPtr;
|
||||||
|
DWORD Attributes;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct TokenUserData {
|
||||||
|
SidAndAttributes User;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct TokenStatisticsData {
|
||||||
|
LUID tokenId{};
|
||||||
|
LUID authenticationId{};
|
||||||
|
LARGE_INTEGER expirationTime{};
|
||||||
|
DWORD tokenType = 0;
|
||||||
|
DWORD impersonationLevel = 0;
|
||||||
|
DWORD dynamicCharged = 0;
|
||||||
|
DWORD dynamicAvailable = 0;
|
||||||
|
DWORD groupCount = 0;
|
||||||
|
DWORD privilegeCount = 0;
|
||||||
|
LUID modifiedId{};
|
||||||
|
};
|
||||||
|
|
||||||
|
struct TokenPrimaryGroupStub {
|
||||||
|
Sid *PrimaryGroup;
|
||||||
|
};
|
||||||
|
|
||||||
size_t alignToDword(size_t value) { return (value + (kAceAlignment - 1)) & ~(kAceAlignment - 1); }
|
size_t alignToDword(size_t value) { return (value + (kAceAlignment - 1)) & ~(kAceAlignment - 1); }
|
||||||
|
|
||||||
size_t sidLength(const Sid *sid) {
|
size_t sidLength(const Sid *sid) {
|
||||||
@ -57,31 +81,20 @@ bool computeAclUsedSize(const ACL *acl, size_t capacity, size_t &used) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct SidAndAttributes {
|
bool writeLocalSystemSid(Sid *sid) {
|
||||||
Sid *SidPtr;
|
if (!sid) {
|
||||||
DWORD Attributes;
|
return false;
|
||||||
};
|
}
|
||||||
|
sid->Revision = 1;
|
||||||
struct TokenUserData {
|
sid->SubAuthorityCount = 1;
|
||||||
SidAndAttributes User;
|
SidIdentifierAuthority authority{};
|
||||||
};
|
for (size_t i = 0; i < std::size(kNtAuthority); ++i) {
|
||||||
|
authority.Value[i] = kNtAuthority[i];
|
||||||
struct TokenStatisticsData {
|
}
|
||||||
LUID tokenId{};
|
sid->IdentifierAuthority = authority;
|
||||||
LUID authenticationId{};
|
sid->SubAuthority[0] = SECURITY_LOCAL_SYSTEM_RID;
|
||||||
LARGE_INTEGER expirationTime{};
|
return true;
|
||||||
DWORD tokenType = 0;
|
}
|
||||||
DWORD impersonationLevel = 0;
|
|
||||||
DWORD dynamicCharged = 0;
|
|
||||||
DWORD dynamicAvailable = 0;
|
|
||||||
DWORD groupCount = 0;
|
|
||||||
DWORD privilegeCount = 0;
|
|
||||||
LUID modifiedId{};
|
|
||||||
};
|
|
||||||
|
|
||||||
struct TokenPrimaryGroupStub {
|
|
||||||
Sid *PrimaryGroup;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
@ -6,12 +6,77 @@
|
|||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
constexpr WCHAR kAccountSystem[] = {u'S', u'Y', u'S', u'T', u'E', u'M', u'\0'};
|
constexpr WCHAR kAccountSystem[] = {u'S', u'Y', u'S', u'T', u'E', u'M', u'\0'};
|
||||||
constexpr WCHAR kDomainNtAuthority[] = {u'N', u'T', u' ', u'A', u'U', u'T', u'H', u'O', u'R', u'I', u'T', u'Y', u'\0'};
|
constexpr WCHAR kDomainNtAuthority[] = {u'N', u'T', u' ', u'A', u'U', u'T', u'H', u'O', u'R', u'I', u'T', u'Y', u'\0'};
|
||||||
|
|
||||||
|
std::mutex g_privilegeMapMutex;
|
||||||
|
std::unordered_map<std::string, LUID> g_privilegeLuidCache;
|
||||||
|
|
||||||
|
bool isLocalSystemSid(const Sid *sid) {
|
||||||
|
if (!sid) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (sid->Revision != 1 || sid->SubAuthorityCount != 1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (size_t i = 0; i < std::size(kNtAuthority); ++i) {
|
||||||
|
if (sid->IdentifierAuthority.Value[i] != kNtAuthority[i]) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sid->SubAuthority[0] == SECURITY_LOCAL_SYSTEM_RID;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string normalizePrivilegeName(const std::string &name) {
|
||||||
|
std::string normalized;
|
||||||
|
normalized.reserve(name.size());
|
||||||
|
for (unsigned char ch : name) {
|
||||||
|
normalized.push_back(static_cast<char>(std::tolower(ch)));
|
||||||
|
}
|
||||||
|
return normalized;
|
||||||
|
}
|
||||||
|
|
||||||
|
LUID generateDeterministicLuid(const std::string &normalizedName) {
|
||||||
|
uint32_t hash = 2166136261u;
|
||||||
|
for (unsigned char ch : normalizedName) {
|
||||||
|
hash ^= ch;
|
||||||
|
hash *= 16777619u;
|
||||||
|
}
|
||||||
|
if (hash == 0) {
|
||||||
|
hash = 1;
|
||||||
|
}
|
||||||
|
LUID luid{};
|
||||||
|
luid.LowPart = hash;
|
||||||
|
luid.HighPart = 0;
|
||||||
|
return luid;
|
||||||
|
}
|
||||||
|
|
||||||
|
LUID lookupOrGeneratePrivilegeLuid(const std::string &normalizedName) {
|
||||||
|
std::lock_guard<std::mutex> lock(g_privilegeMapMutex);
|
||||||
|
static const std::unordered_map<std::string, uint32_t> predefined = {
|
||||||
|
{"se_debug_name", 0x14},
|
||||||
|
{"se_shutdown_name", 0x13},
|
||||||
|
};
|
||||||
|
auto it = g_privilegeLuidCache.find(normalizedName);
|
||||||
|
if (it != g_privilegeLuidCache.end()) {
|
||||||
|
return it->second;
|
||||||
|
}
|
||||||
|
LUID luid{};
|
||||||
|
auto predefinedIt = predefined.find(normalizedName);
|
||||||
|
if (predefinedIt != predefined.end()) {
|
||||||
|
luid.LowPart = predefinedIt->second;
|
||||||
|
luid.HighPart = 0;
|
||||||
|
} else {
|
||||||
|
luid = generateDeterministicLuid(normalizedName);
|
||||||
|
}
|
||||||
|
g_privilegeLuidCache[normalizedName] = luid;
|
||||||
|
return luid;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
namespace advapi32 {
|
namespace advapi32 {
|
||||||
@ -25,7 +90,7 @@ BOOL WIN_FUNC LookupAccountSidW(LPCWSTR lpSystemName, PSID Sid, LPWSTR Name, LPD
|
|||||||
wibo::lastError = ERROR_INVALID_PARAMETER;
|
wibo::lastError = ERROR_INVALID_PARAMETER;
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
auto *sidStruct = reinterpret_cast<const ::advapi32::Sid *>(Sid);
|
auto *sidStruct = reinterpret_cast<const struct Sid *>(Sid);
|
||||||
if (!isLocalSystemSid(sidStruct)) {
|
if (!isLocalSystemSid(sidStruct)) {
|
||||||
wibo::lastError = ERROR_NONE_MAPPED;
|
wibo::lastError = ERROR_NONE_MAPPED;
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
@ -14,7 +14,6 @@
|
|||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <fnmatch.h>
|
#include <fnmatch.h>
|
||||||
#include <limits>
|
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <random>
|
#include <random>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
@ -150,7 +150,7 @@ BOOL WIN_FUNC CloseHandle(HANDLE hObject) {
|
|||||||
} else if (data.type == handles::TYPE_PROCESS) {
|
} else if (data.type == handles::TYPE_PROCESS) {
|
||||||
delete reinterpret_cast<processes::Process *>(data.ptr);
|
delete reinterpret_cast<processes::Process *>(data.ptr);
|
||||||
} else if (data.type == handles::TYPE_TOKEN) {
|
} else if (data.type == handles::TYPE_TOKEN) {
|
||||||
advapi32::releaseToken(data.ptr);
|
delete reinterpret_cast<TokenObject *>(data.ptr);
|
||||||
} else if (data.type == handles::TYPE_MUTEX) {
|
} else if (data.type == handles::TYPE_MUTEX) {
|
||||||
releaseMutexObject(reinterpret_cast<MutexObject *>(data.ptr));
|
releaseMutexObject(reinterpret_cast<MutexObject *>(data.ptr));
|
||||||
} else if (data.type == handles::TYPE_EVENT) {
|
} else if (data.type == handles::TYPE_EVENT) {
|
||||||
|
@ -9,20 +9,20 @@
|
|||||||
namespace kernel32 {
|
namespace kernel32 {
|
||||||
|
|
||||||
struct ThreadObject {
|
struct ThreadObject {
|
||||||
pthread_t thread;
|
pthread_t thread{};
|
||||||
bool finished = false;
|
bool finished = false;
|
||||||
bool joined = false;
|
bool joined = false;
|
||||||
bool detached = false;
|
bool detached = false;
|
||||||
bool synthetic = false;
|
bool synthetic = false;
|
||||||
DWORD exitCode = 0;
|
DWORD exitCode = 0;
|
||||||
int refCount = 1;
|
int refCount = 1;
|
||||||
pthread_mutex_t mutex;
|
pthread_mutex_t mutex{};
|
||||||
pthread_cond_t cond;
|
pthread_cond_t cond{};
|
||||||
unsigned int suspendCount = 0;
|
unsigned int suspendCount = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct MutexObject {
|
struct MutexObject {
|
||||||
pthread_mutex_t mutex;
|
pthread_mutex_t mutex{};
|
||||||
bool ownerValid = false;
|
bool ownerValid = false;
|
||||||
pthread_t owner = 0;
|
pthread_t owner = 0;
|
||||||
unsigned int recursionCount = 0;
|
unsigned int recursionCount = 0;
|
||||||
@ -31,8 +31,8 @@ struct MutexObject {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct EventObject {
|
struct EventObject {
|
||||||
pthread_mutex_t mutex;
|
pthread_mutex_t mutex{};
|
||||||
pthread_cond_t cond;
|
pthread_cond_t cond{};
|
||||||
bool manualReset = false;
|
bool manualReset = false;
|
||||||
bool signaled = false;
|
bool signaled = false;
|
||||||
std::u16string name;
|
std::u16string name;
|
||||||
@ -40,8 +40,8 @@ struct EventObject {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct SemaphoreObject {
|
struct SemaphoreObject {
|
||||||
pthread_mutex_t mutex;
|
pthread_mutex_t mutex{};
|
||||||
pthread_cond_t cond;
|
pthread_cond_t cond{};
|
||||||
LONG count = 0;
|
LONG count = 0;
|
||||||
LONG maxCount = 0;
|
LONG maxCount = 0;
|
||||||
std::u16string name;
|
std::u16string name;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user