Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent process from being closed in task manager

Tags:

c++

windows

I am developing a little program for becoming more productive. It should disconnect the user from the Internet or shut your computer down after a preset number of minutes. The program shouldn't be closed with task manager. I could compile the program and it run, but I could close it with task manager. I got my inspiration from this page:

#include <iostream>
#include <Windows.h>

#include <AccCtrl.h>
#include <AclAPI.h>
#include <tchar.h>

#include "shutdown.cpp"
#include "disconnect.cpp"



static const bool ProtectProcess()
{
    
    HANDLE hProcess = GetCurrentProcess();
    EXPLICIT_ACCESS denyAccess = {0};
    DWORD dwAccessPermissions = GENERIC_WRITE|PROCESS_ALL_ACCESS|WRITE_DAC|DELETE|WRITE_OWNER|READ_CONTROL;
    BuildExplicitAccessWithName( &denyAccess, _T("CURRENT_USER"), dwAccessPermissions, DENY_ACCESS, NO_INHERITANCE );
    PACL pTempDacl = NULL;
    DWORD dwErr = 0;
    dwErr = SetEntriesInAcl( 1, &denyAccess, NULL, &pTempDacl );
    // check dwErr...
    dwErr = SetSecurityInfo( hProcess, SE_KERNEL_OBJECT, DACL_SECURITY_INFORMATION, NULL, NULL, pTempDacl, NULL );
    // check dwErr...
    LocalFree( pTempDacl );
    CloseHandle( hProcess );
    return dwErr == ERROR_SUCCESS;

}


int main() 
{
    using namespace std;
    int abfrage;

    ProtectProcess();

    for (;;)
    {
        cout << "10.Cut your Internet connection" << endl
             << "11.Cut your Internet connection after 'x' minutes of surfing" << endl
             << "20.Shutdown"                   << endl;
        cin >> abfrage;
    
        switch(abfrage)
        {
            case 10: disconnectnow(); break;
            case 11: disconnectlater(); break;
            case 20: shutdown(); break;

            default: cout << "nothing to see here" << endl;
        }
    }
    return EXIT_SUCCESS;
}
like image 843
AaronP Avatar asked Jan 17 '23 04:01

AaronP


1 Answers

This functionality is, deliberately, unsupported and actively made intractable:

Why can't you trap TerminateProcess?

If a user fires up Task Manager and clicks the End Task button on the Applications tab, Windows first tries to shut down your program nicely, by sending WM_CLOSE messages to GUI programs and CTRL_CLOSE_EVENT events to console programs. But you don't get a chance to intercept TerminateProcess. Why not?

TerminateProcess is the low-level process-killing function. It bypasses DLL_PROCESS_DETACH and anything else in the process. When you kill with TerminateProcess, no more user-mode code will run in that process. It's gone. Do not pass go. Do not collect $200.

If you could intercept TerminateProcess, you would be escalating the arms race between programs and users. Suppose you could intercept it. Well, then if you wanted to make your program unkillable, you would just hand in your TerminateProcess handler! And then people would ask for "a way to kill a process that is refusing to be killed with TerminateProcess," and we'd be back to where we started.

In practice, programs attempting to evade detection and task kill try to rename themselves to near isoforms of the Windows system processes. Don't do this. It guarantees your program will be submitted as malware and will kill your credibility dead.

like image 73
MrGomez Avatar answered Jan 28 '23 20:01

MrGomez