本帖最后由 522235677 于 2022-8-29 22:00 编辑
- $evt=Get-WinEvent -LogName Security -FilterXPath "Event[System[EventID=4625 and TimeCreated[timediff(@SystemTime)<=600000]]]" -MaxEvents 1 -ErrorAction SilentlyContinue
- if($null -ne $evt){
- $ip = $evt.Properties[19].Value
- $username = $evt.Properties[5].Value
- $ip = curl "https://www.abc.cn/failed?ip=$($ip)&username=$($username)"
- if ($ip.StatusCode -eq 200){
- if ($ip.Content -ne ''){
- $firewallRuleDisplayName = "远程桌面黑名单"
- $newRemoteIP = $ip.Content
- Get-NetFirewallRule -DisplayName $firewallRuleDisplayName | ForEach-Object {
- $addrfilter = $_ | Get-NetFirewallAddressFilter
- $arrOldRemoteIP = $addrfilter.CimInstanceProperties['RemoteAddress'].Value
- $arrNewRemoteIP = @(
- if ($null -ne $arrOldRemoteIP) { $arrOldRemoteIP }
- $newRemoteIP
- ) | Sort-Object -Unique
- $_ | Set-NetFirewallRule -RemoteAddress $arrNewRemoteIP
- }
- }
- }
- }
复制代码 向@flashercs大佬求助,上面的代码是之前请教后我自己给结合了一下,功能是当有远程桌面登录失败日志产生时将ip和用户名提交到一个url,如果url返回了一个IP地址,就把返回的那个IP地址加入的防火墙中。
现在想改进一下,当有事件EventID=4625发生时,记录IP、用户名、当前时间到一个文本(文本以当天时间命名,比如d:\20200823.txt),当当天记录的这个IP达到5次时就在防火墙加入阻止IP。 |