跳到正文

个人记录

防 JC 脚本

Segment_Bugcat约 81 分钟
原文

这是 2025年9月6日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 81 分钟

版本

  1. v3当前
  2. v2
  3. v1

UNJC.cpp

C++
#include <bits/stdc++.h>
#include <windows.h>
#include <tlhelp32.h>

std::atomic <bool> disableInput(true); // Input disable flag
std::atomic <bool> shutdownFlag(false); // Program shutdown flag
std::vector <DWORD> secretSequence; // Unlock sequence storage
std::vector <DWORD> randomSequence; // Random target sequence
std::atomic <size_t> sequencePosition(0); // Current sequence position
std::atomic <bool> sequenceActive(false); // Sequence input active status
std::atomic <long long> lastKeyTime(0); // Last key press time

// 函数声明
void sigint_handler(int sig);
long long getCurrentTime();
void generateRandomSequence();
void initializeSecretSequence();
bool checkUnlockCombination(DWORD vkCode);
bool checkContinuousSequence(DWORD vkCode);
bool checkSecretSequence(DWORD vkCode);
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam);
void DisableSecurityOptions();
void EnableSecurityOptions();
bool TerminateTaskManager();
void securityMonitorThread();
bool TerminateProcessByName(const std::string& processName);
void BlockCommandPrompts();
void UnblockCommandPrompts();

// Block Ctrl+C interrupt
void sigint_handler(int sig) {
    // Do nothing, ignore Ctrl+C
}

// Get current timestamp (milliseconds)
long long getCurrentTime() {
    return std::chrono::duration_cast <std::chrono::milliseconds>(
        std::chrono::system_clock::now().time_since_epoch()
    ).count();
}

// Generate random target sequence
void generateRandomSequence() {
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution <> dis(65, 90); // A-Z
    
    randomSequence.clear();
    int sequenceLength = 5 + dis(gen) % 6; // 5-10 keys
    
    for (int i = 0;i < sequenceLength;i++) secretSequence.push_back(dis(gen)), randomSequence.push_back(dis(gen)); // Random letter A-Z
}

void initializeSecretSequence() {
    // Generate random target sequence
    generateRandomSequence();
}

// Check for unlock combination keys
bool checkUnlockCombination(DWORD vkCode) {
    // Multiple unlock methods
    static const std::vector <std::vector <DWORD> > unlockCombinations = {
        Set Yours Keys Here;
    };

    static std::vector <size_t> combinationPositions(unlockCombinations.size(), 0);
    static long long lastCheckTime = getCurrentTime();

    long long currentTime = getCurrentTime();
    
    // Timeout reset all sequences
    if (currentTime - lastCheckTime > 5000) // 5 second timeout
        for (size_t i = 0;i < combinationPositions.size();i++) combinationPositions[i] = 0;
    lastCheckTime = currentTime;

    for (size_t i = 0;i < unlockCombinations.size();i++) {
        if (vkCode == unlockCombinations[i][combinationPositions[i]]) {
            combinationPositions[i]++;
            if (combinationPositions[i] == unlockCombinations[i].size()) {
                // Successfully matched a combination
                for (size_t j = 0;j < combinationPositions.size();j++) combinationPositions[j] = 0;
                return true;
            }
        }
		else combinationPositions[i] = 0;
    }

    return false;
}

// Check if any continuous sequence matches the target sequence
bool checkContinuousSequence(DWORD vkCode) {
    static std::vector <DWORD> inputBuffer;
    static long long lastInputTime = getCurrentTime();
    
    long long currentTime = getCurrentTime();
    
    // Reset buffer if more than 3 seconds between key presses
    if (currentTime - lastInputTime > 3000) inputBuffer.clear();
    lastInputTime = currentTime;
    
    // Add current key to buffer
    inputBuffer.push_back(vkCode);
    
    // Keep buffer size reasonable
    if (inputBuffer.size() > 20) inputBuffer.erase(inputBuffer.begin());
    
    // Check if buffer contains the target sequence
    if (inputBuffer.size() >= randomSequence.size()) {
        for (size_t i = 0;i <= inputBuffer.size() - randomSequence.size();i++) {
            bool match = true;
            for (size_t j = 0;j < randomSequence.size();j++) {
                if (inputBuffer[i + j] != randomSequence[j]) {
                    match = false;
                    break;
                }
            }
            if (match) {
                inputBuffer.clear();
                return true;
            }
        }
    }
    
    return false;
}

// Check secret sequence
bool checkSecretSequence(DWORD vkCode) {
    long long currentTime = getCurrentTime();
    
    // Reset sequence if no key pressed for 2 seconds
    if (currentTime - lastKeyTime > 2000) {
        sequencePosition = 0;
        sequenceActive = false;
    }
    
    lastKeyTime = currentTime;

    if (!sequenceActive) {
        // Check if sequence starts (first key)
        if (vkCode == secretSequence[0]) {
            sequenceActive = true;
            sequencePosition = 1;
            return false;
        }
    }
	else {
        // Check next key in sequence
        if (vkCode == secretSequence[sequencePosition]) {
            sequencePosition++;
            if (sequencePosition == secretSequence.size()) {
                // Sequence completed
                sequencePosition = 0;
                sequenceActive = false;
                return true;
            }
        }
		else {
            // Key doesn't match, reset sequence
            sequencePosition = 0;
            sequenceActive = false;
        }
    }
    
    return false;
}

