-
Notifications
You must be signed in to change notification settings - Fork 183
/
easy.py
461 lines (406 loc) · 16.9 KB
/
easy.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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# -*- coding: utf-8 -*-
# TODO:
# - Add documentation
# - Add HowToUse examples
from .ip4tc import Rule, Table, Chain, IPTCError
from .ip6tc import Rule6, Table6
_BATCH_MODE = False
def flush_all(ipv6=False):
""" Flush all available tables """
for table in get_tables(ipv6):
flush_table(table, ipv6)
def flush_table(table, ipv6=False, raise_exc=True):
""" Flush a table """
try:
iptc_table = _iptc_gettable(table, ipv6)
iptc_table.flush()
except Exception as e:
if raise_exc: raise
def flush_chain(table, chain, ipv6=False, raise_exc=True):
""" Flush a chain in table """
try:
iptc_chain = _iptc_getchain(table, chain, ipv6)
iptc_chain.flush()
except Exception as e:
if raise_exc: raise
def zero_all(table, ipv6=False):
""" Zero all tables """
for table in get_tables(ipv6):
zero_table(table, ipv6)
def zero_table(table, ipv6=False):
""" Zero a table """
iptc_table = _iptc_gettable(table, ipv6)
iptc_table.zero_entries()
def zero_chain(table, chain, ipv6=False):
""" Zero a chain in table """
iptc_chain = _iptc_getchain(table, chain, ipv6)
iptc_chain.zero_counters()
def has_chain(table, chain, ipv6=False):
""" Return True if chain exists in table False otherwise """
return _iptc_gettable(table, ipv6).is_chain(chain)
def has_rule(table, chain, rule_d, ipv6=False):
""" Return True if rule exists in chain False otherwise """
iptc_chain = _iptc_getchain(table, chain, ipv6)
iptc_rule = encode_iptc_rule(rule_d, ipv6)
return iptc_rule in iptc_chain.rules
def add_chain(table, chain, ipv6=False, raise_exc=True):
""" Return True if chain was added successfully to a table, raise Exception otherwise """
try:
iptc_table = _iptc_gettable(table, ipv6)
iptc_table.create_chain(chain)
return True
except Exception as e:
if raise_exc: raise
return False
def add_rule(table, chain, rule_d, position=0, ipv6=False):
""" Add a rule to a chain in a given position. 0=append, 1=first, n=nth position """
iptc_chain = _iptc_getchain(table, chain, ipv6)
iptc_rule = encode_iptc_rule(rule_d, ipv6)
if position == 0:
# Insert rule in last position -> append
iptc_chain.append_rule(iptc_rule)
elif position > 0:
# Insert rule in given position -> adjusted as iptables CLI
iptc_chain.insert_rule(iptc_rule, position - 1)
elif position < 0:
# Insert rule in given position starting from bottom -> not available in iptables CLI
nof_rules = len(iptc_chain.rules)
_position = position + nof_rules
# Insert at the top if the position has looped over
if _position <= 0:
_position = 0
iptc_chain.insert_rule(iptc_rule, _position)
def insert_rule(table, chain, rule_d, ipv6=False):
""" Add a rule to a chain in the 1st position """
add_rule(table, chain, rule_d, position=1, ipv6=ipv6)
def delete_chain(table, chain, ipv6=False, flush=False, raise_exc=True):
""" Delete a chain """
try:
if flush:
flush_chain(table, chain, ipv6, raise_exc)
iptc_table = _iptc_gettable(table, ipv6)
iptc_table.delete_chain(chain)
except Exception as e:
if raise_exc: raise
def delete_rule(table, chain, rule_d, ipv6=False, raise_exc=True):
""" Delete a rule from a chain """
try:
iptc_chain = _iptc_getchain(table, chain, ipv6)
iptc_rule = encode_iptc_rule(rule_d, ipv6)
iptc_chain.delete_rule(iptc_rule)
except Exception as e:
if raise_exc: raise
def get_tables(ipv6=False):
""" Get all tables """
iptc_tables = _iptc_gettables(ipv6)
return [t.name for t in iptc_tables]
def get_chains(table, ipv6=False):
""" Return the existing chains of a table """
iptc_table = _iptc_gettable(table, ipv6)
return [iptc_chain.name for iptc_chain in iptc_table.chains]
def get_rule(table, chain, position=0, ipv6=False, raise_exc=True):
""" Get a rule from a chain in a given position. 0=all rules, 1=first, n=nth position """
try:
if position == 0:
# Return all rules
return dump_chain(table, chain, ipv6)
elif position > 0:
# Return specific rule by position
iptc_chain = _iptc_getchain(table, chain, ipv6)
iptc_rule = iptc_chain.rules[position - 1]
return decode_iptc_rule(iptc_rule, ipv6)
elif position < 0:
# Return last rule -> not available in iptables CLI
iptc_chain = _iptc_getchain(table, chain, ipv6)
iptc_rule = iptc_chain.rules[position]
return decode_iptc_rule(iptc_rule, ipv6)
except Exception as e:
if raise_exc: raise
def replace_rule(table, chain, old_rule_d, new_rule_d, ipv6=False):
""" Replaces an existing rule of a chain """
iptc_chain = _iptc_getchain(table, chain, ipv6)
iptc_old_rule = encode_iptc_rule(old_rule_d, ipv6)
iptc_new_rule = encode_iptc_rule(new_rule_d, ipv6)
iptc_chain.replace_rule(iptc_new_rule, iptc_chain.rules.index(iptc_old_rule))
def get_rule_counters(table, chain, rule_d, ipv6=False):
""" Return a tuple with the rule counters (numberOfBytes, numberOfPackets) """
if not has_rule(table, chain, rule_d, ipv6):
raise AttributeError('Chain <{}@{}> has no rule <{}>'.format(chain, table, rule_d))
iptc_chain = _iptc_getchain(table, chain, ipv6)
iptc_rule = encode_iptc_rule(rule_d, ipv6)
iptc_rule_index = iptc_chain.rules.index(iptc_rule)
return iptc_chain.rules[iptc_rule_index].get_counters()
def get_rule_position(table, chain, rule_d, ipv6=False):
""" Return the position of a rule within a chain """
if not has_rule(table, chain, rule_d):
raise AttributeError('Chain <{}@{}> has no rule <{}>'.format(chain, table, rule_d))
iptc_chain = _iptc_getchain(table, chain, ipv6)
iptc_rule = encode_iptc_rule(rule_d, ipv6)
return iptc_chain.rules.index(iptc_rule)
def test_rule(rule_d, ipv6=False):
""" Return True if the rule is a well-formed dictionary, False otherwise """
try:
encode_iptc_rule(rule_d, ipv6)
return True
except:
return False
def test_match(name, value, ipv6=False):
""" Return True if the match is valid, False otherwise """
try:
iptc_rule = Rule6() if ipv6 else Rule()
_iptc_setmatch(iptc_rule, name, value)
return True
except:
return False
def test_target(name, value, ipv6=False):
""" Return True if the target is valid, False otherwise """
try:
iptc_rule = Rule6() if ipv6 else Rule()
_iptc_settarget(iptc_rule, {name:value})
return True
except:
return False
def get_policy(table, chain, ipv6=False):
""" Return the default policy of chain in a table """
iptc_chain = _iptc_getchain(table, chain, ipv6)
return iptc_chain.get_policy().name
def set_policy(table, chain, policy='ACCEPT', ipv6=False):
""" Set the default policy of chain in a table """
iptc_chain = _iptc_getchain(table, chain, ipv6)
iptc_chain.set_policy(policy)
def dump_all(ipv6=False):
""" Return a dictionary representation of all tables """
return {table: dump_table(table, ipv6) for table in get_tables(ipv6)}
def dump_table(table, ipv6=False):
""" Return a dictionary representation of a table """
return {chain: dump_chain(table, chain, ipv6) for chain in get_chains(table, ipv6)}
def dump_chain(table, chain, ipv6=False):
""" Return a list with the dictionary representation of the rules of a table """
iptc_chain = _iptc_getchain(table, chain, ipv6)
return [decode_iptc_rule(iptc_rule, ipv6) for iptc_rule in iptc_chain.rules]
def batch_begin(table = None, ipv6=False):
""" Disable autocommit on a table """
_BATCH_MODE = True
if table:
tables = (table, )
else:
tables = get_tables(ipv6)
for table in tables:
iptc_table = _iptc_gettable(table, ipv6)
iptc_table.autocommit = False
def batch_end(table = None, ipv6=False):
""" Enable autocommit on table and commit changes """
_BATCH_MODE = False
if table:
tables = (table, )
else:
tables = get_tables(ipv6)
for table in tables:
iptc_table = _iptc_gettable(table, ipv6)
iptc_table.autocommit = True
def batch_add_chains(table, chains, ipv6=False, flush=True):
""" Add multiple chains to a table """
iptc_table = _batch_begin_table(table, ipv6)
for chain in chains:
if iptc_table.is_chain(chain):
iptc_chain = Chain(iptc_table, chain)
else:
iptc_chain = iptc_table.create_chain(chain)
if flush:
iptc_chain.flush()
_batch_end_table(table, ipv6)
def batch_delete_chains(table, chains, ipv6=False):
""" Delete multiple chains of a table """
iptc_table = _batch_begin_table(table, ipv6)
for chain in chains:
if iptc_table.is_chain(chain):
iptc_chain = Chain(iptc_table, chain)
iptc_chain.flush()
iptc_table.delete_chain(chain)
_batch_end_table(table, ipv6)
def batch_add_rules(table, batch_rules, ipv6=False):
""" Add multiple rules to a table with format (chain, rule_d, position) """
iptc_table = _batch_begin_table(table, ipv6)
for (chain, rule_d, position) in batch_rules:
iptc_chain = Chain(iptc_table, chain)
iptc_rule = encode_iptc_rule(rule_d, ipv6)
if position == 0:
# Insert rule in last position -> append
iptc_chain.append_rule(iptc_rule)
elif position > 0:
# Insert rule in given position -> adjusted as iptables CLI
iptc_chain.insert_rule(iptc_rule, position-1)
elif position < 0:
# Insert rule in given position starting from bottom -> not available in iptables CLI
nof_rules = len(iptc_chain.rules)
iptc_chain.insert_rule(iptc_rule, position + nof_rules)
_batch_end_table(table, ipv6)
def batch_delete_rules(table, batch_rules, ipv6=False, raise_exc=True):
""" Delete multiple rules from table with format (chain, rule_d) """
try:
iptc_table = _batch_begin_table(table, ipv6)
for (chain, rule_d) in batch_rules:
iptc_chain = Chain(iptc_table, chain)
iptc_rule = encode_iptc_rule(rule_d, ipv6)
iptc_chain.delete_rule(iptc_rule)
_batch_end_table(table, ipv6)
except Exception as e:
if raise_exc: raise
def encode_iptc_rule(rule_d, ipv6=False):
""" Return a Rule(6) object from the input dictionary """
# Sanity check
assert(isinstance(rule_d, dict))
# Basic rule attributes
rule_attr = ('src', 'dst', 'protocol', 'in-interface', 'out-interface', 'fragment')
iptc_rule = Rule6() if ipv6 else Rule()
# Set default target
rule_d.setdefault('target', '')
# Avoid issues with matches that require basic parameters to be configured first
for name in rule_attr:
if name in rule_d:
setattr(iptc_rule, name.replace('-', '_'), rule_d[name])
for name, value in rule_d.items():
try:
if name in rule_attr:
continue
elif name == 'counters':
_iptc_setcounters(iptc_rule, value)
elif name == 'target':
_iptc_settarget(iptc_rule, value)
else:
_iptc_setmatch(iptc_rule, name, value)
except Exception as e:
#print('Ignoring unsupported field <{}:{}>'.format(name, value))
continue
return iptc_rule
def decode_iptc_rule(iptc_rule, ipv6=False):
""" Return a dictionary representation of the Rule(6) object
Note: host IP addresses are appended their corresponding CIDR """
d = {}
if ipv6==False and iptc_rule.src != '0.0.0.0/0.0.0.0':
_ip, _netmask = iptc_rule.src.split('/')
_netmask = _netmask_v4_to_cidr(_netmask)
d['src'] = '{}/{}'.format(_ip, _netmask)
elif ipv6==True and iptc_rule.src != '::/0':
d['src'] = iptc_rule.src
if ipv6==False and iptc_rule.dst != '0.0.0.0/0.0.0.0':
_ip, _netmask = iptc_rule.dst.split('/')
_netmask = _netmask_v4_to_cidr(_netmask)
d['dst'] = '{}/{}'.format(_ip, _netmask)
elif ipv6==True and iptc_rule.dst != '::/0':
d['dst'] = iptc_rule.dst
if iptc_rule.protocol != 'ip':
d['protocol'] = iptc_rule.protocol
if iptc_rule.in_interface is not None:
d['in-interface'] = iptc_rule.in_interface
if iptc_rule.out_interface is not None:
d['out-interface'] = iptc_rule.out_interface
if ipv6 == False and iptc_rule.fragment:
d['fragment'] = iptc_rule.fragment
for m in iptc_rule.matches:
if m.name not in d:
d[m.name] = m.get_all_parameters()
elif isinstance(d[m.name], list):
d[m.name].append(m.get_all_parameters())
else:
d[m.name] = [d[m.name], m.get_all_parameters()]
if iptc_rule.target and iptc_rule.target.name and len(iptc_rule.target.get_all_parameters()):
name = iptc_rule.target.name.replace('-', '_')
d['target'] = {name:iptc_rule.target.get_all_parameters()}
elif iptc_rule.target and iptc_rule.target.name:
if iptc_rule.target.goto:
d['target'] = {'goto':iptc_rule.target.name}
else:
d['target'] = iptc_rule.target.name
# Get counters
d['counters'] = iptc_rule.counters
# Return a filtered dictionary
return _filter_empty_field(d)
### INTERNAL FUNCTIONS ###
def _iptc_table_available(table, ipv6=False):
""" Return True if the table is available, False otherwise """
try:
iptc_table = Table6(table) if ipv6 else Table(table)
return True
except:
return False
def _iptc_gettables(ipv6=False):
""" Return an updated view of all available iptc_table """
iptc_cls = Table6 if ipv6 else Table
return [_iptc_gettable(t, ipv6) for t in iptc_cls.ALL if _iptc_table_available(t, ipv6)]
def _iptc_gettable(table, ipv6=False):
""" Return an updated view of an iptc_table """
iptc_table = Table6(table) if ipv6 else Table(table)
if _BATCH_MODE is False:
iptc_table.commit()
iptc_table.refresh()
return iptc_table
def _iptc_getchain(table, chain, ipv6=False, raise_exc=True):
""" Return an iptc_chain of an updated table """
try:
iptc_table = _iptc_gettable(table, ipv6)
if not iptc_table.is_chain(chain):
raise AttributeError('Table <{}> has no chain <{}>'.format(table, chain))
return Chain(iptc_table, chain)
except Exception as e:
if raise_exc: raise
def _iptc_setcounters(iptc_rule, value):
# Value is a tuple (numberOfBytes, numberOfPackets)
iptc_rule.counters = value
def _iptc_setmatch(iptc_rule, name, value):
# Iterate list/tuple recursively
if isinstance(value, list) or isinstance(value, tuple):
for inner_value in value:
_iptc_setmatch(iptc_rule, name, inner_value)
# Assign dictionary value
elif isinstance(value, dict):
iptc_match = iptc_rule.create_match(name)
[iptc_match.set_parameter(k, v) for k, v in value.items()]
# Assign value directly
else:
iptc_match = iptc_rule.create_match(name)
iptc_match.set_parameter(name, value)
def _iptc_settarget(iptc_rule, value):
# Target is dictionary - Use only 1st pair key/value
if isinstance(value, dict):
t_name, t_value = next(iter(value.items()))
if t_name == 'goto':
iptc_target = iptc_rule.create_target(t_value, goto=True)
else:
iptc_target = iptc_rule.create_target(t_name)
[iptc_target.set_parameter(k, v) for k, v in t_value.items()]
# Simple target
else:
iptc_target = iptc_rule.create_target(value)
def _batch_begin_table(table, ipv6=False):
""" Disable autocommit on a table """
iptc_table = _iptc_gettable(table, ipv6)
iptc_table.autocommit = False
return iptc_table
def _batch_end_table(table, ipv6=False):
""" Enable autocommit on table and commit changes """
iptc_table = _iptc_gettable(table, ipv6)
iptc_table.autocommit = True
return iptc_table
def _filter_empty_field(data_d):
"""
Remove empty lists from dictionary values
Before: {'target': {'CHECKSUM': {'checksum-fill': []}}}
After: {'target': {'CHECKSUM': {'checksum-fill': ''}}}
Before: {'tcp': {'dport': ['22']}}}
After: {'tcp': {'dport': '22'}}}
"""
for k, v in data_d.items():
if isinstance(v, dict):
data_d[k] = _filter_empty_field(v)
elif isinstance(v, list) and len(v) != 0:
v = [_filter_empty_field(_v) if isinstance(_v, dict) else _v for _v in v ]
if isinstance(v, list) and len(v) == 1:
data_d[k] = v.pop()
elif isinstance(v, list) and len(v) == 0:
data_d[k] = ''
return data_d
def _netmask_v4_to_cidr(netmask_addr):
# Implement Subnet Mask conversion without dependencies
return sum([bin(int(x)).count('1') for x in netmask_addr.split('.')])
### /INTERNAL FUNCTIONS ###