|
| 1 | +#pragma once |
| 2 | +#include <windows.h> |
| 3 | +#include <stdio.h> |
| 4 | +#include <stdlib.h> |
| 5 | +#include <cstdlib> |
| 6 | +#include <psapi.h> |
| 7 | +#include <tlhelp32.h> |
| 8 | +#include <winternl.h> |
| 9 | + |
| 10 | +typedef struct _MEMORY_COMBINE_INFORMATION_EX { |
| 11 | + HANDLE Handle; |
| 12 | + ULONG_PTR PagesCombined; |
| 13 | + ULONG Flags; |
| 14 | +} MEMORY_COMBINE_INFORMATION_EX, *PMEMORY_COMBINE_INFORMATION_EX; |
| 15 | + |
| 16 | +typedef struct _SYSTEM_FILECACHE_INFORMATION { |
| 17 | + SIZE_T CurrentSize; |
| 18 | + SIZE_T PeakSize; |
| 19 | + ULONG PageFaultCount; |
| 20 | + SIZE_T MinimumWorkingSet; |
| 21 | + SIZE_T MaximumWorkingSet; |
| 22 | + SIZE_T CurrentSizeIncludingTransitionInPages; |
| 23 | + SIZE_T PeakSizeIncludingTransitionInPages; |
| 24 | + ULONG TransitionRePurposeCount; |
| 25 | + ULONG Flags; |
| 26 | +} SYSTEM_FILECACHE_INFORMATION, *PSYSTEM_FILECACHE_INFORMATION; |
| 27 | + |
| 28 | +typedef enum _SYSTEM_MEMORY_LIST_COMMAND { |
| 29 | + MemoryCaptureAccessedBits, |
| 30 | + MemoryCaptureAndResetAccessedBits, |
| 31 | + MemoryEmptyWorkingSets, |
| 32 | + MemoryFlushModifiedList, |
| 33 | + MemoryPurgeStandbyList, |
| 34 | + MemoryPurgeLowPriorityStandbyList, |
| 35 | + MemoryCommandMax |
| 36 | +} SYSTEM_MEMORY_LIST_COMMAND; |
| 37 | + |
| 38 | + |
| 39 | +// definicion de SYSTEM_INFORMATION_CLASS |
| 40 | +typedef enum _SYSTEM_INFORMATION_CLASS_MOD { |
| 41 | + SystemCombinePhysicalMemoryInformation = 130, |
| 42 | + SystemFileCacheInformationEx = 81, |
| 43 | + SystemMemoryListInformation = 80, |
| 44 | + SystemRegistryReconciliationInformation = 155, |
| 45 | +} SYSTEM_INFORMATION_CLASS_MOD; |
| 46 | + |
| 47 | +extern "C"{ |
| 48 | +typedef NTSTATUS LONG; |
| 49 | +// Definir funciones internas |
| 50 | +NTSYSAPI |
| 51 | +NTSTATUS |
| 52 | +NTAPI |
| 53 | +NtSetSystemInformation( |
| 54 | + IN SYSTEM_INFORMATION_CLASS_MOD SystemInformationClass, |
| 55 | + IN PVOID SystemInformation, |
| 56 | + IN ULONG SystemInformationLength |
| 57 | +); |
| 58 | +} |
| 59 | + |
| 60 | +DWORD GetChildProcesses(DWORD ParentPID, DWORD* ChildPIDs) { |
| 61 | + HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 62 | + if (hSnapshot == INVALID_HANDLE_VALUE) { |
| 63 | + printf("Error al crear un snapshot de procesos"); |
| 64 | + return 0; |
| 65 | + } |
| 66 | + |
| 67 | + PROCESSENTRY32 pe32; |
| 68 | + pe32.dwSize = sizeof(PROCESSENTRY32); |
| 69 | + |
| 70 | + DWORD NumProcesses = 0; |
| 71 | + |
| 72 | + if (Process32First(hSnapshot, &pe32)) { |
| 73 | + do { |
| 74 | + if (pe32.th32ParentProcessID == ParentPID) { |
| 75 | + if (NumProcesses < 64) { |
| 76 | + ChildPIDs[NumProcesses++] = pe32.th32ProcessID; |
| 77 | + } else { |
| 78 | + printf("Se alcanzó el límite máximo de procesos hijos"); |
| 79 | + break; |
| 80 | + } |
| 81 | + } |
| 82 | + } while (Process32Next(hSnapshot, &pe32)); |
| 83 | + } |
| 84 | + |
| 85 | + CloseHandle(hSnapshot); |
| 86 | + return NumProcesses; |
| 87 | +} |
| 88 | + |
| 89 | +DWORD GetPID(const char* processName) { |
| 90 | + HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 91 | + if (snapshot == INVALID_HANDLE_VALUE) { |
| 92 | + printf("Error al crear un snapshot de procesos"); |
| 93 | + return 0; |
| 94 | + } |
| 95 | + |
| 96 | + PROCESSENTRY32 entry; |
| 97 | + entry.dwSize = sizeof(PROCESSENTRY32); |
| 98 | + if (!Process32First(snapshot, &entry)) { |
| 99 | + CloseHandle(snapshot); |
| 100 | + printf("Error al obtener la primera entrada de proceso"); |
| 101 | + return 0; |
| 102 | + } |
| 103 | + |
| 104 | + DWORD processId = 0; |
| 105 | + do { |
| 106 | + if (strcmp(entry.szExeFile, processName) == 0) { |
| 107 | + processId = entry.th32ProcessID; |
| 108 | + break; |
| 109 | + } |
| 110 | + } while (Process32Next(snapshot, &entry)); |
| 111 | + |
| 112 | + CloseHandle(snapshot); |
| 113 | + return processId; |
| 114 | +} |
| 115 | + |
| 116 | +bool EnablePrivilege(DWORD processId, LPCSTR privilegeName, HANDLE hProcess = NULL) { |
| 117 | + |
| 118 | + TOKEN_PRIVILEGES tp; |
| 119 | + tp.PrivilegeCount = 1; |
| 120 | + tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 121 | + if (!LookupPrivilegeValue(NULL, privilegeName, &tp.Privileges[0].Luid)) { |
| 122 | + printf("Error al buscar el valor del privilegio "); |
| 123 | + return false; |
| 124 | + } |
| 125 | + |
| 126 | + if (!hProcess) { |
| 127 | + hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processId); |
| 128 | + // comprobar por ultima vez |
| 129 | + if (!hProcess){ |
| 130 | + printf("Error al abrir el token del proceso"); |
| 131 | + return false; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + HANDLE hToken; |
| 136 | + if (!OpenProcessToken(hProcess, TOKEN_ALL_ACCESS, &hToken)) { |
| 137 | + printf("Error al abrir el token del proceso"); |
| 138 | + CloseHandle(hProcess); |
| 139 | + return false; |
| 140 | + } |
| 141 | + |
| 142 | + if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL)) { |
| 143 | + printf("Error al ajustar los privilegios del token"); |
| 144 | + CloseHandle(hToken); |
| 145 | + CloseHandle(hProcess); |
| 146 | + return false; |
| 147 | + } |
| 148 | + |
| 149 | + CloseHandle(hToken); |
| 150 | + CloseHandle(hProcess); |
| 151 | + return true; |
| 152 | +} |
| 153 | + |
0 commit comments