【转】自己开发程序管理WINDOWS防火墙

本文涉及的产品
云防火墙,500元 1000GB
简介: 2008-05-21 07:20作者:肖波出处:天极网责任编辑:nancy     最近项目中需要通过程序自动设置windows 防火墙,查了一下资料,可以通过命令行netsh firewall来实现。

    2008-05-21 07:20作者:肖波出处:天极网责任编辑:nancy

    最近项目中需要通过程序自动设置windows 防火墙,查了一下资料,可以通过命令行netsh firewall来实现。封装了一个类来实现对运行放开的程序(Allowed program)进行管理。管理其他内容比如放开端口等方法是类似的。

  程序中用到一个公共类 RunProcess,这个类可从另一篇文章 《一个C#写的调用外部进程类》 获得

 


 namespace WinFirewall
  {
  
   public enum TScope
   {
   ALL,
   SUBNET,
   CUSTOM,
   }
  
   public enum TMode
   {
   ENABLE,
   DISABLE,
   }
  
   /**//// 
   /// Manage the allowed program with the Windows Firewall.
   /// 
   public class AllowedProgram
   {
   Set AllowedProgram Help#region Set AllowedProgram Help
   /**//*
   set allowedprogram
  
   [ program = ] path
   [ [ name = ] name
   [ mode = ] ENABLE|DISABLE
   [ scope = ] ALL|SUBNET|CUSTOM
   [ addresses = ] addresses
   [ profile = ] CURRENT|DOMAIN|STANDARD|ALL ]
  
   Sets firewall allowed program configuration.
  
   Parameters:
  
   program - Program path and file name.
  
   name - Program name (optional).
  
   mode - Program mode (optional).
   ENABLE - Allow through firewall (default).
   DISABLE - Do not allow through firewall.
  
   scope - Program scope (optional).
   ALL - Allow all traffic through firewall (default).
   SUBNET - Allow only local network (subnet) traffic through firewall.
   CUSTOM - Allow only specified traffic through firewall.
  
   addresses - Custom scope addresses (optional).
  
   profile - Configuration profile (optional).
   CURRENT - Current profile (default).
   DOMAIN - Domain profile.
   STANDARD - Standard profile.
   ALL - All profiles.
  
   Remarks: 'scope' must be 'CUSTOM' to specify 'addresses'.
  
   Examples:
  
   set allowedprogram C:\MyApp\MyApp.exe MyApp ENABLE
  
   set allowedprogram C:\MyApp\MyApp.exe MyApp DISABLE
  
   set allowedprogram C:\MyApp\MyApp.exe MyApp ENABLE CUSTOM
  
   157.60.0.1,172.16.0.0/16,10.0.0.0/255.0.0.0,LocalSubnet
   set allowedprogram program = C:\MyApp\MyApp.exe name = MyApp mode = ENABLE
  
   set allowedprogram program = C:\MyApp\MyApp.exe name = MyApp mode = DISABLE
   set allowedprogram program = C:\MyApp\MyApp.exe name = MyApp mode = ENABLE
  
   scope = CUSTOM addresses =
   157.60.0.1,172.16.0.0/16,10.0.0.0/255.0.0.0,LocalSubnet
   */
   #endregion
  
   private field#region private field
   private String m_Program;
   private String m_Name;
   private TScope m_Scope = TScope.ALL;
   private TMode m_Mode = TMode.ENABLE;
   private String m_Address;
   #endregion
  
   public property#region public property
   /**//// 
   /// Program path and file name.
   /// 
   public String Program
   {
   get
   {
   return m_Program;
   }
  
   set
   {
   m_Program = value;
   }
   }
  
   /**//// 
   /// Program name (optional).
   /// 
   public String Name
   {
   get
   {
   return m_Name;
   }
  
   set
   {
   m_Name = value;
   }
   }
  
   /**//// 
   /// Program scope (optional).
   /// ALL - Allow all traffic through firewall (default).
   /// SUBNET - Allow only local network (subnet) traffic through firewall.
   /// CUSTOM - Allow only specified traffic through firewall. /// 
   public TScope Scope
   {
   get
   {
   return m_Scope;
   }
  
   set
   {
   m_Scope = value;
   }
   }
  
   /**//// 
   /// Program mode (optional).
   /// ENABLE - Allow through firewall (default).
   /// DISABLE - Do not allow through firewall
   /// 
   public TMode Mode
   {
   get
   {
   return m_Mode;
   }
  
   set
   {
   m_Mode = value;
   }
   }
  
   /**//// 
   /// Custom scope addresses (optional).
   /// 
   /// 
   /// 157.60.0.1,172.16.0.0/16,10.0.0.0/255.0.0.0
   /// 
   public String Address
   {
   get
   {
   return m_Address;
   }
  
   set
   {
   m_Address = value;
   }
   }
  
   #endregion
  
   public method#region public method
  
   /**//// 
   /// Set allowed program
   /// 
   public void Set()
   {
   Debug.Assert(Program != null);
  
   if (Name == null)
   {
   Name = System.IO.Path.GetFileNameWithoutExtension(Program);
   }
  
   if (Scope == TScope.CUSTOM)
   {
   Debug.Assert(Address != null);
   }
  
   RunProcess runCmd = new RunProcess();
   String command;
  
   command = String.Format("firewall set allowedprogram {0} {1} {2} {3}",
   Program, Name, Mode.ToString(), Scope.ToString());
  
   if (Scope == TScope.CUSTOM)
   {
   command += " " + Address;
   }
  
   runCmd.Run("netsh", command);
  
   if (runCmd.Error != null && runCmd.Error != "")
   {
   throw new Exception(runCmd.Error);
   }
  
   if (!runCmd.Output.ToLower().Contains("ok."))
   {
   throw new Exception(runCmd.Output);
   }
   }
  
   /**//// 
   /// Delete allowed program
   /// 
   public void Delete()
   {
   Debug.Assert(Program != null);
  
   RunProcess runCmd = new RunProcess();
  
   String command = String.Format("firewall delete allowedprogram {0}",
   Program);
  
   runCmd.Run("netsh", command);
  
   if (runCmd.Error != null && runCmd.Error != "")
   {
   throw new Exception(runCmd.Error);
   }
  
   if (!runCmd.Output.ToLower().Contains("ok."))
   {
   throw new Exception(runCmd.Output);
   }
   }
  
   #endregion
   }
  }
  
  调用的相关例程
   private void buttonSetAllowProgram_Click(object sender, EventArgs e)
   {
   try
   {
   AllowedProgram allowedProgram = new AllowedProgram();
   allowedProgram.Program = textBoxProgramFilePath.Text.Trim();
  
   if (checkBoxEnable.Checked)
   {
   allowedProgram.Mode = TMode.ENABLE;
   }
   else
   {
   allowedProgram.Mode = TMode.DISABLE;
   }
  
   allowedProgram.Scope = (TScope)comboBoxScope.SelectedItem;
  
   allowedProgram.Address = textBoxAddress.Text.Trim();
  
   allowedProgram.Set();
  
   MessageBox.Show("OK", "Information", MessageBoxButtons.OK);
   }
   catch (Exception e1)
   {
   MessageBox.Show(e1.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
   }
   }
  
   private void buttonDelAllowProgram_Click(object sender, EventArgs e)
   {
   try
   {
   AllowedProgram allowedProgram = new AllowedProgram();
   allowedProgram.Program = textBoxProgramFilePath.Text.Trim();
   allowedProgram.Delete();
  
   MessageBox.Show("OK", "Information", MessageBoxButtons.OK);
   }
   catch (Exception e1)
   {
   MessageBox.Show(e1.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
   }
   }

 

目录
相关文章
|
2月前
|
弹性计算 监控 网络安全
如何轻松使用AWS Web应用程序防火墙?
AWS WAF是Web应用防火墙,可防护常见网络攻击。通过创建Web ACL并设置规则,保护CloudFront、API网关、负载均衡器等资源。支持自定义规则与OWASP预定义规则集,结合CloudWatch实现监控日志,提升应用安全性和稳定性。
|
1月前
|
安全 Ubuntu iOS开发
Nessus Professional 10.10 Auto Installer for Windows - Nessus 自动化安装程序
Nessus Professional 10.10 Auto Installer for Windows - Nessus 自动化安装程序
122 3
Nessus Professional 10.10 Auto Installer for Windows - Nessus 自动化安装程序
|
3月前
|
Ubuntu Linux Windows
如何在Ubuntu系统中安装Wine,借此来运行Windows程序
熟悉的登录画面出现,在Ubuntu系统中扫描登录微信程序。
|
3月前
|
Unix Linux 编译器
解决在Windows平台上运行Golang程序时出现的syscall.SIGUSR1未定义错误。
通过这种结构,你的代码既可以在支持 SIGUSR1 信号的系统上正常工作,又可以在不支持这些信号的 Windows 系统上编译通过,确保跨平台的兼容性和功能的完整性。
169 0
|
4月前
|
Windows
Windows下版本控制器(SVN)-验证是否安装成功+配置版本库+启动服务器端程序
Windows下版本控制器(SVN)-验证是否安装成功+配置版本库+启动服务器端程序
136 2
|
5月前
|
Windows
Windows下版本控制器(SVN)-启动服务器端程序
Windows下版本控制器(SVN)-启动服务器端程序
180 4
|
6月前
|
安全 Devops 测试技术
AppSpider 7.5.018 for Windows - Web 应用程序安全测试
AppSpider 7.5.018 for Windows - Web 应用程序安全测试
137 0
AppSpider 7.5.018 for Windows - Web 应用程序安全测试
|
9月前
|
安全 JavaScript Java
AppSpider Pro 7.5.015 for Windows - Web 应用程序安全测试
AppSpider Pro 7.5.015 for Windows - Web 应用程序安全测试
153 12
AppSpider Pro 7.5.015 for Windows - Web 应用程序安全测试
|
8月前
|
Windows
Windows程序的数字签名证书怎么申请
Windows程序的数字签名证书申请流程包括:准备企业资料(营业执照、税务登记证等),提交申请表及企业资料。经过初审、实名认证和二审后,等待1-5个工作日审核结果。审核通过后,CA机构颁发证书并通过邮件或邮寄方式发送。收到证书后按指南安装并使用签名工具对程序进行数字签名,确保软件完整性和可信度。注意证书有效期、管理和兼容性问题。
|
8月前
|
自然语言处理 安全 测试技术
HCL AppScan Standard 10.8.0 (Windows) - Web 应用程序安全测试
HCL AppScan Standard 10.8.0 (Windows) - Web 应用程序安全测试
534 0
HCL AppScan Standard 10.8.0 (Windows) - Web 应用程序安全测试