专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-963-0016      微信咨询  |  联系我们

C#向IIS的黑名单写IP

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 1:08:57       共计:3614 浏览

通过前台WEB记录IP访问情况,后台服务定时作规则统计,写黑名单写入IIS的拒绝访问列表里,很有意义噢

static void Main(string[] args)
        {
            try
            {
                // retrieve the directory entry for the root of the IIS server

                System.DirectoryServices.DirectoryEntry IIS =
                   new System.DirectoryServices.DirectoryEntry(
                   "IIS://localhost/w3svc/17679966/root");      //17679966  为网站标记ID

                // retrieve the list of currently denied IPs
                Console.WriteLine("Retrieving the list of currently denied IPs.");
                // get the IPSecurity property
                Type typ = IIS.Properties["IPSecurity"][0].GetType();
                object IPSecurity = IIS.Properties["IPSecurity"][0];
                // retrieve the IPDeny list from the IPSecurity object
                Array origIPDenyList = (Array)typ.InvokeMember("IPDeny",
                           BindingFlags.DeclaredOnly |
                           BindingFlags.Public | BindingFlags.NonPublic |
                           BindingFlags.Instance | BindingFlags.GetProperty,
                           null, IPSecurity, null);

                // display what was being denied
                foreach (string s in origIPDenyList)
                    Console.WriteLine("Before: " + s);

                // check GrantByDefault.  This has to be set to true,
                // or what we are doing will not work.
                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[4];
                newIPDenyList[0] = "192.168.1.1, 255.255.255.255";
                newIPDenyList[1] = "192.168.1.2, 255.255.255.255";
                newIPDenyList[2] = "192.168.1.3, 255.255.255.255";
                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();


                // 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);
            }

            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.Message.ToString());

            }

        }

版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:C# DDOS攻击代码(伪IP) | ·下一条:C#实现通用对IP地址的检查是否合法与Ping指定IP是否畅通的

Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有