Tuesday, December 4, 2007

VBScript: Ban IPs in IIS Programatically

VBScript: Ban IPs in IIS Programatically

I used the following code a while back as part of a solution to automate the banning of spammers via their IP address.

'Here, we will pretend this is an imported list
Dim XMLarr(1)
XMLarr(0) = "65.19.238.21"
XMLarr(1) = "198.31.175.100"
  Set objIIS = GetObject("IIS://localhost/w3svc") 'careful, this sets IPDENY on every child web
  Set IISipsec = objIIS.IPSecurity
    If (IISipsec.GrantByDefault = True) Then
      arrIP = IISipsec.IPDeny
      arrIPSize = ubound(arrIP)
        For i = 0 to arrIPSize
        arrIPstring = arrIPstring & "," & arrIP(i) 'going to use for dupes later.
        Next
          ReDim preserve arrIP(arrIPSize + UBound(XMLarr)+1)
            for i = 0 to UBound(XMLarr)
            myNum = arrIPSize + i+1
              If InStr(arrIPstring,XMLarr(i)) = 0 Then
                arrIP(myNum) = XMLarr(i)
              End If
            Next
          IISipsec.IPDeny = arrIP
          objIIS.IPSecurity = IISipsec
          objIIS.SetInfo
    End If
  Set IISipsec = nothing
  Set objIIS = nothing