Hi in this blog we see how to add inbound and outbound rule to windows firewall using c#.Create a new windows form application and design a form like this.
Full Source Code:
using NetFwTypeLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FirewallCheck
{
public partial class FirewallStatus : Form
{
private const string firewallid = "{304CE942-6E39-40D8-943A-B913C40C9CD4}";
public FirewallStatus()
{
InitializeComponent();
lblFirewallStatus.Text = "FirewallStatus:"+firewallstatus();
AddRule();
}
public string firewallstatus()
{
INetFwMgr manager = FirewallManager();
bool isFirewallEnabled = manager.LocalPolicy.CurrentProfile.FirewallEnabled;
if (isFirewallEnabled)
return "ON";
else
return "OFF";
}
private static NetFwTypeLib.INetFwMgr FirewallManager()
{
Type objectType = Type.GetTypeFromCLSID(new Guid(firewallid));
return Activator.CreateInstance(objectType) as NetFwTypeLib.INetFwMgr;
}
private void AddRule()
{
try
{
string snamelist = File.ReadAllText("firewallrule.txt");
string[] sFwrule = snamelist.Split('|');
if (sFwrule[0] != String.Empty)
{
for (int i = 0; i < sFwrule.Length; i++)
{
cmdRules.Items.Add(sFwrule[i]);
}
}
cmdRules.SelectedIndex = 0;
}
catch (Exception ex)
{
MessageBox.Show("Exception:" + ex.ToString());
}
}
//Add Inbound and Outbound Rules
private void btnAddFilter_Click(object sender, EventArgs e)
{
try
{
INetFwRule firewallRule = fwInstance();
if (cmdAction.SelectedItem.ToString() == "Block")
{
firewallRule.Action = NET_FW_ACTION_.NET_FW_ACTION_BLOCK;
}
else
{
firewallRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
}
firewallRule.Name = txtName.Text.ToString();
if (cmdDirection.SelectedItem.ToString() == "InBound")
{
firewallRule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN;
firewallRule.RemoteAddresses = txtIP.Text.Trim();
}
else if (cmdDirection.SelectedItem.ToString() == "OutBound")
{
firewallRule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT;
firewallRule.RemoteAddresses = txtIP.Text.Trim();
}
if (cmdPackets.SelectedItem.ToString() == "HTTP")
{
firewallRule.Protocol = (int)NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP;
firewallRule.RemotePorts = "80";
}
else if (cmdPackets.SelectedItem.ToString() == "HTTPS")
{
firewallRule.Protocol = (int)NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP;
firewallRule.RemotePorts = "443";
}
else
{
txtIP.Enabled = true;
}
firewallRule.RemoteAddresses = txtIP.Text;
firewallRule.Enabled = true;
firewallRule.InterfaceTypes = "All";
INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
firewallPolicy.Rules.Add(firewallRule);
cmdRules.Items.Add(txtName.Text);
cmdRules.SelectedIndex = 0;
using (FileStream fs = new FileStream("firewallrule.txt", FileMode.Append, FileAccess.Write))
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine(txtName.Text + "|");
}
MessageBox.Show("Rule Added Sucessfully ...");
}
catch (Exception ex)
{
MessageBox.Show("Exception:" + ex.ToString());
}
}
private INetFwRule fwInstance()
{
try
{
INetFwRule firewallRule = (INetFwRule)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule"));
return firewallRule;
}
catch (Exception ex)
{
MessageBox.Show("Exception:" + ex.ToString());
return null;
}
}
}
}
Output:
Our Rule Added to Windows Firewall :
Hello, I tried your code but I am recieving Access Denied Error... can you please help...thanks
ReplyDeleteRun program as Administrator
ReplyDelete