-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpolicyanalyzer.py
More file actions
433 lines (348 loc) · 14.2 KB
/
Copy pathpolicyanalyzer.py
File metadata and controls
433 lines (348 loc) · 14.2 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# -*- coding: utf-8 -*-
"""
Copyright 2021-2025 Maen Artimy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import re
import ipaddress
from definitions import PORTS, PROTOCOLS, RRule, RField, Anomaly
from parsing import parse_ports_string, protocols_to_numbers
UniversalSet = {0}
ANY = "ANY"
class Action:
"""
A Rule's action
"""
_permit = ["PERMIT", "ALLOW", "ACCEPT", "PASS"]
_deny = ["DENY", "REJECT", "DROP"]
def __init__(self, action: bool):
# Do not use this constructor directly, use get_port() instead
self.action = action
def __eq__(self, other):
if isinstance(other, Action):
return self.action == other.action
return False
def __repr__(self):
return "ACCEPT" if self.action else "DENY"
@classmethod
def get_action(cls, action):
if isinstance(action, str):
if action.upper() in Action._permit:
return cls(True)
if action.upper() in Action._deny:
return cls(False)
raise ValueError(f"Invalid action '{action}'.")
class PortSet:
"""
A TCP/UDP Port
"""
def __init__(self, ports):
# Do not use this constructor directly, use get_port() instead
self.port_set = ports
def __eq__(self, other):
return self.port_set == other.port_set
def __repr__(self):
if self.port_set == UniversalSet:
return ANY
elif len(self.port_set) == 1:
return str(next(iter(self.port_set)))
return str(self.port_set)
def superset_of(self, other):
if self.port_set == UniversalSet: # a Universal Set is a superset of any set
return True
if other.port_set == UniversalSet:
return False
return self.port_set.issuperset(other.port_set)
def subset_of(self, other):
if other.port_set == UniversalSet:
return True
if self.port_set == UniversalSet:
return False
return self.port_set.issubset(other.port_set)
@classmethod
def get_port(cls, ports_string):
if isinstance(ports_string, str) and ports_string.strip().upper() == ANY:
return cls(UniversalSet) # represents a Universal Set
else:
a_set = parse_ports_string(ports_string)
ports = protocols_to_numbers(a_set, PORTS)
return cls(ports)
class Protocol:
"""
A Protcol
"""
def __init__(self, protocol):
# Do not use this constructor directly, use get_protocol() instead
self.protocol = protocol.upper()
def __eq__(self, other):
return self.protocol == other.protocol
def __repr__(self):
return self.protocol
def superset_of(self, other):
return self.protocol == "IP" and other.protocol in PROTOCOLS
def subset_of(self, other):
return self.protocol in PROTOCOLS and other.protocol == "IP"
@classmethod
def get_protocol(cls, protocol):
if not isinstance(protocol, str):
raise ValueError(f"Not a recognized protocol '{protocol}'")
protocol = "IP" if protocol.upper() == ANY else protocol.upper()
if protocol not in PROTOCOLS:
raise ValueError(f"Not a recognized protocol '{protocol}'")
return cls(protocol)
class Address:
"""
An IPv4 Address
"""
@classmethod
def get_address(cls, address):
if address.upper() == ANY:
address = "0.0.0.0/0"
return ipaddress.ip_interface(address).network
class Interface:
"""
An Interface
"""
def __init__(self, interface):
# Do not use this constructor directly, use get_port() instead
self.interface = interface
def __eq__(self, other):
return self.interface == other.interface
def __repr__(self):
return self.interface
def superset_of(self, other):
return self.interface == ANY
def subset_of(self, other):
return other.interface == ANY
@classmethod
def get_interface(cls, interface):
if not isinstance(interface, str):
raise ValueError(f"Not a valid interface '{interface}'")
# Validate the address
if not re.match(r"^[A-Za-z][A-Za-z0-9_-]*$", interface):
raise ValueError(
f"Interface '{interface}' must start with a letter and can only include letters, digits, '_' or '-'"
)
return cls(interface.upper())
class Packet:
"""
Packet header information
"""
def __init__(self, protocol, src, s_port, dst, d_port, *, interface=ANY):
self.fields = {
"interface": Interface.get_interface(interface.strip()),
"protocol": Protocol.get_protocol(protocol.strip()),
"src": Address.get_address(src.strip()),
"sport": PortSet.get_port(s_port.strip()),
"dst": Address.get_address(dst.strip()),
"dport": PortSet.get_port(d_port.strip()),
}
def __repr__(self):
return ",".join(map(str, self.fields.values()))
class Policy(Packet):
"""
Firewall Policy
"""
def __init__(self, **policy_fields):
interface = policy_fields.get("interface", ANY)
protocol = policy_fields.get("protocol", ANY)
src = policy_fields.get("src", ANY)
s_port = policy_fields.get("s_port", ANY)
dst = policy_fields.get("dst", ANY)
d_port = policy_fields.get("d_port", ANY)
super().__init__(protocol, src, s_port, dst, d_port, interface=interface)
self.action = Action.get_action(policy_fields.get("action"))
def compare_two_fields(self, a, b):
"""
Get the relation between two policy fields.
"""
if a == b:
return RField.EQUAL
if a.subset_of(b):
return RField.STRICT_SUBSET if a != b else RField.SUBSET
if a.superset_of(b):
return RField.STRICT_SUPERSET if a != b else RField.SUPERSET
return RField.UNEQUAL
def compare_two_addresses(self, a, b):
"""
Get the relation between two policy fields representing IP addresses.
"""
if a == b:
return RField.EQUAL
if a.subnet_of(b):
return RField.STRICT_SUBSET if a != b else RField.SUBSET
if a.supernet_of(b):
return RField.STRICT_SUPERSET if a != b else RField.SUPERSET
return RField.UNEQUAL
def compare_fields(self, other):
# compare fields with another policy or packet
return [
self.compare_two_fields(
self.fields["interface"], other.fields["interface"]
),
self.compare_two_fields(self.fields["protocol"], other.fields["protocol"]),
self.compare_two_addresses(self.fields["src"], other.fields["src"]),
self.compare_two_fields(self.fields["sport"], other.fields["sport"]),
self.compare_two_addresses(self.fields["dst"], other.fields["dst"]),
self.compare_two_fields(self.fields["dport"], other.fields["dport"]),
]
def compare_actions(self, other):
return self.action == other.action
def is_exact_match(self, relations):
return all(f is RField.EQUAL for f in relations)
def is_inclusive_match_superset(self, relations):
condition1 = all(
relation in {RField.EQUAL, RField.SUPERSET, RField.STRICT_SUPERSET}
for relation in relations
)
condition2 = any(relation in {RField.STRICT_SUPERSET} for relation in relations)
return condition1 and condition2
def is_inclusive_match_subset(self, relations):
condition1 = all(
relation in {RField.EQUAL, RField.SUBSET, RField.STRICT_SUBSET}
for relation in relations
)
condition2 = any(relation in {RField.STRICT_SUBSET} for relation in relations)
return condition1 and condition2
def is_corrlated_relation(self, relations):
condition1 = all(
relation
in {
RField.EQUAL,
RField.SUBSET,
RField.SUPERSET,
RField.STRICT_SUBSET,
RField.STRICT_SUPERSET,
}
for relation in relations
)
condition2 = any(relation in {RField.STRICT_SUBSET} for relation in relations)
condition3 = any(relation in {RField.STRICT_SUPERSET} for relation in relations)
return condition1 and condition2 and condition3
def is_partial_disjoint(self, relations):
condition1 = any(relation in {RField.EQUAL} for relation in relations)
condition2 = any(relation not in {RField.EQUAL} for relation in relations)
return condition1 and condition2
def is_disjoint(self, relations):
return all(f is RField.UNEQUAL for f in relations)
def get_rule_relation(self, other):
"""
Determines the relationship between two rules based on field comparisons.
Returns the resulting relationship between the two rules.
"""
# Compare the fields of the two rules
relations = self.compare_fields(other)
# Map condition checks to rule relations
relation_checks = [
(self.is_exact_match, RRule.EM), # Exact match
(self.is_inclusive_match_superset, RRule.IMP), # Inclusive match (superset)
(self.is_inclusive_match_subset, RRule.IMB), # Inclusive match (subset)
(self.is_corrlated_relation, RRule.CC), # Correlated relation
(self.is_partial_disjoint, RRule.PD), # Partial disjoint
(self.is_disjoint, RRule.CD), # Completely disjoint
]
# Iterate through checks and return the first match
for check, rule_relation in relation_checks:
# call the check function
if check(relations):
return rule_relation
# Unknwon if no condition matches (fallback)
return RRule.UN
def is_match(self, packet):
# the packet matches this policy if all fields in policy are
# equal or supersets of the packet fields
return all(
f in [RField.SUPERSET, RField.EQUAL] for f in self.compare_fields(packet)
)
def get_action(self):
return self.action
def __repr__(self):
return ",".join(map(str, self.fields.values())) + "," + str(self.action)
class PolicyAnalyzer:
"""
Firewall Policy Analyzer
"""
anamoly = {
(RRule.IMB, False): Anomaly.GEN,
(RRule.IMP, False): Anomaly.SHD,
(RRule.CC, False): Anomaly.COR,
(RRule.IMP, True): Anomaly.RYD,
(RRule.EM, True): Anomaly.RYD,
(RRule.IMB, True): Anomaly.RXD,
}
def __init__(self, policies):
self.policies = policies
def _get_anamoly(self, rule_relation, same_action):
return PolicyAnalyzer.anamoly.get((rule_relation, same_action), Anomaly.AOK)
def get_relations(self):
# compare each policy with the previous ones
rule_relations = {}
for y, y_policy in enumerate(self.policies):
rule_relations[y] = [
(x, x_policy.get_rule_relation(y_policy))
for x, x_policy in enumerate(self.policies[0:y])
]
return rule_relations
def get_action_relations(self):
# compare each policy action with the previous ones
rule_action_relations = {}
for y, y_policy in enumerate(self.policies):
rule_action_relations[y] = [
x_policy.compare_actions(y_policy) for x_policy in self.policies[0:y]
]
return rule_action_relations
# def get_anomalies(self):
# anomalies = {}
# rule_relations = self.get_relations()
# action_relations = self.get_action_relations()
# for ry, ry_relations in rule_relations.items():
# for rx, relation in ry_relations:
# anamoly = self._get_anamoly(relation, action_relations[ry][rx])
# if anamoly is Anomaly.RXD:
# # check the rules in between for additional conditions
# for rz in range(rx + 1, ry):
# if any(
# a == rx
# and not action_relations[rz][rx]
# and b in [RRule.CC, RRule.IMB]
# for a, b in rule_relations[rz]
# ):
# anamoly = Anomaly.AOK
# break
# if anamoly is not Anomaly.AOK:
# anomalies.setdefault(ry, []).append((rx, anamoly))
# return anomalies
def get_anomalies(self):
anomalies = {}
rule_relations = self.get_relations()
action_relations = self.get_action_relations()
for y_rule, y_relations in rule_relations.items():
for x_rule, relation in y_relations:
anomaly = self._get_anamoly(relation, action_relations[y_rule][x_rule])
if anomaly is Anomaly.RXD:
# Check intermediate rules for additional conditions
for intermediate_rule in range(x_rule + 1, y_rule):
if any(
src == x_rule
and not action_relations[intermediate_rule][x_rule]
and rel in [RRule.CC, RRule.IMB]
for src, rel in rule_relations[intermediate_rule]
):
anomaly = Anomaly.AOK
break
if anomaly is not Anomaly.AOK:
anomalies.setdefault(y_rule, []).append((x_rule, anomaly))
return anomalies
def get_first_match(self, packet):
for i, policy in enumerate(self.policies):
if policy.is_match(packet):
return i, policy
return None