-
Notifications
You must be signed in to change notification settings - Fork 1
/
ACL.py
411 lines (373 loc) · 16.5 KB
/
ACL.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
import argparse
import collections
import copy
import json
import os
import pprint
import re
import pandas as pd
import plotly
import plotly.graph_objs as go
from docopt import docopt
from matplotlib import cm
from matplotlib.colors import rgb2hex
from munkres import Munkres
from operator import itemgetter
import commonFunctions
import PrefixList
LINE_PENALTY = 40
ATTRIBUTES = 20
LINENUM = -1 # Line number attribute id in a line
class Block:
""" Class to represent information about a IPV4 ACL block.
:ivar lineNum: The line number in the sequence of blocks.
:ivar blockJson: The representation of a parsed block in JSON.
:ivar action: Whether its a permit or deny block.
"""
def __init__(self, lineNum, action, blockJson):
self.action = {}
# Action doesn't have lineNum for ACLs.
self.action["type"] = action
self.lines = list()
for line in blockJson:
line[LINENUM] = lineNum[0]
lineNum[0] += 1
self.lines.append(line)
class ACL:
""" The ACL Class - Defined in a generic way to fit the StructuredGeneralization but it always has one block. """
def __init__(self, aclName, device, ACLJson, configurationFormat):
"""
:param ACLJson: Parsed Batfish JSON representation of the ACL
:param routerName: the router in which this ACL was found
:param ACLName: the ACL name
:param configurationFormat: The vendor specific format language
"""
self.name = aclName
self.deviceName = device
self.blocks = list()
self.configurationFormat = configurationFormat
self.createBlocks(ACLJson)
def getSrcOrDstIps(self, headerSpace, IPtype):
ips = ["0.0.0.0/0"]
if headerSpace[IPtype]:
if "IpWildcardIpSpace" in headerSpace[IPtype]['class']:
ips = [headerSpace[IPtype]["ipWildcard"]]
elif "PrefixIpSpace" in headerSpace[IPtype]['class']:
ips = [headerSpace[IPtype]["prefix"]]
elif "AclIpSpace" in headerSpace[IPtype]['class']:
ips = []
for dstLine in headerSpace[IPtype]["lines"]:
if "PERMIT" != dstLine["action"]:
print(
"Unhandled ACTION mismatch in Juniper ACL {} for {}".format(self.name, self.deviceName))
raise NotImplementedError
else:
ips.append(dstLine["ipSpace"]["ipWildcard"])
elif "UniverseIpSpace" in headerSpace[IPtype]["class"]:
ips = ["0.0.0.0/0"]
elif "IpIpSpace" in headerSpace[IPtype]["class"]:
ips = [headerSpace[IPtype]["ip"]+"/32"]
else:
print("Unknown {} format: ACL {} for {}".format(
IPtype, self.name, self.deviceName))
raise NotImplementedError
return ips
def updatePorts(self, headerSpace, srcPorts, dstPorts):
if len(headerSpace["dstPorts"]):
if len(dstPorts):
raise TypeError
else:
dstPorts = headerSpace["dstPorts"]
if len(headerSpace["srcPorts"]):
if len(srcPorts):
raise TypeError
else:
srcPorts = headerSpace["srcPorts"]
return srcPorts, dstPorts
def processACLDisjunctsConjuncts(self, entities, protocols, srcIps, dstIps, srcPorts, dstPorts):
for entity in entities:
if "disjuncts" in entity:
protocols, srcIps, dstIps, srcPorts, dstPorts = self.processACLDisjunctsConjuncts(
entity["disjuncts"], protocols, srcIps, dstIps, srcPorts, dstPorts)
elif "conjuncts" in entity:
protocols, srcIps, dstIps, srcPorts, dstPorts = self.processACLDisjunctsConjuncts(
entity["conjuncts"], protocols, srcIps, dstIps, srcPorts, dstPorts)
elif "headerSpace" in entity:
srcPorts, dstPorts = self.updatePorts(
entity["headerSpace"], srcPorts, dstPorts)
tmp = self.getSrcOrDstIps(entity["headerSpace"], "srcIps")
if tmp != ["0.0.0.0/0"]:
srcIps.extend(tmp)
tmp = self.getSrcOrDstIps(entity["headerSpace"], "dstIps")
if tmp != ["0.0.0.0/0"]:
srcIps.extend(tmp)
if len(entity["headerSpace"]["ipProtocols"]) > 0:
protocols = entity["headerSpace"]["ipProtocols"]
elif "FalseExpr" in entity["class"] or "TrueExpr" in entity["class"]:
pass
else:
raise NotImplementedError
return protocols, srcIps, dstIps, srcPorts, dstPorts
def createBlocks(self, ACLJson):
presentAction = None
block = list()
startingLineNumber = 0
count = 0
for idx, line in enumerate(ACLJson['lines']):
if not presentAction:
presentAction = line['action']
elif presentAction != line['action']:
self.blocks.append(
Block([startingLineNumber], presentAction, block))
block = list()
presentAction = line['action']
startingLineNumber = count
if "headerSpace" in line["matchCondition"]:
headerSpace = line["matchCondition"]["headerSpace"]
if len(headerSpace["ipProtocols"]) > 0:
protocols = headerSpace["ipProtocols"]
else:
protocols = ["ip"]
srcIps = self.getSrcOrDstIps(headerSpace, "srcIps")
dstIps = self.getSrcOrDstIps(headerSpace, "dstIps")
dstPorts = headerSpace.get("dstPorts")
srcPorts = headerSpace.get("srcPorts")
elif "conjuncts" in line["matchCondition"]:
# First conjunct would have the protocol, srcIp, dstIp (Cisco nxos) and the next ones would have the ports
protocols, srcIps, dstIps, srcPorts, dstPorts = self.processACLDisjunctsConjuncts(
line["matchCondition"]["conjuncts"], [], [], [], [], [])
if not len(protocols):
protocols = ["ip"]
if not len(srcIps):
srcIps = ["0.0.0.0/0"]
if not len(dstIps):
dstIps = ["0.0.0.0/0"]
else:
print("Unhandled ACL Json model - ACL: {} for {}".format(
self.name, self.deviceName))
raise NotImplementedError
for protocol in protocols:
for srcPrefix in srcIps:
srcIp, srcMask = self.getIpAndMask(srcPrefix)
for dstPrefix in dstIps:
dstIp, dstMask = self.getIpAndMask(dstPrefix)
if len(dstPorts):
if len(srcPorts):
for dstPort in dstPorts:
for srcPort in srcPorts:
block.append(self.getLine(
protocol, srcIp, srcMask, dstIp, dstMask, srcPort, dstPort))
count += 1
else:
for dstPort in dstPorts:
block.append(self.getLine(
protocol, srcIp, srcMask, dstIp, dstMask, None, dstPort))
count += 1
elif len(srcPorts):
for srcPort in srcPorts:
block.append(self.getLine(
protocol, srcIp, srcMask, dstIp, dstMask, srcPort, None))
count += 1
else:
block.append(self.getLine(
protocol, srcIp, srcMask, dstIp, dstMask, None, None))
count += 1
self.blocks.append(Block([startingLineNumber], presentAction, block))
def getIpAndMask(self, prefix):
if prefix == "0.0.0.0/0":
ip = "0.0.0.0"
mask = "255.255.255.255"
elif "/" in prefix:
ip = prefix.split("/")[0]
mask = self.convertToMask(prefix.split("/")[1])
elif re.match(r'\d+.\d+.\d+.\d+$', prefix):
ip = prefix
mask = "0.0.0.0"
elif prefix == "172.20.0.0:0.0.235.255":
ip = "172.20.0.0"
mask = "0.0.255.255"
else:
print(
"IP and Mask Syntax error- ACL: {} for {}".format(self.name, self.deviceName))
raise ValueError
return ip, mask
def convertToMask(self, mask):
"""
type (int) -> str
Converts a source or destination mask from integer to wildcard notation (inverse mask)
Ex: 26 -> 0.0.0.63
"""
try:
value = int(mask)
other = 32-value
seq = ""
while(value > 0):
seq += "0"
value -= 1
while(other > 0):
seq += "1"
other -= 1
firstoctet = str(int(seq[0:8], 2))
secondoctet = str(int(seq[8:16], 2))
thirdoctet = str(int(seq[16:24], 2))
fourthoctet = str(int(seq[24:32], 2))
return firstoctet + "."+secondoctet+"."+thirdoctet+"."+fourthoctet
except:
return mask
def getLine(self, protocol, srcIp, srcMask, dstIp, dstMask, srcPort, dstPort):
"""
Returns the acl line with fields separated as a dict.
Representation based on batfish parser
Attributes:
-2 : protocol
-1 : LineNumber
0-3 : srcIp
4-7 : srcMask
8-11 : dstIp
12-15 : dstMask
16-17 : srcPort
18-19 : dstPort
"""
line = {}
line[-2] = protocol
i = 0
for octet in srcIp.split("."):
line[i] = octet
i += 1
for octet in srcMask.split("."):
line[i] = octet
i += 1
for octet in dstIp.split("."):
line[i] = octet
i += 1
for octet in dstMask.split("."):
line[i] = octet
i += 1
if srcPort:
line[i], line[i+1] = srcPort.split("-")
else:
line[i] = line[i+1] = "-1"
i += 2
if dstPort:
line[i], line[i+1] = dstPort.split("-")
else:
line[i] = line[i+1] = "-1"
return line
def GetBlockSequence(device, deviceInfo, pattern, foundDevices, emptyDefDevices, exactDefMatchMap):
""" Generates block sequence for ACL from the parsed JSON object.
:ivar device: The name of the device.
:ivar deviceInfo: The JSON model of the configuration.
:ivar pattern: The ACL pattern that is templated.
:ivar foundDevices: The set of devices which have at least one ACL matching the pattern.
:ivar emptyDefDevices: The set of devices that have an empty definition for the ACL.
:ivar exactDefMatchMap: The bookkeeping used for exact equality optimization.
"""
patternMatchSegments = []
patternMatchSegmentsLineCounts = []
if deviceInfo.get("ipAccessLists"):
segments = deviceInfo.get("ipAccessLists")
for segmentName in segments:
if pattern.match(segmentName):
if device in foundDevices:
rname = device + "#" + segmentName
else:
rname = device
parsedSegment = ACL(
segmentName, rname, segments[segmentName], deviceInfo['configurationFormat'])
if len(parsedSegment.blocks) > 0 and len(parsedSegment.blocks[0].lines) > 0:
foundDevices.add(rname)
if not commonFunctions.checkJSONEquality(exactDefMatchMap, segments[segmentName], rname):
# Last block's last line's (-1) attribute.
totalLines = parsedSegment.blocks[-1].lines[-1][LINENUM]
patternMatchSegments.append(parsedSegment)
patternMatchSegmentsLineCounts.append(totalLines)
else:
emptyDefDevices.add(rname)
return patternMatchSegments, patternMatchSegmentsLineCounts
def GapPenalty(block):
"""Returns the score for matching the input block with a gap."""
return len(block.lines)*LINE_PENALTY
def LineSequence(block):
"""Returns the line sequences for a block."""
return block.lines
def LineScore(templateLine, deviceLine, paramValueMap):
"""Returns the score for matching the line from the metatemplate with a line from the device."""
# Return infinity if the protocols of the lines do not match
if templateLine[-2] != deviceLine[-2]:
return commonFunctions.INFINITY
score = 0
for i in range(ATTRIBUTES):
if templateLine.get(i) and deviceLine.get(i):
if templateLine[i] != deviceLine[i]:
if templateLine[i] not in paramValueMap or deviceLine[i] not in paramValueMap[templateLine[i]]:
score += 2
else:
score += 1
else:
score += 2
return score
def BipartiteMatching(LS1, LS2, paramValueMap, noOfAttributes):
""" Score and matching calculator for matching LineSequence1 with LineSequence2."""
return PrefixList.BipartiteMatching(LS1, LS2, paramValueMap, noOfAttributes)
def TemplateGenerator(block1Alignment, block2Alignment, lineMatchings, parametersLines, device, noOfAttributes):
""" Given the alignment and line matchings the function returns the merged terms."""
return PrefixList.TemplateGenerator(block1Alignment, block2Alignment, lineMatchings, parametersLines, device, noOfAttributes)
def MinimizeParameters(metaTemplate, parametersLines, noOfAttributes):
PrefixList.MinimizeParameters(
metaTemplate, parametersLines, noOfAttributes)
def FormatBlock(configFormat, action, lines, linePredicateMap, patternString):
"""Produces the output meta template in Cisco IOS format for a block
Attributes:
-2 : protocol
-1 : LineNumber
0-3 : srcIp
4-7 : srcMask
8-11 : dstIp
12-15 : dstMask
16-17 : srcPort
18-19 : dstPort
"""
output = ""
htmlCmds = list()
common = "\t" + action.lower()
for line in lines:
tmp = {}
tmp[0] = linePredicateMap.get(line[LINENUM])
tmp[1] = common
tmp[2] = line[-2].lower()
if line[4] == line[5] == line[6] == line[7] == "255":
tmp[3] = "any"
tmp[4] = ""
elif line[4] == line[5] == line[6] == line[7] == "0":
tmp[3] = "host"
tmp[4] = ".".join([line[0], line[1], line[2], line[3]])
else:
tmp[3] = ".".join([line[0], line[1], line[2], line[3]])
tmp[4] = ".".join([line[4], line[5], line[6], line[7]])
if line[16] == line[17] == "-1":
tmp[5] = ""
elif line[16] == line[17]:
tmp[5] = " eq " + line[16]
else:
tmp[5] = " range " + line[16] + " " + line[17]
if line[12] == line[13] == line[14] == line[15] == "255":
tmp[6] = "any"
tmp[7] = ""
elif line[12] == line[13] == line[14] == line[15] == "0":
tmp[6] = "host"
tmp[7] = ".".join([line[8], line[9], line[10], line[11]])
else:
tmp[6] = ".".join([line[8], line[9], line[10], line[11]])
tmp[7] = ".".join([line[12], line[13], line[14], line[15]])
if line[18] == line[19] == "-1":
tmp[8] = ""
elif line[18] == line[19]:
tmp[8] = " eq " + line[18]
else:
tmp[8] = " range " + line[18] + " " + line[19]
output += "{:<3}: {:<3}: {} {} {} {} {} {} {} {}\n".format(
str(line[LINENUM]), tmp[0], tmp[1], tmp[2], tmp[3], tmp[4], tmp[5], tmp[6], tmp[7], tmp[8])
htmlCmds.append(tmp)
return output, htmlCmds
def PrintTemplate(metaTemplate, parametersLines, outputDirectory, patternString, noOfAttributes):
return PrefixList.PrintTemplate(metaTemplate, parametersLines, outputDirectory, patternString, noOfAttributes)