-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfsm_pseudopy.py
64 lines (47 loc) · 2.16 KB
/
fsm_pseudopy.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
import sys
import json
def getNodeName(node):
return str(node["mId"]) + "-" + node["mName"]
class AttrDict(dict):
def __init__(self, *args, **kwargs):
super(AttrDict, self).__init__(*args, **kwargs)
self.__dict__ = self
def getConditionName(condition):
conditionList = condition["mpRootNode"]["mpChildList"]
if not isinstance(conditionList, list):
conditionList = [conditionList]
return f"{str(condition['mName']['mId'])}-{str([n['mVariable']['mPropertyName'] for n in conditionList if 'mVariable' in n])}"
main_dict = json.load(open(sys.argv[1], 'r', encoding="utf8"), object_hook=lambda d:AttrDict(d))
main_dict = AttrDict(main_dict)
nodes = [n for n in main_dict["root"]["mpRootCluster"]["mpNodeList"]]
conditions = [cond for cond in main_dict["root"]["mpConditionTree"]["mpTreeList"]]
f = open(sys.argv[1] + ".pseudo.py", 'w', encoding="utf8")
for node in nodes:
print(f"def attack_{node.mId}():", file=f)
print(f" name='{node.mName}'", file=f)
if not isinstance(node.mpLinkList, list):
node.mpLinkList = [node.mpLinkList]
for link in node.mpLinkList:
print(f" if condition_{link.mConditionId}():", file=f)
# conditionList = links[link.mConditionId].mpRootNode.mpChildList
# if not isinstance(conditionList, list):
# conditionList = [conditionList]
# for condition in conditionList:
# import pdb; pdb.set_trace()
print(f" attack_{link.mDestinationNodeId}()", file=f)
processList = node.mpProcessList
if not isinstance(processList, list):
processList = [processList]
for process in processList:
print(f" {process.mContainerName}({process.mpParameter})", file=f)
print("\n", file=f)
for condition in conditions:
print(f"def condition_{condition.mName.mId}():", file=f)
conditionList = condition.mpRootNode.mpChildList
if not isinstance(conditionList, list):
conditionList = [conditionList]
for subcond in conditionList:
if "mVariable" in subcond:
print(f" cond='{subcond.mVariable.mPropertyName}'", file=f)
print(" return", file=f)
print("\n", file=f)