1 #include <cstdio> 2 #include <windows.h> 3 #include <tlhelp32.h> 4 5 using namespace std; 6 7 const int MAX = 10; 8 9 char SysProcList[MAX][MAX_PATH] = { 10 "[system process]", 11 "system", 12 "smss.exe", 13 "csrss.exe", 14 "winlogon.exe", 15 "services.exe", 16 "lsass.exe", 17 "svchost.exe", 18 "explorer.exe", 19 "ctfmon.exe" 20 }; 21 22 bool CheckSysProcList(char *lpText); 23 void PrintfNormal(char *lpText); 24 void PrintfSucceed(char *lpText); 25 void PrintfError(char *lpText); 26 27 int main() 28 { 29 SetConsoleTitleA("Process Cleaner v0.1 Made By gwsbhqt"); 30 31 HANDLE hToken; 32 OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &hToken); 33 34 LUID luid = {}; 35 LookupPrivilegeValueA(NULL, "SeDebugPrivilege", &luid); 36 37 TOKEN_PRIVILEGES tp = {}; 38 tp.PrivilegeCount = 1; 39 tp.Privileges[0].Luid = luid; 40 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 41 if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), NULL, NULL)) 42 { 43 PrintfError("ERROR_01\n"); 44 Sleep(5000); 45 return 0; 46 } 47 48 HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL); 49 if (INVALID_HANDLE_VALUE == hSnapshot) 50 { 51 PrintfError("ERROR_02\n"); 52 Sleep(5000); 53 return 0; 54 } 55 56 tagPROCESSENTRY32 pe32; 57 pe32.dwFlags = sizeof(pe32); 58 59 BOOL ret = Process32First(hSnapshot, &pe32); 60 while (ret) 61 { 62 Sleep(400); 63 64 if (pe32.th32ProcessID == GetCurrentProcessId()) 65 { 66 ret = Process32Next(hSnapshot, &pe32); 67 continue; 68 } 69 70 bool isSysProc = CheckSysProcList(strlwr(pe32.szExeFile)); 71 72 char str[MAX_PATH]; 73 sprintf(str, "%d %s %s", pe32.th32ProcessID, strlwr(pe32.szExeFile), isSysProc ? "ignored -\n" : ""); 74 PrintfNormal(str); 75 76 if (!isSysProc) 77 { 78 BOOL tpret = TerminateProcess(OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID), NULL); 79 sprintf(str, "shutdown %s\n", tpret ? "succeed √" : "faild ×"); 80 81 if (tpret) 82 PrintfSucceed(str); 83 else 84 PrintfError(str); 85 } 86 87 ret = Process32Next(hSnapshot, &pe32); 88 } 89 90 PrintfNormal("\nProcess Clean Finish...\n"); 91 Sleep(5000); 92 return 0; 93 } 94 95 bool CheckSysProcList(char *lpText) 96 { 97 for (int i = 0; i < MAX; i++) 98 if (0 == strcmp(lpText, SysProcList[i])) 99 return true; 100 101 return false; 102 } 103 104 void PrintfNormal(char *lpText) 105 { 106 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); 107 printf("%s", lpText); 108 } 109 110 void PrintfSucceed(char *lpText) 111 { 112 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY); 113 printf("%s", lpText); 114 } 115 116 void PrintfError(char *lpText) 117 { 118 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_INTENSITY); 119 printf("%s", lpText); 120 }
main.cpp
时间: 2024-10-20 13:10:37