-
Notifications
You must be signed in to change notification settings - Fork 0
/
w4sp_webapp.py
530 lines (381 loc) · 14.5 KB
/
w4sp_webapp.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
import os
import re
import sys
import pwd
import time
#docker handling code
import w4sp
import argparse
import netifaces
import traceback
import subprocess
from multiprocessing import Process
parser = argparse.ArgumentParser(description='Wireshark for Security Professionals Lab')
parser.add_argument('--debug', action='store_true', default=False)
args = parser.parse_args()
NSROOT = w4sp.ns_root
# import the Flask class from the flask module, try to install if we don't have it
try:
from flask import Flask, render_template, request, jsonify
except:
try:
subprocess.check_call(['pip', 'install', 'flask'])
from flask import Flask, render_template, request, jsonify
except:
subprocess.check_call(['apt-get', 'install', 'python-flask'])
from flask import Flask, render_template, request, jsonify
# create the application object
app = Flask(__name__)
app.config.from_object(__name__)
def get_connections():
"""this should return all of the machines that are connected"""
tmp = []
for ns in w4sp.ns_root.ns:
for nic in ns.nics:
if 'root' in nic:
yield 1,ns.pid
for os in w4sp.ns_root.ns:
if os != ns and nic in os.nics and nic not in tmp:
tmp.append(nic)
print('%s connected %s' % (ns.pid,os.pid))
yield ns.pid,os.pid
def psef(grep):
"""this is python replacement for ps -ef, based off of
http://stackoverflow.com/questions/2703640/process-list-on-linux-via-python"""
pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]
for pid in pids:
try:
#read the command line from /proc/<pid>/cmdline
with open(os.path.join('/proc', pid, 'cmdline'), 'rb') as cmd:
cmd = cmd.read()
if grep in cmd:
return pid, cmd
#if the proc terminates before we read it
except IOError:
continue
return False
# use decorators to link the function to a url
@app.route('/')
def launcher():
dockers = []
for docker in NSROOT.ns:
dockers.append(docker)
return render_template('launcher2.html', dockers=dockers)
@app.route('/getnet')
def getnet():
"""This returns the nodes and edges used by visjs, node = { 'id': ns.pid, 'label': ns.name, 'title': ip_address }
edges = { 'from': ns_connected_from, 'to': ns_connected_to }"""
data = {}
data['nodes'] = []
data['edges'] = []
for ns in w4sp.ns_root.ns:
tmp = {}
tmp['id'] = ns.pid
tmp['label'] = ns.name
if ns.name == 'inet':
tmp['color'] = 'rgb(0,255,0)'
tmp_popup = ''
for ips in ns.get_ips():
# { 'nic' : ip }
tmp_popup += '%s : %s <br>' % ips.popitem()
tmp['title'] = tmp_popup
data['nodes'].append(tmp)
tmp_popup = ''
#now add the root ns
for ips in w4sp.ns_root.get_ips():
tmp_popup += '%s : %s <br>' % ips.popitem()
data['nodes'].append({'id' : 1, 'label' : ' kali ', 'color' : 'rgb(204,0,0)', 'title' : tmp_popup})
for f,t in get_connections():
tmp = {}
tmp['from'] = f
tmp['to'] = t
data['edges'].append(tmp)
print(data)
return jsonify(**data)
@app.route('/runshark', methods=['POST', 'GET'])
def runshark():
"""this runs wireshark within the network namespace"""
error = None
if request.method == 'POST':
print('[*] POST IN RUNSHARK')
for key in request.form.keys():
if request.form[key] == '1':
w4sp.runshark('root')
for ns in NSROOT.ns:
if ns.pid == request.form[key]:
print ns.pid
print ns.name
w4sp.runshark(ns.name)
return 'launched'
@app.route('/setup')
def setup():
"""start the network"""
if len(NSROOT.ns) >= 1:
return 'REFRESH'
try:
w4sp.setup_network2('eth0')
time.sleep(3)
return 'REFRESH'
except:
print(traceback.format_exc())
return 'ERROR'
@app.route('/mitm')
def mitm():
"""this connects vic3 to the root ns so we can mitm it"""
#should add a check to see if vic3 already exists
if w4sp.c('vic3'):
return 'ERROR'
NSROOT.register_ns('vic3', 'w4sp/labs:victims')
w4sp.c('vic3').connect(w4sp.ns_root)
for nic in netifaces.interfaces():
if 'root' in nic:
w4sp.r('ip link set $nic down')
w4sp.r('ip link set $nic name vic3')
w4sp.r('ip link set vic3 up')
return 'ok'
@app.route('/add_vic2')
def add_vic():
"""this connects up another victim to the '1st' net so we can do things like dhcp/DNS spoofing"""
if w4sp.c('vic2'):
return 'ERROR'
NSROOT.register_ns('vic2', 'w4sp/labs:victims')
w4sp.c('vic2').connect(w4sp.c('sw1'))
return 'ok'
@app.route('/is_ips')
def is_ips():
"""quick check to see if suricata is running"""
if psef('suricata'):
return 'ok',200
else:
return 'error',404
@app.route('/ips')
def ips():
"""this starts suricata if it isn't running"""
if psef('suricata'):
return 'error',404
#if sw2 isn't even up then we need to bail
if not w4sp.c('sw2'):
return 'error',404
#here I need to start up ELK, then suricata, then logstash
#check if ELK is running and if not start it
if not w4sp.c('elk'):
NSROOT.register_ns('elk', 'w4sp/labs:elk')
#connect elk container to sw2 container
w4sp.c('elk').connect(w4sp.c('sw2'))
#now start suricata on sw1
w4sp.c('sw1').dexec('suricata -i br0')
#also start up logstash
w4sp.c('sw1').dexec('/opt/logstash/bin/logstash -f /etc/logstash/conf.d/logstash.conf')
return 'ok'
@app.route('/sploit')
def sploit():
"""this starts up and connects the sploitable instance"""
#if sploit is already created, just bail
if w4sp.c('sploit'):
return 'error', 404
#create the sploitable container and connect to sw2
NSROOT.register_ns('sploit', 'w4sp/labs:sploitable')
w4sp.c('sploit').connect(w4sp.c('sw2'))
return 'ok'
@app.route('/elk')
def elk():
"""this is just to start up ELK if we want to run it without the IPS"""
#if elk already exists, bail
if w4sp.c('elk'):
return 'error',404
#other create and connect up elk
NSROOT.register_ns('elk', 'w4sp/labs:elk')
#connect elk container to sw2 container
w4sp.c('elk').connect(w4sp.c('sw2'))
return 'ok'
@app.route('/wifi')
def wifi():
"""this sets up and configures the wireless docker
we are going to explicitly ignore the iw help and
screenscrape the output to get our interface names
this function is going to make a lot of assumptions
thar be dragons"""
#check if the wifi docker is already running
if w4sp.c('wifi'):
#if it check if the cleartext hostapd is running
if psef('hostapd_clear'):
return 'wifi already running', 404
#if hostapd isn't running lets start it
else:
w4sp.c('wifi').dexec('hostapd /hostapd_clear.conf')
return 'ok1'
#count of interfaces discovered and var for nic name
count = 0
phy = False
#our regex to find phy%d
match = re.compile('phy\d')
#get iw output
iwo = subprocess.check_output(['iw', 'list'])
for line in iwo.split():
#find they phy interface number
if match.search(line):
count += 1
phy = line.strip()
#check that we got one and only one phy
if count >= 2:
return 'got more than one phy interface, remove one wireless device', 500
if not phy:
return 'didn''t find a valid phy, please check wifi device connection', 500
#we get here we should have a valid phy name
#we are going to spin up the wireless container
NSROOT.register_ns('wifi', 'w4sp/labs:wireless')
#connect wifi container to sw2
w4sp.c('wifi').connect(w4sp.c('sw2'))
#no we need to move our wifi nic into the container
cmd = 'iw phy %s set netns %s' % (phy, w4sp.c('wifi').pid)
try:
subprocess.call(cmd.split(' '))
#ugh, delaying so setup_wifi.py can catch the new interface :/
time.sleep(0.01)
w4sp.c('wifi').dexec('hostapd /hostapd_clear.conf')
return 'ok'
except:
return 'error moving wireless device to container', 500
@app.route('/wpa2')
def wpa2():
"""this sets up and configures the wireless docker
we are going to explicitly ignore the iw help and
screenscrape the output to get our interface names
this function is going to make a lot of assumptions
thar be dragons"""
#check if the wifi docker is already running
if w4sp.c('wifi'):
#if it check if the cleartext hostapd is running
if psef('hostapd_wpa2'):
return 'wifi already running', 404
#if hostapd isn't running lets start it
else:
w4sp.c('wifi').dexec('hostapd /hostapd_wpa2.conf')
return 'ok1'
#count of interfaces discovered and var for nic name
count = 0
phy = False
#our regex to find phy%d
match = re.compile('phy\d')
#get iw output
iwo = subprocess.check_output(['iw', 'list'])
for line in iwo.split():
#find they phy interface number
if match.search(line):
count += 1
phy = line.strip()
#check that we got one and only one phy
if count >= 2:
return 'got more than one phy interface, remove one wireless device', 500
if not phy:
return 'didn''t find a valid phy, please check wifi device connection', 500
#we get here we should have a valid phy name
#we are going to spin up the wireless container
NSROOT.register_ns('wifi', 'w4sp/labs:wireless')
#connect wifi container to sw2
w4sp.c('wifi').connect(w4sp.c('sw2'))
#no we need to move our wifi nic into the container
cmd = 'iw phy %s set netns %s' % (phy, w4sp.c('wifi').pid)
try:
subprocess.call(cmd.split(' '))
w4sp.c('wifi').dexec('hostapd /hostapd_wpa2.conf')
return 'ok'
except:
return 'error moving wireless device to container', 500
@app.route('/shutdown')
def shutdown():
"""cleans up mess"""
w4sp.ns_root.shutdown()
time.sleep(3)
return ''
# start the server with the 'run()' method
if __name__ == '__main__':
script_dir = os.path.dirname(os.path.realpath(__file__))
cwd = os.getcwd()
if script_dir != cwd:
print('[*] Not run from the script directory, changing dirs')
#move to the directory the script is stored in
os.chdir(script_dir)
#check to see if the w4sp-lab user exists
try:
pwd.getpwnam('w4sp-lab')
except KeyError:
print('[*] w4sp-lab user non-existant, creating')
try:
w4sp.utils.r('useradd -m w4sp-lab -s /bin/bash -G sudo,wireshark -U')
except:
w4sp.utils.r('useradd -m w4sp-lab -s /bin/bash -G sudo -U')
print("[*] Please run: 'passwd w4sp-lab' to set your password, then login as w4sp-lab and rerun lab")
sys.exit(-1)
#check to see if we logged in as w4sp-lab
if os.getlogin() != 'w4sp-lab':
print('[*] Please login as w4sp-lab and run script with sudo')
sys.exit(-1)
#check dumpcap
w4sp.check_dumpcap()
#see if we can run docker
try:
images = subprocess.check_output(['docker', 'images']).split('\n')
except (OSError,subprocess.CalledProcessError) as e:
#if e is of type subprocess.CalledProcessError, assume docker is installed but service isn't started
if type(e) == subprocess.CalledProcessError:
subprocess.call(['service', 'docker', 'start'])
images = subprocess.check_output(['docker', 'images']).split('\n')
elif e.errno == os.errno.ENOENT:
# handle file not found error, lets install docker
subprocess.call(['apt-get', 'update'])
subprocess.call(['apt-get', 'install', '-y',
'bridge-utils',
'apt-transport-https', 'ca-certificates',
'software-properties-common'])
# check if we already configured docker repos
with open('/etc/apt/sources.list', 'ra+') as f:
if 'docker' not in f.read():
#adding the docker gpg key and repo
subprocess.call(['wget', 'https://yum.dockerproject.org/gpg',
'-O', 'docker.gpg'])
subprocess.call(['apt-key', 'add', 'docker.gpg'])
#add the stretch repo, need to figure out how to map kali versions
#to debian versions
f.write('\ndeb https://apt.dockerproject.org/repo/ debian-stretch main\n')
subprocess.call(['apt-get', 'update'])
subprocess.call(['apt-get', '-y', 'install', 'docker-engine'])
subprocess.call(['service', 'docker', 'start'])
images = subprocess.check_output(['docker', 'images']).split('\n')
else:
# Something else went wrong
raise
try:
tmp_n = 0
for image in images:
if 'w4sp/labs' in image:
tmp_n += 1
#basic check to see if we have at least six w4sp named images
if tmp_n > len(os.listdir('images')):
print('[*] w4sp/labs images available')
else:
print('[*] Not enough w4sp/labs images found, building now')
w4sp.docker_build('images/')
except:
#just a placeholder
raise
#adding logic to handle writing daemon.json so we can disable docker iptables rules
daemon_f = '/etc/docker/daemon.json'
if not os.path.isfile(daemon_f):
with open(daemon_f, 'w+') as f:
f.write('{ "iptables": true }')
subprocess.call(['iptables', '-P', 'INPUT', 'ACCEPT'])
subprocess.call(['iptables', '-P', 'FORWARD', 'ACCEPT'])
subprocess.call(['iptables', '-P', 'OUTPUT', 'ACCEPT'])
subprocess.call(['iptables', '-t', 'nat', '-F'])
subprocess.call(['iptables', '-t', 'mangle', '-F'])
subprocess.call(['iptables', '-F'])
subprocess.call(['iptables', '-X'])
w4sp.docker_clean()
app.config['DEBUG'] = args.debug
p = Process(target=app.run)
p.start()
time.sleep(3)
print('[*] Lab Launched, Starting Browser')
print('[*] Do not close this terminal. Closing Terminal will terminate lab.')
subprocess.call(['su', '-', 'w4sp-lab', '-c', 'firefox 127.0.0.1:5000'])