-
Notifications
You must be signed in to change notification settings - Fork 118
/
SetProcessCritical.cpp
81 lines (73 loc) · 2.29 KB
/
SetProcessCritical.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <windows.h>
#pragma comment(lib,"Advapi32.lib")
#define ProcessBreakOnTermination 29
typedef NTSTATUS(NTAPI *_NtSetInformationProcess)(
HANDLE ProcessHandle,
PROCESS_INFORMATION_CLASS ProcessInformationClass,
PVOID ProcessInformation,
ULONG ProcessInformationLength);
BOOL EnableDebugPrivilege(BOOL fEnable)
{
BOOL fOk = FALSE;
HANDLE hToken;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
{
TOKEN_PRIVILEGES tp;
tp.PrivilegeCount = 1;
LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &tp.Privileges[0].Luid);
tp.Privileges[0].Attributes = fEnable ? SE_PRIVILEGE_ENABLED : 0;
AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), NULL, NULL);
fOk = (GetLastError() == ERROR_SUCCESS);
CloseHandle(hToken);
}
return(fOk);
}
BOOL CallNtSetInformationProcess(HANDLE hProcess, ULONG Flag)
{
_NtSetInformationProcess NtSetInformationProcess = (_NtSetInformationProcess)GetProcAddress(GetModuleHandleA("NtDll.dll"), "NtSetInformationProcess");
if (!NtSetInformationProcess)
{
printf("[!]Could not find NtSetInformationProcess entry point in NTDLL.DLL\n");
return 0;
}
if(NtSetInformationProcess(hProcess, (PROCESS_INFORMATION_CLASS)ProcessBreakOnTermination, &Flag, sizeof(ULONG))<0)
printf("[!]NtSetInformationProcess error\n");
return 1;
}
int main(int argc, char *argv[])
{
if (argc != 3)
{
printf("\nSet the selected process as critical or not.\n");
printf("Usage:\n");
printf(" %s <pid> <flag>\n", argv[0]);
printf("If flag=0: \n Set the selected process is not critical.\n");
printf("If flag=1: \n Set the selected process as critical.If exit the process,the system will cause BSOD.\n");
return 0;
}
if (!EnableDebugPrivilege(TRUE))
{
printf("[!]AdjustTokenPrivileges Failed.<%d>\n", GetLastError());
}
DWORD pid;
sscanf_s(argv[1], "%d", &pid);
HANDLE hProcess;
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
if (memcmp(argv[2], "1", 1) == 0)
{
printf("[+]Try to set the selected process as critical... ");
if(CallNtSetInformationProcess(hProcess,TRUE)==1)
printf("done.\n");
else
printf("false.\n");
}
else
{
printf("[+]Try to set the selected process is not critical... ");
if (CallNtSetInformationProcess(hProcess, FALSE) == 1)
printf("done.\n");
else
printf("false.\n");
}
return 0;
}