-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirewall_manager.py
More file actions
60 lines (53 loc) · 2.26 KB
/
Copy pathfirewall_manager.py
File metadata and controls
60 lines (53 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import subprocess
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
RULE_PREFIX = "PyFire_"
def _run_netsh_command(command, check=True):
try:
result = subprocess.run(
command,
capture_output=True,
text=True,
check=check,
creationflags=subprocess.CREATE_NO_WINDOW,
encoding='utf-8',
errors='ignore'
)
logging.info(f"Successfully executed: {' '.join(command)}")
return result
except FileNotFoundError:
logging.error("`netsh` command not found. Ensure you are on Windows and it's in your system's PATH.")
return None
except subprocess.CalledProcessError as e:
logging.error(f"Error executing command: {' '.join(command)}")
logging.error(f"Return Code: {e.returncode}\nOutput:\n{e.stderr}")
return None
def block_ip(ip_address: str):
rule_name = f"{RULE_PREFIX}{ip_address}"
command = [
"netsh", "advfirewall", "firewall", "add", "rule",
f"name={rule_name}", "dir=out", "action=block", f"remoteip={ip_address}"
]
if not rule_exists(rule_name):
return _run_netsh_command(command)
def unblock_ip(ip_address: str):
rule_name = f"{RULE_PREFIX}{ip_address}"
command = [
"netsh", "advfirewall", "firewall", "delete", "rule", f"name={rule_name}"
]
return _run_netsh_command(command)
def rule_exists(rule_name):
command = ["netsh", "advfirewall", "firewall", "show", "rule", f"name={rule_name}"]
result = _run_netsh_command(command, check=False)
return result and "No rules match the specified criteria." not in result.stdout
def sync_rules_with_state(app_state):
command = ["netsh", "advfirewall", "firewall", "show", "rule", f"name={RULE_PREFIX}*", "verbose"]
result = _run_netsh_command(command, check=False)
if result and result.stdout:
lines = result.stdout.splitlines()
for line in lines:
if line.strip().startswith("RemoteIP"):
ip = line.split(":", 1)[-1].strip()
if ip.lower() != 'any':
app_state.add_blocked_ip(ip)
logging.info(f"Synced {len(app_state.get_blocked_ips())} rules from Windows Firewall.")