LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

【C#】設置Windows Server系統(tǒng)登錄賬戶鎖定閾值(如5次失敗后鎖定30分鐘),防止暴力破解

admin
2025年5月22日 7:24 本文熱度 122

在C#中,可以通過調用系統(tǒng)工具secedit.exe修改安全策略來實現(xiàn)賬戶鎖定策略的配置。以下是實現(xiàn)該功能的代碼示例:

using System;

using System.Diagnostics;

using System.IO;

using System.Linq;


class AccountLockoutPolicySetter

{

    const int TARGET_THRESHOLD = 5;

    const int TARGET_DURATION = 30;

    const int TARGET_RESET = 30;


    static void Main()

    {

        // 先檢查當前策略

        if (CheckCurrentPolicy())

        {

            Console.WriteLine("當前策略已符合要求,無需修改");

            return;

        }


        // 策略配置代碼(原邏輯)

        ApplyNewPolicy();

    }


    static bool CheckCurrentPolicy()

    {

        string exportPath = Path.Combine(Path.GetTempPath(), "current_policy.inf");

        

        try

        {

            // 導出當前策略

            ProcessStartInfo exportInfo = new ProcessStartInfo

            {

                FileName = "secedit",

                Arguments = $"/export /cfg \"{exportPath}\"",

                WindowStyle = ProcessWindowStyle.Hidden,

                UseShellExecute = true,

                Verb = "runas" // 需要管理員權限

            };


            using (Process exportProc = Process.Start(exportInfo))

            {

                exportProc.WaitForExit();

                if (exportProc.ExitCode != 0) return false;

            }


            // 解析策略文件

            var lines = File.ReadAllLines(exportPath);

            bool inSystemAccess = false;

            int currentThreshold = 0;

            int currentDuration = 0;

            int currentReset = 0;


            foreach (string line in lines)

            {

                string cleanLine = line.Split(';')[0].Trim(); // 移除注釋

                if (cleanLine.StartsWith("[System Access]"))

                {

                    inSystemAccess = true;

                    continue;

                }

                else if (cleanLine.StartsWith("["))

                {

                    inSystemAccess = false;

                    continue;

                }


                if (inSystemAccess && cleanLine.Contains("="))

                {

                    string[] parts = cleanLine.Split('=');

                    string key = parts[0].Trim();

                    string value = parts[1].Trim();


                    switch (key)

                    {

                        case "LockoutBadCount":

                            int.TryParse(value, out currentThreshold);

                            break;

                        case "LockoutDuration":

                            int.TryParse(value, out currentDuration);

                            break;

                        case "ResetLockoutCount":

                            int.TryParse(value, out currentReset);

                            break;

                    }

                }

            }


            // 策略比對

            return currentThreshold == TARGET_THRESHOLD && 

                   currentDuration == TARGET_DURATION && 

                   currentReset == TARGET_RESET;

        }

        catch

        {

            return false;

        }

        finally

        {

            if (File.Exists(exportPath)) File.Delete(exportPath);

        }

    }


    static void ApplyNewPolicy()

    {

        string infContent = $@"

[Unicode]

Unicode=yes

[Version]

signature=""$CHICAGO$""

Revision=1

[System Access]

LockoutBadCount = {TARGET_THRESHOLD}

ResetLockoutCount = {TARGET_RESET}

LockoutDuration = {TARGET_DURATION}";


        string tempInfPath = Path.Combine(Path.GetTempPath(), "lockoutpolicy.inf");

        

        try

        {

            File.WriteAllText(tempInfPath, infContent);


            ProcessStartInfo startInfo = new ProcessStartInfo

            {

                FileName = "secedit",

                Arguments = $"/configure /db secedit.sdb /cfg \"{tempInfPath}\"",

                WindowStyle = ProcessWindowStyle.Hidden,

                Verb = "runas",

                UseShellExecute = true

            };


            using (Process proc = Process.Start(startInfo))

            {

                proc.WaitForExit();

                Console.WriteLine(proc.ExitCode == 0 

                    ? "策略已成功更新!" 

                    : $"更新失敗,錯誤碼: {proc.ExitCode}");

            }

        }

        catch (Exception ex)

        {

            Console.WriteLine($"操作異常: {ex.Message}");

        }

        finally

        {

            if (File.Exists(tempInfPath)) File.Delete(tempInfPath);

        }

    }

}?

使用說明:

  1. 管理員權限:代碼中通過Verb = "runas"請求提升權限,運行時需以管理員身份執(zhí)行。

  2. 策略生效:策略修改后立即生效,無需重啟系統(tǒng)。

  3. 參數(shù)說明

    • LockoutBadCount:賬戶鎖定閾值(失敗嘗試次數(shù))

    • LockoutDuration:鎖定持續(xù)時間(分鐘),設為-1表示永久鎖定

    • ResetLockoutCount:失敗計數(shù)器重置時間(分鐘)

注意事項:

  • 實際部署前建議在測試環(huán)境驗證

  • 確保目標系統(tǒng)為Windows Server且支持這些策略

  • 鎖定策略會影響所有本地賬戶

  • 永久鎖定(LockoutDuration = -1)需手動解鎖

可通過組策略編輯器(secpol.msc)驗證策略是否生效,路徑為:安全設置 > 賬戶策略 > 賬戶鎖定策略。

代碼改進說明:

  1. 策略檢查函數(shù) CheckCurrentPolicy()

    • 通過secedit /export導出當前策略

    • 解析策略文件的[System Access]部分

    • 比對三個關鍵值是否匹配目標

  2. 雙階段執(zhí)行邏輯

    • 先檢查當前策略狀態(tài)

    • 僅在策略不匹配時執(zhí)行修改操作

  3. 增強的錯誤處理

    • 策略導出和解析過程增加異常捕獲

    • 自動清理臨時文件

  4. 常量定義

    • 策略目標值使用常量定義,便于維護

使用注意事項:

  1. 必須使用管理員權限運行程序

  2. 策略文件中的LockoutDuration = -1表示永久鎖定

  3. 導出的策略文件可能存在默認值(如0),需結合實際情況判斷

  4. 建議通過secpol.msc手動驗證最終策略狀態(tài)

可以通過在命令提示符中運行以下命令驗證策略狀態(tài):

secedit /export /cfg current_policy.inf

type current_policy.inf | findstr "LockoutBadCount"


該文章在 2025/5/22 10:00:43 編輯過
關鍵字查詢
相關文章
正在查詢...
點晴ERP是一款針對中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
點晴PMS碼頭管理系統(tǒng)主要針對港口碼頭集裝箱與散貨日常運作、調度、堆場、車隊、財務費用、相關報表等業(yè)務管理,結合碼頭的業(yè)務特點,圍繞調度、堆場作業(yè)而開發(fā)的。集技術的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點晴WMS倉儲管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務都免費,不限功能、不限時間、不限用戶的免費OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

黄频国产免费高清视频,久久不卡精品中文字幕一区,激情五月天AV电影在线观看,欧美国产韩国日本一区二区
亚洲青青青在线视频 | 亚洲国产精品久久精品 | 日韩精品久久人人躁人人 | 日韩中文久久影院 | 日本在线观看中文字二区 | 亚洲一品道在线观看 |