private static INetFwRule createFWRule(string addresses, Game game, bool enabled, bool bOutbound, RuleProtocol proto) { INetFwRule firewallRule = (INetFwRule)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule")); firewallRule.Action = NET_FW_ACTION_.NET_FW_ACTION_BLOCK; firewallRule.Protocol = (int)proto; firewallRule.Enabled = enabled; firewallRule.InterfaceTypes = "All"; if (!string.IsNullOrEmpty(addresses)) { firewallRule.RemoteAddresses = addresses; } if (game.BlockLocal) { firewallRule.LocalPorts = proto == RuleProtocol.eRuleProtoTCP ? game.TcpPorts : game.UdpPorts; } else { firewallRule.RemotePorts = proto == RuleProtocol.eRuleProtoTCP ? game.TcpPorts : game.UdpPorts; } firewallRule.Name = proto == RuleProtocol.eRuleProtoTCP ? game.GetTCPRuleName(bOutbound ? "Outbound" : "Inbound") : game.GetUDPRuleName(bOutbound ? "Outbound" : "Inbound"); firewallRule.Direction = bOutbound ? NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT : NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN; return(firewallRule); }
public void addRule(string path, RuleAction ruleAction, RuleDirection ruleDir, RuleProtocol ruleProtoc) { string appName = path; INetFwRule2 inboundRule = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule")); inboundRule.Enabled = true; inboundRule.ApplicationName = appName; inboundRule.Protocol = (int)ruleProtoc; inboundRule.Direction = (NET_FW_RULE_DIRECTION_)ruleDir; inboundRule.Profiles = 7; inboundRule.Name = "_BM_FW_" + ((int)ruleDir).ToString() + ((int)ruleAction).ToString() + ((int)ruleProtoc).ToString() + "_" + appName.Substring(appName.LastIndexOf(@"\") + 1, appName.Length - appName.LastIndexOf(@"\") - 5); inboundRule.Action = (NET_FW_ACTION_)ruleAction; INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2")); firewallPolicy.Rules.Add(inboundRule); }
public bool IsMatch(EthernetPacket packet) { var validationStack = new Stack <Func <bool> >(); if (SourceMac != null) { validationStack.Push(() => SourceMac.Equals(packet.SourceHardwareAddress)); } if (DestinationMac != null) { validationStack.Push(() => DestinationMac.Equals(packet.DestinationHardwareAddress)); } var arp = packet.Extract <ArpPacket>(); if (arp != null && RuleProtocol.Equals(Protocol.Arp)) { return(resolveValidationStack(validationStack)); } var lldp = packet.Extract <LldpPacket>(); if (lldp != null && RuleProtocol.Equals(Protocol.Lldp)) { return(resolveValidationStack(validationStack)); } var ipv4 = packet.Extract <IPv4Packet>(); if (ipv4 != null) { if (SourceIp != null) { validationStack.Push(() => SourceIp.Equals(ipv4.SourceAddress)); } if (DestinationIp != null) { validationStack.Push(() => DestinationIp.Equals(ipv4.DestinationAddress)); } if (RuleProtocol.Equals(Protocol.Ipv4)) { return(resolveValidationStack(validationStack)); } } var ipv6 = packet.Extract <IPv6Packet>(); if (ipv6 != null) { if (SourceIp != null) { validationStack.Push(() => SourceIp.Equals(ipv6.SourceAddress)); } if (DestinationIp != null) { validationStack.Push(() => DestinationIp.Equals(ipv6.DestinationAddress)); } if (RuleProtocol.Equals(Protocol.Ipv6)) { return(resolveValidationStack(validationStack)); } } var icmpv4 = packet.Extract <IcmpV4Packet>(); if (icmpv4 != null && RuleProtocol.Equals(Protocol.Icmpv4)) { if (DestinationPort != null) { validationStack.Push(() => (((ushort)icmpv4.TypeCode) / 256) == DestinationPort); } return(resolveValidationStack(validationStack)); } var icmpv6 = packet.Extract <IcmpV4Packet>(); if (icmpv6 != null && RuleProtocol.Equals(Protocol.Icmpv6)) { if (DestinationPort != null) { validationStack.Push(() => (((ushort)icmpv6.TypeCode) / 256) == DestinationPort); } return(resolveValidationStack(validationStack)); } var tcp = packet.Extract <TcpPacket>(); if (tcp != null && RuleProtocol.Equals(Protocol.Tcp)) { if (SourcePort != null) { validationStack.Push(() => SourcePort.Equals(tcp.SourcePort)); } if (DestinationPort != null) { validationStack.Push(() => DestinationPort.Equals(tcp.DestinationPort)); } return(resolveValidationStack(validationStack)); } var udp = packet.Extract <UdpPacket>(); if (udp != null && RuleProtocol.Equals(Protocol.Udp)) { if (SourcePort != null) { validationStack.Push(() => SourcePort.Equals(udp.SourcePort)); } if (DestinationPort != null) { validationStack.Push(() => DestinationPort.Equals(udp.DestinationPort)); } return(resolveValidationStack(validationStack)); } return(false); }