示例#1
0
 internal void AddRule(WinFWRule NewRule)
 {
     this.AllRules.Add(NewRule);
     foreach (string P in NewRule.RemotePorts)
     {
         if (!RulesByPort.ContainsKey(P))
         {
             RulesByPort[P] = new List <IWinFWRule>();
         }
         RulesByPort[P].Add(NewRule);
     }
 }
示例#2
0
        private static IWindowsFirewall WinFwScan()

        /*
         * Windows Firewall information can be found using the INetFwMgr interface in the NetFwTypeLib namespace.
         * The firewall manager object, HNetCfg.FwMgr, is a COM object; type is retrieved at runtime and instantiated
         * using Activator.CreateInstance()
         */

        /* Each firewall rule in the Windows Firewall has associated remote ports.
         * This subroutine handles retrieving them, and storing them in the WinFW object in the
         * scan result. The WinFW object has a RulesByPort dict that allows looking up
         * what rules are associated with any given port (i.e. GetRulesByPort(string PortNumber))
         * See the ConsoleApp in this solution for a usage example.
         *
         * The RemotePorts property in INetFwRule is just a string; it has comma-separated ports,
         * some actually using alphabetical names instead of numbers. This gets pulled out into
         * a list of strings, so that a program using ports 80 and 443 can be found via
         * GetRulesByPort("80") or GetRulesByPort("443")
         */
        {
            WindowsFirewall WinFW = new WindowsFirewall();

            //Instantiate Firewall Manager object and get current profile
            Type          tNetFirewall = Type.GetTypeFromProgID("HNetCfg.FwMgr", false);
            INetFwMgr     FwMgr        = (INetFwMgr)Activator.CreateInstance(tNetFirewall);
            INetFwProfile FwProfile    = FwMgr.LocalPolicy.CurrentProfile;

            // Populate basic properties
            WinFW.Enabled           = FwProfile.FirewallEnabled;
            WinFW.GloballyOpenPorts = new List <int>();

            foreach (int p in FwProfile.GloballyOpenPorts)
            {
                WinFW.GloballyOpenPorts.Add(p);
            }

            //Get Rule objects
            Type          tFwPolicy = Type.GetTypeFromProgID("HNetCfg.FwPolicy2", false);
            INetFwPolicy2 FwPolicy  = (INetFwPolicy2)Activator.CreateInstance(tFwPolicy);
            INetFwRules   FwRules   = FwPolicy.Rules;

            // Create a new rule for each rule object, pass it to the AddRule method of the
            // WinFW object
            foreach (INetFwRule Rule in FwRules)
            {
                WinFWRule R = new WinFWRule();
                R.Name            = Rule.Name;
                R.Description     = Rule.Description;
                R.ApplicationName = Rule.ApplicationName;
                R.ServiceName     = Rule.serviceName;
                R.Enabled         = Rule.Enabled;
                R.RemotePorts     = new List <string>();
                if (Rule.RemotePorts != null)
                {
                    //Separate by commas
                    R.RemotePorts.AddRange(Rule.RemotePorts.Split(','));
                }
                WinFW.AddRule(R);
            }

            return(WinFW);
        }