Tuesday, January 2. 2007FTP autoban script for IIS
I host an FTP server using IIS on my home network. One thing that I found extremely annoying was constant probing by internet hackers trying to break into my ftp server by guessing passwords. It was bad enough that they were trying to break in in the first place, but the sheer number of attempts was filling up both my ftp logs as well as my server eventlogs. It got so bad that I decided to do something about it. I looked on the internet and found some code that waited for an event to be written to the system log, then if someone tried logging onto the administrator account it banned the ip address. It was a nice piece of code but not exactly what I was looking for. The hackers didn't always attack the administrator account - in fact most of the time they tried other accounts: test, admin, joe, user, guest, etc. What I wanted was a script that checked for a certain number of failed logins in a time period and when it exceeded say 5 bad passwords in a 10 second interval to ban the ip address. I couldn't find that exact code on the net so I decided to modify the code that was there to do what I wanted.
Here is a link to the original script: http://blog.netnerds.net/2006/07/iis-instantly-ban-ips-attempting-to-login-to-ms-ftp-as-administrator/ The original script looks at the eventlogs and that is a neat way to do it, since you can hook into the eventlog and execute an action based on an event. In my case, however I wanted to look for a specific case of password denied error status repeated 5 times in 10 seconds. To do this I needed to look directly at the FTP log files. I parse each line in the logfile looking for bad password attempts by grepping out lines containing the string "PASS - - 530". I maintain a running count of bad password attempts per ip address using a Scripting Dictionary Object, a type of array that allows you to use arbitrary strings as a key index. In this case, I used the IP address as the index of counts. When I am done counting I cycle through the collection of IP address and look for any counts greater than 5 and ban them. The nice thing about creating the script this way is that I can point it at old logfiles in my log directory and it will ban all IP's from failed attacks in the past. The bad thing is that it forces the script to digest the entire logfile with each iteration. Since I plan to run this script every 30 seconds, this can be a lot of extra cpu cycles wasted. To help mitigate this effect I decided to make the process more efficient by only running my script against changes in the most recent logfile since last time the script was run. To do this I created a batch file to execute my vbs script. ###---autobanftp.bat---###
###---autobanftp.vbs---###
Comments
Display comments as
(Linear | Threaded)
I would like to use it on W2K Server. Do I need to make any changes/customizations it either the vbs or bat files. Does it matter where on the C drive I will put the two files?
Thx, Katrin
You can place the scripts anywhere you like, as long as they are both in the same directory. Please note, I am assuming you are using the the first default FTP site and have not changed the default logfile location.
I copied these scripts and when I execute the .vbs script it tells me I must supply a filename, what does this mean? How can this be fixed? I'm new to scripting so I don't use it that much.
The vbs is run with an IIS logfile as a parameter. I should probably have a more descriptive error message. There is a batch file I use for feeding the latest log entries to the vbs script, but you can also run it against old logfiles to pull out and ban bad addresses from the past.
I cannot get this to work on our Win2k3 server. It enters the route line and adds the IP address to IP Security as denied but people can still ftp to the box (I'm testing from an outsite IP address)... Help!!
I just found another script that directly modifies the directory security tab on the properties of the ftp server. If you're configured to allow access to all, then deny the listed addresses, this script will probably suit you. I think it speaks for itself. I added an event create so that a message gets logged to the Application log so that Nagios/Netiq/SCOM can filter and notify (yeah I know you can blat directly from the script too...just trying to maximize our enterprise monitoring software investment)
Anyway here it is - you'll recognize the first half - don't ask what everything does..I just know after a little trial and error I got it to work: Const ForReading = 1 Dim ips dim objIpServer dim objIPSec dim arHostsRead dim nCounter dim IpToaddStr dim tmpStr Set counts = CreateObject("Scripting.Dictionary") Set objFSOstats = CreateObject("Scripting.FileSystemObject") Set objFTPSVC = GetObject("IIS://localhost/MSFTPSVC") Set objFTPIPSec = objFTPSVC.IPSecurity If Wscript.Arguments.Count > 0 Then strFlag = Wscript.arguments.Item(0) End If If IsEmpty(strFlag) Then 'No arguments have been received wscript.echo "Must supply an filename" wscript.quit End If Set objTextFile = objFSOstats.OpenTextFile(strflag, ForReading) 'Read each line and count the number for badpass login attempts per IP i=0 Do Until objTextFile.AtEndOfStream i=i+1 logline = objTextFile.Readline If InStr(logline,"PASS - 530") Then parsevar=split(logline) If not counts.exists(parsevar(1)) Then counts.add parsevar(1),1 wscript.echo "*****" wscript.echo "New IP: " & parsevar(1) Else counts(parsevar(1))=counts(parsevar(1))+1 End If End If Loop wscript.echo ip For each ip in Counts.keys tempd=date() tempt=Time() wscript.echo tempd,tempt wscript.echo "Counted " & counts(ip) & " failed logons on IP: " & ip If counts(ip) > 5 Then 'Kill the route to the machine then add it to the array of banned IPs. wscript.echo "Killing route for ip: " & ip Set WshShell = WScript.CreateObject("WScript.Shell") 'WshShell.Run "ROUTE ADD " & ip & " MASK 255.255.255.255 192.168.1.2", 1, True 'Set WshShell = Nothing IpToaddStr=ip & "," & " 255.255.255.0" wscript.echo "setting" & IpToaddStr WshShell.Run "C:\WINDOWS\system32\eventcreate.exe /L Application /ID 198 /T INFORMATION /D ""User from " & IpToaddStr & " has triggered a hack warning and has been blocked. If you wish to unblock connections from this IP address, please log on to the IIS Manager for this ftp server, right click on the server under the FTP sites, and from the properties menu, select the Directory Security Tab and remove the ip from the list""" set objIpServer=GetObject("IIS://localhost/MSFTPSVC/1/root") objIpServer.getinfo wscript.echo "Got the IP list object" SET objIPSec = objIpServer.get("IPSecurity") wscript.echo "Got IP List" arHostsRead=objIPSec.IPDeny for nCounter=0 to ubound(arHostsRead) tmpStr = arHostsRead(nCounter) wscript.echo tmpStr if tmpStr=IpToAddStr then wscript.echo "The new record is already in the IP list" wscript.echo "No need to add" wscript.echo "Program exits" wscript.quit return end if next redim preserve arHostsRead (UBound(arHostsRead)+1) arHostsRead (UBound(arHostsRead)) = IpToAddStr objIPSec.ipDeny=arHostsRead objIPSec.grantbydefault=true objIpServer.put "IPSecurity" ,objIPSec wscript.echo "Setting info" objIpServer.setinfo wscript.echo "Done" End If Next |
Calendar
QuicksearchArchivesCategoriesSyndicate This BlogBlog Administration |
|||||||||||||||||||||||||||||||||||||||||||||||||

From time to time, many users asked about how to configure IIS FTP to prevent brute force or dictionary
Tracked: May 27, 01:46