动态封杀与解封IP
不论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的更多相关文章
- Linux防火墙:iptables禁IP与解封IP常用命令
在Linux服务器被攻击的时候,有的时候会有几个主力IP.如果能拒绝掉这几个IP的攻击的话,会大大减轻服务器的压力,说不定服务器就能恢复正常了. 在Linux下封停IP,有封杀网段和封杀单个IP两种形 ...
- python + redis +ipset实现IP黑名单的动态添加及解封禁
1.抽空用python做了一个 动态添加/删除IP黑名单 的程序(或者说实现方案),项目地址: https://gitee.com/lowmanisbusy/ip_blacklists, 2.这里的实 ...
- 服务器放在不同省份的IDC机房,数据如何同步?一个域名如何动态解析到不同IP的服务器
服务器放在不同省份的IDC机房,数据如何同步?淘宝的做法是不同IDC机房之间拉光纤,异地容灾和性能无关,异地容灾是应对断电.地震这种不可抗拒因素的 同城分流,异地容灾 是什么意思?你比如说公司的业务涉 ...
- 阿里云CentOS使用iptables禁止某IP访问
在CentOS下封停IP,有封杀网段和封杀单个IP两种形式.一般来说,现在的攻击者不会使用一个网段的IP来攻击(太招摇了),IP一般都是散列的.于是下面就详细说明一下封杀单个IP的命令,和解封单个IP ...
- 虚拟IP(VIP)
高可用性HA(High Availability)指的是通过尽量缩短因日常维护操作(计划)和突发的系统崩溃(非计划)所导致的停机时间,以提高系统和应用的可用性.HA系统是目前企业防止核心计算机系统因故 ...
- [Linux] - 攻击查看与IP查封
查看80端口的连接数 netstat -nat|grep -i "|wc -l 查看Nginx的日志命令 tail -f access.log 统计IP访问数 awk '{aaa[$1]++ ...
- 9、FTP封杀用户、限制传输速率、限制访问目录、为匿名用户提供下载资源
一.封杀某些用户访问FTP服务器 例如 封杀 yanji [root@localhost root]# vi /etc/vsftpd.ftpusers (用户控制配置文件,主要用于限 ...
- TCP/IP协议详解 卷1—读书笔记(1)
0. 前言 本系列简要记录该书的关键点,用以梳理知识点. 1. 简介 简述链路层下的一些相关协议,如以太网IP数据报,802标准,SLIP,CSLIP,PPP. 链路层主要为上层(IP)和本层(ARP ...
- Linux-eth0 eth0:1 和eth0.1关系、ifconfig以及虚拟IP实现介绍
eth0 eth0:1 和eth0.1三者的关系对应于物理网卡.子网卡.虚拟VLAN网卡的关系:物理网卡:物理网卡这里指的是服务器上实际的网络接口设备,这里我服务器上双网卡,在系统中看到的2个物理网卡 ...
随机推荐
- STM32驱动ht1621b显示LCD
这几天在写ht1621b显示LCD的程序,主芯片是Stm32f10的芯片.对于stm32和ht1621b的运用和操作本人是新手,属于赶鸭子上架,通过查看datasheet等资料和网上查看前人写的程序终 ...
- java中使用数组和链表简单实现SJBMap
import java.util.LinkedList; public class SJBMap { private Object[] elementData; private int size; p ...
- Tomcat多域名的配置
有时候我们有好几个项目需要发布在同一个tomcat服务器上,每个项目有不同的域名.这就需要在tomcat里配置多域名,添加多个虚拟主机. 主要在server.xml里面设置: 在<Engine& ...
- HttpGet和HttpPost的区别
HttpGet和HttpPost的区别总结就是下面这样: Get一般用于从服务器取数据,而且不改变原来的内容: Post一般用于向服务器传递数据,这需要改变服务器的内容. 从安全性上考虑,Get的安全 ...
- Flexslider图片轮播、文字图片相结合滑动切换效果
Flexslider是一款基于的jQuery内容滚动插件.它能让你轻松的创建内容滚动的效果,具有非常高的可定制性.开发者可以使用Flexslider轻松创建各种图片轮播效果.焦点图效果.图文混排滚动效 ...
- 微信小程序文档解读(一)--api提供支持有哪些
本文重点在于小程序API提供的微信功能支持及获取用户信息的解读,具体的用法和调用不在本文讨论范围之内,文章基于20161222版文档解读 API官方文档原文链接 小程序API官方定义: 框架提供丰富的 ...
- uiwebview 加载html时字体变小 加载前或加载后改变字体大小
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #6122ae } p.p2 { margin: 0.0px 0. ...
- Python学习之旅--第二周--python基础
一.什么是pyc? 1.Python是解释性语言,那么.pyc是什么文件? 2.解释性语言和编译型语言区别: 计算机是不能够识别高级语言的,所以当我们运行一个高级别语言程序时,就需要一个&quo ...
- js构造函数的完美继承(欢迎吐槽)
function Animal(){ //定义父类 this.leibie="动物"; } Animal.prototype.test1=[1,2]; function Cat(n ...
- opencv BP神经网络使用过程
1.OpenCV中的神经网络 OpenCV中封装了类CvANN_MLP,因而神经网络利用很方便. 首先构建一个网络模型: CvANN_MLP ann; Mat structu ...