狠狠色丁香婷婷综合尤物/久久精品综合一区二区三区/中国有色金属学报/国产日韩欧美在线观看 - 国产一区二区三区四区五区tv

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

C#實(shí)現(xiàn)Windows系統(tǒng)遠(yuǎn)程桌面3389端口來(lái)訪者IP地址檢測(cè)并強(qiáng)制斷開(kāi)不在白名單的非法連接

admin
2025年3月13日 12:17 本文熱度 686

?以下是一個(gè)C#實(shí)現(xiàn)的解決方案,用于C#實(shí)現(xiàn)Windows系統(tǒng)遠(yuǎn)程桌面3389端口來(lái)訪者IP地址檢測(cè),并強(qiáng)制斷開(kāi)不在白名單的非法IP地址連接,支持IPv4和IPv6地址判斷,如果是IPv6地址則直接強(qiáng)制斷開(kāi)

using System;

using System.Collections.Generic;

using System.Net;

using System.Net.NetworkInformation;

using System.Net.Sockets;

using System.Runtime.InteropServices;


namespace TcpConnectionMonitor

{

    public class TcpConnectionManager

    {

        // IPv4連接結(jié)構(gòu)體

        [StructLayout(LayoutKind.Sequential)]

        public struct MIB_TCPROW

        {

            public uint dwState;

            public uint dwLocalAddr;

            public uint dwLocalPort;

            public uint dwRemoteAddr;

            public uint dwRemotePort;

        }


        // IPv6連接結(jié)構(gòu)體

        [StructLayout(LayoutKind.Sequential)]

        public struct MIB_TCP6ROW

        {

            public uint dwState;

            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]

            public byte[] localAddr;

            public uint localScopeId;

            public uint localPort;

            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]

            public byte[] remoteAddr;

            public uint remoteScopeId;

            public uint remotePort;

        }


        [DllImport("iphlpapi.dll", SetLastError = true)]

        public static extern int SetTcpEntry(ref MIB_TCPROW pTcpRow);


        [DllImport("iphlpapi.dll", SetLastError = true)]

        public static extern int SetTcpEntry6(ref MIB_TCP6ROW pTcp6Row);


        public static void DisconnectNonWhitelistedConnections(int targetPort, HashSet<string> whiteList)

        {

            IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();

            TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();


            foreach (TcpConnectionInformation connection in connections)

            {

                if (connection.LocalEndPoint.Port == targetPort)

                {

                    IPAddress remoteAddress = connection.RemoteEndPoint.Address;

                    string remoteIp = remoteAddress.ToString();


                    // 處理IPv6連接

                    if (remoteAddress.AddressFamily == AddressFamily.InterNetworkV6)

                    {

                        try

                        {

                            MIB_TCP6ROW row6 = new MIB_TCP6ROW

                            {

                                dwState = 12, // DELETE_TCB

                                localAddr = connection.LocalEndPoint.Address.GetAddressBytes(),

                                localPort = (uint)IPAddress.HostToNetworkOrder((short)connection.LocalEndPoint.Port),

                                remoteAddr = remoteAddress.GetAddressBytes(),

                                remotePort = (uint)IPAddress.HostToNetworkOrder((short)connection.RemoteEndPoint.Port),

                                localScopeId = 0,

                                remoteScopeId = 0

                            };


                            int result = SetTcpEntry6(ref row6);

                            Console.WriteLine(result == 0 ? 

                                $"已斷開(kāi)IPv6連接:{remoteIp}" : 

                                $"IPv6斷開(kāi)失敗(錯(cuò)誤碼:{result}):{remoteIp}");

                        }

                        catch (Exception ex)

                        {

                            Console.WriteLine($"處理IPv6連接時(shí)出錯(cuò):{ex.Message}");

                        }

                    }

                    // 處理IPv4連接

                    else if (!whiteList.Contains(remoteIp))

                    {

                        try

                        {

                            MIB_TCPROW row = new MIB_TCPROW

                            {

                                dwState = 12,

                                dwLocalAddr = BitConverter.ToUInt32(connection.LocalEndPoint.Address.GetAddressBytes(), 0),

                                dwLocalPort = (uint)IPAddress.HostToNetworkOrder((short)connection.LocalEndPoint.Port),

                                dwRemoteAddr = BitConverter.ToUInt32(remoteAddress.GetAddressBytes(), 0),

                                dwRemotePort = (uint)IPAddress.HostToNetworkOrder((short)connection.RemoteEndPoint.Port)

                            };


                            int result = SetTcpEntry(ref row);

                            Console.WriteLine(result == 0 ? 

                                $"已斷開(kāi)IPv4連接:{remoteIp}" : 

                                $"IPv4斷開(kāi)失敗(錯(cuò)誤碼:{result}):{remoteIp}");

                        }

                        catch (Exception ex)

                        {

                            Console.WriteLine($"處理IPv4連接時(shí)出錯(cuò):{ex.Message}");

                        }

                    }

                }

            }

        }

    }


    class Program

    {

        static void Main(string[] args)

        {

            HashSet<string> whiteList = new HashSet<string>

            {

                "192.168.1.100",

                "10.0.0.5"

            };


            try

            {

                TcpConnectionManager.DisconnectNonWhitelistedConnections(3389, whiteList);

            }

            catch (Exception ex)

            {

                Console.WriteLine($"運(yùn)行時(shí)錯(cuò)誤:{ex.Message}");

            }

        }

    }

}

  1. 雙協(xié)議支持

    • 新增MIB_TCP6ROW結(jié)構(gòu)體處理IPv6連接

    • 添加SetTcpEntry6 API調(diào)用接口

  2. 智能檢測(cè)邏輯

