不论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. ModelState.IsValid一直为false的原因

    一,问题:ModelState.IsValid一直为false 二,解决方法和原因, 由于这个方法中传过来的RegisterForm模型的字段,某一个为空值,则会造成这个验证验证为false,去注释掉 ...

  2. 《css揭秘》

    <css揭秘> 第一章:引言 引言 案例们 第二章:背景与边框 背景和边框 半透明边框(rgba/hsla.background-clip) 多重边框(box-shadow) 灵活的背景定 ...

  3. 类型“System.Data.Objects.DataClasses.EntityObject”在未被引用的程序集中定义。

    说明: 在编译向该请求提供服务所需资源的过程中出现错误.请检查下列特定错误详细信息并适当地修改源代码. 编译器错误消息: CS0012: 类型“System.Data.Objects.DataClas ...

  4. java装饰模式

    在java的IO中就是运用装饰模式设计的.一层装饰一层 如:DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(n ...

  5. Error(10028)

    两个进程里都有同一个条件判断的话,会产生并行信号冲突的问题. 同一个信号不允许在多个进程中赋值,否则则为多驱动. 进程的并行性决定了多进程不同能对同一个对象进行赋值.

  6. i++与++i区别使用

    中心:使用变量自增或者处减使用尽量使用++i与--i,而不要去使用i++与i-- ++i与--i可以做左值也可以做右值,而i++与i--只能做右值. 对于基础类型i++与++i结果相同,但是i++肯定 ...

  7. python 学习 [day7]面向对象

    编程模式概念 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发“更快更好更强...” 面向对象的三大 ...

  8. python生产者消费者模型

    业界用的比较广泛,多线程之间进行同步数据的方法,解决线程之间堵塞,互相不影响. server --> 生产者 client --> 消费者 在一个程序中实现又有生产者又有消费者 ,生产者不 ...

  9. 利用StringBuffer向字符串特定的重复子字符串插入数据

    public class InsertDetail {    public void insertInvoiceDetail(StringBuffer sb, String Label, String ...

  10. C# WebBrowser禁止F5刷新

    在用写一个桌面软件的过程中,用到webbrowser实现界面.这时有一个禁止webbrowser通过f5按键进行刷新的要求.本着边做边学的原则,本菜查了一下百度,原来这么简单,代码如下: this.w ...