Create a directory called App_Code under your ScreenConnect install. Put this HTTP module in it called IPv4SecurityModule.cs:
Code:using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Net;
public class IPv4SecurityModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += delegate
{
var restrictIPs = ConfigurationManager.AppSettings["RestrictIPs"];
if (restrictIPs != null)
{
var restrictIPsParts = restrictIPs.Split(new[] { ' ', ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
var restrictIPAddresses = new uint[restrictIPsParts.Length];
var restrictIPShifts = new int[restrictIPsParts.Length];
for (var i = 0; i < restrictIPsParts.Length; i++)
{
var parts = restrictIPsParts[i].Split('/');
restrictIPAddresses[i] = this.GetIPInt(parts[0]);
restrictIPShifts[i] = (parts.Length > 1 ? 32 - int.Parse(parts[1]) : 0);
}
if (!this.IsAllowed(restrictIPAddresses, restrictIPShifts, context.Request.UserHostAddress))
{
context.Response.StatusCode = 403;
context.Response.End();
}
}
};
}
bool IsAllowed(uint[] restrictIPAddresses, int[] restrictIPShifts, string userHostAddress)
{
var userHostIPInt = this.GetIPInt(userHostAddress);
for (var i = 0; i < restrictIPAddresses.Length; i++)
if (userHostIPInt >> restrictIPShifts[i] == restrictIPAddresses[i] >> restrictIPShifts[i])
return true;
return false;
}
uint GetIPInt(string ipString)
{
var ip = IPAddress.Parse(ipString);
var ipBytes = ip.GetAddressBytes();
return BitConverter.ToUInt32(ipBytes, 0);
}
public void Dispose()
{
}
}
In your web.config you'll need to add it to the httpModules to activate it:
Code: <add name="CompressionModule" type="Elsinore.ScreenConnect.CompressionModule, Elsinore.ScreenConnect.Web" />
<add name="IPv4SecurityModule" type="IPv4SecurityModule" />
</httpModules>
Then for each area you want protected, add a location section to your web.config:
Code:<configuration>
<location path="Host.aspx">
<appSettings>
<add key="RestrictIPs" value="192.168.2.140/24"/>
</appSettings>
</location>
<location path="Administration.aspx">
<appSettings>
<add key="RestrictIPs" value="192.168.2.0/24 192.168.2.140/32"/>
</appSettings>
</location>
<system.web>
You'll need to use the IP address with slash notation. Multiple entries can be separated with a space.