if (remoteAddress.AddressFamily == AddressFamily.InterNetworkV6)

{

    // 強(qiáng)制斷開(kāi)所有IPv6連接

}

else if (!whiteList.Contains(remoteIp))

{

    // 處理非白名單IPv4連接

}

  1. 增強(qiáng)的錯(cuò)誤處理

    • 分離IPv4/IPv6的錯(cuò)誤日志

    • 明確顯示操作結(jié)果和錯(cuò)誤碼

使用注意事項(xiàng):

  1. 需要先添加System.Net.NetworkInformation引用

  2. 白名單IP需根據(jù)實(shí)際情況修改

  3. 測(cè)試前建議改用非關(guān)鍵端口進(jìn)行驗(yàn)證

  4. 生產(chǎn)環(huán)境應(yīng)考慮添加日志記錄和異常處理

  1. 權(quán)限要求

    • 程序需要以管理員權(quán)限運(yùn)行,否則無(wú)法修改TCP連接表。

    • 需要啟用IPv6支持的操作系統(tǒng)

  2. 端口范圍

    • 支持同時(shí)監(jiān)控IPv4和IPv6的3389端口

    • 自動(dòng)過(guò)濾其他端口流量

  3. 網(wǎng)絡(luò)字節(jié)序

    • 自動(dòng)處理IPv6地址的128位字節(jié)轉(zhuǎn)換

    • 正確轉(zhuǎn)換端口號(hào)的網(wǎng)絡(luò)字節(jié)序

  4. 結(jié)構(gòu)體轉(zhuǎn)換

    1. 使用MIB_TCPROW結(jié)構(gòu)體與Windows API交互

    2. IP地址和端口需要轉(zhuǎn)換為網(wǎng)絡(luò)字節(jié)序

  5. 白名單檢查:直接使用HashSet進(jìn)行快速查找

  6. 日志輸出

    • 明確區(qū)分IPv4/IPv6操作結(jié)果

    • 顯示API調(diào)用的詳細(xì)錯(cuò)誤碼

該版本實(shí)現(xiàn)了對(duì)IPv6連接的主動(dòng)斷開(kāi)功能,同時(shí)優(yōu)化了以下方面:

  1. 使用更精確的地址族檢測(cè)邏輯

  2. 改進(jìn)結(jié)構(gòu)體字段的初始化方式

  3. 增強(qiáng)網(wǎng)絡(luò)字節(jié)序轉(zhuǎn)換的可靠性

  4. 提供更詳細(xì)的運(yùn)行時(shí)反饋信息

對(duì)于無(wú)法處理的連接類(lèi)型(如IPv6連接在舊系統(tǒng)上的兼容性問(wèn)題),程序會(huì)通過(guò)錯(cuò)誤碼反饋具體故障原因。

此代碼會(huì)實(shí)時(shí)檢測(cè)所有連接到3389端口的TCP連接,并自動(dòng)斷開(kāi)非白名單IP的連接,有效增強(qiáng)RDP服務(wù)的安全性。

其他注意事項(xiàng):

1、HashSet 白名單地址維護(hù)方法參見(jiàn):http://29753.oa22.cn?

2、Windows系統(tǒng)的遠(yuǎn)程桌面端口是允許更改的,所以更穩(wěn)妥的做法是要先讀取Windows系統(tǒng)遠(yuǎn)程桌面的服務(wù)端口,以下是獲取遠(yuǎn)程桌面服務(wù)端口的代碼:

string PortNumber = "";

RegistryKey localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32);

//遠(yuǎn)程桌面端口,判斷操作系統(tǒng)版本(64位\32位)打開(kāi)注冊(cè)表項(xiàng)

try

{

    RegistryKey rk_Winlogon = localKey.OpenSubKey(@"System\CurrentControlSet\Control\Terminal Server\Wds\rdpwd\Tds\Tcp", true);

    if (rk_Winlogon != null)

    {

        foreach (string vname in rk_Winlogon.GetValueNames())

        {

            if (vname == "PortNumber")

            {

                PortNumber = rk_Winlogon.GetValue("PortNumber").ToString();

                break;

            }

        }

        rk_Winlogon.Close();

    }

}

catch (Exception) { }


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