?
Solved

Locking Down IIS files and sites

Posted on 2009-06-30
2
Medium Priority
?
831 Views
Last Modified: 2012-05-07
We need assistance in building a VB Script that can set IP/Subnet restrictions on  multiple folders and/or files in IIS for our web based application. Will need to accept a list of addresses and/or subnets. It needs to work under IIS 7, 6, 5.

The current script on hand does not work in IIS7 without the IIS6 compatibility layer. Even then, running it twice will cause problems. Running the script below multiple times on IIS 4/5/6 causes no problems.

on error resume next
 
 
'SiteIndex will typically be 1, but may change where Windows SBS, Sharepoint, or Intranets are pre-existing
  'Running against wrong site will typically be benign as the script is looking for very specific names
  'If they are not there, not too much happens! 
  'You should also see that the "The following IP addresses are now Allowed:" will show nothing, indicating failure.
  'The 'ListSiteIndex' subroutine will show the current setup and will help if '1' is not working for you
    
 
'ToFiles array is for individual files and is geared towards locking individual files to local/admin access only
'ToFolders array is suited to locking the whole virtual folder to just the CU subnet(s)
'Any IP/Subnet not specified will be DENIED
  'For just an IP use ("nnn.nnn.nnn.nnn") 
  'For Subnet, ("nnn.nnn.nnn.nnn,nnn.nnn.nnn.nnn")
  'Adding Multiple entries is ok, and you can mix IP and Subnets together like ("127.0.0.1","192.168.5.0,255.255.255.0")
 
 
'Start - Edit Name/Index/IPs as required. *************************************************************************************************
'Calling from command line will override these values if specified.
 
  strServerName = "localhost" 
  strSiteIndex = "1"
   
  arrGrantTheseIPs_ToFiles = Array("127.0.0.1")
  arrGrantTheseIPs_ToFolders = Array("127.0.0.1","192.168.5.0,255.255.255.0")
  
'End - Edit Name/Index/IPs ****************************************************************************************************************
 
 
 
'Do not edit! *****************************************************************************************************************************
 
  strSiteRoot = "IIS://" & strServerName & "/W3SVC" & "/" & strSiteIndex & "/ROOT"
  ListSiteIndex
  wscript.echo vbTab & "Current Site Index: " & strSiteIndex
  
'/Do not edit! ****************************************************************************************************************************
 
 
'Grab argument from command line and overrride settings embedded in file
 
 
IF WScript.Arguments.Named("grantFiles") <> "" THEN
                   tgf=WScript.Arguments.Named("grantFiles")
                   wscript.echo vbCRLF & "Granting IP/Networks " & tgf & " to config files"
                   arrGrantTheseIPs_ToFiles = Array(tgf)
END IF
 
IF WScript.Arguments.Named("grantFolders") <> "" THEN
                   tgfl=WScript.Arguments.Named("grantFolders")
                   wscript.echo vbCRLF & "Granting IP/Networks " & tgfl & " to user facing folders"
                   arrGrantTheseIPs_ToFiles = Array(tgfl)
END IF
 
 
 
      'Create a config block for *each* config file/folder that needs ip restrictions
      'Use the examples below for guidence - then copy/paste and adapt the block to the desired configuration
      'When using the examples, ensure you uncomment the lines between Start and End!
      'The only value(s) to edit/replace are indicated with *xxxxxx*
 
 
 
      'Start Folder IP Restrict Definition Block EXAMPLE----------------------------------------------------------
        'strFolderPath = strSiteRoot & "/*theFoldername*"
        'strConfigFilePath = strFolderPath & "/" & strConfigFileName
        'wscript.echo vbCRLF & "Setting permissions on: " & vbCRLF & strFolderPath
        '    SetFolderIPSec
      'End Folder IP Restrict Definition Block EXAMPLE------------------------------------------------------------
 
 
          'Start File IP Restrict Definition Block EXAMPLE------------------------------------------------------------
            'strConfigFileName = "*thefile.extension*"
            'strFolderPath = strSiteRoot & "/*theFoldername*"
            'strConfigFilePath = strFolderPath & "/" & strConfigFileName
            'wscript.echo vbCRLF & "Setting permissions on: " & vbCRLF & strConfigFilePath
            '    CreateFile
            '    SetFileIPSec
          'End File IP Restrict Definition Block EXAMPLE--------------------------------------------------------------
 
 
 
 
