根据 X 次不成功的登录尝试来禁止 IP 地址?

根据 X 次不成功的登录尝试来禁止 IP 地址?

是否可以在 X 次尝试登录 Windows 服务器失败后禁止某个 IP 地址?不是针对某个特定帐户(我知道如何操作),而是针对整个计算机。

我们在尝试猜测用户名时遭受了暴力攻击的严重打击,所以这确实有助于减轻服务器的负载。

答案1

您可以使用 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

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

调度器触发器

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

运行 powershell 脚本

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

答案2

我知道这个问题已经过时了,但实际上这是我几周前开始尝试做同样的事情时偶然发现的第一篇论坛帖子。我设法想出了一个工作脚本,它将解析 24 小时前的事件日志,仅查找错误登录事件日志条目,抓取那些有超过 10 个错误登录的条目,然后使用 netsh 命令将它们放入 ipsec 筛选器列表中。然后我用这行编写了一个批处理文件,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}

我知道这个脚本可能效率不高,但当我开始编写这个脚本时,我完全没有使用过 powershell,所以我优化脚本的能力还有很多不足之处。然而,尽管如此,我还是想与任何可以使用它的人分享这个脚本。

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

答案3

这个脚本建立在 remunda 的答案的基础上,并更进一步https://serverfault.com/a/397637/155102它说明“BlockAttackers”规则尚未输入任何 IP(返回“*”字符串)。它还会向日志文件写入注释,让您知道 IP 何时添加到规则中。

一个好建议是创建“BlockAttackers”规则来阻止 IP 地址,但首先将其禁用。然后,手动运行此脚本一次,以便它可以用应阻止的实际 IP 地址填充“RemoteAddresses”字段。查看这些 IP 地址以确保没有添加任何关键内容,然后启用防火墙规则。按照 remunda 所述将此规则添加到您的防火墙。

此脚本的 git

#Checks for IP addresses that used incorrect password more than 10 times
#within 24 hours and blocks them using a firewall rule 'BlockAttackers'

#Check only last 24 hours
$DT = [DateTime]::Now.AddHours(-24)

#Select Ip addresses that has audit failure
$l = Get-EventLog -LogName 'Security' -InstanceId 4625 -After $DT | Select-Object @{n='IpAddress';e={$_.ReplacementStrings[-2]} }

#Get ip adresses, that have more than 10 wrong logins
$g = $l | group-object -property IpAddress | where {$_.Count -gt 10} | Select -property Name

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

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

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

#Only collect IPs that aren't already in the firewall rule
$w = $g | where {$_.Name.Length -gt 1 -and !($arRemote -contains $_.Name + '/255.255.255.255') }

#Add the new IPs to firewall rule
$w| %{
  if ($ar.RemoteAddresses -eq '*') {
    $ar.remoteaddresses = $_.Name
  }else{
    $ar.remoteaddresses += ',' + $_.Name
  }
}

#Write to logfile
if ($w.length -gt 1) {
  $w| %{(Get-Date).ToString() + ' ' + $_.Name >> '.\blocked.txt'}
}

答案4

让其他人控制你的防火墙规则通常不是一个好主意。这基本上就是你在这里要求的。

相关内容