不论IIS6还是IIS7 都可以把需要封杀的IP加入封杀列表。但是需要注意的是我们代码写的是全部替换原先的数据。但是在IIS7下,执行的效果是原先的不替换,新加一批封杀 IP。当然IIS7下,如果新加的IP原来就有了,则会报如下异常:

System.Runtime.InteropServices.COMException was caught 
  Message="当文件已存在时,无法创建该文件。 (异常来自 HRESULT:0x800700B7)" 
  Source="System.DirectoryServices" 
  ErrorCode=-2147024713 
  StackTrace: 
       在 System.DirectoryServices.DirectoryEntry.CommitChanges() 
       在 IIS_Security_ConsoleApplication.Program.IPDeny() 位置 D:\MyCodes\IIS_Security_ConsoleApplication \IIS_Security_ConsoleApplication\Program.cs:行号 109 
  InnerException:

这就是说,IIS7, 我们可以通过编程接口增加封杀IP名单,但是没发通过编程接口剔出封杀IP。

如果谁知道怎么在iis7下面提出限制ip的请M我 571111539@qq.com,下面代码不是很完整,自行修改

       [STAThread]
static void Main(string[] args)
{
string webName = "xiangpi";//网站名
string flag = "-a";
string denyflag = "-d";//-d黑名单 else白名单
string ipString = "192.168.0.89";//要限制的ip
string[] ipstringlist = ipString.Split(','); int objID = GetObjID(webName);
if (objID <= )
return; string objName = "IIS://localhost/W3SVC/" + objID+"/ROOT";
Console.WriteLine(objName);
try
{
DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");
foreach (DirectoryEntry dir in root.Children)
{ if (dir.SchemaClassName == "IIsWebServer")
{
string ww = dir.Properties["ServerComment"].Value.ToString(); Console.WriteLine(string.Format(dir.Path+"/ROOT/{1}", dir.Name, ww));
}
}
Console.ReadLine();
DirectoryEntry IIS = new DirectoryEntry(objName);
Type typ = IIS.Properties["IPSecurity"][].GetType();
object IPSecurity = IIS.Properties["IPSecurity"][];
Array origIPDenyList = null;
if (denyflag.Equals("-d"))//
{
//retrieve the IPDeny list from the IPSecurity object
origIPDenyList = (Array)typ.InvokeMember("IPDeny",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.GetProperty,
null, IPSecurity, null); // display what was being denied
List<string> iplist = new List<string>(); foreach (string s in origIPDenyList)
{
//iplist.Add(s);
Console.WriteLine("已有拒绝条目: " + s);
}
// check GrantByDefault. This has to be set to true,
// or what we are doing will not work. if (flag.Equals("-a"))
{
foreach (string s in ipstringlist)
{
string ipstring = s + ", 255.255.255.255";
if (!iplist.Contains(ipstring))
iplist.Add(ipstring);
}
}
else if (flag.Equals("-g"))
{
foreach (string ip in iplist)
{
Console.WriteLine(ip);
} }
else
{
foreach (string s in ipstringlist)
{
string ipstring = s + ", 255.255.255.255";
if (iplist.Contains(ipstring))
iplist.Remove(ipstring);
} } bool bGrantByDefault = (bool)typ.InvokeMember("GrantByDefault",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.GetProperty,
null, IPSecurity, null); // Console.WriteLine("GrantByDefault = " + bGrantByDefault);
if (!bGrantByDefault)
{
typ.InvokeMember("GrantByDefault",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.SetProperty,
null, IPSecurity, new object[] { true });
} object[] newIPDenyList = new object[iplist.Count];
int i = ;
foreach (string s in iplist)
{
newIPDenyList[i] = s;
i++;
} typ.InvokeMember("IPDeny",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.SetProperty,
null, IPSecurity, new object[] { ipString }); IIS.Properties["IPSecurity"][] = IPSecurity;
// Console.WriteLine("Commiting the changes."); // commit the changes
IIS.CommitChanges();
IIS.RefreshCache(); // check to see if the update took
Console.WriteLine("Checking to see if the update took.");
IPSecurity = IIS.Properties["IPSecurity"][];
Array y = (Array)typ.InvokeMember("IPDeny",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.GetProperty,
null, IPSecurity, null);
// foreach (string s in y)
// / Console.WriteLine("After: " + s);
}
else
{
origIPDenyList = (Array)typ.InvokeMember("IPGrant",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.GetProperty,
null, IPSecurity, null); // display what was being denied
List<string> iplist = new List<string>(); foreach (string s in origIPDenyList)
{
iplist.Add(s);
//Console.WriteLine("Before: " + s);
}
// check GrantByDefault. This has to be set to true,
// or what we are doing will not work. if (flag.Equals("-a"))
{
foreach (string s in ipstringlist)
{
string ipstring = s + ", 255.255.255.255";
if (!iplist.Contains(ipstring))
iplist.Add(ipstring);
}
}
else if (flag.Equals("-g"))
{
foreach (string ip in iplist)
{
Console.WriteLine(ip);
} }
else
{
foreach (string s in ipstringlist)
{
string ipstring = s + ", 255.255.255.255";
if (iplist.Contains(ipstring))
iplist.Remove(ipstring);
} } bool bGrantByDefault = (bool)typ.InvokeMember("GrantByDefault",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.GetProperty,
null, IPSecurity, null); // Console.WriteLine("GrantByDefault = " + bGrantByDefault);
if (bGrantByDefault)
{
typ.InvokeMember("GrantByDefault",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.SetProperty,
null, IPSecurity, new object[] { false });
} object[] newIPDenyList = new object[iplist.Count];
int i = ;
foreach (string s in iplist)
{
newIPDenyList[i] = s;
i++;
} typ.InvokeMember("IPGrant",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.SetProperty,
null, IPSecurity, new object[] { newIPDenyList }); IIS.Properties["IPSecurity"][] = IPSecurity;
// Console.WriteLine("Commiting the changes."); // commit the changes
IIS.CommitChanges();
IIS.RefreshCache(); // check to see if the update took
Console.WriteLine("Checking to see if the update took.");
IPSecurity = IIS.Properties["IPSecurity"][];
Array y = (Array)typ.InvokeMember("IPGrant",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.GetProperty,
null, IPSecurity, null);
}
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.ToString());
} Console.ReadLine();
}
static int GetObjID(string strWebSite)
{
string objName = strWebSite;// args[0];
//Console.WriteLine(objName);
Regex regex = new Regex(objName); string tmpStr; // string entPath = "IIS://LocalHost/W3SVC/";// String.Format("IIS://{0}/w3svc", objName);
//
DirectoryEntry ent = new DirectoryEntry("IIS://Localhost/W3SVC"); foreach (DirectoryEntry child in ent.Children)
{
//Console.WriteLine(child.Name); if (child.SchemaClassName == "IIsWebServer")
{ if (child.Properties["ServerBindings"].Value != null)
{ tmpStr = child.Properties["ServerBindings"].Value.ToString(); if (regex.Match(tmpStr).Success)
{
Console.WriteLine(child.Name);
return int.Parse(child.Name);
//return child.Name; } } if (child.Properties["ServerComment"].Value != null)
{ tmpStr = child.Properties["ServerComment"].Value.ToString(); if (regex.Match(tmpStr).Success)
{
//onsole.WriteLine(child.Properties["path"].Value);
// Console.WriteLine(child.Name);
return int.Parse(child.Name);
}
}
}
}
return -;
}

动态封杀与解封IP的更多相关文章

  1. Linux防火墙:iptables禁IP与解封IP常用命令

    在Linux服务器被攻击的时候,有的时候会有几个主力IP.如果能拒绝掉这几个IP的攻击的话,会大大减轻服务器的压力,说不定服务器就能恢复正常了. 在Linux下封停IP,有封杀网段和封杀单个IP两种形 ...

  2. python + redis +ipset实现IP黑名单的动态添加及解封禁

    1.抽空用python做了一个 动态添加/删除IP黑名单 的程序(或者说实现方案),项目地址: https://gitee.com/lowmanisbusy/ip_blacklists, 2.这里的实 ...

  3. 服务器放在不同省份的IDC机房,数据如何同步?一个域名如何动态解析到不同IP的服务器

    服务器放在不同省份的IDC机房,数据如何同步?淘宝的做法是不同IDC机房之间拉光纤,异地容灾和性能无关,异地容灾是应对断电.地震这种不可抗拒因素的 同城分流,异地容灾 是什么意思?你比如说公司的业务涉 ...

  4. 阿里云CentOS使用iptables禁止某IP访问

    在CentOS下封停IP,有封杀网段和封杀单个IP两种形式.一般来说,现在的攻击者不会使用一个网段的IP来攻击(太招摇了),IP一般都是散列的.于是下面就详细说明一下封杀单个IP的命令,和解封单个IP ...

  5. 虚拟IP(VIP)

    高可用性HA(High Availability)指的是通过尽量缩短因日常维护操作(计划)和突发的系统崩溃(非计划)所导致的停机时间,以提高系统和应用的可用性.HA系统是目前企业防止核心计算机系统因故 ...

  6. [Linux] - 攻击查看与IP查封

    查看80端口的连接数 netstat -nat|grep -i "|wc -l 查看Nginx的日志命令 tail -f access.log 统计IP访问数 awk '{aaa[$1]++ ...

  7. 9、FTP封杀用户、限制传输速率、限制访问目录、为匿名用户提供下载资源

    一.封杀某些用户访问FTP服务器 例如  封杀 yanji [root@localhost root]#   vi   /etc/vsftpd.ftpusers     (用户控制配置文件,主要用于限 ...

  8. TCP/IP协议详解 卷1—读书笔记(1)

    0. 前言 本系列简要记录该书的关键点,用以梳理知识点. 1. 简介 简述链路层下的一些相关协议,如以太网IP数据报,802标准,SLIP,CSLIP,PPP. 链路层主要为上层(IP)和本层(ARP ...

  9. Linux-eth0 eth0:1 和eth0.1关系、ifconfig以及虚拟IP实现介绍

    eth0 eth0:1 和eth0.1三者的关系对应于物理网卡.子网卡.虚拟VLAN网卡的关系:物理网卡:物理网卡这里指的是服务器上实际的网络接口设备,这里我服务器上双网卡,在系统中看到的2个物理网卡 ...

随机推荐

  1. shrio登录验证

    shiro的认证过程也就是判断用户名和密码的过程,在认证过程中,用户需要提交实体信息(用户名)(Principals)和凭据信息(密码)(Credentials)来判断用户是否合法,最常见的" ...

  2. 制作自己的web字体

    今天教给大家制作自己的web字体

  3. hdu1036

    #include<stdio.h>int main(){ int n; double d; int num; char h,m1,m2,s1,s2; scanf("%d" ...

  4. css3变形动画

    transform:变形 rotate:旋转 translate:移动 scale:缩放 skew:扭曲 一切变化都是“形变”引起的变化,所以transform就是老大,大家都围着他转 1.trans ...

  5. List<T>集合导出csv方法参考,通过增加自定义的属性控制输出的字段。

    public string CreateAdvExcel(List<GridScoreManager> lt) { StringBuilder builder = new StringBu ...

  6. JAVA读取本地配置文件实例

    import java.io.InputStream; import java.util.Properties; public class FileProperties extends Propert ...

  7. QDataStream对QVector的序列化

    最近发现QDataStream这个好东东,序列化发送数据很方便,与大家分享一下. 客户端: line.h #ifndef LINE_H #define LINE_H #include <QStr ...

  8. snmp协议接口

    所有网络设备上都会支持smap,获取服务器的基本信息,这样就不用在客户端上装应用就可以检测到基本的信息,是基于socket开发 内存调用这些命令来提取服务器的信息 snmpgetlocalhost - ...

  9. Java中泛型的理解

    Java中的泛型,本质上来说,就是是参数化类型,就是说所操作的数据类型被指定为一个参数,而不是确定的某种类型.这种数据类型可以用在类.接口和方法创建中.即泛型类.泛型接口.泛型方法.这样说可能不够生动 ...

  10. fiddler抓包使用①

    链接:http://jingyan.baidu.com/article/3a2f7c2e0d5f2126aed61175.html   设置好代理后,有的设备需要访问"192.168.1.1 ...