Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 将异常添加到防火墙脚本_Powershell_Powershell 2.0_Powershell 3.0 - Fatal编程技术网

Powershell 将异常添加到防火墙脚本

Powershell 将异常添加到防火墙脚本,powershell,powershell-2.0,powershell-3.0,Powershell,Powershell 2.0,Powershell 3.0,所以我不打算为这个脚本负责,但我需要一些帮助。 我是PS新手,所以请原谅这些愚蠢的问题 我需要为这个脚本添加异常,比如“192.168.”/“10.0.”范围 我知道会是这样的: 如果remoteaddress=“blah”,则跳过 但我不知道如何为powershell设置格式 如果有人能告诉我或者给我指出正确的方向 #Checks for IP addresses that used incorrect password more than 10 times #within 24 hours

所以我不打算为这个脚本负责,但我需要一些帮助。 我是PS新手,所以请原谅这些愚蠢的问题

我需要为这个脚本添加异常,比如“192.168.”/“10.0.”范围

我知道会是这样的: 如果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'}
}

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

如果你遇到一个被列入白名单的IP,那么你的循环就会失败

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

对于您的powershell体验来说,这可能有点棘手,但请看一看,我创建的powershell脚本就是为了解决这个问题。

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

如果你遇到一个被列入白名单的IP,那么你的循环就会失败

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

对于您的powershell体验来说,这可能有点棘手,但请看一看,我创建了一个powershell脚本来解决这个问题。

嘿,谢谢您的回复。我很想使用wail2ban,但GFIMAX的限制不允许我大规模部署它。(50多台服务器),因此我们只想做一个非常基本的脚本。就像贴的那个。因为我们知道什么样的IP将访问什么样的服务器,所以每天早上进来,看到一台服务器上有5000次尝试,总是让人感到烦恼。我不确定GFIMAX对您的限制是什么,但我为类似规模的机队创建了wail2ban。最初它只是根据参数每隔X分钟检查一次是否有太多的事件,但为了简单起见,我将其更改为侦听新的日志事件。理想情况下,它可以是一项服务,但我没有时间去做。如果你通读自述文件,你至少应该能够看到它试图做什么的逻辑,然后应用到你自己的脚本中。嘿,谢谢你的回复。我很想使用wail2ban,但GFIMAX的限制不允许我大规模部署它。(50多台服务器),因此我们只想做一个非常基本的脚本。就像贴的那个。因为我们知道什么样的IP将访问什么样的服务器,所以每天早上进来,看到一台服务器上有5000次尝试,总是让人感到烦恼。我不确定GFIMAX对您的限制是什么,但我为类似规模的机队创建了wail2ban。最初它只是根据参数每隔X分钟检查一次是否有太多的事件,但为了简单起见,我将其更改为侦听新的日志事件。理想情况下,它可以是一项服务,但我没有时间去做。如果您通读自述文件,您至少应该能够看到它试图做什么的逻辑,然后应用到您自己的脚本中。