-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgraph_traverser.py
55 lines (48 loc) · 2.57 KB
/
graph_traverser.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
class GraphTraverser(object):
def __init__(self, graph, eventSet, eventMapping, networkNodes):
self.graph = graph
self.eventSet = eventSet
self.eventMapping = eventMapping
self.networkNodes = networkNodes
def dfs(self, v, reverseList, timestamp, dst, port, src=None):
# print("dfs called")
# print(v.to_string())
# for i in self.graph.predecessors(v):
# print(i.to_string())
if v.type == 'vuln' and v.entry and src not in self.networkNodes:
# reverseList.reverse()
# print("Printing at node {}".format(v.to_string()))
print('')
return self.print_path(reverseList[::-1])
for i in self.graph.predecessors(v):
# print("Predecessor: {}".format(i.to_string()))
if i.type == 'vuln':
description = self.eventMapping[i.vulnerabilityName]
eventList = self.eventSet.containsVulnEvent(description, dst, i.vulnerabilityPort, timestamp)
if eventList:
for event in eventList:
event_string = event['TIMESTAMP'] + ', ' + event['SRCHOST'] + ', ' + event['DSTHOST'] + ', ' + description
# print("Adding event: {}".format(event_string))
reverseList.append(event_string)
self.dfs(i, reverseList, event['TIMESTAMP'], event['DSTHOST'], event['DSTPORT'], event['SRCHOST'])
reverseList.pop()
# print("Returned from state node")
elif i.type == 'state':
self.dfs(i, reverseList, timestamp, src, port)
# print("Returned from vuln node")
def start_traversal(self, timestamp, src, dst, port, description, accessLevel):
reverseList = []
reverseList.append('Notable event: ' + str(timestamp) + ', ' + src + ', '+ dst + ', ' + description)
notableEventNode = self.find_node(src, accessLevel)
if notableEventNode:
eventSequence = self.dfs(notableEventNode, reverseList, timestamp, src, port)
else:
print("The attacker cannot have access level {} at host {}".format(accessLevel, src))
def find_node(self, dst, accessLevel):
for i in self.graph.nodes:
if i.type == 'state' and i.hostname == dst and i.accessLevel == accessLevel:
return i
def print_path(self, list):
print("Entry: {}".format(list[0]))
for i in list[1:]:
print(' -> ' + i)