![]() |
Tutorial [PowerShell] Automatically ban IP addresses - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Visual Basic & .NET Framework (https://sinister.ly/Forum-Visual-Basic-NET-Framework) +--- Thread: Tutorial [PowerShell] Automatically ban IP addresses (/Thread-Tutorial-PowerShell-Automatically-ban-IP-addresses) |
[PowerShell] Automatically ban IP addresses - phyrrus9 - 12-18-2020 Maybe you're like me, and occasionally you'll get a random person somewhere in the world that points a fuzzer at one of your applications. For me, this means nothing more than 500+ error log emails in a night before I can get around to banning their IP via firewall. Well, since I've already got code that hooks into any uncaught exceptions and logs them, I figured why not extract out the IP and put it in a database so I can easily ban them. Then I thought, why shouldn't I just automate banning them too? Well, if anybody else has had that problem, here's some code for you: SQL: Code: CREATE TABLE sys_ErrorIPs (Time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, Ip VARCHAR(30) NOT NULL) GO When you get an error, log your error as normal but insert the ip of the user into sys_ErrorIPs, then use the following PS script: Code: $cn = 'YOUR_CN_HERE' For me, I have that script set up in windows task scheduler to run every minute, so if somebody causes 5 or more unhandled exceptions in under a minute, the script will ban their IP, log that in the db, and send me an email about it. Note: this assumes that you have a firewall rule named Blacklist. Hope you enjoy. RE: [PowerShell] Automatically ban IP addresses - sashimi0 - 12-18-2020 I’m reading this through foggy glasses... Can this be adapted to block anyone browsing through a VPN, like how I was using with algo through Digital Ocean? RE: [PowerShell] Automatically ban IP addresses - phyrrus9 - 12-18-2020 (12-18-2020, 08:14 PM)sarcrates Wrote: I’m reading this through foggy glasses... The powershell script is basically just a script that loops over an array of ip addresses that its given, and adds them to an existing network policy. it has no idea if the ip is on a vpn or not (and that's probably not the route you'd want to go for that either) |