-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding clean (apart from C901) flake8 passing
- Loading branch information
Showing
4 changed files
with
159 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,85 @@ | ||
|
||
# import Node classes | ||
from attacktree.models import Action, Block, Detect, Discovery, Edge, Root, Goal, Node | ||
from attacktree.models import Action, Block, Goal, Node | ||
|
||
# import some useful dicts | ||
from attacktree.models import mitreAttack, rules | ||
from attacktree.models import rules | ||
|
||
import logging | ||
import copy | ||
|
||
# TODO: use logging framework | ||
class Brain(object): | ||
def __init__(self): | ||
self.exploitChain = [] | ||
|
||
# Walk the tree, adding to the chain (DFS) | ||
# If we hit the goal, add that chain to our paths | ||
# If we hit the goal, add that chain to our paths | ||
def pathsToVictory(self, | ||
node: Node, | ||
paths: list=None, | ||
chain: list=None, | ||
walked: dict=None): | ||
if walked == None: | ||
paths: list = None, | ||
chain: list = None, | ||
walked: dict = None): | ||
|
||
if walked is None: | ||
walked = {} | ||
|
||
if paths == None: | ||
if paths is None: | ||
paths = [] | ||
|
||
if chain == None: | ||
if chain is None: | ||
chain = [] | ||
|
||
chain.append(node) | ||
|
||
# If this node is a Goal then YAY! We have a goal | ||
if isinstance(node, Goal): | ||
paths.append(chain.copy()) | ||
return paths | ||
|
||
edges = node.getEdges() | ||
for edge in edges: | ||
if edge not in walked: | ||
self.pathsToVictory(edge.childNode, paths, chain=chain.copy(), walked=walked) | ||
walked[edge] = True # Stops walking a cycle more than once | ||
self.pathsToVictory(edge.childNode, paths, | ||
chain=chain.copy(), walked=walked) | ||
walked[edge] = True # Stops walking a cycle more than once | ||
|
||
return paths | ||
|
||
# Walk the given path, add up stats and annotate edges | ||
def evaluatePath(self, path): | ||
# It's not the nodes we need to evaluate, it's the edges. As those are what get changed adding a block | ||
results = {} | ||
for key in rules: #Pre-load data from rules | ||
for key in rules: # Pre-load data from rules | ||
results[key] = rules[key]['startWith'] | ||
|
||
prevNode = None | ||
for node in path: | ||
#TODO: Introduce pDiscovery value (or pSuccess on Discovery() ) | ||
# TODO: Introduce pDiscovery value (or pSuccess on Discovery() ) | ||
if isinstance(node, (Action)): | ||
results['attackCost'] += node.cost | ||
results['time'] += node.time | ||
results['pSuccess'] = int((results['pSuccess'] * node.pSuccess) / 100) | ||
results['pSuccess'] = int( | ||
(results['pSuccess'] * node.pSuccess) / 100) | ||
if isinstance(node, (Block)): | ||
results['defenceCost'] += node.cost | ||
results['defenceCost'] += node.cost | ||
results['pSuccess'] -= node.pDefend | ||
#TODO block time | ||
# TODO block time | ||
|
||
if prevNode is not None: | ||
edgeToThisNode = None | ||
edgeToThisNode = None | ||
for edge in prevNode.edges: | ||
if edge.childNode == node: | ||
edgeToThisNode = edge | ||
|
||
if edgeToThisNode is None: # This shouldn't happen and we should try to get rid of this check. | ||
# This shouldn't happen and we should try to get rid of this check. | ||
if edgeToThisNode is None: | ||
print(f"Could not find an edge to {node.label}") | ||
print(f"PrevNode: {prevNode.label}") | ||
print(f"Path: {path}\n") | ||
else: | ||
edgeToThisNode.pSuccess = results['pSuccess'] | ||
|
||
prevNode = node | ||
# Can't just throw in a backfref because a node can have multiple parents | ||
# End outer for by setting current node as the next (prevNode ) | ||
|
||
return results | ||
|
||
|
||
|
||
return results |
Oops, something went wrong.