forked from dhondta/rpl-attacks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
189 lines (168 loc) · 7.31 KB
/
fabfile.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from lib import *
# ****************************** TASKS ON INDIVIDUAL EXPERIMENT ******************************
@task
def clean(name):
logging.debug(" > Cleaning folder...")
with hide(*HIDDEN_ALL):
with lcd(EXPERIMENT_FOLDER):
local("rm -rf {}".format(name))
@task
def cooja(name, with_malicious=False):
logging.debug("STARTING COOJA WITH EXPERIMENT '{}'".format(name))
path = get_path(EXPERIMENT_FOLDER, name)
get_path(EXPERIMENT_FOLDER, name, 'data')
with hide(*HIDDEN_ALL):
with lcd(path):
local("make cooja-with{}-malicious".format("" if with_malicious else "out"))
@task
def make(name, **kwargs):
global reuse_bin_path
logging.info("CREATING EXPERIMENT '{}'".format(name))
logging.debug(" > Validating parameters...")
params = validated_parameters(kwargs)
ext_lib = params.get("ext_lib")
logging.debug(" > Creating simulation...")
# create experiment's directories
path = get_path(EXPERIMENT_FOLDER, name)
get_path(EXPERIMENT_FOLDER, name, 'motes')
get_path(EXPERIMENT_FOLDER, name, 'data')
get_path(EXPERIMENT_FOLDER, name, 'results')
# select the right malicious mote template and duplicate the simulation file
copy_files(TEMPLATES_FOLDER, TEMPLATES_FOLDER,
('motes/malicious-{}.c'.format(params["mtype"]), 'motes/malicious.c'),
('simulation.csc', 'simulation_without_malicious.csc'),
('simulation.csc', 'simulation_with_malicious.csc'))
# create experiment's files from templates
render_templates(path, **params)
# then clean the templates folder from previously created files
remove_files(TEMPLATES_FOLDER,
'motes/malicious.c',
'simulation_without_malicious.csc',
'simulation_with_malicious.csc')
logging.debug(" > Making motes...")
path = get_path(EXPERIMENT_FOLDER, name)
if ext_lib and not exists(ext_lib):
logging.error("External library does not exist !")
logging.critical("Make aborded.")
return
with hide(*HIDDEN_ALL):
with lcd(path):
# for every simulation, root and sensor compilation is the same ;
# then, if these were not previously compiled, proceed
if reuse_bin_path is None:
local("make motes/root TARGET={}".format(params["target"]))
local("make motes/sensor TARGET={}".format(params["target"]))
# after compiling, clean artifacts
remove_files(path,
'contiki-{}.a'.format(params["target"]),
'contiki-{}.map'.format(params["target"]),
'symbols.c',
'symbols.h',
'motes/root.c',
'motes/sensor.c')
remove_folder((path, 'obj_{}'.format(params["target"])))
reuse_bin_path = path
# otherwise, reuse them by copying the compiled files to the current experiment folder
else:
copy_files(reuse_bin_path, path, "motes/root.{}".format(params["target"]),
"motes/sensor.{}".format(params["target"]))
with hide(*HIDDEN_KEEP_STDERR):
with lcd(path):
if ext_lib is not None:
# backup original RPL library and replace it with attack's library
get_path('.tmp')
move_folder((CONTIKI_FOLDER, 'core', 'net', 'rpl'), '.tmp')
copy_folder(ext_lib, (CONTIKI_FOLDER, 'core', 'net', 'rpl'))
with settings(warn_only=True, abort_exception=Exception):
try:
local("make motes/malicious TARGET={}".format(params["target"]))
except Exception as e:
logging.error(str(e))
if ext_lib is not None:
# then, clean temporary files
remove_folder((CONTIKI_FOLDER, 'core', 'net', 'rpl'))
move_folder('.tmp/rpl', (CONTIKI_FOLDER, 'core', 'net'))
remove_folder('.tmp')
remove_files(path,
'contiki-{}.a'.format(params["target"]),
'contiki-{}.map'.format(params["target"]),
'symbols.c',
'symbols.h',
'motes/malicious.c')
remove_folder((path, 'obj_{}'.format(params["target"])))
with lcd(COOJA_FOLDER):
local("ant clean")
local("ant jar")
@task
def run(name):
logging.info("PROCESSING EXPERIMENT '{}'".format(name))
path = get_path(EXPERIMENT_FOLDER, name)
with hide(*HIDDEN_ALL):
with lcd(path):
logging.debug(" > Running both simulations (with and without the malicious mote)...")
local("make run-without-malicious")
local("make run-with-malicious")
remove_files(path,
'COOJA.log',
'COOJA.testlog')
# ****************************** TASKS ON SIMULATION CAMPAIGN ******************************
@task
@expand_file(EXPERIMENT_FOLDER, 'json')
def make_all(exp_file="experiments"):
global reuse_bin_path
for name, params in get_experiments(exp_file).items():
clean(name)
make(name, **params)
@task
@expand_file(EXPERIMENT_FOLDER, 'json')
def prepare(exp_file='my_simulation'):
logging.debug("CREATING NEW EXPERIMENT CAMPAIGN AT '{}'".format(exp_file))
copy_files(TEMPLATES_FOLDER, dirname(exp_file), ('experiments.json', exp_file))
@task
@expand_file(EXPERIMENT_FOLDER, 'json')
def run_all(exp_file="experiments"):
for name in get_experiments(exp_file).keys():
run(name)
# ****************************** SETUP TASKS FOR CONTIKI AND COOJA ******************************
@task
def config(contiki_folder='~/contiki', experiments_folder='~/Experiments'):
logging.debug("CREATING CONFIGURATION FILE AT '~/.rpl-attacks.conf'")
with open(expanduser('~/.rpl-attacks.conf'), 'w') as f:
f.write('[RPL Attacks Framework Configuration]\n')
f.write('contiki_folder = {}\n'.format(contiki_folder))
f.write('experiments_folder = {}\n'.format(experiments_folder))
@task
def test():
setup()
make("test-simulation")
with lcd(FRAMEWORK_FOLDER):
local("python -m unittest tests")
@task
def setup():
try:
with open('.cooja_addons_installed') as f:
f.read()
logging.debug("COOJA ADD-ONS ALREADY INSTALLED")
return
except IOError:
with open('.cooja_addons_installed', 'w') as f:
f.write("")
logging.debug("INSTALLING COOJA ADD-ONS")
with hide(*HIDDEN_ALL):
modify_cooja(COOJA_FOLDER)
update_cooja_build(COOJA_FOLDER)
update_cooja_user_properties()
visualizer = join(COOJA_FOLDER, 'apps', 'visualizer_screenshot')
if not exists(visualizer):
copy_folder('src/visualizer_screenshot', visualizer)
with lcd(COOJA_FOLDER):
with settings(warn_only=True):
local("ant clean")
local("ant jar")
# **************************************** MAGIC TASK ****************************************
@task
def rip_my_slip():
# TODO: prepare 'rpl-attacks' campaign, make all its experiments then run them
print("yeah, man !")