不论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. zabbix 布署实践【2 agent安装】

    客户端的安装相对较为简单,主要是更新它的repo源   以CentOS7为例 rpm -ivh http://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabb ...

  2. Angular2 + NativeScript 跨平台开发笔记(一)

    NativeScript 是一款跟 ReactNative 对着怼的移动开发技术,其官方钦定了 Angular2 作为推荐的技术框架,那么如何让在浏览器中运行的 Angular2 Web app 项目 ...

  3. PHP文件上传预览

    主页 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...

  4. flex 4 datagrid 奇偶行颜色设置

    <s:DataGrid width="100%" height="100%" alternatingRowColors="[#ffFFff,#e ...

  5. Qt 学习笔记

    继承自QObject 的Qt类都具有支持信号和槽的能力,并且在子类的实现代码中直接使用connect()函数 pwdLineEdit->setEchoMode(QLineEdit::Passwo ...

  6. Ansible Lookup

    1.  文件内容的读取 --- - hosts: all vars: contents: "{{ lookup('file', '/etc/foo.txt') }}" tasks: ...

  7. web前端-雅虎34条规则优化

    1.尽量减少HTTP请求次数      终端用户响应的时间中,有80%用于下载各项内容.这部分时间包括下载页面中的图像.样式表.脚本.Flash等.通过减少页面中的元素可以减少HTTP请求的次数.这是 ...

  8. 寒假学干货之------ 初学者关于fragment_main(碎片的困扰)

    我们在activity_main中编写的框架,会被fragment_main中的取代掉,是因为新版的ADT为了配合平板Android3.0开发 起作用的代码在MainActivity.java中 pa ...

  9. php生成图片缩略图的类方法

    //php生成缩略图片的类 class ResizeImage{ public $type;//图片类型 public $width;//实际宽度 public $height;//实际高度 publ ...

  10. CentOS 7.0 64位 安装TFTP

    1.安装tftp软件# yum -y install tftp*2.修改配置文件 # vi /etc/xinetd.d/tftp server_args = -s /tftpboot '加-cdisa ...