forked from maxvonhippel/AttackerSynthesis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacterize.py
More file actions
187 lines (145 loc) · 4.84 KB
/
Characterize.py
File metadata and controls
187 lines (145 loc) · 4.84 KB
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
# ==============================================================================
# File : Characterize.py
# Author : Max von Hippel and Cole Vick
# Authored : 30 November 2019 - 13 March 2020
# Purpose : Checks when models do or do not satisfy properties. Also inter-
# prets various outputs of Spin.
# How to run: This code is used by Korg.py, which is what you want to run.
# ==============================================================================
import subprocess
import sys
import os
from glob import glob
'''
Given the path to a model containing one or more properties,
checks if that model violates one or more of those properties.
INPUT:
modelFile - the string containing the path to the model file
OUTPUT:
true iff modelFile runs successfully without acceptance cycles
or violations else false
'''
def check(modelFile, maxDepth=10000):
if maxDepth > 10000 * 20:
print("maxDepth too large for any realistic run. Edit the code if " \
+ "you actually really want to do this ... in Characterize.py.")
return False
args = "spin -run -a -m" + str(maxDepth) + " -RS88 " + modelFile
args = [a.strip() for a in args.split(" ")]
args = [a for a in args if len(a) > 0]
ret = None
with open(os.devnull, 'w') as devnull:
try:
ret = subprocess.check_output(args, stderr=devnull)
if sys.stdout.encoding != None:
ret = ret.decode(sys.stdout.encoding).strip()
else:
ret = str(ret)
except Exception as e:
raise e
if ret == None:
return False
if "depth too small" in ret:
print("Search depth was too small at " \
+ str(maxDepth), \
" doubling depth ...")
return check(modelFile, maxDepth * 2)
return not ("violated" in ret or "acceptance cycle" in ret)
def makeAllTrails(modelFile, numTrails=100):
args = ""
if numTrails <= 1:
args = "spin -run -a " + modelFile
else:
args = "spin -run -a -e -c" + str(numTrails - 1) + " " + modelFile
subprocess.run(args.split(" "))
'''
Given the paths to a model and a property, checks if that
model makes true that property.
INPUT:
model - the string containing the path to the model file
phi - the string containing the path to the ltl model file
N - the string containing the path to the N model file
OUTPUTS:
true iff the model (model || phi || N) runs successfully without
acceptance cycles or violations else false
'''
def models(model, phi, N, name):
if None in { model, phi, N, name }:
return False
fmrLines = ""
fNrLines = ""
fprLines = ""
with open(model, 'r') as fmr:
fmrLines = fmr.read()
with open(N, 'r') as fNr:
fNrLines = fNr.read()
with open(phi, 'r') as fpr:
fprLines = fpr.read()
with open(name, 'w') as fw:
fw.write(fmrLines + "\n" + fNrLines + "\n" + fprLines)
assert(os.path.isfile(name))
return check(name)
# Parses output of reading trail using Spin.
def parseTrail(trailBody):
ret, i = [[], []], 0
for line in trailBody.split("\n"):
if "(daisy:" in line:
# https://stackoverflow.com/a/29571669/1586231
LL = line.rstrip("*")
chan = LL[line.rfind("(")+1:-1]
msg, evt = None, None
if "Recv " in line:
msg = LL[line.rfind("Recv ")+5:].split()[0]
evt = "?"
elif "Send" in line:
msg = LL[line.rfind("Send ")+5:].split()[0]
evt = "!"
if evt != None and msg != None:
ret[i].append(chan + " " + evt + " " + msg)
elif "CYCLE" in line:
i = 1
return ret
def parseAllTrails(cmds, with_recovery=False, debug=False):
ret = []
prov = []
with open(os.devnull, 'w') as devnull:
for cmd in cmds:
output = subprocess.check_output(cmd, stderr=devnull)
if sys.stdout.encoding != None:
output = output.decode(sys.stdout.encoding)
output = str(output).strip().replace("\\n", "\n")\
.replace("\\t", "\t")
parsed = parseTrail(output)
ret.append(parsed)
prov.append(cmd)
return ret, prov
def attackType(A, E):
# if A == 1 then E == 1
# <=> not (A == 1 and E == 0)
assert(not (A and not E))
if A:
return "A-attack"
if E:
return "E-attack"
return "NOT AN ATTACK"
def characterizeAttacks(model, phi, with_recovery=True, name="run"):
assert(os.path.isdir("out/" + name))
nE, nA = 0, 0
with open("out/" + name + "/log.txt", "w") as fw:
if not (os.path.isdir("out/" + name + "/artifacts")):
os.mkdir("out/" + name + "/artifacts")
fw.write("model,A/E,with_recovery?\n")
for attackModel in glob("out/" + name + "/attacker*.pml"):
# is it a forall attack?
attackName = os.path.basename(attackModel).replace(".pml", "")
aName = "out/" + name + "/artifacts/" + attackName + "_A.pml"
eName = "out/" + name + "/artifacts/" + attackName + "_E.pml"
A = (models(model, "negated.pml", attackModel, aName) == True )
E = (models(model, phi, attackModel, eName) == False)
if A:
nA += 1
elif E:
nE += 1
fw.write(",".join([ \
attackModel, attackType(A, E), str(with_recovery)]) + "\n")
return (nE, nA)