-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirewall.py
More file actions
66 lines (58 loc) · 2.21 KB
/
Copy pathfirewall.py
File metadata and controls
66 lines (58 loc) · 2.21 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
61
62
63
64
65
66
#!/usr/bin/env python3
import os
def block_port(port):
"""Block incoming and outgoing TCP traffic on a specific port."""
print(f"\n[+] Blocking port {port} ...")
os.system(f"sudo iptables -A INPUT -p tcp --dport {port} -j DROP")
os.system(f"sudo iptables -A OUTPUT -p tcp --dport {port} -j DROP")
print(f"[✓] Port {port} is now blocked.")
def unblock_port(port):
"""Remove the blocking rule for the given port."""
print(f"\n[+] Unblocking port {port} ...")
os.system(f"sudo iptables -D INPUT -p tcp --dport {port} -j DROP")
os.system(f"sudo iptables -D OUTPUT -p tcp --dport {port} -j DROP")
print(f"[✓] Port {port} is now unblocked.")
def show_rules():
"""Display current iptables rules."""
print("\n[+] Current iptables rules:")
os.system("sudo iptables -L -n --line-numbers")
def clear_all_rules():
"""Flush all firewall rules."""
print("\n[!] Clearing all iptables rules...")
os.system("sudo iptables -F")
print("[✓] All rules cleared.")
def main():
while True:
print("\n========== Python Firewall ==========")
print("1. Block a port")
print("2. Unblock a port")
print("3. View current rules")
print("4. Clear all rules")
print("5. Exit")
print("====================================")
choice = input("Enter your choice (1-5): ").strip()
if choice == "1":
port = input("Enter port number to block: ").strip()
if port.isdigit():
block_port(port)
else:
print("Invalid port number.")
elif choice == "2":
port = input("Enter port number to unblock: ").strip()
if port.isdigit():
unblock_port(port)
else:
print("Invalid port number.")
elif choice == "3":
show_rules()
elif choice == "4":
confirm = input("Are you sure you want to clear all rules? (y/n): ").lower()
if confirm == "y":
clear_all_rules()
elif choice == "5":
print("Exiting firewall control...")
break
else:
print("Invalid option, try again.")
if __name__ == "__main__":
main()