'Edit below here! *************************************************************************************************************************
 
      'Start Folder IP Restrict Definition Block------------------------------------------------------------------
        strFolderPath = strSiteRoot & "/CDPManageWebService"
        strConfigFilePath = strFolderPath & "/" & strConfigFileName
        wscript.echo vbCRLF & "Setting permissions on: " & vbCRLF & strFolderPath
            SetFolderIPSec
      'End Folder IP Restrict Definition Block--------------------------------------------------------------------
 
 
      'Start Folder IP Restrict Definition Block------------------------------------------------------------------
        strFolderPath = strSiteRoot & "/CDPManageWebServiceMega"
        strConfigFilePath = strFolderPath & "/" & strConfigFileName
        wscript.echo vbCRLF & "Setting permissions on: " & vbCRLF & strFolderPath
            SetFolderIPSec
      'End Folder IP Restrict Definition Block--------------------------------------------------------------------
 
 
          'Start File IP Restrict Definition Block -------------------------------------------------------------------
            strConfigFileName = "configuration.aspx"
            strFolderPath = strSiteRoot & "/CDPManageWebServiceMega"
            strConfigFilePath = strFolderPath & "/" & strConfigFileName
            wscript.echo vbCRLF & "Setting permissions on: " & vbCRLF & strConfigFilePath
                CreateFile
                SetFileIPSec
          'End File IP Restrict Definition Block ---------------------------------------------------------------------
 
 
      'Start Folder IP Restrict Definition Block------------------------------------------------------------------
        strFolderPath = strSiteRoot & "/CDPManageDocumentSearch"
        strConfigFilePath = strFolderPath & "/" & strConfigFileName
        wscript.echo vbCRLF & "Setting permissions on: " & vbCRLF & strFolderPath
            SetFolderIPSec
      'End Folder IP Restrict Definition Block--------------------------------------------------------------------
 
 
      'Start Folder IP Restrict Definition Block------------------------------------------------------------------
        strFolderPath = strSiteRoot & "/CDPCentralWebService"
        strConfigFilePath = strFolderPath & "/" & strConfigFileName
        wscript.echo vbCRLF & "Setting permissions on: " & vbCRLF & strFolderPath
            SetFolderIPSec
      'End Folder IP Restrict Definition Block--------------------------------------------------------------------
 
 
          'Start File IP Restrict Definition Block -------------------------------------------------------------------
            strConfigFileName = "configuration.aspx"
            strFolderPath = strSiteRoot & "/CDPCentralWebService"
            strConfigFilePath = strFolderPath & "/" & strConfigFileName
            wscript.echo vbCRLF & "Setting permissions on: " & vbCRLF & strConfigFilePath
                CreateFile
                SetFileIPSec
          'End File IP Restrict Definition Block ---------------------------------------------------------------------
 
 
      'Start Folder IP Restrict Definition Block------------------------------------------------------------------
        strFolderPath = strSiteRoot & "/CDPCentralWebServiceMega"
        strConfigFilePath = strFolderPath & "/" & strConfigFileName
        wscript.echo vbCRLF & "Setting permissions on: " & vbCRLF & strFolderPath
            SetFolderIPSec
      'End Folder IP Restrict Definition Block--------------------------------------------------------------------
 
 
          'Start File IP Restrict Definition Block -------------------------------------------------------------------
            strConfigFileName = "configuration.aspx"
            strFolderPath = strSiteRoot & "/CDPCentralWebServiceMega"
            strConfigFilePath = strFolderPath & "/" & strConfigFileName
            wscript.echo vbCRLF & "Setting permissions on: " & vbCRLF & strConfigFilePath
                CreateFile
                SetFileIPSec
          'End File IP Restrict Definition Block ---------------------------------------------------------------------
 
 
      'Start Folder IP Restrict Definition Block------------------------------------------------------------------
        strFolderPath = strSiteRoot & "/CDPTellerWebInquiry"
        strConfigFilePath = strFolderPath & "/" & strConfigFileName
        wscript.echo vbCRLF & "Setting permissions on: " & vbCRLF & strFolderPath
            SetFolderIPSec
      'End Folder IP Restrict Definition Block--------------------------------------------------------------------
 
 
          'Start File IP Restrict Definition Block -------------------------------------------------------------------
            strConfigFileName = "configuration.aspx"
            strFolderPath = strSiteRoot & "/CDPTellerWebInquiry"
            strConfigFilePath = strFolderPath & "/" & strConfigFileName
            wscript.echo vbCRLF & "Setting permissions on: " & vbCRLF & strConfigFilePath
                CreateFile
                SetFileIPSec
          'End File IP Restrict Definition Block ---------------------------------------------------------------------
 
 
      'Start Folder IP Restrict Definition Block------------------------------------------------------------------
        strFolderPath = strSiteRoot & "/CDPCentralWebService3_98"
        strConfigFilePath = strFolderPath & "/" & strConfigFileName
        wscript.echo vbCRLF & "Setting permissions on: " & vbCRLF & strFolderPath
            SetFolderIPSec
      'End Folder IP Restrict Definition Block--------------------------------------------------------------------
 
 
          'Start File IP Restrict Definition Block -------------------------------------------------------------------
            strConfigFileName = "configuration.aspx"
            strFolderPath = strSiteRoot & "/CDPCentralWebService3_98"
            strConfigFilePath = strFolderPath & "/" & strConfigFileName
            wscript.echo vbCRLF & "Setting permissions on: " & vbCRLF & strConfigFilePath
                CreateFile
                SetFileIPSec
          'End File IP Restrict Definition Block ---------------------------------------------------------------------
 
 
 
