From c9d38f3536a95311e3bf5dad1825e191c43446ab Mon Sep 17 00:00:00 2001 From: Aetopia Date: Wed, 13 Sep 2023 19:31:21 +0530 Subject: [PATCH] Prevent MCBE from being suspended by Windows. --- minimal-cpp/CMakeLists.txt | 2 +- minimal-cpp/main.cpp | 284 +++++++++++++++++++++---------------- 2 files changed, 164 insertions(+), 122 deletions(-) diff --git a/minimal-cpp/CMakeLists.txt b/minimal-cpp/CMakeLists.txt index bce132d..7a83387 100644 --- a/minimal-cpp/CMakeLists.txt +++ b/minimal-cpp/CMakeLists.txt @@ -12,5 +12,5 @@ target_include_directories(flarialminimal PRIVATE "${CMAKE_CURRENT_LIST_DIR}/inc add_library(sussy SHARED IMPORTED GLOBAL) set_target_properties(sussy PROPERTIES IMPORTED_IMPLIB "${CMAKE_CURRENT_SOURCE_DIR}/lib/discord-rpc.lib") -target_link_libraries(flarialminimal PRIVATE dwrite.lib d2d1.lib urlmon.lib windowscodecs.lib shlwapi.lib) +target_link_libraries(flarialminimal PRIVATE Ole32.lib dwrite.lib d2d1.lib urlmon.lib windowscodecs.lib shlwapi.lib) target_link_libraries(flarialminimal PUBLIC sussy) diff --git a/minimal-cpp/main.cpp b/minimal-cpp/main.cpp index ec4edb4..5a8a110 100644 --- a/minimal-cpp/main.cpp +++ b/minimal-cpp/main.cpp @@ -1,4 +1,8 @@ +#define _WIN32_WINNT _WIN32_WINNT_WIN10 +#include #include +#include +#include #include #include #include @@ -14,38 +18,41 @@ namespace fs = std::filesystem; - HWND hwnd; HFONT hFont; -ID2D1Factory* pD2DFactory = nullptr; -ID2D1HwndRenderTarget* pRT = nullptr; -ID2D1SolidColorBrush* pBrush = nullptr; -IDWriteFactory* pDWriteFactory = nullptr; +ID2D1Factory *pD2DFactory = nullptr; +ID2D1HwndRenderTarget *pRT = nullptr; +ID2D1SolidColorBrush *pBrush = nullptr; +IDWriteFactory *pDWriteFactory = nullptr; bool isMouseOverButton = false; int windowWidth = 275; int windowHeight = 120; LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); -void WaitForModules(const std::string& processName, int moduleCount); +void WaitForModules(const std::string &processName, int moduleCount); void updateStatus(); static int64_t eptime = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); std::string currIp = "looool"; - -DWORD GetProcessIdByName(const std::string& processName) { +DWORD GetProcessIdByName(const std::string &processName) +{ DWORD processId = 0; HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); - if (snapshot != INVALID_HANDLE_VALUE) { + if (snapshot != INVALID_HANDLE_VALUE) + { PROCESSENTRY32 processEntry; processEntry.dwSize = sizeof(PROCESSENTRY32); - if (Process32First(snapshot, &processEntry)) { - do { + if (Process32First(snapshot, &processEntry)) + { + do + { std::string currentProcessName = processEntry.szExeFile; - if (currentProcessName == processName) { + if (currentProcessName == processName) + { processId = processEntry.th32ProcessID; break; } @@ -58,38 +65,47 @@ DWORD GetProcessIdByName(const std::string& processName) { return processId; } -void WaitForModules(const std::string& processName, int moduleCount) { +void WaitForModules(const std::string &processName, int moduleCount) +{ DWORD processId = GetProcessIdByName(processName); - if (processId == 0) { + if (processId == 0) + { return; } HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, 0, processId); - if (hProc == NULL) { + if (hProc == NULL) + { return; } bool modulesLoaded = false; - while (!modulesLoaded) { + while (!modulesLoaded) + { MODULEENTRY32 moduleEntry; moduleEntry.dwSize = sizeof(MODULEENTRY32); HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, processId); - if (snapshot != INVALID_HANDLE_VALUE) { - if (Module32First(snapshot, &moduleEntry)) { + if (snapshot != INVALID_HANDLE_VALUE) + { + if (Module32First(snapshot, &moduleEntry)) + { int count = 0; - do { + do + { count++; } while (Module32Next(snapshot, &moduleEntry) && count < moduleCount); - if (count >= moduleCount) { + if (count >= moduleCount) + { modulesLoaded = true; } } CloseHandle(snapshot); } - if (!modulesLoaded) { + if (!modulesLoaded) + { Sleep(100); } } @@ -102,11 +118,9 @@ int performInjection(DWORD procId, std::string dllPath) { HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, 0, procId); - - if (hProc && hProc != INVALID_HANDLE_VALUE) { - void* loc = VirtualAllocEx(hProc, 0, MAX_PATH, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); + void *loc = VirtualAllocEx(hProc, 0, MAX_PATH, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); WriteProcessMemory(hProc, loc, dllPath.c_str(), dllPath.length() + 1, 0); // length * 2 for bytes + 2 for end string @@ -124,13 +138,13 @@ int performInjection(DWORD procId, std::string dllPath) return 0; } -void SetAccessControl(std::string ExecutableName, const wchar_t* AccessString) +void SetAccessControl(std::string ExecutableName, const wchar_t *AccessString) { PSECURITY_DESCRIPTOR SecurityDescriptor = nullptr; - EXPLICIT_ACCESSW ExplicitAccess = { 0 }; + EXPLICIT_ACCESSW ExplicitAccess = {0}; - ACL* AccessControlCurrent = nullptr; - ACL* AccessControlNew = nullptr; + ACL *AccessControlCurrent = nullptr; + ACL *AccessControlNew = nullptr; SECURITY_INFORMATION SecurityInfo = DACL_SECURITY_INFORMATION; PSID SecurityIdentifier = nullptr; @@ -144,9 +158,7 @@ void SetAccessControl(std::string ExecutableName, const wchar_t* AccessString) nullptr, &AccessControlCurrent, nullptr, - &SecurityDescriptor - ) == ERROR_SUCCESS - ) + &SecurityDescriptor) == ERROR_SUCCESS) { ConvertStringSidToSidW(AccessString, &SecurityIdentifier); if (SecurityIdentifier != nullptr) @@ -156,26 +168,23 @@ void SetAccessControl(std::string ExecutableName, const wchar_t* AccessString) ExplicitAccess.grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT; ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_SID; ExplicitAccess.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP; - ExplicitAccess.Trustee.ptstrName = reinterpret_cast(SecurityIdentifier); + ExplicitAccess.Trustee.ptstrName = reinterpret_cast(SecurityIdentifier); if ( SetEntriesInAclW( 1, &ExplicitAccess, AccessControlCurrent, - &AccessControlNew - ) == ERROR_SUCCESS - ) + &AccessControlNew) == ERROR_SUCCESS) { SetNamedSecurityInfoA( - (char*)ExecutableName.c_str(), + (char *)ExecutableName.c_str(), SE_FILE_OBJECT, SecurityInfo, nullptr, nullptr, AccessControlNew, - nullptr - ); + nullptr); } } } @@ -187,16 +196,15 @@ void SetAccessControl(std::string ExecutableName, const wchar_t* AccessString) { LocalFree(reinterpret_cast(AccessControlNew)); } - } -HRESULT LoadBitmapFromFile(ID2D1RenderTarget* pRenderTarget, PCWSTR uri, ID2D1Bitmap** ppBitmap) +HRESULT LoadBitmapFromFile(ID2D1RenderTarget *pRenderTarget, PCWSTR uri, ID2D1Bitmap **ppBitmap) { - IWICImagingFactory* pIWICFactory = nullptr; - IWICBitmapDecoder* pDecoder = nullptr; - IWICBitmapFrameDecode* pSource = nullptr; - IWICStream* pStream = nullptr; - IWICFormatConverter* pConverter = nullptr; + IWICImagingFactory *pIWICFactory = nullptr; + IWICBitmapDecoder *pDecoder = nullptr; + IWICBitmapFrameDecode *pSource = nullptr; + IWICStream *pStream = nullptr; + IWICFormatConverter *pConverter = nullptr; HRESULT hr = CoCreateInstance(CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pIWICFactory)); if (FAILED(hr)) @@ -259,7 +267,6 @@ HRESULT LoadBitmapFromFile(ID2D1RenderTarget* pRenderTarget, PCWSTR uri, ID2D1Bi int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) { - WNDCLASS wc = {}; wc.lpfnWndProc = WindowProc; wc.hInstance = hInstance; @@ -276,19 +283,58 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine std::string exeDirectory = exePath.string(); std::string folderPath = exeDirectory + "/Resources"; - if (!fs::exists(folderPath)) { + WCHAR **rgszPackageFullNames = NULL, + *pBuffer = NULL; + IPackageDebugSettings *pPackageDebugSettings = NULL; + UINT32 u32Index = 0, + u32Count = 0, + u32BufferLength = 0; + HANDLE hHeap = GetProcessHeap(); + + (VOID) CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); + (VOID) CoCreateInstance(CLSID_PackageDebugSettings, + NULL, + CLSCTX_INPROC_SERVER, + IID_IPackageDebugSettings, + (LPVOID *)&pPackageDebugSettings); + + if (GetPackagesByPackageFamily(L"Microsoft.MinecraftUWP_8wekyb3d8bbwe", + &u32Count, + NULL, + &u32BufferLength, + NULL) != ERROR_INSUFFICIENT_BUFFER) + return; + + rgszPackageFullNames = (WCHAR **)HeapAlloc(hHeap, HEAP_ZERO_MEMORY, sizeof(PWSTR) * u32Count); + pBuffer = (WCHAR *)HeapAlloc(hHeap, HEAP_ZERO_MEMORY, sizeof(WCHAR) * u32BufferLength); + if (GetPackagesByPackageFamily(L"Microsoft.MinecraftUWP_8wekyb3d8bbwe", + &u32Count, + rgszPackageFullNames, + &u32BufferLength, + pBuffer) == ERROR_SUCCESS) + for (u32Index = 0; u32Index < u32Count; u32Index += 1) + (VOID) pPackageDebugSettings->EnableDebugging(rgszPackageFullNames[u32Index], + NULL, + NULL); + + (VOID) pPackageDebugSettings->Release(); + (VOID) HeapFree(hHeap, 0, rgszPackageFullNames); + (VOID) HeapFree(hHeap, 0, pBuffer); + (VOID) CloseHandle(hHeap); + (VOID) CoUninitialize(); + + if (!fs::exists(folderPath)) + { fs::create_directory(folderPath); } std::string logoPath = folderPath + "/logo.png"; - std::string url = "https://cdn-c6f.pages.dev/assets/logo.png"; URLDownloadToFileA(nullptr, url.c_str(), logoPath.c_str(), 0, nullptr); - hwnd = CreateWindowExW(0, L"Flarial Minimal", L"Flarial Minimal", WS_OVERLAPPED, - 100, 100, windowWidth, windowHeight, nullptr, nullptr, hInstance, nullptr); + 100, 100, windowWidth, windowHeight, nullptr, nullptr, hInstance, nullptr); if (hwnd == nullptr) { @@ -296,10 +342,9 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine } hFont = CreateFontW(16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, - CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Space Grotesk"); + CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Space Grotesk"); SendMessage(hwnd, WM_SETFONT, (WPARAM)hFont, TRUE); - ShowWindow(hwnd, nCmdShow); @@ -311,13 +356,12 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine pD2DFactory->CreateHwndRenderTarget(D2D1::RenderTargetProperties(), D2D1::HwndRenderTargetProperties(hwnd, size), &pRT); - - std::thread statusThread([]() { + std::thread statusThread([]() + { while (true) { updateStatus(); Sleep(5000); - } - }); + } }); statusThread.detach(); MSG msg; @@ -327,8 +371,6 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine DispatchMessage(&msg); } - - if (hFont) { DeleteObject(hFont); @@ -357,95 +399,101 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine return 0; } -std::string readIp() { +std::string readIp() +{ std::string filePath = std::string(getenv("LOCALAPPDATA")) + "\\Packages\\Microsoft.MinecraftUWP_8wekyb3d8bbwe\\RoamingState\\Flarial\\serverip.txt"; - const char* filePathW = filePath.c_str(); + const char *filePathW = filePath.c_str(); std::ifstream file(filePathW); std::string fileContent; - if (file.is_open()) { + if (file.is_open()) + { std::string line; - while (std::getline(file, line)) { + while (std::getline(file, line)) + { fileContent += line + "\n"; } } - file.close(); file.seekg(0); return fileContent; } -void doStuffWithIp(DiscordRichPresence& presence, const std::string& ip) { +void doStuffWithIp(DiscordRichPresence &presence, const std::string &ip) +{ - if (ip.find("hive") != std::string::npos) { + if (ip.find("hive") != std::string::npos) + { presence.smallImageKey = "hivemc"; presence.details = "Hive Network"; } - else if (ip.find("nethergames") != std::string::npos) { + else if (ip.find("nethergames") != std::string::npos) + { presence.smallImageKey = "ngmc"; presence.details = "Nethergames Network"; } - else if (ip.find("hyperland") != std::string::npos) { + else if (ip.find("hyperland") != std::string::npos) + { presence.smallImageKey = "hlmc"; presence.details = "Hyperlands Network"; } - else if (ip.find("cubecraft") != std::string::npos) { + else if (ip.find("cubecraft") != std::string::npos) + { presence.smallImageKey = "ccmc"; presence.details = "Cubecraft Network"; } - else if (ip.find("zeqa") != std::string::npos) { + else if (ip.find("zeqa") != std::string::npos) + { presence.smallImageKey = "zeqamc"; presence.details = "Zeqa Network"; } - else if (ip.find("none") != std::string::npos) { + else if (ip.find("none") != std::string::npos) + { presence.smallImageKey = "mcicon"; presence.details = "Ready to play"; } - else if (ip.find("world") != std::string::npos) { + else if (ip.find("world") != std::string::npos) + { presence.smallImageKey = "mcicon"; presence.details = "Playing Singleplayer"; } - else { + else + { std::string shit = "Playing " + ip; presence.details = shit.c_str(); presence.smallImageKey = "mcicon"; } } -void updateStatus() { - - std::string ip = readIp(); - - - +void updateStatus() +{ - if (currIp != ip) { + std::string ip = readIp(); - DiscordRichPresence discordPresence; - DiscordEventHandlers handlers; - memset(&handlers, 0, sizeof(handlers)); - Discord_Initialize("1067854754518151168", &handlers, 1, nullptr); + if (currIp != ip) + { - memset(&discordPresence, 0, sizeof(discordPresence)); + DiscordRichPresence discordPresence; + DiscordEventHandlers handlers; + memset(&handlers, 0, sizeof(handlers)); + Discord_Initialize("1067854754518151168", &handlers, 1, nullptr); + memset(&discordPresence, 0, sizeof(discordPresence)); - discordPresence.largeImageKey = "flarialbig"; - doStuffWithIp(discordPresence, ip); - discordPresence.startTimestamp = eptime; - Discord_UpdatePresence(&discordPresence); + discordPresence.largeImageKey = "flarialbig"; + doStuffWithIp(discordPresence, ip); + discordPresence.startTimestamp = eptime; + Discord_UpdatePresence(&discordPresence); - currIp = ip; - } - + currIp = ip; + } } - - LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) @@ -460,13 +508,13 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) pRT->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::White), &pBrush); } - DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), reinterpret_cast(&pDWriteFactory)); + DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), reinterpret_cast(&pDWriteFactory)); - IDWriteTextFormat* pTextFormat; + IDWriteTextFormat *pTextFormat; pDWriteFactory->CreateTextFormat(L"Space Grotesk", nullptr, DWRITE_FONT_WEIGHT_NORMAL, - DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, 16, L"en-us", &pTextFormat); + DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, 16, L"en-us", &pTextFormat); - ID2D1Bitmap* pLogoBitmap = nullptr; + ID2D1Bitmap *pLogoBitmap = nullptr; HRESULT hr = LoadBitmapFromFile(pRT, L"Resources/logo.png", &pLogoBitmap); if (SUCCEEDED(hr)) { @@ -546,36 +594,30 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) if (wParam == MK_LBUTTON) { - std::thread statusThread([&hwnd]() { - - - wchar_t currentExePath[MAX_PATH]; - GetModuleFileNameW(nullptr, currentExePath, MAX_PATH); - - fs::path exePath(currentExePath); - std::string exeDirectory = exePath.parent_path().string(); - std::string latestDllPath = fs::path(exeDirectory).append(L"latest.dll").string(); - - std::string url = "https://cdn-c6f.pages.dev/dll/latest.dll"; - - HRESULT hr = URLDownloadToFileA(nullptr, url.c_str(), latestDllPath.c_str(), 0, nullptr); - if (FAILED(hr)) - { - MessageBoxW(hwnd, L"Failed to download the DLL.", L"Error", MB_ICONERROR); - return 0; - } - + std::thread statusThread([&hwnd]() + { + wchar_t currentExePath[MAX_PATH]; + GetModuleFileNameW(nullptr, currentExePath, MAX_PATH); - SetAccessControl(latestDllPath, L"S-1-15-2-1"); + fs::path exePath(currentExePath); + std::string exeDirectory = exePath.parent_path().string(); + std::string latestDllPath = fs::path(exeDirectory).append(L"latest.dll").string(); - ShellExecuteW(hwnd, L"open", L"minecraft://", nullptr, nullptr, SW_SHOWNORMAL); + std::string url = "https://cdn-c6f.pages.dev/dll/latest.dll"; + HRESULT hr = URLDownloadToFileA(nullptr, url.c_str(), latestDllPath.c_str(), 0, nullptr); + if (FAILED(hr)) + { + MessageBoxW(hwnd, L"Failed to download the DLL.", L"Error", MB_ICONERROR); + return 0; + } + SetAccessControl(latestDllPath, L"S-1-15-2-1"); - WaitForModules("Minecraft.Windows.exe", 160); - performInjection(GetProcessIdByName("Minecraft.Windows.exe"), fs::path(exeDirectory).append("latest.dll").string()); + ShellExecuteW(hwnd, L"open", L"minecraft://", nullptr, nullptr, SW_SHOWNORMAL); - }); + WaitForModules("Minecraft.Windows.exe", 160); + performInjection(GetProcessIdByName("Minecraft.Windows.exe"), fs::path(exeDirectory).append("latest.dll").string()); }); statusThread.detach(); }