// Keyboard hook, monitor all keys
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
    if (nCode >= 0 && wParam == WM_KEYDOWN) {
        KBDLLHOOKSTRUCT* pKey = (KBDLLHOOKSTRUCT*)lParam;
        DWORD vkCode = pKey -> vkCode;

        // Check various unlock conditions
        if (checkUnlockCombination(vkCode) || 
            checkSecretSequence(vkCode) ||
            checkContinuousSequence(vkCode)) {
            disableInput = false;
            shutdownFlag = true;
            PostQuitMessage(0); // Send quit message
            return 1;
        }

        // Block all keyboard input
        if (disableInput) {
            // Specifically block system shortcuts including Win+L, Win+D, Alt+F4, etc.
            if (vkCode == VK_ESCAPE || vkCode == VK_TAB || 
                vkCode == VK_LWIN || vkCode == VK_RWIN ||
                vkCode == 'L' || vkCode == 'D' || // Block Win+L (lock), Win+D (desktop)
                vkCode == VK_F4 || // Block F4 key (part of Alt+F4)
                (GetAsyncKeyState(VK_CONTROL) && GetAsyncKeyState(VK_MENU) && vkCode == VK_DELETE) || // Ctrl+Alt+Del
                (GetAsyncKeyState(VK_LWIN) && vkCode == 'L') || // Win+L
                (GetAsyncKeyState(VK_LWIN) && vkCode == 'D') || // Win+D
                (GetAsyncKeyState(VK_CONTROL) && vkCode == VK_ESCAPE) || // Ctrl+Esc (Start menu)
                (GetAsyncKeyState(VK_MENU) && vkCode == VK_F4)) { // Alt+F4 (Close window)
                return 1;
            }
            return 1; // Block all other keyboard input
        }
    }
    
    // Also block Alt+F4 when Alt key is pressed and F4 is released
    if (nCode >= 0 && wParam == WM_KEYUP) {
        KBDLLHOOKSTRUCT* pKey = (KBDLLHOOKSTRUCT*)lParam;
        DWORD vkCode = pKey -> vkCode;
        
        if (disableInput && vkCode == VK_F4 && GetAsyncKeyState(VK_MENU) & 0x8000) return 1; // Block F4 key release when Alt is held down
    }
    
    return CallNextHookEx(NULL, nCode, wParam, lParam);
}

// Mouse hook, block all mouse input
LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam) {
    if (disableInput) {
        // 扩展鼠标操作禁用范围
        if (wParam == WM_LBUTTONDOWN || wParam == WM_RBUTTONDOWN ||
            wParam == WM_MBUTTONDOWN || wParam == WM_MOUSEWHEEL ||
            wParam == WM_MOUSEHWHEEL || wParam == WM_LBUTTONUP ||
            wParam == WM_RBUTTONUP || wParam == WM_MBUTTONUP ||
            wParam == WM_LBUTTONDBLCLK || wParam == WM_RBUTTONDBLCLK ||
            wParam == WM_MBUTTONDBLCLK || wParam == WM_XBUTTONDOWN ||
            wParam == WM_XBUTTONUP || wParam == WM_XBUTTONDBLCLK) {
            return 1; // 阻止所有鼠标输入
        }
    }
    return CallNextHookEx(NULL, nCode, wParam, lParam);
}

