forked from RedClouud/ICS-test-bed-PoC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
237 lines (197 loc) · 5.27 KB
/
utils.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
"""
swat-s1 utils.py
sqlite and enip use name (string) and pid (int) has key and the state stores
values as strings.
Actuator tags are redundant, we will use only the XXX_XXX_OPEN tag ignoring
the XXX_XXX_CLOSE with the following convention:
- 0 = error
- 1 = off
- 2 = on
sqlite uses float keyword and cpppo use REAL keyword.
"""
# from minicps.utils import build_debug_logger
# swat = build_debug_logger(
# name=__name__,
# bytes_per_file=10000,
# rotating_files=2,
# lformat='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
# ldir='logs/',
# suffix='')
# physical process {{{1
# SPHINX_SWAT_TUTORIAL PROCESS UTILS(
GRAVITATION = 9.81 # m.s^-2
TANK_DIAMETER = 1.38 # m
TANK_SECTION = 1.5 # m^2
PUMP_FLOWRATE_IN = 2.55 # m^3/h spec say btw 2.2 and 2.4
PUMP_FLOWRATE_OUT = 2.45 # m^3/h spec say btw 2.2 and 2.4
SYSTEM_FLOWRATE_OUT_METER_CUBED = 1.5
# periods in msec
# R/W = Read or Write
T_PLC_R = 100E-3
T_PLC_W = 100E-3
T_PP_R = 200E-3
T_PP_W = 200E-3
T_HMI_R = 100E-3
# ImageTk
DISPLAYED_SAMPLES = 14
# Control logic thresholds
LIT_101_MM = { # raw water tank mm
'LL': 250.0,
'L': 500.0,
'H': 800.0,
'HH': 1200.0,
}
LIT_101_M = { # raw water tank m
'LL': 0.250,
'L': 0.500,
'H': 0.800,
'HH': 1.200,
}
LIT_301_MM = { # ultrafiltration tank mm
'LL': 250.0,
'L': 800.0,
'H': 1000.0,
'HH': 1200.0,
}
LIT_301_M = { # ultrafiltration tank m
'LL': 0.250,
'L': 0.800,
'H': 1.000,
'HH': 1.200,
}
TANK_HEIGHT = 1.600 # m
PLC_PERIOD_SEC = 0.4 # plc update rate in seconds (divided by 52.8342957981 to update at the same rate per US gallon)
PLC_PERIOD_HOURS = PLC_PERIOD_SEC / 3600.0
PLC_SAMPLES = 205000 # ~ 24 hours run time
PP_RESCALING_HOURS = 100
PP_PERIOD_SEC = 0.2 # physical process update rate in seconds (divided by 52.8342957981 to update at the same rate per US gallon)
PP_PERIOD_HOURS = (PP_PERIOD_SEC / 3600.0) * PP_RESCALING_HOURS
PP_SAMPLES = int(PLC_PERIOD_SEC / PP_PERIOD_SEC) * PLC_SAMPLES
T101_INIT_LEVEL = 0.5
T301_INIT_LEVEL = 0.5
LIT_101_INIT_LEVEL = T101_INIT_LEVEL
LIT_301_INIT_LEVEL = T301_INIT_LEVEL
FIT_101_INIT_FLOW = PUMP_FLOWRATE_IN
FIT_201_INIT_FLOW = PUMP_FLOWRATE_OUT
MV_101_INIT_STATE = 0
P_101_INIT_STATE = 1
# m^3 / h
FIT_201_THRESH = 1.00
# SPHINX_SWAT_TUTORIAL PROCESS UTILS)
# topo {{{1
IP = {
'plc1': '192.168.1.10',
'plc2': '192.168.1.20',
'plc3': '192.168.1.30',
'plc4': '192.168.1.40',
'plc5': '192.168.1.50',
'plc6': '192.168.1.60',
'attacker': '192.168.1.77',
}
NETMASK = '/24'
MAC = {
'plc1': '00:1D:9C:C7:B0:70',
'plc2': '00:1D:9C:C8:BC:46',
'plc3': '00:1D:9C:C8:BD:F2',
'plc4': '00:1D:9C:C7:FA:2C',
'plc5': '00:1D:9C:C8:BC:2F',
'plc6': '00:1D:9C:C7:FA:2D',
'attacker': 'AA:AA:AA:AA:AA:AA',
}
# others
# TODO
PLC1_DATA = {
'TODO': 'TODO',
}
# TODO
PLC2_DATA = {
'TODO': 'TODO',
}
# TODO
PLC3_DATA = {
'TODO': 'TODO',
}
# SPHINX_SWAT_TUTORIAL PLC1 UTILS(
PLC1_ADDR = IP['plc1']
PLC1_TAGS = (
('FIT101', 1, 'REAL'),
('MV101', 1, 'INT'),
('LIT101', 1, 'REAL'),
('P101', 1, 'INT'),
# interlocks does NOT go to the statedb
('FIT201', 1, 'REAL'),
('MV201', 1, 'INT'),
('LIT301', 1, 'REAL'),
)
PLC1_SERVER = {
'address': PLC1_ADDR,
'tags': PLC1_TAGS
}
PLC1_PROTOCOL = {
'name': 'enip',
'mode': 0,
'server': PLC1_SERVER
}
# SPHINX_SWAT_TUTORIAL PLC1 UTILS)
PLC2_ADDR = IP['plc2']
PLC2_TAGS = (
('FIT201', 2, 'REAL'),
('MV201', 2, 'INT'),
# no interlocks
)
PLC2_SERVER = {
'address': PLC2_ADDR,
'tags': PLC2_TAGS
}
PLC2_PROTOCOL = {
'name': 'enip',
'mode': 0,
'server': PLC2_SERVER
}
PLC3_ADDR = IP['plc3']
PLC3_TAGS = (
('LIT301', 3, 'REAL'),
# no interlocks
)
PLC3_SERVER = {
'address': PLC3_ADDR,
'tags': PLC3_TAGS
}
PLC3_PROTOCOL = {
'name': 'enip',
'mode': 0,
'server': PLC3_SERVER
}
# state {{{1
# SPHINX_SWAT_TUTORIAL STATE(
PATH = 'swat_s1_db.sqlite'
NAME = 'swat_s1'
STATE = {
'name': NAME,
'path': PATH
}
# SPHINX_SWAT_TUTORIAL STATE)
SCHEMA = """
CREATE TABLE swat_s1 (
name TEXT NOT NULL,
pid INTEGER NOT NULL,
value TEXT,
PRIMARY KEY (name, pid)
);
"""
# SCHEMA_INIT = """
# INSERT INTO swat_s1 VALUES ('FIT101', 1, '2.55');
# INSERT INTO swat_s1 VALUES ('MV101', 1, '0');
# INSERT INTO swat_s1 VALUES ('LIT101', 1, '0.500');
# INSERT INTO swat_s1 VALUES ('P101', 1, '1');
# INSERT INTO swat_s1 VALUES ('FIT201', 2, '2.45');
# INSERT INTO swat_s1 VALUES ('LIT301', 3, '0.500');
# """
SCHEMA_INIT = "INSERT INTO swat_s1 VALUES ('FIT101', 1, '" + str(FIT_101_INIT_FLOW) + "');\n"
SCHEMA_INIT = SCHEMA_INIT + "INSERT INTO swat_s1 VALUES ('MV101', 1, '" + str(MV_101_INIT_STATE) + "');\n"
SCHEMA_INIT = SCHEMA_INIT + "INSERT INTO swat_s1 VALUES ('LIT101', 1, '" + str(LIT_101_INIT_LEVEL) + "');\n"
SCHEMA_INIT = SCHEMA_INIT + "INSERT INTO swat_s1 VALUES ('P101', 1, '" + str(P_101_INIT_STATE) + "');\n"
SCHEMA_INIT = SCHEMA_INIT + "INSERT INTO swat_s1 VALUES ('FIT201', 2, '" + str(FIT_201_INIT_FLOW) + "');\n"
SCHEMA_INIT = SCHEMA_INIT + "INSERT INTO swat_s1 VALUES ('LIT301', 3, '"+ str(LIT_301_INIT_LEVEL) + "');\n"
print "INIT STATE:"
print SCHEMA_INIT