Solved

Restrict IIS websites by IP

Posted on 2013-01-27
2
968 Views
Last Modified: 2013-02-27
HI

Running Windows 2008 Server/ IIS 7. I need to restrict access to a particular site from the web server - the restriction should be based on IP. There are about 30 IP addresses only we want to allow, they are in a file named IP_Safe.csv

I've seen this article here: http://www.hrzdata.com/node/46

Which shows how to do this manually.

But we have about 30 servers, is there any way to automate this via Powershell?
0
Comment
Question by:neil4933
2 Comments
 
LVL 28

Expert Comment

by:becraig
ID: 38824221
Here is a quick how to :


http://technet.microsoft.com/en-us/library/cc733090%28v=ws.10%29.aspx


You can simply loop the command indicated in a powershell script.
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 38824260
in your csv file, have each ip in different line, without headers
create servers.txt file contains all server names in each line.

cls
[Object[]] $ipArray = @()
gc c:\temp\IP_Safe.csv | %{
	$ipArray += $_
}

gc c:\temp\servers.txt | %{
	$server = $_
	
	#get iis object
	$iisObject = new-object System.DirectoryServices.DirectoryEntry("IIS://" + $server + "/w3svc/1/root")
	
	#get iis security property object
	$ipSec = $iisObject.Properties["IPSecurity"].Value
	
	#update binding flags to GetProperty
	$bindingFlags = [Reflection.BindingFlags] "Public, Instance, GetProperty"
	
	#get currently deny list
	$ipList = $ipSec.GetType().InvokeMember("IPDeny", $bindingFlags, $null, $ipSec, $null);
	
	#add to new ip list
	$ipArray += , $ipList
	
	#update binding flags to SetProperty
	$bindingFlags = [Reflection.BindingFlags] "Public, Instance, SetProperty"
	
	#update deny ip list
	$ipList = $ipSec.GetType().InvokeMember("IPDeny", $bindingFlags, $null, $ipSec, $ipArray);
	
	#set iis security property object
	$iisObject.Properties["IPSecurity"].Value = $ipSec
	
	#commit changes
	$iisObject.CommitChanges()
}

Open in new window


based on this post:
Using Powershell to Manipulate IP Restrictions on IIsWebVirtualDir
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This tutorial will show how to push an installation of Backup Exec to an additional server in both 2012 and 2014 versions of the software. Click on the Backup Exec button in the upper left corner. From here, select Installation and Licensing, then I…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now