void DisableSecurityOptions() {
    HKEY hKey;
    DWORD value = 1;
    
    // Disable Task Manager
    if (RegCreateKeyEx(HKEY_CURRENT_USER, 
                      "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", 
                      0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
        RegSetValueEx(hKey, "DisableTaskMgr", 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
        RegCloseKey(hKey);
    }

    // Disable Lock Workstation
    if (RegCreateKeyEx(HKEY_CURRENT_USER, 
                      "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", 
                      0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
        RegSetValueEx(hKey, "DisableLockWorkstation", 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
        RegCloseKey(hKey);
    }

    // Disable Change Password
    if (RegCreateKeyEx(HKEY_CURRENT_USER, 
                      "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", 
                      0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
        RegSetValueEx(hKey, "DisableChangePassword", 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
        RegCloseKey(hKey);
    }

    // Disable Switch User (Fast User Switching)
    if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, 
                      "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", 
                      0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
        RegSetValueEx(hKey, "HideFastUserSwitching", 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
        RegCloseKey(hKey);
    }

    // Disable Logoff
    if (RegCreateKeyEx(HKEY_CURRENT_USER, 
                      "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer", 
                      0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
        RegSetValueEx(hKey, "NoLogoff", 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
        RegCloseKey(hKey);
    }

    // Disable Shutdown/Restart (also affects logoff)
    if (RegCreateKeyEx(HKEY_CURRENT_USER, 
                      "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer", 
                      0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
        RegSetValueEx(hKey, "NoClose", 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
        RegCloseKey(hKey);
    }

    // Disable Alt+F4 via registry (for some applications)
    if (RegCreateKeyEx(HKEY_CURRENT_USER, 
                      "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", 
                      0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
        RegSetValueEx(hKey, "NoWinKeys", 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
        RegCloseKey(hKey);
    }

    // Block command prompts
    BlockCommandPrompts();
}

#include <bits/stdc++.h>
#include <windows.h>
#include <tlhelp32.h>

std::atomic <bool> disableInput(true); // Input disable flag
std::atomic <bool> shutdownFlag(false); // Program shutdown flag
std::vector <DWORD> secretSequence; // Unlock sequence storage
std::vector <DWORD> randomSequence; // Random target sequence
std::atomic <size_t> sequencePosition(0); // Current sequence position
std::atomic <bool> sequenceActive(false); // Sequence input active status
std::atomic <long long> lastKeyTime(0); // Last key press time

// 函数声明
void sigint_handler(int sig);
long long getCurrentTime();
void generateRandomSequence();
void initializeSecretSequence();
bool checkUnlockCombination(DWORD vkCode);
bool checkContinuousSequence(DWORD vkCode);
bool checkSecretSequence(DWORD vkCode);
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam);
void DisableSecurityOptions();
void EnableSecurityOptions();
bool TerminateTaskManager();
void securityMonitorThread();
bool TerminateProcessByName(const std::string& processName);
void BlockCommandPrompts();
void UnblockCommandPrompts();

// Block Ctrl+C interrupt
void sigint_handler(int sig) {
    // Do nothing, ignore Ctrl+C
}

// Get current timestamp (milliseconds)
long long getCurrentTime() {
    return std::chrono::duration_cast <std::chrono::milliseconds>(
        std::chrono::system_clock::now().time_since_epoch()
    ).count();
}

// Generate random target sequence
void generateRandomSequence() {
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution <> dis(65, 90); // A-Z
    
    randomSequence.clear();
    int sequenceLength = 5 + dis(gen) % 6; // 5-10 keys
    
    for (int i = 0;i < sequenceLength;i++) secretSequence.push_back(dis(gen)), randomSequence.push_back(dis(gen)); // Random letter A-Z
}

void initializeSecretSequence() {
    // Generate random target sequence
    generateRandomSequence();
}

// Check for unlock combination keys
bool checkUnlockCombination(DWORD vkCode) {
    // Multiple unlock methods
    static const std::vector <std::vector <DWORD> > unlockCombinations = {
		{VK_F5, 55, 56, 57, 49, 49, 49, 52, 53, 49, 52, 49, 57, 49, 57, 56, 49, 48}, 
        {VK_F7, VK_F8, VK_F9, VK_F1, VK_F1, VK_F1, VK_F4, VK_F5, VK_F1, VK_F4, 
		VK_F12, VK_F12, VK_F12, VK_F12, VK_F12, VK_F12, VK_F12, VK_F12, VK_F12, VK_F12, 
		VK_F12, VK_F12, VK_F12, VK_F12, VK_F12, VK_F12, VK_F12, VK_F12, VK_F12, VK_F12, 
		VK_F12, VK_F12, VK_F12, VK_F12, VK_F12, VK_F12, VK_F12, VK_F12, VK_F12, VK_F12, 
		VK_F12, VK_F12, VK_F12, VK_F12, VK_F12, VK_F12, VK_F12, VK_F12, VK_F12, VK_F12, 
		VK_F12, VK_F12, VK_F12, VK_F12, VK_F12, VK_F12, VK_F12, VK_F12, VK_F12, VK_F12}
    };

    static std::vector <size_t> combinationPositions(unlockCombinations.size(), 0);
    static long long lastCheckTime = getCurrentTime();

    long long currentTime = getCurrentTime();
    
    // Timeout reset all sequences
    if (currentTime - lastCheckTime > 5000) // 5 second timeout
        for (size_t i = 0;i < combinationPositions.size();i++) combinationPositions[i] = 0;
    lastCheckTime = currentTime;

    for (size_t i = 0;i < unlockCombinations.size();i++) {
        if (vkCode == unlockCombinations[i][combinationPositions[i]]) {
            combinationPositions[i]++;
            if (combinationPositions[i] == unlockCombinations[i].size()) {
                // Successfully matched a combination
                for (size_t j = 0;j < combinationPositions.size();j++) combinationPositions[j] = 0;
                return true;
            }
        }
		else combinationPositions[i] = 0;
    }

    return false;
}

// Check if any continuous sequence matches the target sequence
bool checkContinuousSequence(DWORD vkCode) {
    static std::vector <DWORD> inputBuffer;
    static long long lastInputTime = getCurrentTime();
    
    long long currentTime = getCurrentTime();
    
    // Reset buffer if more than 3 seconds between key presses
    if (currentTime - lastInputTime > 3000) inputBuffer.clear();
    lastInputTime = currentTime;
    
    // Add current key to buffer
    inputBuffer.push_back(vkCode);
    
    // Keep buffer size reasonable
    if (inputBuffer.size() > 20) inputBuffer.erase(inputBuffer.begin());
    
    // Check if buffer contains the target sequence
    if (inputBuffer.size() >= randomSequence.size()) {
        for (size_t i = 0;i <= inputBuffer.size() - randomSequence.size();i++) {
            bool match = true;
            for (size_t j = 0;j < randomSequence.size();j++) {
                if (inputBuffer[i + j] != randomSequence[j]) {
                    match = false;
                    break;
                }
            }
            if (match) {
                inputBuffer.clear();
                return true;
            }
        }
    }
    
    return false;
}

// Check secret sequence
bool checkSecretSequence(DWORD vkCode) {
    long long currentTime = getCurrentTime();
    
    // Reset sequence if no key pressed for 2 seconds
    if (currentTime - lastKeyTime > 2000) {
        sequencePosition = 0;
        sequenceActive = false;
    }
    
    lastKeyTime = currentTime;

    if (!sequenceActive) {
        // Check if sequence starts (first key)
        if (vkCode == secretSequence[0]) {
            sequenceActive = true;
            sequencePosition = 1;
            return false;
        }
    }
	else {
        // Check next key in sequence
        if (vkCode == secretSequence[sequencePosition]) {
            sequencePosition++;
            if (sequencePosition == secretSequence.size()) {
                // Sequence completed
                sequencePosition = 0;
                sequenceActive = false;
                return true;
            }
        }
		else {
            // Key doesn't match, reset sequence
            sequencePosition = 0;
            sequenceActive = false;
        }
    }
    
    return false;
}

// Keyboard hook, monitor all keys
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
    if (nCode >= 0 && wParam == WM_KEYDOWN) {
        KBDLLHOOKSTRUCT* pKey = (KBDLLHOOKSTRUCT*)lParam;
        DWORD vkCode = pKey -> vkCode;

        // Check various unlock conditions
        if (checkUnlockCombination(vkCode) || 
            checkSecretSequence(vkCode) ||
            checkContinuousSequence(vkCode)) {
            disableInput = false;
            shutdownFlag = true;
            PostQuitMessage(0); // Send quit message
            return 1;
        }

        // Block all keyboard input
        if (disableInput) {
            // Specifically block system shortcuts including Win+L, Win+D, Alt+F4, etc.
            if (vkCode == VK_ESCAPE || vkCode == VK_TAB || 
                vkCode == VK_LWIN || vkCode == VK_RWIN ||
                vkCode == 'L' || vkCode == 'D' || // Block Win+L (lock), Win+D (desktop)
                vkCode == VK_F4 || // Block F4 key (part of Alt+F4)
                (GetAsyncKeyState(VK_CONTROL) && GetAsyncKeyState(VK_MENU) && vkCode == VK_DELETE) || // Ctrl+Alt+Del
                (GetAsyncKeyState(VK_LWIN) && vkCode == 'L') || // Win+L
                (GetAsyncKeyState(VK_LWIN) && vkCode == 'D') || // Win+D
                (GetAsyncKeyState(VK_CONTROL) && vkCode == VK_ESCAPE) || // Ctrl+Esc (Start menu)
                (GetAsyncKeyState(VK_MENU) && vkCode == VK_F4)) { // Alt+F4 (Close window)
                return 1;
            }
            return 1; // Block all other keyboard input
        }
    }
    
    // Also block Alt+F4 when Alt key is pressed and F4 is released
    if (nCode >= 0 && wParam == WM_KEYUP) {
        KBDLLHOOKSTRUCT* pKey = (KBDLLHOOKSTRUCT*)lParam;
        DWORD vkCode = pKey -> vkCode;
        
        if (disableInput && vkCode == VK_F4 && GetAsyncKeyState(VK_MENU) & 0x8000) return 1; // Block F4 key release when Alt is held down
    }
    
    return CallNextHookEx(NULL, nCode, wParam, lParam);
}

// Mouse hook, block all mouse input
LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam) {
    if (disableInput) {
        // 扩展鼠标操作禁用范围
        if (wParam == WM_LBUTTONDOWN || wParam == WM_RBUTTONDOWN || 
            wParam == WM_MBUTTONDOWN || wParam == WM_MOUSEWHEEL ||
            wParam == WM_MOUSEHWHEEL || wParam == WM_LBUTTONUP ||
            wParam == WM_RBUTTONUP || wParam == WM_MBUTTONUP ||
            wParam == WM_LBUTTONDBLCLK || wParam == WM_RBUTTONDBLCLK ||
            wParam == WM_MBUTTONDBLCLK || wParam == WM_XBUTTONDOWN ||
            wParam == WM_XBUTTONUP || wParam == WM_XBUTTONDBLCLK) {
            return 1; // 阻止所有鼠标输入
        }
    }
    return CallNextHookEx(NULL, nCode, wParam, lParam);
}

void DisableSecurityOptions() {
    HKEY hKey;
    DWORD value = 1;
    
    // Disable Task Manager
    if (RegCreateKeyEx(HKEY_CURRENT_USER, 
                      "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", 
                      0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
        RegSetValueEx(hKey, "DisableTaskMgr", 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
        RegCloseKey(hKey);
    }

    // Disable Lock Workstation
    if (RegCreateKeyEx(HKEY_CURRENT_USER, 
                      "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", 
                      0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
        RegSetValueEx(hKey, "DisableLockWorkstation", 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
        RegCloseKey(hKey);
    }

    // Disable Change Password
    if (RegCreateKeyEx(HKEY_CURRENT_USER, 
                      "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", 
                      0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
        RegSetValueEx(hKey, "DisableChangePassword", 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
        RegCloseKey(hKey);
    }

    // Disable Switch User (Fast User Switching)
    if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, 
                      "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", 
                      0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
        RegSetValueEx(hKey, "HideFastUserSwitching", 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
        RegCloseKey(hKey);
    }

    // Disable Logoff
    if (RegCreateKeyEx(HKEY_CURRENT_USER, 
                      "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer", 
                      0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
        RegSetValueEx(hKey, "NoLogoff", 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
        RegCloseKey(hKey);
    }

    // Disable Shutdown/Restart (also affects logoff)
    if (RegCreateKeyEx(HKEY_CURRENT_USER, 
                      "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer", 
                      0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
        RegSetValueEx(hKey, "NoClose", 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
        RegCloseKey(hKey);
    }

    // Disable Alt+F4 via registry (for some applications)
    if (RegCreateKeyEx(HKEY_CURRENT_USER, 
                      "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", 
                      0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
        RegSetValueEx(hKey, "NoWinKeys", 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
        RegCloseKey(hKey);
    }

    // Block command prompts
    BlockCommandPrompts();
}

void EnableSecurityOptions() {
    HKEY hKey;
    
    // 恢复 HKEY_CURRENT_USER 下的系统策略
    if (RegOpenKeyEx(HKEY_CURRENT_USER, 
                    "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", 
                    0, KEY_WRITE, &hKey) == ERROR_SUCCESS) {
        RegDeleteValue(hKey, "DisableTaskMgr");
        RegDeleteValue(hKey, "DisableLockWorkstation");
        RegDeleteValue(hKey, "DisableChangePassword");
        RegDeleteValue(hKey, "NoWinKeys");
        RegDeleteValue(hKey, "HideFastUserSwitching"); // 移动到这里
        RegCloseKey(hKey);
    }

    // 恢复 HKEY_LOCAL_MACHINE 下的系统策略(需要管理员权限)
    if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, 
                    "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", 
                    0, KEY_WRITE, &hKey) == ERROR_SUCCESS) {
        RegDeleteValue(hKey, "HideFastUserSwitching");
        RegCloseKey(hKey);
    }

    // 恢复 Explorer 策略(电源选项关键)
    if (RegOpenKeyEx(HKEY_CURRENT_USER, 
                    "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer", 
                    0, KEY_WRITE, &hKey) == ERROR_SUCCESS) {
        RegDeleteValue(hKey, "NoLogoff");
        RegDeleteValue(hKey, "NoClose");
        RegDeleteValue(hKey, "NoRun");
        RegDeleteValue(hKey, "NoFind");
        RegCloseKey(hKey);
    }

    // 恢复系统策略的其他可能位置
    if (RegOpenKeyEx(HKEY_CURRENT_USER, 
                    "Software\\Policies\\Microsoft\\Windows\\System", 
                    0, KEY_WRITE, &hKey) == ERROR_SUCCESS) {
        RegDeleteValue(hKey, "DisableCMD");
        RegCloseKey(hKey);
    }

    // 恢复 PowerShell 策略
    if (RegOpenKeyEx(HKEY_CURRENT_USER, 
                    "Software\\Policies\\Microsoft\\Windows\\PowerShell", 
                    0, KEY_WRITE, &hKey) == ERROR_SUCCESS) {
        RegDeleteValue(hKey, "Disable");
        RegCloseKey(hKey);
    }

    // 强制刷新注册表更改
    SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 
                      (LPARAM)"Policy", SMTO_BLOCK, 1000, NULL);
    SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 
                      (LPARAM)"Windows", SMTO_BLOCK, 1000, NULL);
}

// 阻止命令行程序通过注册表
void BlockCommandPrompts() {
    HKEY hKey;
    DWORD value = 1;
    
    // 禁用命令提示符 (CMD) - 主要方法
    if (RegCreateKeyEx(HKEY_CURRENT_USER, 
                      "Software\\Policies\\Microsoft\\Windows\\System", 
                      0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
        RegSetValueEx(hKey, "DisableCMD", 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
        RegCloseKey(hKey);
    }
    
    // 禁用 PowerShell - 主要方法
    if (RegCreateKeyEx(HKEY_CURRENT_USER, 
                      "Software\\Policies\\Microsoft\\Windows\\PowerShell", 
                      0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
        RegSetValueEx(hKey, "Disable", 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
        RegCloseKey(hKey);
    }
    
    // 禁用运行对话框和查找功能
    if (RegCreateKeyEx(HKEY_CURRENT_USER, 
                      "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer", 
                      0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
        RegSetValueEx(hKey, "NoRun", 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
        RegSetValueEx(hKey, "NoFind", 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
        RegCloseKey(hKey);
    }
    
    // 使用更安全的方法:创建AppCompatFlags阻止命令行程序运行
    if (RegCreateKeyEx(HKEY_CURRENT_USER, 
                      "Software\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers", 
                      0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
        // 阻止cmd.exe运行
        RegSetValueEx(hKey, "C:\\Windows\\System32\\cmd.exe", 0, REG_SZ, 
                     (const BYTE*)"RUNASINVOKER", 12);
        // 阻止powershell.exe运行
        RegSetValueEx(hKey, "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe", 0, REG_SZ, 
                     (const BYTE*)"RUNASINVOKER", 12);
        RegCloseKey(hKey);
    }
}

void UnblockCommandPrompts() {
    HKEY hKey;
    
    // 恢复命令提示符
    if (RegOpenKeyEx(HKEY_CURRENT_USER, 
                    "Software\\Policies\\Microsoft\\Windows\\System", 
                    0, KEY_WRITE, &hKey) == ERROR_SUCCESS) {
        RegDeleteValue(hKey, "DisableCMD");
        RegCloseKey(hKey);
    }
    
    // 恢复 PowerShell
    if (RegOpenKeyEx(HKEY_CURRENT_USER, 
                    "Software\\Policies\\Microsoft\\Windows\\PowerShell", 
                    0, KEY_WRITE, &hKey) == ERROR_SUCCESS) {
        RegDeleteValue(hKey, "Disable");
        RegCloseKey(hKey);
    }
    
    // 恢复运行和查找功能
    if (RegOpenKeyEx(HKEY_CURRENT_USER, 
                    "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer", 
                    0, KEY_WRITE, &hKey) == ERROR_SUCCESS) {
        RegDeleteValue(hKey, "NoRun");
        RegDeleteValue(hKey, "NoFind");
        RegCloseKey(hKey);
    }
    
    // 移除AppCompatFlags限制
    if (RegOpenKeyEx(HKEY_CURRENT_USER, 
                    "Software\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers", 
                    0, KEY_WRITE, &hKey) == ERROR_SUCCESS) {
        RegDeleteValue(hKey, "C:\\Windows\\System32\\cmd.exe");
        RegDeleteValue(hKey, "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe");
        RegCloseKey(hKey);
    }
}

bool TerminateProcessByName(const std::string& processName) {
    HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (hSnapshot == INVALID_HANDLE_VALUE) {
        return false;
    }

    PROCESSENTRY32 pe;
    pe.dwSize = sizeof(PROCESSENTRY32);

    bool found = false;
    if (Process32First(hSnapshot, &pe)) {
        do {
            if (_stricmp(pe.szExeFile, processName.c_str()) == 0) {
                // Found target process
                HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pe.th32ProcessID);
                if (hProcess != NULL) {
                    TerminateProcess(hProcess, 0);
                    CloseHandle(hProcess);
                    found = true;
                }
            }
        } while (Process32Next(hSnapshot, &pe));
    }

    CloseHandle(hSnapshot);
    return found;
}

// Check if taskmgr.exe is running and terminate it
bool TerminateTaskManager() {
    return TerminateProcessByName("taskmgr.exe");
}

void securityMonitorThread() {
    while (!shutdownFlag) {
        // Check and terminate Task Manager if running
        TerminateTaskManager();

        // Check and terminate command prompts if running - 更全面的列表
        const char* blockedProcesses[] = {
            "cmd.exe",
            "powershell.exe",
            "pwsh.exe",
            "conhost.exe",
            "wscript.exe",
            "cscript.exe",
            "bash.exe", // WSL
            "wsl.exe",  // WSL
            "cmd.hta",  // HTML Application
            NULL
        };

        for (int i = 0; blockedProcesses[i] != NULL; i++) {
            TerminateProcessByName(blockedProcesses[i]);
        }

        // Check if registry was modified
        HKEY hKey;
        if (RegOpenKeyEx(HKEY_CURRENT_USER, 
                        "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", 
                        0, KEY_READ, &hKey) == ERROR_SUCCESS) {
            DWORD value = 0;
            DWORD size = sizeof(value);
            
            if (RegQueryValueEx(hKey, "DisableTaskMgr", NULL, NULL, (LPBYTE)&value, &size) != ERROR_SUCCESS || value != 1) {
                DisableSecurityOptions();
            }
            
            RegCloseKey(hKey);
        }

        // Additional protection: Block Alt key state to prevent Alt+F4
        if (disableInput && (GetAsyncKeyState(VK_MENU) & 0x8000)) {
            // Simulate Alt key up to break Alt+F4 combination
            INPUT input[2] = {0};
            input[0].type = INPUT_KEYBOARD;
            input[0].ki.wVk = VK_MENU;
            input[0].ki.dwFlags = KEYEVENTF_KEYUP;
            
            input[1].type = INPUT_KEYBOARD;
            input[1].ki.wVk = VK_F4;
            input[1].ki.dwFlags = KEYEVENTF_KEYUP;
            
            SendInput(2, input, sizeof(INPUT));
        }

        std::this_thread::sleep_for(std::chrono::milliseconds(500)); // 适中的监控频率
    }
}

int main() {
    // Set signal handler to ignore Ctrl+C
    signal(SIGINT, sigint_handler);

    // Initialize unlock sequence and generate random target
    initializeSecretSequence();

    // Install keyboard hook
    HHOOK keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, NULL, 0);
    if (keyboardHook == NULL) {
        std::cerr << "Failed to install keyboard hook!" << std::endl;
        return 1;
    }

    // Install mouse hook
    HHOOK mouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseProc, NULL, 0);
    if (mouseHook == NULL) {
        std::cerr << "Failed to install mouse hook!" << std::endl;
        return 1;
    }

    // Disable security options
    DisableSecurityOptions();

    // Start security monitoring thread
    std::thread monitorThread(securityMonitorThread);

    // Set cleanup function on exit
    atexit([]() {
        EnableSecurityOptions();
        shutdownFlag = true;
    });

    // Message loop
    MSG msg;
    while (!shutdownFlag) {
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
            if (msg.message == WM_QUIT) break;
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
		else std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }

    // Cleanup
    UnhookWindowsHookEx(keyboardHook);
    UnhookWindowsHookEx(mouseHook);
    
    shutdownFlag = true;
    if (monitorThread.joinable()) monitorThread.join();

    // Restore security options
    EnableSecurityOptions();

    std::cout << "Input unlocked. Program exiting." << std::endl;
    return 0;
}

// 阻止命令行程序通过注册表
void BlockCommandPrompts() {
    HKEY hKey;
    DWORD value = 1;
    
    // 禁用命令提示符 (CMD) - 主要方法
    if (RegCreateKeyEx(HKEY_CURRENT_USER, 
                      "Software\\Policies\\Microsoft\\Windows\\System", 
                      0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
        RegSetValueEx(hKey, "DisableCMD", 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
        RegCloseKey(hKey);
    }
    
    // 禁用 PowerShell - 主要方法
    if (RegCreateKeyEx(HKEY_CURRENT_USER, 
                      "Software\\Policies\\Microsoft\\Windows\\PowerShell", 
                      0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
        RegSetValueEx(hKey, "Disable", 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
        RegCloseKey(hKey);
    }
    
    // 禁用运行对话框和查找功能
    if (RegCreateKeyEx(HKEY_CURRENT_USER, 
                      "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer", 
                      0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
        RegSetValueEx(hKey, "NoRun", 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
        RegSetValueEx(hKey, "NoFind", 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
        RegCloseKey(hKey);
    }
    
    // 使用更安全的方法:创建AppCompatFlags阻止命令行程序运行
    if (RegCreateKeyEx(HKEY_CURRENT_USER, 
                      "Software\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers", 
                      0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
        // 阻止cmd.exe运行
        RegSetValueEx(hKey, "C:\\Windows\\System32\\cmd.exe", 0, REG_SZ, 
                     (const BYTE*)"RUNASINVOKER", 12);
        // 阻止powershell.exe运行
        RegSetValueEx(hKey, "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe", 0, REG_SZ, 
                     (const BYTE*)"RUNASINVOKER", 12);
        RegCloseKey(hKey);
    }
}

// 修改 UnblockCommandPrompts 函数,移除环境变量相关的操作
void UnblockCommandPrompts() {
    HKEY hKey;
    
    // 恢复命令提示符
    if (RegOpenKeyEx(HKEY_CURRENT_USER, 
                    "Software\\Policies\\Microsoft\\Windows\\System", 
                    0, KEY_WRITE, &hKey) == ERROR_SUCCESS) {
        RegDeleteValue(hKey, "DisableCMD");
        RegCloseKey(hKey);
    }
    
    // 恢复 PowerShell
    if (RegOpenKeyEx(HKEY_CURRENT_USER, 
                    "Software\\Policies\\Microsoft\\Windows\\PowerShell", 
                    0, KEY_WRITE, &hKey) == ERROR_SUCCESS) {
        RegDeleteValue(hKey, "Disable");
        RegCloseKey(hKey);
    }
    
    // 恢复运行和查找功能
    if (RegOpenKeyEx(HKEY_CURRENT_USER, 
                    "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer", 
                    0, KEY_WRITE, &hKey) == ERROR_SUCCESS) {
        RegDeleteValue(hKey, "NoRun");
        RegDeleteValue(hKey, "NoFind");
        RegCloseKey(hKey);
    }
    
    // 移除AppCompatFlags限制
    if (RegOpenKeyEx(HKEY_CURRENT_USER, 
                    "Software\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers", 
                    0, KEY_WRITE, &hKey) == ERROR_SUCCESS) {
        RegDeleteValue(hKey, "C:\\Windows\\System32\\cmd.exe");
        RegDeleteValue(hKey, "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe");
        RegCloseKey(hKey);
    }
}

bool TerminateProcessByName(const std::string& processName) {
    HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (hSnapshot == INVALID_HANDLE_VALUE) {
        return false;
    }

    PROCESSENTRY32 pe;
    pe.dwSize = sizeof(PROCESSENTRY32);

    bool found = false;
    if (Process32First(hSnapshot, &pe)) {
        do {
            if (_stricmp(pe.szExeFile, processName.c_str()) == 0) {
                // Found target process
                HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pe.th32ProcessID);
                if (hProcess != NULL) {
                    TerminateProcess(hProcess, 0);
                    CloseHandle(hProcess);
                    found = true;
                }
            }
        } while (Process32Next(hSnapshot, &pe));
    }

    CloseHandle(hSnapshot);
    return found;
}

// Check if taskmgr.exe is running and terminate it
bool TerminateTaskManager() {
    return TerminateProcessByName("taskmgr.exe");
}

void securityMonitorThread() {
    while (!shutdownFlag) {
        // Check and terminate Task Manager if running
        TerminateTaskManager();

        // Check and terminate command prompts if running - 更全面的列表
        const char* blockedProcesses[] = {
            "cmd.exe",
            "powershell.exe",
            "pwsh.exe",
            "conhost.exe",
            "wscript.exe",
            "cscript.exe",
            "bash.exe", // WSL
            "wsl.exe",  // WSL
            "cmd.hta",  // HTML Application
            NULL
        };

        for (int i = 0; blockedProcesses[i] != NULL; i++) {
            TerminateProcessByName(blockedProcesses[i]);
        }

        // Check if registry was modified
        HKEY hKey;
        if (RegOpenKeyEx(HKEY_CURRENT_USER, 
                        "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", 
                        0, KEY_READ, &hKey) == ERROR_SUCCESS) {
            DWORD value = 0;
            DWORD size = sizeof(value);
            
            if (RegQueryValueEx(hKey, "DisableTaskMgr", NULL, NULL, (LPBYTE)&value, &size) != ERROR_SUCCESS || value != 1) {
                DisableSecurityOptions();
            }
            
            RegCloseKey(hKey);
        }

        // Additional protection: Block Alt key state to prevent Alt+F4
        if (disableInput && (GetAsyncKeyState(VK_MENU) & 0x8000)) {
            // Simulate Alt key up to break Alt+F4 combination
            INPUT input[2] = {0};
            input[0].type = INPUT_KEYBOARD;
            input[0].ki.wVk = VK_MENU;
            input[0].ki.dwFlags = KEYEVENTF_KEYUP;
            
            input[1].type = INPUT_KEYBOARD;
            input[1].ki.wVk = VK_F4;
            input[1].ki.dwFlags = KEYEVENTF_KEYUP;
            
            SendInput(2, input, sizeof(INPUT));
        }

        std::this_thread::sleep_for(std::chrono::milliseconds(500)); // 适中的监控频率
    }
}

int main() {
    // Set signal handler to ignore Ctrl+C
    signal(SIGINT, sigint_handler);

    // Initialize unlock sequence and generate random target
    initializeSecretSequence();

    // Install keyboard hook
    HHOOK keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, NULL, 0);
    if (keyboardHook == NULL) {
        std::cerr << "Failed to install keyboard hook!" << std::endl;
        return 1;
    }

    // Install mouse hook
    HHOOK mouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseProc, NULL, 0);
    if (mouseHook == NULL) {
        std::cerr << "Failed to install mouse hook!" << std::endl;
        return 1;
    }

    // Disable security options
    DisableSecurityOptions();

    // Start security monitoring thread
    std::thread monitorThread(securityMonitorThread);

    // Set cleanup function on exit
    atexit([]() {
        EnableSecurityOptions();
        shutdownFlag = true;
    });

    // Message loop
    MSG msg;
    while (!shutdownFlag) {
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
            if (msg.message == WM_QUIT) break;
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
		else std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }

    // Cleanup
    UnhookWindowsHookEx(keyboardHook);
    UnhookWindowsHookEx(mouseHook);
    
    shutdownFlag = true;
    if (monitorThread.joinable()) monitorThread.join();

    // Restore security options
    EnableSecurityOptions();

    std::cout << "Input unlocked. Program exiting." << std::endl;
    return 0;
}