Web Connection
Blocking IIS IP Addresses with Foxpro
Gravatar is a globally recognized avatar based on your email address. Blocking IIS IP Addresses with Foxpro
  Marcel DESMET
  All
  May 12, 2020 @ 02:25am

Hello, I lose a couple of hours to found how to automate IpSecurity with Fox, here is the code ( Tested with Windows 10 and local Server ). Code is ok just want to share it, you have tu run Foxpro as Administrator

LOCAL loAdminManager,loIpSecuritySection,loAddElement,lnI,loElement 

loAdminManager = CREATEOBJECT('Microsoft.ApplicationHost.WritableAdminManager')

loAdminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"

loIpSecuritySection = loAdminManager.GetAdminSection("system.webServer/security/ipSecurity", "MACHINE/WEBROOT/APPHOST/Default Web Site")
loIpSecurityCollection = loIpSecuritySection.Collection


*-* Adding to the collection 
loAddElement = loIpSecurityCollection.CreateNewElement("add")
loAddElement.Properties.Item("ipAddress").Value = "192.168.100.1"
loAddElement.Properties.Item("allowed").Value = .F.
loIpSecurityCollection.AddElement(loAddElement)

loAddElement1 = loIpSecurityCollection.CreateNewElement("add")
loAddElement1.Properties.Item("ipAddress").Value = "169.254.0.0"
loAddElement1.Properties.Item("subnetMask").Value = "255.255.0.0"
loAddElement1.Properties.Item("allowed").Value = .F.
loIpSecurityCollection.AddElement(loAddElement1)
loAdminManager.CommitChanges()

*-* List the collection 
? "* List the collection "
FOR lnI=1 TO loIpSecurityCollection.Count
  loElement = loIpSecurityCollection.Item(lnI-1)
  ? loElement.GetPropertyByName("ipAddress").Value
ENDFOR 

*-* Remove from the collection  
loAddElement = loIpSecurityCollection.CreateNewElement("remove")
loAddElement.Properties.Item("ipAddress").Value = "192.168.100.1"
loIpSecurityCollection.AddElement(loAddElement)

loAdminManager.CommitChanges()

? "* Remove 192.168.100.1 and list the collection "
FOR lnI = 1 TO loIpSecurityCollection.Count
  loElement = loIpSecurityCollection.Item(lnI-1)
  ? loElement.GetPropertyByName("ipAddress").Value
ENDFOR 


*-* Clear the collection 
loIpSecurityCollection.Clear()
loAdminManager.CommitChanges()

? "* Print the collection count after Clear "
? loIpSecurityCollection.Count

RETURN

Gravatar is a globally recognized avatar based on your email address. re: Blocking IIS IP Addresses with Foxpro
  Rick Strahl
  Marcel DESMET
  May 12, 2020 @ 02:05pm

Thanks for posting.

I've done this myself many years ago directly in IIS with ASP.NET.

Blocking IIS IP Address with ASP.NET

That code is somewhat generic - you provide a list of strings and it'll block any you provide so you can have a text box on a form to show blocked IPs and then modify the list.

Never worked really well though because these days any hack attempts tend to come from bots that are constantly switching IP addresses - any blockage's effect tends to be short lived. The only things that really work for blocking are allowed lists (white listing) in my experience. For other things I think it's more important to have effective measuers that make pages unparseable by bots or put them behind authentication.

+++ Rick ---

© 1996-2020