热线(9:00-18:00):13544706711
当前位置: 首页 > 教程技巧 > 

xp IP安全策略 ipseccmd

时间: 2016/10/30 13:04:06

///下载  ipseccmd.exe


   //禁止 xp 连接
        public static void BannedXPRunCmd()
        {
            string str = Console.ReadLine();
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;    //是否使用操作系统shell启动
            p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
            p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
            p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
            p.StartInfo.CreateNoWindow = true;//不显示程序窗口
            p.Start();//启动程序

            //跳转到指定的路径,并输入信息
            str = "cd " + Application.StartupPath + @"\lib";
            p.StandardInput.WriteLine(str);

            //创建一个Ip策略(阻止所有连接),并指派
            str = "ipseccmd -w REG -p BannedConnectIP -r BannedConnectIP -f 0+* -n BLOCK -x ";
            p.StandardInput.WriteLine(str);

            //运行配置IP访问
            string StrIPArr = ConfigurationSettings.AppSettings["RemoteIPAddr"];
            if (StrIPArr.Contains('',''))
            {
                string[] strArr = StrIPArr.Split('','');
                for (int i = 0; i < strArr.Length; i++)
                {
                    string strarr = strArr[i].ToString();
                    str = "ipseccmd -w REG -p BannedConnectIP -r AllowConnectIP(" + strarr + ") -f 0+" + strarr + " -n PASS -x ";
                    p.StandardInput.WriteLine(str);
                }
            }
            else
            {
                str = "ipseccmd -w REG -p BannedConnectIP -r AllowConnectIP(" + StrIPArr + ") -f 0+" + StrIPArr + " -n PASS -x ";
                p.StandardInput.WriteLine(str);
            }
            ////创建一个Ip策略(允许155连接),并指派
            //str = "ipseccmd -w REG -p BannedConnectIP -r AllowConnectIP(155) -f 0+192.168.1.155 -n PASS -x ";
            //p.StandardInput.WriteLine(str);

            p.StandardInput.WriteLine("exit");
            p.StandardInput.AutoFlush = true;
            //向标准输入写入要执行的命令。这里使用&是批处理命令的符号,表示前面一个命令不管是否执行成功都执行后面(exit)命令,如果不执行exit命令,后面调用ReadToEnd()方法会假死
            //同类的符号还有&&和||前者表示必须前一个命令执行成功才会执行后面的命令,后者表示必须前一个命令执行失败才会执行后面的命令

            p.WaitForExit();//等待程序执行完退出进程
            p.Close();
        }

        //开放 xp 连接
        public static void AllowXPRunCmd()
        {
            string str = Console.ReadLine();
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;    //是否使用操作系统shell启动
            p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
            p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
            p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
            p.StartInfo.CreateNoWindow = true;//不显示程序窗口
            p.Start();//启动程序

            //跳转到指定的路径,并输入信息
            str = "cd " + Application.StartupPath + @"\lib";
            p.StandardInput.WriteLine(str);

            //取消指派
            str = " ipseccmd -w REG -p BannedConnectIP -y  ";
            p.StandardInput.WriteLine(str);

            //删除策略(删除前,需要取消指派)
            str = " ipseccmd -p BannedConnectIP -w reg -o ";
            p.StandardInput.WriteLine(str);

            p.StandardInput.WriteLine("exit");
            p.StandardInput.AutoFlush = true;
            //向标准输入写入要执行的命令。这里使用&是批处理命令的符号,表示前面一个命令不管是否执行成功都执行后面(exit)命令,如果不执行exit命令,后面调用ReadToEnd()方法会假死
            //同类的符号还有&&和||前者表示必须前一个命令执行成功才会执行后面的命令,后者表示必须前一个命令执行失败才会执行后面的命令

            p.WaitForExit();//等待程序执行完退出进程
            p.Close();
        }