Last active
November 26, 2020 20:48
-
-
Save erfg12/568232ceafa76371da95101e2249defa to your computer and use it in GitHub Desktop.
Block all connections in Windows firewall, except some IP addresses during work hours. For Windows Vista, 7, 8 and 10.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static INetFwPolicy2 getCurrPolicy() | |
{ | |
INetFwPolicy2 fwPolicy2; | |
Type tNetFwPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2"); | |
fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(tNetFwPolicy2); | |
return fwPolicy2; | |
} | |
INetFwPolicy2 fwPolicy2 = getCurrPolicy(); | |
private async void Form1_LoadAsync(object sender, EventArgs e) | |
{ | |
bool addRule = true; | |
if (fwPolicy2.get_FirewallEnabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_DOMAIN) == false) //turn on firewall, if it's off | |
fwPolicy2.FirewallEnabled[NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_DOMAIN] = true; | |
foreach (INetFwRule rule in fwPolicy2.Rules) | |
{ | |
if (rule.Name == "Allow all local intranet") | |
addRule = false; | |
} | |
if (addRule) | |
{ | |
//let local IPs through (intranet, jobboss, etc) | |
INetFwRule firewallRule = (INetFwRule)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule")); | |
firewallRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW; | |
firewallRule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT; | |
firewallRule.Enabled = true; | |
firewallRule.InterfaceTypes = "All"; | |
firewallRule.LocalAddresses = "10.1.10.1-10.1.10.255"; //local IP's assumed to be in 10.1.10. range | |
firewallRule.RemoteAddresses = "10.1.10.1-10.1.10.255"; | |
firewallRule.Name = "Allow all local intranet"; | |
fwPolicy2.Rules.Add(firewallRule); | |
} | |
TimeSpan span = new TimeSpan(0, 0, 0, 1, 0); //once per second | |
await checkTime(span); | |
} | |
public async Task checkTime(TimeSpan interval) | |
{ | |
while (true) | |
{ | |
//written in military time | |
//work is from 6-9, 9:20-12:00, 12:30-5:00 | |
//9:00-9:20 break, 12:00-12:30 break, 5:00 left work | |
if (Convert.ToInt32(DateTime.Now.ToString("HHmmss")) > 000000 && Convert.ToInt32(DateTime.Now.ToString("HHmmss")) < 060000 || | |
Convert.ToInt32(DateTime.Now.ToString("HHmmss")) > 090000 && Convert.ToInt32(DateTime.Now.ToString("HHmmss")) < 091959 || | |
Convert.ToInt32(DateTime.Now.ToString("HHmmss")) > 120000 && Convert.ToInt32(DateTime.Now.ToString("HHmmss")) < 122959 || | |
Convert.ToInt32(DateTime.Now.ToString("HHmmss")) > 170000 && Convert.ToInt32(DateTime.Now.ToString("HHmmss")) < 240000) | |
fwPolicy2.DefaultOutboundAction[NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_DOMAIN] = NET_FW_ACTION_.NET_FW_ACTION_ALLOW; | |
else | |
fwPolicy2.DefaultOutboundAction[NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_DOMAIN] = NET_FW_ACTION_.NET_FW_ACTION_BLOCK; //block all outbound default | |
await Task.Delay(interval); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
None is blocked but allowed