Skip to content

Commit dc31638

Browse files
authored
Add files via upload
1 parent 2b12120 commit dc31638

File tree

7 files changed

+773
-0
lines changed

7 files changed

+773
-0
lines changed

SetProcess/HelpProcess.h

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
#pragma once
2+
#include <windows.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <cstdlib>
6+
#include <tlhelp32.h>
7+
8+
#define PROCESS_AFFINITY_ENABLE_AUTO_UPDATE __MSABI_LONG(0x1U)
9+
10+
extern "C" {
11+
12+
typedef enum _PROC_INF_CLASS {
13+
ProcessIoPriority = 0x21
14+
} PROC_INF_CLASS;
15+
16+
NTSYSAPI LONG NTAPI NtSetInformationProcess(HANDLE ProcessHandle, PROC_INF_CLASS ProcessInformationClass, PVOID ProcessInformation, ULONG ProcessInformationLenght);
17+
18+
NTSYSAPI LONG NTAPI NtSuspendProcess(HANDLE ProcessHandle);
19+
20+
NTSYSAPI LONG NTAPI NtResumeProcess(HANDLE ProcessHandle);
21+
22+
typedef struct _MEMORY_PRIORITY_INFORMATION {
23+
ULONG MemoryPriority;
24+
} MEMORY_PRIORITY_INFORMATION, *PMEMORY_PRIORITY_INFORMATION;
25+
26+
typedef struct _IO_PRIORITY_INFORMATION {
27+
ULONG IoPriority;
28+
} IO_PRIORITY_INFORMATION, *PIO_PRIORITY_INFORMATION;
29+
30+
WINBASEAPI WINBOOL WINAPI SetProcessAffinityUpdateMode(HANDLE hProcess, DWORD dwFlags);
31+
32+
WINBASEAPI WINBOOL WINAPI SetProcessDefaultCpuSets(HANDLE Process, const ULONG* CpuSetIds, ULONG CpuSetIdCount);
33+
34+
WINBASEAPI WINBOOL WINAPI SetProcessInformation(HANDLE hProcess, PROCESS_INFORMATION_CLASS ProcessInformationClass, LPVOID ProcessInformation, DWORD ProcessInformationSize);
35+
}
36+
37+
bool EnablePrivilege(DWORD processId, LPCSTR privilegeName, HANDLE hProcess = NULL) {
38+
39+
TOKEN_PRIVILEGES tp;
40+
tp.PrivilegeCount = 1;
41+
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
42+
if (!LookupPrivilegeValue(NULL, privilegeName, &tp.Privileges[0].Luid)) {
43+
printf("Error al buscar el valor del privilegio ");
44+
return false;
45+
}
46+
47+
if (!hProcess) {
48+
hProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE, processId);
49+
// comprobar por ultima vez
50+
if (!hProcess){
51+
printf("Error al abrir el token del proceso");
52+
return false;
53+
}
54+
}
55+
56+
HANDLE hToken;
57+
if (!OpenProcessToken(hProcess, TOKEN_ALL_ACCESS, &hToken)) {
58+
printf("Error al abrir el token del proceso");
59+
CloseHandle(hProcess);
60+
return false;
61+
}
62+
63+
if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL)) {
64+
printf("Error al ajustar los privilegios del token");
65+
CloseHandle(hToken);
66+
CloseHandle(hProcess);
67+
return false;
68+
}
69+
70+
CloseHandle(hToken);
71+
CloseHandle(hProcess);
72+
return true;
73+
}
74+
75+
DWORD GetChildProcesses(DWORD ParentPID, DWORD* ChildPIDs) {
76+
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
77+
if (hSnapshot == INVALID_HANDLE_VALUE) {
78+
printf("Error al crear un snapshot de procesos\n");
79+
return 0;
80+
}
81+
82+
PROCESSENTRY32 pe32;
83+
pe32.dwSize = sizeof(PROCESSENTRY32);
84+
85+
DWORD NumProcesses = 0;
86+
87+
if (Process32First(hSnapshot, &pe32)) {
88+
do {
89+
if (pe32.th32ParentProcessID == ParentPID) {
90+
if (NumProcesses < 64) {
91+
ChildPIDs[NumProcesses++] = pe32.th32ProcessID;
92+
} else {
93+
printf("Se alcanzó el límite máximo de procesos hijos\n");
94+
break;
95+
}
96+
}
97+
} while (Process32Next(hSnapshot, &pe32));
98+
}
99+
100+
CloseHandle(hSnapshot);
101+
return NumProcesses;
102+
}
103+
104+
105+
DWORD GetPID(const char* processName) {
106+
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
107+
if (snapshot == INVALID_HANDLE_VALUE) {
108+
printf("Error al crear un snapshot de procesos");
109+
return 0;
110+
}
111+
112+
PROCESSENTRY32 entry;
113+
entry.dwSize = sizeof(PROCESSENTRY32);
114+
if (!Process32First(snapshot, &entry)) {
115+
CloseHandle(snapshot);
116+
printf("Error al obtener la primera entrada de proceso");
117+
return 0;
118+
}
119+
120+
DWORD processId = 0;
121+
do {
122+
if (strcmp(entry.szExeFile, processName) == 0) {
123+
processId = entry.th32ProcessID;
124+
break;
125+
}
126+
} while (Process32Next(snapshot, &entry));
127+
128+
CloseHandle(snapshot);
129+
return processId;
130+
}
131+
132+
bool ImpersonateSecurity() {
133+
// Obtener el handle del proceso SYSTEM
134+
DWORD SecurityPID = GetPID("lsass.exe");
135+
136+
HANDLE hSecurityProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, SecurityPID);
137+
if (!hSecurityProcess) {
138+
printf("Error al obtener la impersonacion");
139+
return false;
140+
}
141+
142+
// Obtener el handle del token del proceso SYSTEM
143+
HANDLE hSecurityToken;
144+
if (!OpenProcessToken(hSecurityProcess, TOKEN_DUPLICATE, &hSecurityToken)) {
145+
printf("Error al obtener la impersonacion");
146+
CloseHandle(hSecurityProcess);
147+
return false;
148+
}
149+
150+
// Duplicar el token para la impersonación
151+
HANDLE hDuplicateToken;
152+
if (!DuplicateTokenEx(hSecurityToken, TOKEN_ALL_ACCESS, nullptr, SecurityImpersonation, TokenPrimary, &hDuplicateToken)) {
153+
printf("Error al obtener la impersonacion");
154+
CloseHandle(hSecurityToken);
155+
CloseHandle(hSecurityProcess);
156+
return false;
157+
}
158+
159+
// Iniciar la impersonación
160+
if (!ImpersonateLoggedOnUser(hDuplicateToken)) {
161+
printf("Error al impersonar al usuario");
162+
CloseHandle(hDuplicateToken);
163+
CloseHandle(hSecurityToken);
164+
CloseHandle(hSecurityProcess);
165+
return false;
166+
}
167+
168+
// Cerrar los handles que ya no necesitamos
169+
CloseHandle(hDuplicateToken);
170+
CloseHandle(hSecurityToken);
171+
CloseHandle(hSecurityProcess);
172+
173+
return true;
174+
}
175+
176+
DWORD CountSetBits(DWORD mask) {
177+
DWORD counts = 0;
178+
while (mask) {
179+
counts += mask & 1;
180+
mask >>= 1;
181+
}
182+
return counts;
183+
}
184+
185+
DWORD ConvertToBitMask(const char* Str, DWORD counts = NULL) {
186+
DWORD Mask = 0;
187+
char* endptr;
188+
char* ptr = (char*)Str;
189+
190+
while (*ptr != '\0') {
191+
int Num = strtol(ptr, &endptr, 10);
192+
if (ptr == endptr) {
193+
break; // No se pudo convertir a número
194+
}
195+
Mask |= (1 << Num);
196+
ptr = endptr;
197+
if (*ptr == ',') {
198+
++ptr; // Saltar la coma
199+
}
200+
}
201+
202+
if (counts){
203+
CountSetBits(Mask);
204+
}
205+
return Mask;
206+
}
207+
208+
BOOL SetProcessDefaultCpuSetsID(DWORD ProcessID, const ULONG* CpuSetIds, ULONG CpuSetIdCount) {
209+
HANDLE hProcess = OpenProcess(PROCESS_SET_LIMITED_INFORMATION, FALSE, ProcessID);
210+
if (hProcess == NULL) {
211+
printf("Error al abrir el proceso");
212+
return FALSE;
213+
}
214+
215+
BOOL success = SetProcessDefaultCpuSets(hProcess, CpuSetIds, CpuSetIdCount);
216+
if (!success) {
217+
printf("Error al establecer los conjuntos de CPU");
218+
}
219+
220+
CloseHandle(hProcess);
221+
return success;
222+
}

SetProcess/SetProcess.cbp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
2+
<CodeBlocks_project_file>
3+
<FileVersion major="1" minor="6" />
4+
<Project>
5+
<Option title="SetProcess" />
6+
<Option pch_mode="2" />
7+
<Option compiler="gcc" />
8+
<Build>
9+
<Target title="Release">
10+
<Option platforms="Windows;" />
11+
<Option output="bin/Release/sp" prefix_auto="1" extension_auto="1" />
12+
<Option object_output="obj/Release/" />
13+
<Option type="1" />
14+
<Option compiler="gcc" />
15+
</Target>
16+
</Build>
17+
<Compiler>
18+
<Add option="-Wall" />
19+
</Compiler>
20+
<Linker>
21+
<Add library="Kernel32" />
22+
<Add library="ntdll" />
23+
</Linker>
24+
<Unit filename="HelpProcess.h" />
25+
<Unit filename="SetProcess.cpp" />
26+
<Unit filename="SetProcess.rc">
27+
<Option compilerVar="WINDRES" />
28+
</Unit>
29+
<Extensions />
30+
</Project>
31+
</CodeBlocks_project_file>

0 commit comments

Comments
 (0)