'No Edits below here! *********************************************************************************************************************
 
 
 
Sub CreateFile
 
  Set objFolder = GetObject(strFolderPath)
  set objCreateConfigFile = objFolder.Create("IISWebFile",strConfigFileName)
      objCreateConfigFile.SetInfo
 
      Set objFolder = Nothing
      set objCreateConfigFile = Nothing
    
End Sub
 
 
 
Sub SetFileIPSec
 
  Set nub_object = GetObject(strConfigFilePath)
  Set objIPRestrict = nub_object.IPSecurity
  objIPRestrict.GrantByDefault = False
  'Leaving these in for flexibility - reverses things to allow all EXCEPT the addresses indicated
  'objIPRestrict.GrantByDefault = True
  'objIPRestrict.IPDeny = arrDenyTheseIPs_ToFiles
  objIPRestrict.IPGrant = arrGrantTheseIPs_ToFiles
  nub_object.IPSecurity = objIPRestrict
  nub_object.SetInfo
 
  WScript.Echo vbTab & "The following IP addresses are now Allowed:"
  arrGrantedIPs = objIPRestrict.IPGrant
  for i = 0 to Ubound(arrGrantedIPs)
    WScript.Echo vbTab & arrGrantedIPs(i)
  next
  
  WScript.Echo vbTab & "ALL OTHER ADDRESSES ARE DENIED!!!"
  
    Set nub_object = Nothing
    Set objIPRestrict = Nothing
    strConfigFileName = Nothing
    strFolderPath = Nothing
    strConfigFilePath = Nothing
  
End Sub
 
 
 
Sub SetFolderIPSec
 
  Set nub_object = GetObject(strFolderPath)
  Set objIPRestrict = nub_object.IPSecurity
  objIPRestrict.GrantByDefault = False
  'Leaving these in for flexibility - reverses things to allow all EXCEPT the addresses indicated
  'objIPRestrict.GrantByDefault = True
  'objIPRestrict.IPDeny = arrDenyTheseIPs_ToFolders
  objIPRestrict.IPGrant = arrGrantTheseIPs_ToFolders
  nub_object.IPSecurity = objIPRestrict
  nub_object.SetInfo
 
  WScript.Echo vbTab & "The following IP addresses are now Allowed:"
  arrGrantedIPs = objIPRestrict.IPGrant
  for i = 0 to Ubound(arrGrantedIPs)
    WScript.Echo vbTab & arrGrantedIPs(i)
  next
  
    WScript.Echo vbTab & "ALL OTHER ADDRESSES ARE DENIED!!!"
  
    Set nub_object = Nothing
    Set objIPRestrict = Nothing
    strConfigFileName = Nothing
    strFolderPath = Nothing
    strConfigFilePath = Nothing
  
End Sub
 
 
 
Sub ListSiteIndex
 
  Dim parent_object 
  Dim child_object 
  Dim allsites 
 
  IISPath = "IIS://" & strServerName & "/W3SVC"
  Set parent_object = GetObject(IISPath)
  For each child_object in parent_object 
      If IsNumeric(child_object.Name) then
        Wscript.echo "Site Index ID: " & child_object.Name & vbTab                     & "ServerComment: " & child_object.ServerComment 
      End If
  Next
  
    IISPath = Nothing
    parent_object = Nothing
    child_object = Nothing
    allsites = Nothing
  
End Sub

Open in new window

0
Comment
Question by:carswelldp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 37

Assisted Solution

by:meverest
meverest earned 2000 total points
ID: 24755696
Hi,

although WMI is supposed to be supported on IIS7, it tends to be problematic in some cases - as you have apparently discovered.

For IIS7 config, it is a better idea to use appCmd.exe to apply configuration changes, or to use httpmodule support using c# or other managed code.

for appcmd.exe see:  http://learn.iis.net/page.aspx/114/getting-started-with-appcmdexe/

for httpmodules see: http://learn.iis.net/page.aspx/241/configuration-extensibility/

cheers.
0
 
LVL 1

Accepted Solution

by:
carswelldp earned 0 total points
ID: 24798325
This has led us into the proper direction we believe.  This is something that we are going to explore in greater detail.  

cheers!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is an ISAPI filter?   •      It's an assembly (.dll file) that can add or change the way IIS works.   •      They can be enabled globally for your web server or on a site-by-site basis.   When the IIS server receives a request, enabling the ISAPI fi…
Lync server 2013 or Skype for business Backup Service Error ID 4049 – After File Share Migration
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question