How to terminate a virus stuck in UAC authorization loop without logging out in Windows?

127 Views Asked by At

The C++ code below constantly asks for administrator privileges. When Windows prompts for User Account Control (UAC) authorization, we are unable to perform any actions, which means we can't open Task Manager to terminate this program. So, how can we exit the program without logging out?

#include<windows.h>
int main()
{
    ShowWindow(GetConsoleWindow(), SW_HIDE);
    BOOL isAdmin = FALSE;
    SID_IDENTIFIER_AUTHORITY ntAuthority = SECURITY_NT_AUTHORITY;
    PSID adminGroup;

    if (AllocateAndInitializeSid(&ntAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &adminGroup))
    {
        CheckTokenMembership(NULL, adminGroup, &isAdmin);
        FreeSid(adminGroup);
    }
    if (isAdmin) {
        MessageBoxA(NULL, "Something Evil", "Evil Prog", MB_ICONWARNING);
    }
    else {
        //MessageBoxA(NULL, "Not admin", "Not Admin", MB_OK);
        WCHAR szPath[MAX_PATH];
        GetModuleFileName(NULL, szPath, ARRAYSIZE(szPath));
        SHELLEXECUTEINFO sei = { sizeof(sei) };
        sei.lpVerb = L"runas";
        sei.lpFile = szPath;
        sei.hwnd = NULL;
        sei.nShow = SW_HIDE;
        while(ShellExecuteEx(&sei)==FALSE);
    }
}

Currently, my solution is to press Ctrl+Alt+Del and log out. However, I believe there should be another way to terminate this program without logging out of Windows.

1

There are 1 best solutions below

2
Ian Boyd On

The reason the UAC prompt keeps appearing over and over in an endless loop is that the code keeps showing the UAC prompt over and over in an endless loop:

while(ShellExecuteEx(&sei)==FALSE);

Change the code to:

WCHAR szPath[MAX_PATH];
GetModuleFileName(NULL, szPath, ARRAYSIZE(szPath));
SHELLEXECUTEINFO sei = { sizeof(sei) };
sei.lpVerb = L"runas";
sei.lpFile = szPath;
sei.hwnd = NULL;
sei.nShow = SW_HIDE;
ShellExecuteEx(&sei);