-
Notifications
You must be signed in to change notification settings - Fork 0
/
anansi.py
461 lines (380 loc) · 13 KB
/
anansi.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#! /usr/bin/python
##### IMPORT IMPORT IMPORT #####
## CANON IMPORTS
import multiprocessing, ctypes, os, inspect
## HOMEBREW LIBS
import lib_workers as workers
##### VARS VARS VARS #####
SPIDERNEST = [] # will be populated with the workers
IS_RUNNING = True # is the whole Anansi running
NET_ENGINE = False # is the net engine runninh
CRAFT = None # which craft is on
APPLICATION = None # which application is on
##### FX #####
def killswitch_all():
global NET_ENGINE, CRAFT, APPLICATION
print "\nANANSI : killswitch activated, for all !"
for i in SPIDERNEST:
print "\tANANSI : killing "+str(i)
i.is_running = False
i.terminate()
i.join()
NET_ENGINE = False
CRAFT = None
APPLICATION = None
def killswitch_bytype(wtype):
global NET_ENGINE, CRAFT, APPLICATION
print "\nANANSI : killswitch activated for "+str(wtype)+" spiders"
for i in SPIDERNEST:
if (i.wtype == wtype):
print "\tANANSI : killing "+str(i)
i.is_running = False
i.terminate()
i.join()
if wtype == "engine":
NET_ENGINE = False
elif wtype == "craft":
CRAFT = None
elif wtype == "application":
APPLICATION = None
def workerstatus():
print "\nANANSI : spiders status"
for i in SPIDERNEST:
print "\tANANSI : "+str(i)
def workerstatus_bytype(wtype):
print "\nANANSI : "+str(wtype)+" spiders status"
for i in SPIDERNEST:
if (i.wtype == wtype):
print "\tANANSI : "+str(i)
def workercount_bytype(wtype):
k = 0
for i in SPIDERNEST:
if ((i.is_running) and (i.wtype == wtype)):
k += 1
return k
## NET
def netprobe_start():
print "\nANANSI : starting probe spider"
num = 3
while num not in [0,1,2]:
try:
num = int(raw_input("> Please, choose the device : dev0 (0), dev1 (1), cancel probe (2)\n"))
except Exception:
print "\nANANSI : Sorry, I could not understand your answer"
num = 3
if num in [0,1]:
vb = str(raw_input("> Verbose [y/n] ? (achtung, will print every packet content)\n"))
bvb = False
if vb.lower() == 'y':
bvb = True
db = workers.ProbeSpider(num,verbose=bvb)
SPIDERNEST.append(db)
db.start()
print str(db)+" started"
else:
print "\nANANSI : ProbeSpider cancelled"
def netengine_start():
global NET_ENGINE
print "\nANANSI : starting the net engine"
sr0 = workers.ReadingSpider(0)
SPIDERNEST.append(sr0)
sr0.start()
print "\nANANSI : reading spider 0 started"
sm0 = workers.MiddleSpider(0)
SPIDERNEST.append(sm0)
sm0.start()
print "\nANANSI : middle spider 0 started"
sw0 = workers.WritingSpider(0)
SPIDERNEST.append(sw0)
sw0.start()
print "\nANANSI : writing spider 0 started"
sr1 = workers.ReadingSpider(1)
SPIDERNEST.append(sr1)
sr1.start()
print "\nANANSI : reading spider 1 started"
sm1 = workers.MiddleSpider(1)
SPIDERNEST.append(sm1)
sm1.start()
print "\nANANSI : middle spider 1 started"
sw1 = workers.WritingSpider(1)
SPIDERNEST.append(sw1)
sw1.start()
print "\nANANSI : writing spider 1 started"
NET_ENGINE = True
## SCENARIOS
## WORK IN PROGRESS
def changeScenario():
print "\nANANSI : stegconf.py => stegconf.py.old (backup)"
print "\nANANSI : new scenario (from 'scenarios' folder) => stegconf.py"
scenario_name = str(raw_input("\nANANSI : scenario name ? (extension not included, eg. 'canonical')\n> "))
workers.changeScenar(scenario_name)
print "\nANANSI : refreshing the global variables"
workers.loadConf()
def checkScenario():
scenario_name = str(raw_input("\nANANSI : scenario name ? (extension not included, eg. 'canonical')\n> "))
description = workers.checkScenar(scenario_name)
print "\nANANSI : "+str(scenario_name)+"\n"+str(description)
def genScenariosList():
slist = []
tpath = os.path.join(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))),workers.SCENARIO_FOLDER,workers.STEGSCENAR_FOLDER)
for i in os.listdir(tpath):
if ".pyc" not in i:
if "__init__" not in i:
slist += [i]
return slist
def getScenariosList():
print "\nANANSI : looking for *.py in "+str(workers.SCENARIO_FOLDER)+"/"+str(workers.STEGSCENAR_FOLDER)
slist = genScenariosList()
print "\nANANSI : "+', '.join(slist)
## CRAFT
def nocraft_start():
global CRAFT
sc0 = workers.NoviceSpider(0)
SPIDERNEST.append(sc0)
sc0.start()
print "\nANANSI : crafting spider 0 started"
sc1 = workers.NoviceSpider(1)
SPIDERNEST.append(sc1)
sc1.start()
print "\nANANSI : crafting spider 1 started"
CRAFT = "novice (no craft)"
def stegcraft_start():
global CRAFT
sc0 = workers.StegSpider(0)
SPIDERNEST.append(sc0)
sc0.start()
print "\nANANSI : crafting spider 0 started"
sc1 = workers.StegSpider(1)
SPIDERNEST.append(sc1)
sc1.start()
print "\nANANSI : crafting spider 1 started"
CRAFT = "stegano"
## APPLICATION
def spam_start():
global APPLICATION
print "\nSPAM : sending k random bits on the wire"
k = int(raw_input("\nSPAM : value of k ?\n>"))
sks = workers.SpamSpider(k)
SPIDERNEST.append(sks)
sks.start()
APPLICATION = "spam"
def filex_start():
global APPLICATION
print "\nFILE EXCHANGE : sending/receiving a file"
num = 2
while num not in [0,1]:
num = int(raw_input("\nFILE EXCHANGE : should we send (0) or receive (1) ?\n>"))
filename = str(raw_input("\nFILE EXCHANGE : please enter the filename\n>"))
sfx = workers.FileSpider(num,filename)
SPIDERNEST.append(sfx)
sfx.start()
APPLICATION = "filexchange"
def chat_start():
global APPLICATION
print "WELCOME TO THE POINT-TO-POINT CHAT"
print "(please tape EXIT to end the conversation)"
sa0 = workers.ShamirChatSpider(0)
SPIDERNEST.append(sa0)
sa0.start()
sa1 = workers.ShamirChatSpider(1)
SPIDERNEST.append(sa1)
sa1.start()
APPLICATION = "chat"
## INTERFACE ##
def splash_screen():
splashcii = """
('-. .-') _ ('-. .-') _ .-')
( OO ).-. ( OO ) ) ( OO ).-. ( OO ) ) ( OO ).
/ . --. /,--./ ,--,' / . --. /,--./ ,--,' (_)---\_) ,-.-')
| \-. \ | \ | |\ | \-. \ | \ | |\ / _ | | |OO)
.-'-' | || \| | ).-'-' | || \| | )\ :` `. | | \
\| |_.' || . |/ \| |_.' || . |/ '..`''.) | |(_/
| .-. || |\ | | .-. || |\ | .-._) \ ,| |_.'
| | | || | \ | | | | || | \ | \ /(_| |
`--' `--'`--' `--' `--' `--'`--' `--' `-----' `--' """
print splashcii
def header():
print "\n################################"
print "\tANANSI\n"
def main_header():
print "DEVICE 0 : "+str(workers.DEV0IP)
print "DEVICE 1 : "+str(workers.DEV1IP)
print "NET ENGINE : "+str(NET_ENGINE)+" ("+str(workercount_bytype("engine"))+" spiders)"
print "SCENARIO : "+str(workers.SCENARIO_NAME)
print "CRAFT : "+str(CRAFT)+" ("+str(workercount_bytype("craft"))+" spiders)"
print "APPLICATION : "+str(APPLICATION)+" ("+str(workercount_bytype("application"))+" spiders)"
print "################################\n"
def engine_header():
print "DEVICE 0 : "+str(workers.DEV0IP)
print "DEVICE 1 : "+str(workers.DEV1IP)
print "NET ENGINE : "+str(NET_ENGINE)+" ("+str(workercount_bytype("engine"))+" spiders)"
print [x for x in SPIDERNEST if (x.wtype == "engine" and x.is_running)]
print "################################\n"
def craft_header():
print "SCENARIO : "+str(workers.SCENARIO_NAME)
print "SCENARIOS LIST : "+str(genScenariosList())
print "CRAFT : "+str(CRAFT)+" ("+str(workercount_bytype("craft"))+" spiders)"
print [x for x in SPIDERNEST if (x.wtype == "craft" and x.is_running)]
print "################################\n"
def application_header():
print "APPLICATION : "+str(APPLICATION)+" ("+str(workercount_bytype("application"))+" spiders)"
print [x for x in SPIDERNEST if (x.wtype == "application" and x.is_running)]
print "################################\n"
def main_menu():
global IS_RUNNING
while IS_RUNNING:
header()
main_header()
print "MAIN MENU"
print "\t0. Leave Anansi"
print "\t1. Engine menu"
print "\t2. Scenario menu"
print "\t3. Craft menu"
print "\t4. Application menu"
print "\t5. Maintenance menu"
print ""
temp = str(raw_input("ANANSI : Please, choose.\n> "))
if temp == '0':
killswitch_all() # safety first
workers.close_queues() # avant ou apres killswitch ?
workers.close_handlers()
IS_RUNNING = False
elif temp == '1':
engine_menu()
elif temp == '2':
scenario_menu()
elif temp == '3':
craft_menu()
elif temp == '4':
application_menu()
elif temp == '5':
maintenance_menu()
else:
print "ANANSI : sorry, I can't understand "+temp
print ""
print "Leaving Anansi ..."
def engine_menu():
temp = '5'
while temp != '0':
header()
engine_header()
print "ENGINE MENU"
print "\t0. Main menu"
print "\t1. Engine spiders status"
print "\t2. Engine : net engine spiders"
print "\t3. Engine : net probe spider"
print "\t4. Kill engine spiders"
print ""
temp = str(raw_input("ANANSI : Please, choose.\n> "))
if temp == '0':
main_menu()
elif temp == '1':
workerstatus_bytype("engine")
elif temp == '2':
netengine_start()
elif temp == '3':
netprobe_start()
elif temp == '4':
killswitch_bytype("engine")
else:
print "ANANSI : Sorry, I can't understand "+temp
def scenario_menu():
temp = '8'
while temp != '0':
header()
craft_header()
print "SCENARIO MENU"
print "\t0. Main menu"
print "\t1. Get scenarios list"
print "\t2. Check scenario"
print "\t3. Change scenario"
print ""
temp = str(raw_input("ANANSI : Please, choose.\n> "))
if temp == '0':
main_menu()
elif temp == '1':
getScenariosList()
elif temp == '2':
checkScenario()
elif temp == '3':
changeScenario()
else:
print "ANANSI : Sorry, I can't understand "+temp
def craft_menu():
temp = '8'
while temp != '0':
header()
craft_header()
print "CRAFT MENU"
print "\t0. Main menu"
print "\t1. Craft spiders status"
print "\t2. Craft : novice spider (no craft)"
print "\t3. Craft : stegano spider"
print "\t4. Kill craft spiders"
print ""
temp = str(raw_input("ANANSI : Please, choose.\n> "))
if temp == '0':
main_menu()
elif temp == '1':
workerstatus_bytype("craft")
elif temp == '2':
nocraft_start()
elif temp == '3':
stegcraft_start()
elif temp == '4':
killswitch_bytype("craft")
else:
print "ANANSI : Sorry, I can't understand "+temp
def application_menu():
temp = '9'
while temp != '0':
header()
application_header()
print "APPLICATIONS MENU"
print "\t0. Main menu"
print "\t1. Application spiders status"
print "\t2. Application : spam spider"
print "\t3. Application : filexchange spider"
print "\t4. Application : Shamir chat spider (wip)"
print "\t5. Kill application spiders"
print ""
temp = str(raw_input("ANANSI : Please, choose.\n> "))
if temp == '0':
main_menu()
elif temp == '1':
workerstatus_bytype("application")
elif temp == '2':
spam_start()
elif temp == '3':
filex_start()
elif temp == '4':
chat_start()
elif temp == '5':
killswitch_bytype("application")
else:
print "ANANSI : Sorry, I can't understand "+temp
def maintenance_menu():
temp = '5'
while temp != '0':
header()
main_header()
print "MAINTENANCE MENU"
print "\t0. Main menu"
print "\t1. Kill all spiders"
print "\t2. Restart all queues"
print "\t3. Restart pcap handlers"
temp = str(raw_input("ANANSI : Please, choose.\n> "))
if temp == '0':
main_menu()
elif temp == '1':
killswitch_all()
elif temp == '2':
workers.restart_queues()
elif temp == '3':
workers.restart_handlers()
else:
print "ANANSI : Sorry, I can't understand "+temp
##### MAIN MAIN MAIN #####
if __name__ == '__main__':
splash_screen()
main_menu()