PowerShell 实现 RDP 远程桌面防火墙 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Qetesh
V2EX    Windows

PowerShell 实现 RDP 远程桌面防火墙

  •  
  •   Qetesh 2024-02-28 15:16:26 +08:00 2774 次点击
    这是一个创建于 665 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近观察系统日志(事件查看器-Windows 日志-安全-事件 ID4625 ),在互联网默认 3389 端口下,每天暴力破解 RDP 账号的次数大约在 1200 次左右

    虽然使用强密码,但是微软一直没有实现基于 IP 的自动封锁功能( Windows 11 22H2 后微软启用“帐户锁定阈值”,登录失败 10 次后,封锁账号 10 分钟),仍有暴力破解的风险

    通过 chatgpt 及优化后,写了个简单的 PowerShell 脚本,每 60 秒检测 ID4625 日志攻击 IP ,超过 3 次失败,添加到系统防火墙中禁止访问。并实现白名单、控制台输出登录失败 IP 及登录成功 IP

    # PowerShell 脚本 - 每分钟检查 RDP 登录失败,并将失败超过 3 次的 IP 阻止 Write-Host -ForegroundColor Green "========================================================================" Write-Host -ForegroundColor Cyan " RDP login failure monitoring script has been started" " This script checks for RDP login failures every minute and blocks IP that have failed more than 3 times." Write-Host -ForegroundColor Green "========================================================================" # 配置变量 $ErrorActiOnPreference= "SilentlyContinue" $LogName = "Security" $EventID = 4625,4624 # Windows 安全事件 ID $TimeSleepSecOnds= 60 $FailedAttempsLimit = 3 $WhiteListIPs = @("127.0.0.1", "172.16.0.110") $FirewallRuleName = "BlockedRDPAttempt_IPs" $newBlockIPs = @() $FailedIPs = @{} # 初始化首次查询间隔 $startTime = (Get-Date).Add(-(New-TimeSpan -Hours 1)) $endTime = Get-Date # 创建一个无限循环,每分钟运行一次 while ($true) { # 计算时间间隔 $TimeSpan = $endTime - $startTime $startTime = Get-Date $Events = Get-WinEvent -FilterHashtable @{ LogName = $LogName ID = $EventID StartTime = (Get-Date).Add(-$TimeSpan) } # 解析失败的 IP 地址并计算每个 IP 的失败次数 foreach ($event in $Events) { $IpAddress = $event.Properties[19].Value if ((![string]::IsNullOrEmpty($IpAddress)) -and ($IpAddress -ne "-") -and ($IpAddress -ne "0")) { $FailedIPs[$IpAddress] = $FailedIPs[$IpAddress] + 1 Write-Host -ForegroundColor Yellow "$(Get-Date) Detected failed login from: $IpAddress at $($event.TimeCreated)" } } # 打印登录成功的 IP 地址 foreach ($event in $Events) { $SuccessIpAddress = $event.Properties[18].Value if ((![string]::IsNullOrEmpty($SuccessIpAddress)) -and ($SuccessIpAddress -ne "-") -and ($SuccessIpAddress -ne "0") -and ($event.Properties[8].Value -eq 3)) { Write-Host -ForegroundColor Green "$(Get-Date) Detected successful login from: $SuccessIpAddress at $($event.TimeCreated)" } } # 过滤出失败次数达到限制的 IP 并进行处理 $BlockedIPs = $FailedIPs.Keys | Where-Object { $FailedIPs[$_] -ge $FailedAttempsLimit } $BlockedIPs = $BlockedIPs | Where-Object { $_ -notin $WhiteListIPs } if (Compare-Object -ReferenceObject $BlockedIPs -DifferenceObject $newBlockIPs -PassThru) { foreach ($ip in $BlockedIPs) { # 检查防火墙规则是否存在 $ruleExists = Get-NetFirewallRule -DisplayName $FirewallRuleName -ErrorAction SilentlyContinue # 如果规则不存在,则创建一个新规则 if (-not $ruleExists) { New-NetFirewallRule -DisplayName $FirewallRuleName -Direction Inbound -Action Block -RemoteAddress $ip -Protocol TCP -LocalPort 3389 Write-Host -ForegroundColor Blue "$(Get-Date) Created new Firewall rule for IP: $ip" } else { # 如果规则已存在,更新规则以添加新 IP $existingBlockIPs = (Get-NetFirewallRule -DisplayName $FirewallRuleName | Get-NetFirewallAddressFilter).RemoteAddress $newBlockIPs = @() $newBlockIPs += $existingBlockIPs if ($newBlockIPs -notcontains $ip) { $newBlockIPs += $ip Write-Host -ForegroundColor Red "$(Get-Date) Detected IP with multiple RDP failures: $ip" } Set-NetFirewallRule -DisplayName $FirewallRuleName -RemoteAddress $newBlockIPs } } if (![string]::IsNullOrEmpty($newBlockIPs)) { Write-Host -ForegroundColor Red "$(Get-Date) Updated Firewall rule to add IP: $newBlockIPs" } } # 等待 60 秒后继续下一轮循环 Start-Sleep -Seconds $TimeSleepSeconds # 结束时间戳 $endTime = Get-Date } 

    https://github.com/Qetesh/rdpFail2Ban

    可从 GitHub 下载,或复制代码到 ps1 文件,使用管理员权限终端运行 ps1 程序

    最后希望 RDP 永远不会中毒~

    18 条回复    2024-09-20 09:47:34 +08:00
    HenryHe613
        1
    HenryHe613  
       2024-02-28 15:33:36 +08:00
    大力支持!但是,为什么不把端口改一下呢?这样能减少很多爆破了
    CloudMx
        2
    CloudMx  
       2024-02-28 15:42:41 +08:00
    1 、端口改改
    2 、用户名改改
    随它破
    realJamespond
        3
    realJamespond  
       2024-02-28 15:53:36 +08:00
    套一层 ssh
    AliceFizzy
        4
    AliceFizzy  
       2024-02-28 16:06:19 +08:00
    好像已经有 wail2ban 了,不过还是支持自己造轮子
    cc666
        5
    cc666  
       2024-02-28 16:06:36 +08:00
    套 ssh+1
    LiYanHong
        6
    LiYanHong  
       2024-02-28 16:08:59 +08:00
    lsearsea
        7
    lsearsea  
       2024-02-28 16:49:06 +08:00
    v6 没有爆破记录
    hez2010
        8
    hez2010  
       2024-02-28 17:50:37 +08:00
    其实可以参考这篇文章给你的 RDP 添加多因素认证,这样就算破解进来了一样要输入动态口令才能访问: https://inc.sysu.edu.cn/article/1050
    idragonet
        9
    idragonet  
       2024-02-28 20:29:29 +08:00
    何必了,使用端口转发+直接 IP 白名单。
    Autonomous
        10
    Autonomous  
       2024-02-28 22:24:45 +08:00
    从来不敢把 22 ,3389 之类的端口向公网开放,在网关路由上还专门屏蔽掉了。
    PrinceofInj
        11
    PrinceofInj  
       2024-02-29 12:19:20 +08:00
    只要使用微软账号这一个方式,就可以隔绝 99.9999%的爆破了。
    wheat0r
        12
    wheat0r  
       2024-02-29 18:02:14 +08:00
    套 ssh ,不仅防爆破,还防漏洞
    Qetesh
        13
    Qetesh  
    OP
       2024-02-29 18:04:42 +08:00
    @lsearsea 我在 Windows 11 23H2 可以看到 IPv6 ,也可以更新防火墙策略。方便告知下系统版本,我测试下

    端口其实有换的,但我有个五位数 RDP 端口也有在被扫描,但扫描数确实少

    @hez2010 感谢补充,这个有试过,安全性应该是没啥问题的

    我现在主要用的是 WireGuard ,很方便,安全、速度、稳定性也都是可以的
    lsearsea
        14
    lsearsea  
       2024-03-01 16:11:03 +08:00
    就 win10
    mingtdlb
        15
    mingtdlb  
       2024-09-18 09:46:43 +08:00
    不用解封 IP ?
    Qetesh
        16
    Qetesh  
    OP
       2024-09-18 17:24:35 +08:00
    @mingtdlb 使用 PowerShell 不太适合复杂的程序,调用的系统防火墙,可以在防火墙里面手动删除。同时也欢迎在 GitHub 贡献代码
    mingtdlb
        17
    mingtdlb  
       2024-09-19 10:10:04 +08:00
    @Qetesh 这也没啥复杂不复杂,最简单的把 ban 的 ip 、时间戳之类的存到一个文本,启动脚本都读一下文本。

    我推荐你用火绒,有 rdp 爆破防护。
    Qetesh
        18
    Qetesh  
    OP
       2024-09-20 09:47:34 +08:00
    @mingtdlb 感谢分享
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5155 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 29ms UTC 08:32 PVG 16:32 LAX 00:32 JFK 03:32
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86