-
Notifications
You must be signed in to change notification settings - Fork 0
/
nft_simulator.py
176 lines (145 loc) · 5.22 KB
/
nft_simulator.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/python3
import sys
def uniq(seq):
seen = []
return [x for x in seq if not (x in seen or seen.append(x))]
def get_chain(entry):
return entry["chain"]
def get_rule(entry):
return entry["rule"]
def chain_priority(entry):
return entry["chain"]["prio"]
def is_chain(entry, family, table_name, chain_name):
if "chain" not in entry:
return False
if entry["chain"]["family"] != family:
return False
if entry["chain"]["table"] != table_name:
return False
if entry["chain"]["name"] != chain_name:
return False
return True
def is_rule_in_chain(entry, family, table_name, chain_name):
if "rule" not in entry:
return False
if "chain" not in entry["rule"]:
return False
if entry["rule"]["chain"] != chain_name:
return False
if entry["rule"]["table"] != table_name:
return False
if entry["rule"]["family"] != family:
return False
return True
def is_input_chain(entry):
if "chain" not in entry:
return False
if "hook" not in entry["chain"]:
return False
if "type" not in entry["chain"]:
return False
if entry["chain"]["hook"] != "input":
return False
if entry["chain"]["type"] != "filter":
return False
return True
class PacketSimulation:
def __init__(self, nftables, packet_exprs, print_trace=False):
self.nftables = nftables
self.packet_exprs = packet_exprs
self.compared_exprs = []
self.print_trace = print_trace
def trace(self, msg):
if self.print_trace:
print("trace: {msg}",
file=sys.stderr)
def eval_nftables(self):
self.trace("eval_nftables")
ip6_expr = {
"match": {
"op": "==",
"left": {
"meta": {
"key": "nfproto"
}
},
"right": "ipv6"
}
}
families = [
"inet",
"ip6" if self.eval_expr(ip6_expr) else "ip"
]
for chain in map(get_chain, sorted(filter(is_input_chain, self.nftables), key=chain_priority)):
if chain["family"] not in families:
continue
verdict = self.eval_chain(chain["family"],
chain["table"],
chain["name"])
if verdict == "drop":
return verdict
return "accept"
def eval_rule(self, family, table_name, rule):
self.trace(f" eval_rule {family} {table_name}")
verdict = self.eval_rule_real(family, table_name, rule)
self.trace(f" eval_rule {family} {table_name} => {verdict}")
return verdict
def eval_rule_real(self, family, table_name, rule):
eval_true = True
for expr in rule["expr"][:-1]:
if not self.eval_expr(expr):
eval_true = False
break
if not eval_true:
return None
operation = rule["expr"][-1]
if "log" in operation:
return None
elif "drop" in operation:
return "drop"
elif "xt" in operation and operation["xt"]["type"] == "target" and operation["xt"]["name"] == "REJECT":
return "drop"
elif "accept" in operation:
return "accept"
elif "counter" in operation:
return None
elif "jump" in operation:
return self.eval_chain(family,
table_name,
operation["jump"]["target"])
elif "return" in operation:
return "return"
elif "xt" in operation and operation["xt"]["type"] == "target" and operation["xt"]["name"] == "LOG":
return None
else:
print(f"Unsupported operation: {operation}",
file=sys.stderr)
sys.exit(1)
def eval_chain(self, family, table_name, chain_name):
self.trace(f" eval_chain {family} {table_name} {chain_name}")
verdict = None
policy = list(map(get_chain, filter(lambda entry: is_chain(entry, family, table_name, chain_name), self.nftables)))[0].get("policy", None)
for rule in map(get_rule, filter(lambda entry: is_rule_in_chain(entry, family, table_name, chain_name), self.nftables)):
verdict = self.eval_rule(family, table_name, rule)
if verdict is not None:
break
if verdict is None:
verdict = policy
self.trace(f" eval_chain {family} {table_name} {chain_name} => {verdict}")
if verdict == "return":
return None
else:
return verdict
def eval_expr(self, expr):
if "counter" in expr:
return True
if "limit" in expr:
return True
self.compared_exprs.append(expr)
ret = expr in self.packet_exprs
self.trace(f" eval_expr {expr} => {ret}")
return ret
def simulate(nftables, packet_exprs, print_trace=False):
simulation = PacketSimulation(nftables, packet_exprs, print_trace=print_trace)
verdict = simulation.eval_nftables()
return verdict, uniq(simulation.compared_exprs)