-
Notifications
You must be signed in to change notification settings - Fork 0
/
firewall.py
162 lines (127 loc) · 9.91 KB
/
firewall.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import subprocess
def configure_firewall(source_ip, destination_ip, source_all, dest_all, port, allow_rule):
try:
if allow_rule:
if source_all and dest_all:
subprocess.run(['iptables', '-A', 'INPUT', '-p', 'tcp', '--dport', str(port), '-j', 'ACCEPT'])
subprocess.run(['iptables', '-A', 'OUTPUT', '-p', 'tcp', '--dport', str(port), '-j', 'ACCEPT'])
subprocess.run(['iptables', '-A', 'FORWARD', '-p', 'tcp', '--dport', str(port), '-j', 'ACCEPT'])
elif source_all:
subprocess.run(['iptables', '-A', 'INPUT', '-d', destination_ip, '-p', 'tcp', '--dport', str(port), '-j', 'ACCEPT'])
subprocess.run(['iptables', '-A', 'OUTPUT', '-d', destination_ip, '-p', 'tcp', '--dport', str(port), '-j', 'ACCEPT'])
subprocess.run(['iptables', '-A', 'FORWARD', '-d', destination_ip, '-p', 'tcp', '--dport', str(port), '-j', 'ACCEPT'])
elif dest_all:
subprocess.run(['iptables', '-A', 'INPUT', '-s', source_ip, '-p', 'tcp', '--dport', str(port), '-j', 'ACCEPT'])
subprocess.run(['iptables', '-A', 'OUTPUT', '-s', source_ip, '-p', 'tcp', '--dport', str(port), '-j', 'ACCEPT'])
subprocess.run(['iptables', '-A', 'FORWARD', '-s', source_ip, '-p', 'tcp', '--dport', str(port), '-j', 'ACCEPT'])
else:
subprocess.run(['iptables', '-A', 'INPUT', '-s', source_ip, '-d', destination_ip, '-p', 'tcp', '--dport', str(port), '-j', 'ACCEPT'])
subprocess.run(['iptables', '-A', 'OUTPUT', '-s', source_ip, '-d', destination_ip, '-p', 'tcp', '--dport', str(port), '-j', 'ACCEPT'])
subprocess.run(['iptables', '-A', 'FORWARD', '-s', source_ip, '-d', destination_ip, '-p', 'tcp', '--dport', str(port), '-j', 'ACCEPT'])
else:
if source_all and dest_all:
subprocess.run(['iptables', '-A', 'INPUT', '-p', 'tcp', '--dport', str(port), '-j', 'DROP'])
subprocess.run(['iptables', '-A', 'OUTPUT', '-p', 'tcp', '--dport', str(port), '-j', 'DROP'])
subprocess.run(['iptables', '-A', 'FORWARD', '-p', 'tcp', '--dport', str(port), '-j', 'DROP'])
elif source_all:
subprocess.run(['iptables', '-A', 'INPUT', '-d', destination_ip, '-p', 'tcp', '--dport', str(port), '-j', 'DROP'])
subprocess.run(['iptables', '-A', 'OUTPUT', '-d', destination_ip, '-p', 'tcp', '--dport', str(port), '-j', 'DROP'])
subprocess.run(['iptables', '-A', 'FORWARD', '-d', destination_ip, '-p', 'tcp', '--dport', str(port), '-j', 'DROP'])
elif dest_all:
subprocess.run(['iptables', '-A', 'INPUT', '-s', source_ip, '-p', 'tcp', '--dport', str(port), '-j', 'DROP'])
subprocess.run(['iptables', '-A', 'OUTPUT', '-s', source_ip, '-p', 'tcp', '--dport', str(port), '-j', 'DROP'])
subprocess.run(['iptables', '-A', 'FORWARD', '-s', source_ip, '-p', 'tcp', '--dport', str(port), '-j', 'DROP'])
else:
subprocess.run(['iptables', '-A', 'INPUT', '-s', source_ip, '-d', destination_ip, '-p', 'tcp', '--dport', str(port), '-j', 'DROP'])
subprocess.run(['iptables', '-A', 'OUTPUT', '-s', source_ip, '-d', destination_ip, '-p', 'tcp', '--dport', str(port), '-j', 'DROP'])
subprocess.run(['iptables', '-A', 'FORWARD', '-s', source_ip, '-d', destination_ip, '-p', 'tcp', '--dport', str(port), '-j', 'DROP'])
subprocess.run(['iptables-save'])
except Exception as e:
print(f"Error: {e}")
def block_by_port():
pass
def block_by_ip_address():
pass
def block_by_domain():
pass
def create_firewall_management(parent_frame, ctk):
firewall_frame = ctk.CTkFrame(parent_frame)
firewall_frame.pack()
tab_control = ctk.CTkTabview(master=firewall_frame, height=500, width=800, corner_radius=20)
tab_control.add('port-management')
block_by_port_frame = ctk.CTkFrame(master=tab_control.tab('port-management'))
port_label = ctk.CTkLabel(master=block_by_port_frame, text="Port Management", font=('Ubuntu', 20, 'bold'))
port_label.grid_configure(padx=10, pady=20, row=0, column=0, columnspan=5)
port_entry = ctk.CTkEntry(master=block_by_port_frame, placeholder_text='Enter The Port Number', corner_radius=10, width=200)
port_entry.grid_configure(padx=10, pady=10, columnspan=4, row=1, column=0)
tcp_protocol_var = ctk.StringVar()
tcp_checkbox = ctk.CTkCheckBox(master=block_by_port_frame, text="TCP", variable=tcp_protocol_var, onvalue="TCP", offvalue="", font=('Ubuntu', 15, 'bold'))
tcp_checkbox.grid_configure(column=1, row=2, padx=10)
udp_protocol_var = ctk.StringVar()
udp_checkbox = ctk.CTkCheckBox(master=block_by_port_frame, text="UDP", variable=udp_protocol_var, onvalue="UDP", offvalue="", font=('Ubuntu', 15, 'bold'))
udp_checkbox.grid_configure(column=2, row=2, padx=10)
interface_label = ctk.CTkLabel(master=block_by_port_frame, text="Select The Interface to block :",font=('verdana', 15, 'bold'))
interface_label.grid_configure(pady=10, padx=10, column=0, row=3)
input_interface_var = ctk.IntVar()
input_interface_checkbox = ctk.CTkCheckBox(master=block_by_port_frame, text="Input Interface", variable=input_interface_var,font=('verdana', 15, 'bold'))
input_interface_checkbox.grid_configure(column=1 ,row=3, padx=10)
output_interface_var = ctk.IntVar()
output_interface_checkbox = ctk.CTkCheckBox(master=block_by_port_frame, text="Output Interface", variable=output_interface_var,font=('verdana', 15, 'bold'))
output_interface_checkbox.grid_configure(column=2, row=3, padx=10)
forwarding_var = ctk.IntVar()
forwarding_checkbox = ctk.CTkCheckBox(master=block_by_port_frame, text="Forwarding", variable=forwarding_var,font=('verdana', 15, 'bold'))
forwarding_checkbox.grid_configure(column=1, row=4, padx=10,pady=(5,20))
block_button = ctk.CTkButton(master=block_by_port_frame, text="Block", command=block_by_port, corner_radius=10,font=('verdana', 15, 'bold'))
block_button.grid_configure(padx=10, pady=10, columnspan=4, row=6, column=0)
block_by_port_frame.pack()
tab_control.add('ip-address-blocker')
block_by_ip_address_frame = ctk.CTkFrame(master=tab_control.tab('ip-address-blocker'))
#ip section
ip_label = ctk.CTkLabel(master=block_by_ip_address_frame, text="Block Ip Address", font=('verdana', 20, 'bold'))
ip_label.grid_configure(padx=10, pady=20, row=0, column=0, columnspan=5)
ip_entry = ctk.CTkEntry(master=block_by_ip_address_frame, placeholder_text='Enter ip address', corner_radius=10, width=250)
ip_entry.grid_configure(padx=10, pady=10, columnspan=4, row=1, column=0)
protocol_label = ctk.CTkLabel(master=block_by_ip_address_frame, text="Select Protocol :",font=('verdana', 15, 'bold'))
protocol_label.grid_configure(pady=(20,25), padx=10, column=0, row=2)
protocol_var = ctk.StringVar()
tcp_checkbox = ctk.CTkCheckBox(master=block_by_ip_address_frame, text="TCP", variable=tcp_protocol_var, onvalue="TCP", offvalue="",font=('verdana', 15, 'bold'))
tcp_checkbox.grid_configure(column=1, row=2, padx=10)
tcp_checkbox.grid_configure(column=1, row=2, padx=10)
udp_protocol_var = ctk.StringVar()
udp_checkbox = ctk.CTkCheckBox(master=block_by_ip_address_frame, text="UDP", variable=udp_protocol_var, onvalue="UDP",
offvalue="", font=('verdana', 15, 'bold'))
udp_checkbox.grid_configure(column=2, row=2, padx=10)
interface_label = ctk.CTkLabel(master=block_by_ip_address_frame, text="Select The Interface to block :",
font=('verdana', 15, 'bold'))
interface_label.grid_configure(pady=10, padx=10, column=0, row=3)
input_interface_var = ctk.IntVar()
input_interface_checkbox = ctk.CTkCheckBox(master=block_by_ip_address_frame, text="Input Interface",
variable=input_interface_var, font=('verdana', 15, 'bold'))
input_interface_checkbox.grid_configure(column=1, row=3, padx=10)
output_interface_var = ctk.IntVar()
output_interface_checkbox = ctk.CTkCheckBox(master=block_by_ip_address_frame, text="Output Interface",
variable=output_interface_var, font=('verdana', 15, 'bold'))
output_interface_checkbox.grid_configure(column=2, row=3, padx=10)
forwarding_var = ctk.IntVar()
forwarding_checkbox = ctk.CTkCheckBox(master=block_by_ip_address_frame, text="Forwarding", variable=forwarding_var,
font=('verdana', 15, 'bold'))
forwarding_checkbox.grid_configure(column=1, row=4, padx=10, pady=(5, 20))
block_button = ctk.CTkButton(master=block_by_ip_address_frame, text="Block", command=block_by_port, corner_radius=10,
font=('verdana', 15, 'bold'))
block_button.grid_configure(padx=10, pady=10, columnspan=4, row=6, column=0)
block_by_ip_address_frame.pack()
# Block by Domain Name Section
tab_control.add('domain-name-blocker')
block_by_domain_frame = ctk.CTkFrame(master=tab_control.tab('domain-name-blocker'))
domain_label = ctk.CTkLabel(master=block_by_domain_frame, text="Domain Name Blocker",font=('verdana', 20, 'bold'))
domain_label.grid_configure(padx=10, pady=10, row=0, column=0)
domain_entry = ctk.CTkEntry(master=block_by_domain_frame, placeholder_text='Enter Domain Name', width=220, corner_radius=10)
domain_entry.grid_configure(padx=10, pady=10, row=1, column=0)
block_button = ctk.CTkButton(master=block_by_domain_frame, text="Block", command=block_by_domain, corner_radius=10)
block_button.grid_configure(padx=10, pady=10, row=2, column=0, columnspan=2)
block_history_frame = ctk.CTkScrollableFrame(master=block_by_domain_frame, width=560, height=200, corner_radius=10)
block_history_frame.grid(column=0,row=4)
block_by_domain_frame.pack()
# block_by_port_frame.pack()
tab_control.pack()
return firewall_frame