跳到正文

剪贴板

剪贴板 5vyw8a3m

Terabinaryte
原文
剪贴板信息

剪贴板信息

发布
收录
更新

防机惨锁屏

这份代码由 防JC锁屏 修改而来

使用了以下文章中的代码:

win32根据进程名获取进程ID或者终止进程 - CSDN

c++中进程的挂起

禁用Ctrl+Alt+Del最有效的方法

首先,代码中使用挂起了系统进程的操作,有一定风险,但一般不会导致系统出现死机,蓝屏

请按正常方式退出程序,否则可能需要从资源监视器 (perfmon.exe) 手动恢复explorer和winlogon

请以GB-2312/GBK/GB18030编码保存,否则可能出现乱码(Dev-cpp默认GB-2312)

编译要求C++14以上

已实机测试

C++
#include <iostream>
#include <thread>
#include <string>
#include <windows.h>
#include <tlhelp32.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>

typedef LONG(NTAPI *_NtSuspendProcess)(IN HANDLE ProcessHandle);
typedef LONG(NTAPI *_NtResumeProcess)(IN HANDLE ProcessHandle);
HWND hwnd;

void full_screen()
{
    if (!hwnd)
        hwnd = GetForegroundWindow();
    LONG l_WinStyle = GetWindowLong(hwnd, GWL_STYLE);
    SetWindowLong(hwnd, GWL_STYLE, (l_WinStyle | WS_POPUP | WS_MAXIMIZE) & ~WS_CAPTION & ~WS_THICKFRAME & ~WS_BORDER);
}

void keep_top()
{
    full_screen();
    int cx = GetSystemMetrics(SM_CXSCREEN);
    int cy = GetSystemMetrics(SM_CYSCREEN);
    if (hwnd)
        SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, cx, cy, 0);
}

bool IsProcessRunAsAdmin()
{
    SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
    PSID AdministratorsGroup;
    BOOL b = AllocateAndInitializeSid(
        &NtAuthority,
        2,
        SECURITY_BUILTIN_DOMAIN_RID,
        DOMAIN_ALIAS_RID_ADMINS,
        0, 0, 0, 0, 0, 0,
        &AdministratorsGroup);
    if (b)
    {
        CheckTokenMembership(NULL, AdministratorsGroup, &b);
        FreeSid(AdministratorsGroup);
    }
    return b == TRUE;
}

bool AdminOk;
short GetAdmin(LPCSTR Param, int Showcmd)
{
    if (IsProcessRunAsAdmin())
    {
        AdminOk = true;
        return 0;
    }
    TCHAR Path[MAX_PATH];
    ZeroMemory(Path, MAX_PATH);
    ::GetModuleFileName(NULL, Path, MAX_PATH);
    HINSTANCE res;
    res = ShellExecute(NULL, "runas", Path, Param, NULL, Showcmd);
    if ((unsigned long long)res > 32)
        return 1;
    else
    {
        AdminOk = false;
        return 0;
    }
}

bool EnableDebugPrivilege()
{
    HANDLE hToken;
    LUID sedebugnameValue;
    TOKEN_PRIVILEGES tkp;
    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
    {
        return FALSE;
    }
    if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &sedebugnameValue))
    {
        CloseHandle(hToken);
        return false;
    }
    tkp.PrivilegeCount = 1;
    tkp.Privileges[0].Luid = sedebugnameValue;
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    if (!AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(tkp), NULL, NULL))
    {
        CloseHandle(hToken);
        return false;
    }
    return true;
}

bool EscToQuit;
bool Suspended;
std::string getpwd()
{
    std::string pwd;
    while (true)
    {
        char ch = getch();
        if ('0' <= ch && ch <= '9')
        {
            putch('.');
            pwd.push_back(ch);
        }
        else if (ch == '\r')
        {
            putch('\n');
            break;
        }
        else if (ch == '\b' && pwd.length() > 0)
        {
            putch('\b');
            putch(' ');
            putch('\b');
            pwd.pop_back();
        }
        else if (ch == 27 && EscToQuit)
        {
            if (Suspended)
            {
            }
            exit(0);
        }
    }
    return pwd;
}

DWORD __getProcessHandle(LPCTSTR lpProcessName) // 根据进程名查找进程PID
{
    DWORD dwRet = 0;
    HANDLE hSnapShot = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (hSnapShot == INVALID_HANDLE_VALUE)
    {
        printf("\n获得进程快照失败,返回的GetLastError():%ld", GetLastError());
        return dwRet;
    }
    PROCESSENTRY32 pe32;
    pe32.dwSize = sizeof(PROCESSENTRY32);
    Process32First(hSnapShot, &pe32);
    do
    {
        if (!lstrcmp(pe32.szExeFile, lpProcessName))
        {
            dwRet = pe32.th32ProcessID;
            break;
        }
    } while (Process32Next(hSnapShot, &pe32));
    CloseHandle(hSnapShot);
    return dwRet;
}

