禁止基于 X 次登录尝试失败的 IP 地址?

Hea*_*ave 49 windows brute-force-attacks

在尝试登录 Windows 服务器失败 X 次后,是否可以禁止 IP 地址?不是针对特定帐户(我知道该怎么做),而是针对整个机器。

我们受到试图猜测用户名的蛮力攻击的重创,因此这确实有助于减轻服务器的负担。

小智 27

您可以使用 powershell 和任务管理器执行此操作。这可能不是完美的解决方案,但效果很好,我在两个月内有大约 100 个被阻止的 IP 地址。我编写了脚本,从 EventLog 中选择指定的事件(“审计失败”)。如果有许多来自任何 IP 地址的失败登录,则将其添加到名为“BlockAttackers”的防火墙规则(手动创建)中,该规则阻止任何到指定 IP 地址的流量。

PS1脚本:

$DT = [DateTime]::Now.AddDays(-1) # check only last 24 hours

$l = Get-EventLog -LogName 'Security' -InstanceId 4625 -After $DT | Select-Object @{n='IpAddress';e={$_.ReplacementStrings[-2]} } # select Ip addresses that has audit failure 
$g = $l | group-object -property IpAddress  | where {$_.Count -gt 20} | Select -property Name # get ip adresses, that have more than 20 wrong logins

$fw = New-Object -ComObject hnetcfg.fwpolicy2 # get firewall object

$ar = $fw.rules | where {$_.name -eq 'BlockAttackers'} # get firewall rule named 'BlockAttackers' (must be created manually)

$arRemote = $ar.RemoteAddresses -split(',') #split the existing IPs into an array so we can easily search for existing IPs

$w = $g | where {$_.Name.Length -gt 1 -and  !($arRemote -contains $_.Name + '/255.255.255.255') } # get ip addresses that are not already in firewal rule. Include the subnet mask which is automatically added to the firewall remote IP declaration.

$w| %{$ar.remoteaddresses += ',' + $_.Name} # add IPs to firewall rule
Run Code Online (Sandbox Code Playgroud)

在调度程序中创建任务并将触发器设置为事件 4625(Windows 登录,包括终端服务)。但是您可以将触发器设置为每小时运行两次,以避免不必要地加载服务器。

调度程序触发器

并在触发后运行 powershell 脚本。您还必须设置更高的权限才能运行此脚本,否则会因安全异常而失败。

运行 PowerShell 脚本

您还可以将此脚本绑定到其他安全事件。


小智 7

我知道这个问题很老,但它实际上是我几周前开始尝试做同样的事情时偶然发现的第一个论坛帖子。我设法想出了一个工作脚本,该脚本将在 24 小时后解析事件日志,仅针对错误登录事件日志条目,抓取超过 10 个错误登录的条目,然后使用netsh 命令。然后我用这一行写了一个批处理文件powershell .\*scriptname.ps1*并创建了一个计划任务来每 24 小时运行一次批处理文件(由于某种原因它不会直接执行)。

$DATE = [DateTime]::Now.AddDays(-1)

$EVS = Get-EventLog Security -InstanceId 529 -after $DATE

$EVS | select-string -inputobject {$_.message} -pattern "Source Network Address:(.)*\.*\.*\.*"  -allmatches | foreach-object {$_.Matches} | foreach-object {$_.Value} | foreach-object {$_.replace("Source Network Address:", "")} | group-object -property $_ | where-object {$_.count -gt 10} | select-object -property name | format-list | out-file c:\rdpblock.txt 

get-content -path c:\rdpblock.txt | foreach-object {$_.replace("Name :", "")} | out-file c:\rdpblockcleaned.txt 

get-content -path c:\rdpblockcleaned.txt | select-object -unique | out-file c:\rdpblocknospaces.txt

$RDPIP = get-content -path c:\rdpblocknospaces.txt | select-object -skip 1

$RDPIP | foreach-object {$_.replace("     ", "")} | foreach-object {netsh ipsec static add filter filterlist=RDP_BLOCK srcaddr=$($_) dstaddr=any}
Run Code Online (Sandbox Code Playgroud)

我知道这个脚本可能效率低下,但是当我开始工作时,我完全没有使用 powershell 的经验,所以我优化脚本的能力还有很多不足之处。然而,尽管有这个事实,我想我会与任何可以使用它的人分享这个。

我感谢 Remunda 给了我最初的想法,那张海报让我产生了使用 powershell 搜索事件日志的想法。


joe*_*rty 0

您的意思是登录到服务器/域还是登录到服务器上运行的网站?如果您的意思是登录到服务器/域,那么答案是否定的。Windows 没有基于失败登录尝试来阻止 IP 地址的概念,因为 IP 地址不是安全实体。可能有第三方工具可以做到这一点,但我不知道有任何工具,因为我从未研究过它。