当前位置:编程学习 > C#/ASP.NET >>

iis 动态封杀IP


如上图,在test111.com下有许多目录和虚拟目录

DirectoryEntry IIS = new DirectoryEntry(objName);

             

Type typ = IIS.Properties["IPSecurity"][0].GetType();
object IPSecurity = IIS.Properties["IPSecurity"][0];

Array origIPDenyList = null;

//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("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);
}
return 0;
}
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 });
}

// update the list of denied IPs.  This is a 
// complete replace.  If you want to maintain what
// was already being denied, you need to make sure 
// those IPs are in here as well.  This area
// will be where you will most likely modify to
// your needs as this is just an example.
// Console.WriteLine("Updating the list of denied IPs.");
object[] newIPDenyList = new object[iplist.Count];
int i = 0;
foreach (string s in iplist)
{
newIPDenyList[i] = s;
i++;
}




//newIPDenyList[3] = "192.168.1.4, 255.255.255.255";
//  Console.WriteLine("Calling SetProperty");

// add the updated list back to the IPSecurity object
typ.InvokeMember("IPDeny",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.SetProperty,
null, IPSecurity, new object[] { newIPDenyList });

IIS.Properties["IPSecurity"][0] = IPSecurity;
//  Console.WriteLine("Commiting the changes.");

// commit the changes
IIS.CommitChanges();
IIS.RefreshCache();

代码如上,我对test111.com下的虚拟目录img进行操作时是可行的,但是对test111.com下的其他目录进行操作时老是报错


Error: System.Runtime.InteropServices.COMException (0x80070003): 系统找不到指定
的路径。

   在 System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
   在 System.DirectoryServices.DirectoryEntry.Bind()
   在 System.DirectoryServices.DirectoryEntry.get_AdsObject()
   在 System.DirectoryServices.PropertyValueCollection.PopulateList()
   在 System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entr
y, String propertyName)
   在 System.DirectoryServices.PropertyCollection.get_Item(String propertyName)
   在 soccerwrek.IISWMI.Main(String[] args)

D:\vSystem>iplimit.exe test111.com root/images -a -g 1.1.1.1
Error: System.Runtime.InteropServices.COMException (0x80070003): 系统找不到指定
的路径。



求解决方案

iis 解决方案 --------------------编程问答-------------------- 我最近也遇到这个问题了,同求 --------------------编程问答-------------------- 除 --------------------编程问答-------------------- 求牛人指教啊 --------------------编程问答-------------------- wo ding   up mark mark --------------------编程问答-------------------- 帮顶一下,顺便说下,感觉代码很凌乱 --------------------编程问答-------------------- 还没有人解决吗? --------------------编程问答-------------------- 你把路径输出一下,看看取得是哪个目录.

--------------------编程问答--------------------

using System;
using System.IO;
using System.Collections;
using System.DirectoryServices;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Collections.Generic;


namespace soccerwrek
{
    class IISWMI
    {
        [STAThread]
        static int Main(string[] args)
        {
            string webName = args[0];
            string pathname = args[1];
            string flag = args[2];
            string denyflag = args[3];
            string ipString = "";
            string[] ipstringlist = args[4].Split(',');


            int objID = GetObjID(webName);
            if (objID <= 0)
                return -1;

            string objName = "IIS://localhost/w3svc/" + objID + "/" + pathname;
            Console.WriteLine(objName);
            try
            {
                DirectoryEntry IIS = new DirectoryEntry(objName);
                Type typ = IIS.Properties["IPSecurity"][0].GetType();
                object IPSecurity = IIS.Properties["IPSecurity"][0];

                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("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);
                        }
                        return 0;
                    }
                    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 = 0;
                    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[] { newIPDenyList });

                    IIS.Properties["IPSecurity"][0] = 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"][0];
                    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);
                        }
                        return 0;
                    }
                    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 = 0;
                    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"][0] = 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"][0];
                    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());
                return -1;
            }

            return 0;
        }
--------------------编程问答--------------------



        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 -1;
        }
    }
}



以上是全部的源码
调用方式为
www.test111.com root/data -a -g 1.1.1.1,222.2.2.2,3.3.3.3,4.4.4.4

执行结果为
Error: System.Runtime.InteropServices.COMException (0x80070003): 系统找不到指定
的路径。

   在 System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
   在 System.DirectoryServices.DirectoryEntry.Bind()
   在 System.DirectoryServices.DirectoryEntry.RefreshCache()
   在 soccerwrek.IISWMI.Main(String[] args) 位置 C:\Documents and Settings\Admin
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,