-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstraints.py
310 lines (262 loc) · 11 KB
/
constraints.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
# constraints.py: Constraints function for identity linking model
# Copyright (C) 2017 Libor Polčák <ipolcak@fit.vutbr.cz>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from inaccuracy import compute_inaccuracy
# Definition of constraint functions
class ConstraintFunction():
""" A base class for constraint functions. """
def __init__(self, g):
""" Constructor.
@param g The graphs to operate on.
"""
self._g = g
def check_path(self, p):
""" Applies the constraint function on the path p.
@param p The path on which this constraint function should be applied.
This is an abstract function.
"""
raise NotImplementedError("Abstract method called")
class EdgeScopeConstraintFunction(ConstraintFunction):
""" This is a base class for scope constraint functions depending on edges."""
def __init__(self, g):
ConstraintFunction.__init__(self, g)
def _allow_edge(self, src, dst):
""" Edge conformance, abstract mehod. """
raise NotImplementedError("Abstract method")
def _allow_first_edge(self, src, dst):
""" Edge conformance, abstract mehod. """
raise NotImplementedError("Abstract method")
def check_path(self, p):
""" Redefines base class method. """
if len(p) <= 1:
return (False, False)
src1, dst1 = p[0:2]
if self._allow_first_edge(src1, dst1):
for n1, n2 in zip(range(1, len(p)), range(2, len(p))):
src = p[n1]
dst = p[n2]
if not self._allow_edge(src, dst):
return (False, False)
return (True, True)
else:
return (False, False)
class PartialIdentityComponents(EdgeScopeConstraintFunction):
""" A constraint function for Components of a partial identity. """
def __init__(self, g):
self.__rules = {
"alpha": ["alpha"],
"beta": ["alpha"],
"gamma": ["beta"],
"delta": ["beta", "gamma"],
"lambda": ["alpha"],
"rho": [],
}
EdgeScopeConstraintFunction.__init__(self, g)
def _allow_edge(self, src, dst):
return self._g.node[dst]["category"] in self.__rules[self._g.node[src]["category"]]
def _allow_first_edge(self, src, dst):
return self._allow_edge(src, dst)
class SpecificComputerOrInterface(EdgeScopeConstraintFunction):
""" A constraint function for Identifiers used by a specific computer. """
def __init__(self, g):
self.__rules = {
"alpha": ["alpha"],
"beta": ["alpha", "gamma"],
"gamma": ["beta"],
"delta": [],
"lambda": [],
"rho": [],
}
EdgeScopeConstraintFunction.__init__(self, g)
def _allow_edge(self, src, dst):
return self._g.node[dst]["category"] in self.__rules[self._g.node[src]["category"]]
def _allow_first_edge(self, src, dst):
return self._allow_edge(src, dst)
class SpecificComputerOrInterfaceLoggedUser(EdgeScopeConstraintFunction):
""" A constraint function for Identifiers of all computers where a specific user was authenticated or logged in. """
def __init__(self, g):
self.__rules_first = {
"alpha": [],
"beta": [],
"gamma": [],
"delta": ["beta", "gamma"],
"lambda": ["beta"],
"rho": [],
}
self.__specific_computer_or_interface = SpecificComputerOrInterface(g)
EdgeScopeConstraintFunction.__init__(self, g)
def _allow_edge(self, src, dst):
return self.__specific_computer_or_interface.check_path([src, dst])[0]
def _allow_first_edge(self, src, dst):
return self._g.node[dst]["category"] in self.__rules_first[self._g.node[src]["category"]]
class UsersAccessingResource(EdgeScopeConstraintFunction):
""" A constraint function for Identifiers of all users accessing a specific resource. """
def __init__(self, g):
self.__rules_first = {
"alpha": [],
"beta": [],
"gamma": [],
"delta": [],
"lambda": [],
"rho": ["lambda"],
}
EdgeScopeConstraintFunction.__init__(self, g)
def _allow_edge(self, src, dst):
return False
def _allow_first_edge(self, src, dst):
return self._g.node[dst]["category"] in self.__rules_first[self._g.node[src]["category"]]
class UsersLoggedIn(ConstraintFunction):
""" A constraint function for All user accounts logged in from a computer or a set of computers. """
def __init__(self, g):
self.__rules_last = {
"alpha": [],
"beta": ["delta", "lambda"],
"gamma": ["delta"],
"delta": [],
"lambda": [],
"rho": ["lambda"],
}
self.__l2 = SpecificComputerOrInterface(g)
self.__l3 = SpecificComputerOrInterfaceLoggedUser(g)
EdgeScopeConstraintFunction.__init__(self, g)
def check_path(self, p):
""" @Overloads """
if len(p) < 2:
return (False, True)
last_src = p[-2]
last_dst = p[-1]
if self._g.node[last_dst]["category"] not in \
self.__rules_last[self._g.node[last_src]["category"]]:
return (False, True)
all_but_last = p[:-1]
return (len(all_but_last) == 1 or self.__l2.check_path(all_but_last)[0] or
self.__l3.check_path(all_but_last)[0], True)
class AccessedResources(ConstraintFunction):
""" A constraint function for All accessed resources. """
def __init__(self, g):
self.__l2 = SpecificComputerOrInterface(g)
self.__l3 = SpecificComputerOrInterfaceLoggedUser(g)
EdgeScopeConstraintFunction.__init__(self, g)
def check_path(self, p):
""" @Overloads """
if len(p) < 2:
return (False, True)
last_src = p[-2]
last_dst = p[-1]
if self._g.node[last_src]["category"] not in ["beta", "lambda"] or \
self._g.node[last_dst]["category"] != "rho":
return (False, self.__l2.check_path(p) or self.__l3.check_path(p))
all_but_last = p[:-1]
return (len(all_but_last) == 1 or self.__l2.check_path(all_but_last)[0] or
self.__l3.check_path(all_but_last)[0], True)
class Logins(EdgeScopeConstraintFunction):
""" A constraint function revealing all login aliases. """
def __init__(self, g):
self.__rules = {
"alpha": [],
"beta": [],
"gamma": [],
"delta": ["delta", "lambda"],
"lambda": ["delta", "lambda"],
"rho": [],
}
EdgeScopeConstraintFunction.__init__(self, g)
def _allow_edge(self, src, dst):
return self._g.node[dst]["category"] in self.__rules[self._g.node[src]["category"]]
def _allow_first_edge(self, src, dst):
return self._allow_edge(src, dst)
class IPAddrsAccessingResource(EdgeScopeConstraintFunction):
""" A constraint function revealing IP addresses acessing a resource. """
def __init__(self, g):
self.__rules_first = {
"alpha": [],
"beta": [],
"gamma": [],
"delta": [],
"lambda": [],
"rho": ["beta"],
}
EdgeScopeConstraintFunction.__init__(self, g)
def _allow_edge(self, src, dst):
return False
def _allow_first_edge(self, src, dst):
return self._g.node[dst]["category"] in self.__rules_first[self._g.node[src]["category"]]
class EdgeConstraintFunction(ConstraintFunction):
""" This is a base class for constraint functions depending only on edges."""
def __init__(self, g):
ConstraintFunction.__init__(self, g)
def _allow_edge(self, validfrom, validto, identitysource, inaccuracy):
""" Edge conformance, abstract mehod. """
raise NotImplementedError("Abstract method")
def __check_link(self, src, dst):
for i, e in self._g.edge[src][dst].items():
# Test validity of edge, one is sufficient to be valid
if self._allow_edge(**e):
return True
return False
def check_path(self, p):
""" Redefines base class method. """
for n1, n2 in zip(range(len(p)), range(1, len(p))):
src = p[n1]
dst = p[n2]
if not self.__check_link(src, dst):
return (False, False)
return (True, True)
class ActiveContinuouslyDuring(EdgeConstraintFunction):
""" All edges along the path have to be active during the whole duration. """
def __init__(self, g, b_time, e_time):
EdgeConstraintFunction.__init__(self, g)
self.__b_time = b_time
self.__e_time = e_time
def _allow_edge(self, validfrom, validto, identitysource, inaccuracy):
""" Defines base class abstract method. """
return validfrom <= self.__b_time and validto >= self.__e_time
class ActiveDuringTime(ConstraintFunction):
""" Identifiers have to be linkable for at least a moment during the given duration. """
def __init__(self, g, b_time, e_time):
ConstraintFunction.__init__(self, g)
self.__b_time = b_time
self.__e_time = e_time
def _check_path(self, p, prevstart, prevend):
src = p[0]
dst = p[1]
for i, e in self._g.edge[src][dst].items():
r = self._check_edge_and_following(p[1:], prevstart, prevend, **e)
if r:
return True
return False
def _check_edge_and_following(self, p, prevstart, prevend, validfrom, validto, identitysource, inaccuracy):
s = max(self.__b_time, prevstart, validfrom)
e = min(self.__e_time, prevend, validto)
if e < s:
return False
if len(p) < 2:
return True
return self._check_path(p, validfrom, validto)
def check_path(self, p):
""" Redefines base class method. """
if len(p) < 2:
return (False, True)
r = self._check_path(p, self.__b_time, self.__e_time)
return (r, r)
class MaximalPathInaccuracy(ConstraintFunction):
""" Checks the path for a maximal total inaccuracy """
def __init__(self, g, max_inaccuracy):
ConstraintFunction.__init__(self, g)
self._max_inaccuracy = max_inaccuracy
def check_path(self, p):
""" Redefines base class method. """
res = compute_inaccuracy(self._g, p) <= self._max_inaccuracy
return (res, res)