1

所以我不会因为这个脚本而受到赞扬,但我需要一些帮助。我是PS新手,所以请原谅愚蠢的问题。

我需要为此脚本添加例外,例如“192.168”。/ "10.0." 范围。

我知道它会是这样的: IF remoteaddress = "blah" 然后跳过。

但我不知道如何为 powershell 格式化它。

如果有人可以告诉我或指出我正确的方向?

#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'
$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

1 回答 1

1

您想要的是一个您永远不想阻止的 IP 白名单。

然后,如果您遇到列入白名单的 IP,就会导致您的循环失败。

$whitelist = @("10.0.0.1", "192.168.1.1")
..
if ($IP -match $whitelist)  { 
    #do nothing, debug here 
} else { 
    #block things 
}

在您的 powershell 经验水平上,这对您来说可能有点棘手,但请看一下wail2ban,这是我为解决这个确切问题而创建的一个 powershell 脚本。

于 2013-11-11T03:26:09.613 回答