bool kill;
void killtaskmgr()
{
    if (!AdminOk)
        return;
    while (kill)
    {
        system("taskkill -f -im taskmgr.exe>nul 2>nul");
        Sleep(100);
    }
}

void top()
{
    while (kill)
    {
        keep_top();
        Sleep(100);
    }
}

int main(void)
{
    bool succ = GetAdmin("", 1);
    if (succ == 1)
        return 0;
    full_screen();
    EscToQuit = true;
tk1:;
    std::cout << "防JC锁屏\n";
    std::cout << "此程序将暂停explorer.exe,winlogon.exe\n";
    std::cout << "与其相关的所有快捷键将暂时失效\n";
    std::cout << "在锁定期间按下的快捷键将在锁定结束后全部执行\n";
    std::cout << "请勿以非正常方式退出程序,否则可能需要重启恢复\n";
    std::cout << "press esc to quit now现在按下ESC可关闭\n";
    std::cout << "输入密码(纯数字): ";
    std::string pwd1 = getpwd();
    std::cout << "重复输入密码: ";
    std::string pwd2 = getpwd();
    if (pwd2 != pwd1)
    {
        std::cout << "wrong password!" << std::endl;
        Sleep(3);
        system("cls");
        goto tk1;
    }
    EscToQuit = false;
    std::cout << "esc quit disabled";
    kill = true;
    std::thread myThread(killtaskmgr);
    std::thread myThread2(top);
    std::cout << "the password is ok." << std::endl;
    HANDLE pchd1 = 0, pchd2 = 0;
    _NtSuspendProcess NtSuspendProcess = 0;
    _NtResumeProcess NtResumeProcess = 0;
    NtSuspendProcess = (_NtSuspendProcess)
        GetProcAddress(GetModuleHandle(_T("ntdll")), "NtSuspendProcess");
    NtResumeProcess = (_NtResumeProcess)
        GetProcAddress(GetModuleHandle(_T("ntdll")), "NtResumeProcess");
    EnableDebugPrivilege();
    int pcid1 = __getProcessHandle("explorer.exe");
    int pcid2 = __getProcessHandle("winlogon.exe");
    pchd1 = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pcid1);
    pchd2 = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pcid2);
    HWND hwnd2 = GetConsoleWindow();
	HMENU hmenu2 = GetSystemMenu(hwnd2, false);
	RemoveMenu(hmenu2, SC_CLOSE, MF_BYCOMMAND);
	LONG style = GetWindowLong(hwnd2, GWL_STYLE);
	style &= ~(WS_MINIMIZEBOX);
	SetWindowLong(hwnd2, GWL_STYLE, style);
	SetWindowPos(hwnd2, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
	ShowWindow(hwnd2, SW_SHOWNORMAL);
	DestroyMenu(hmenu2);
	ReleaseDC(hwnd2, NULL);
    if (!pchd1)
    {
        printf("未能打开进程:(pid) %d\n", pcid1);
        goto quit;
    }
    if (!pchd2)
    {
        printf("未能打开进程:(pid) %d\n", pcid2);
        goto quit;
    }
    else
    {
        if (NtSuspendProcess)
        {
            NtSuspendProcess(pchd1);
            std::cout << "成功暂停: \"explorer.exe\"\n";
        }
        if (NtSuspendProcess)
        {
            NtSuspendProcess(pchd2);
            std::cout << "成功暂停: \"winlogon.exe\"\n";
        }
    }
    Sleep(1000);
    system("cls");
    while (true)
    {
        std::cout << "please enter the password: ";
        std::string pwd3 = getpwd();
        if (pwd3 == pwd1)
            break;
        std::cout << "wrong password!" << std::endl;
        Sleep(3000);
        system("cls");
    }
    kill = false;
    myThread.join();
    myThread2.join();
    if(NtResumeProcess)
    {
        if (pchd1)
            NtResumeProcess(pchd1), std::cout<<"成功恢复: \"winlogon.exe\"\n";
        if (pchd2)
            NtResumeProcess(pchd2), std::cout<<"成功恢复: \"winlogon.exe\"\n";
    }
quit:
    if (pchd1)
        CloseHandle(pchd1);
    if (pchd2)
        CloseHandle(pchd2);
    return 0;
}