-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvulnsrv.py
executable file
·941 lines (827 loc) · 43.7 KB
/
vulnsrv.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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import with_statement
import base64
import binascii
import bz2
import hashlib
import os
import os.path
import re
import struct
import sqlite3
import threading
import time
import urllib
import sys
try:
import json
except ImportError:
# Python <2.6, use trivialjson (https://github.com/phihag/trivialjson):
unichr = __builtins__.unichr # Trick modern flake8
class json(object):
@staticmethod
def loads(s):
s = s.decode('UTF-8')
def raise_error(msg, i):
raise ValueError(msg + ' at position ' + str(i) + ' of ' + repr(s) + ': ' + repr(s[i:]))
def skip_space(i, expect_more=True):
while i < len(s) and s[i] in ' \t\r\n':
i += 1
if expect_more:
if i >= len(s):
raise_error('Premature end', i)
return i
def decodeEscape(match):
esc = match.group(1)
_STATIC = {
'"': '"',
'\\': '\\',
'/': '/',
'b': unichr(0x8),
'f': unichr(0xc),
'n': '\n',
'r': '\r',
't': '\t',
}
if esc in _STATIC:
return _STATIC[esc]
if esc[0] == 'u':
if len(esc) == len('u') + 4:
return unichr(int(esc[1:5], 16))
if len(esc) == 5 + 6 and esc[5:7] == '\\u':
hi = int(esc[1:5], 16)
low = int(esc[7:11], 16)
return unichr((hi - 0xd800) * 0x400 + low - 0xdc00 + 0x10000)
raise ValueError('Unknown escape ' + str(esc))
def parse_string(i):
i += 1
e = i
while True:
e = s.index('"', e)
bslashes = 0
while s[e - bslashes - 1] == '\\':
bslashes += 1
if bslashes % 2 == 1:
e += 1
continue
break
rexp = re.compile(r'\\(u[dD][89aAbB][0-9a-fA-F]{2}\\u[0-9a-fA-F]{4}|u[0-9a-fA-F]{4}|.|$)')
stri = rexp.sub(decodeEscape, s[i:e])
return (e + 1, stri)
def parseObj(i):
i += 1
res = {}
i = skip_space(i)
if s[i] == '}': # Empty dictionary
return (i + 1, res)
while True:
if s[i] != '"':
raise_error('Expected a string object key', i)
i, key = parse_string(i)
i = skip_space(i)
if i >= len(s) or s[i] != ':':
raise_error('Expected a colon', i)
i, val = parse(i + 1)
res[key] = val
i = skip_space(i)
if s[i] == '}':
return (i + 1, res)
if s[i] != ',':
raise_error('Expected comma or closing curly brace', i)
i = skip_space(i + 1)
def parse_array(i):
res = []
i = skip_space(i + 1)
if s[i] == ']': # Empty array
return (i + 1, res)
while True:
i, val = parse(i)
res.append(val)
i = skip_space(i) # Raise exception if premature end
if s[i] == ']':
return (i + 1, res)
if s[i] != ',':
raise_error('Expected a comma or closing bracket', i)
i = skip_space(i + 1)
def parse_discrete(i):
for k, v in {'true': True, 'false': False, 'null': None}.items():
if s.startswith(k, i):
return (i + len(k), v)
raise_error('Not a boolean (or null)', i)
def parse_number(i):
mobj = re.match('^(-?(0|[1-9][0-9]*)(\.[0-9]*)?([eE][+-]?[0-9]+)?)', s[i:])
if mobj is None:
raise_error('Not a number', i)
nums = mobj.group(1)
if '.' in nums or 'e' in nums or 'E' in nums:
return (i + len(nums), float(nums))
return (i + len(nums), int(nums))
CHARMAP = {'{': parseObj, '[': parse_array, '"': parse_string, 't': parse_discrete, 'f': parse_discrete, 'n': parse_discrete}
def parse(i):
i = skip_space(i)
i, res = CHARMAP.get(s[i], parse_number)(i)
i = skip_space(i, False)
return (i, res)
i, res = parse(0)
if i < len(s):
raise ValueError('Extra data at end of input (index ' + str(i) + ' of ' + repr(s) + ': ' + repr(s[i:]) + ')')
return res
try:
_uc = unicode # Python 2
except NameError:
_uc = str # Python 3
def _b(s):
return s.encode('ascii')
try:
compat_bytes = bytes
except NameError: # Python < 2.6
compat_bytes = str
try:
import Cookie as _cookies
except ImportError:
import http.cookies as _cookies
try:
import Queue
_queue = Queue.Queue
except ImportError: # Python 3
import queue
_queue = queue.Queue
try:
from SocketServer import ThreadingMixIn
except ImportError:
from socketserver import ThreadingMixIn
try:
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
except ImportError:
from http.server import HTTPServer, BaseHTTPRequestHandler
try:
import urlparse
_urlparse = urlparse.urlparse
except ImportError: # Python 3
import urllib.parse
_urlparse = urllib.parse.urlparse
try:
from urllib.parse import urlencode
except ImportError: # Python <3
from urllib import urlencode
try:
import html
html.escape
except (ImportError, AttributeError): # Python < 3.2
_escape_map = {
ord('&'): _uc('&'),
ord('<'): _uc('<'),
ord('>'): _uc('>'),
}
_escape_map_full = {
ord('&'): _uc('&'),
ord('<'): _uc('<'),
ord('>'): _uc('>'),
ord('"'): _uc('"'),
ord('\''): _uc('''),
}
class html(object):
@staticmethod
def escape(s, quote=True):
"""
Replace special characters "&", "<" and ">" to HTML-safe sequences.
If the optional flag quote is true (the default), the quotation mark
character (") is also translated.
"""
s = _uc(s)
if quote:
return s.translate(_escape_map_full)
return s.translate(_escape_map)
def query2dict(query):
""" Raises a ValueError if the input is not valid application/x-www-form-urlencoded bytes """
def _percentDecode(bin):
bin = bin.replace(_b('+'), _b('%20'))
pstrs = bin.split(_b('%'))
resbin = pstrs[0]
for pstr in pstrs[1:]:
intv = int(pstr[:2], 16)
pbyte = struct.pack('!B', intv)
resbin += pbyte + pstr[2:]
res = resbin.decode('UTF-8', 'ignore')
return res
res = {}
for qel in query.split(_b('&')):
if len(qel) == 0:
continue
kbin, eq, vbin = qel.partition(_b('='))
k = _percentDecode(kbin)
v = _percentDecode(vbin)
res[k] = v
return res
_FILES_RAW = "QlpoOTFBWSZTWbNIx2gAHI5fgGAAUA//8j////q////6YB3cn13vdr2Xi971t3dsbdd2u71xWnR3bx61Dnu7rqbXu3XKqpNNXPXrZoO2rqbTd1ru47ubumWHHjDd7aejzjE3trnbjLrt73r25e901ve7vaDREyAjI0AGmmTE0yBoEeimCiYTTQaCAgCYCCaE8mIGqenqekwTRT1NAANACEFGmJNU/Gqmn4EaeqntU9TyT2qPTap6ho0AEiIRAQYkxpoANASb1B6JT9E1PUIekMc0yMhkwQ0YTBGmjRiBpkyMAAQSSBAIGiME0GTRPRpkNAKep5EhkDaAVkjOkmgbQmCP/vYDbxnTxt0xJGg9RMGH/Qmf+1/3hg/7mN/3MJMUuc0FlExiaPUbaRkZEVpBj90fslp6b8eh9vvj/lm4h3MrEo7ek5+p2V13faz/230n9/4633v2a8Tx/6RDaQH7gjIs/9FMDMf0EAfxOZ/aThj85IT/rT+tHT/WXzowrGGNONhwGad4uCNe6kRjbo/y4G/l4TMcaS/2Ldh2p3YpQqyp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decode('UTF-8'))
DBDATA = json.loads(bz2.decompress(base64.b64decode(_DBDATA_RAW.encode('ascii'))).decode('UTF-8'))
FAVICON = base64.b64decode(_FAVICON_RAW.encode('ascii'))
# Some of these headers allow attacks, do not copy for use outside of vulnsrv!
DEFAULT_HEADERS = [
('X-Frame-Options', 'DENY'),
('X-XSS-Protection', '0'),
('Cache-Control', 'private, max-age=0, no-cache'),
('Pragma', 'no-cache'),
]
class SQLliteManager(threading.Thread):
""" Makes the thread-unsafe sqlite work with threads """
def __init__(self):
super(SQLliteManager, self).__init__()
self.daemon = True
self._commandq = _queue()
self._resultq = _queue()
self._clientLock = threading.Lock()
def run(self):
while True:
conn = sqlite3.connect(":memory:")
conn.isolation_level = None # auto-commit
cursor = conn.cursor()
while True:
sql = self._commandq.get()
if sql == '_reset_':
self._resultq.put(None)
break
try:
cursor.execute(sql)
res = cursor.fetchall()
except:
_type, e, _traceback = sys.exc_info()
res = e
self._resultq.put(res)
cursor.close()
conn.close()
def query(self, sql):
""" Called from another thread, blocks until the result is ready """
self._clientLock.acquire()
self._commandq.put(sql)
res = self._resultq.get()
self._clientLock.release()
if isinstance(res, BaseException):
raise res
return res
def _VulnState_locked(f):
def lockf(self, *args, **kwargs):
self._lock.acquire()
try:
return f(self, *args, **kwargs)
finally:
self._lock.release()
return lockf
class VulnState(object):
def __init__(self):
self._lock = threading.Lock()
self._sqlThread = SQLliteManager()
self._sqlThread.start()
self.reset()
@_VulnState_locked
def csrfMessage(self, msg):
self._csrfMessages.append(msg)
@property
@_VulnState_locked
def csrfMessages(self):
res = self._csrfMessages[:]
return res
@_VulnState_locked
def reflected_xss_message(self, msg):
self._reflected_xss_messages.append(msg)
@property
@_VulnState_locked
def reflected_xss_messages(self):
return self._reflected_xss_messages[:]
@_VulnState_locked
def stored_xss_message(self, msg):
self._stored_xss_messages.append(msg)
@_VulnState_locked
def remove_stored_xss_messages(self):
self._stored_xss_messages = []
@property
@_VulnState_locked
def stored_xss_messages(self):
return self._stored_xss_messages[:]
@_VulnState_locked
def sqlQuery(self, sql):
return self._sqlThread.query(sql)
@property
@_VulnState_locked
def macSecret(self):
return self._macSecret
@_VulnState_locked
def reset(self):
self._csrfMessages = []
self._reflected_xss_messages = []
self._stored_xss_messages = []
self._macSecret = hashlib.md5(os.urandom(32)).hexdigest().encode('ascii')
assert len(self._macSecret) == 32
# TODO replace this with a Python in-memory database, which takes DBDATA in its constructor (and only needs to be initialized once)
self._sqlThread.query('_reset_')
for tableName, td in DBDATA.items():
csql = (
'CREATE TABLE ' + tableName + ' (' +
','.join(col['name'] + ' ' + col['type'] for col in td['structure']) +
')')
self._sqlThread.query(csql)
for d in td['data']:
dsql = (
'INSERT INTO ' + tableName +
' (' + ','.join(k for k, v in d.items()) + ')' +
' VALUES(' + ','.join("'" + v.replace("'", "''") + "'" for k, v in d.items()) +
')')
self._sqlThread.query(dsql)
def msgsToHtml(msgs):
res = (_uc('''
<h2>Nachrichten</h2>
<ul class="messages">'''))
for msg in reversed(msgs):
res += _uc('<li>')
res += html.escape(msg)
res += _uc('</li>\n')
res += _uc('</ul>')
return res
class VulnHandler(BaseHTTPRequestHandler):
def do_POST(self):
reqp = _urlparse(self.path)
try:
# getParams = query2dict(reqp.query.encode('ascii'))
postParams = self._readPostParams()
except ValueError:
_type, e, _traceback = sys.exc_info()
self.send_error(400, str(e))
return
sessionID = self._getSessionID(False)
if reqp.path == '/clientauth/secret':
if self._csrfCheck(postParams): # Technically, not a problem here - until we're starting to log who knows the secret
self._writeHtmlDoc(
_uc('<code class="secret">%s</code>') %
html.escape(base64.b16decode('4356452F4D49545245'.encode('ascii')).decode('UTF-8')),
'Geheimnis', sessionID)
elif reqp.path == '/csrf/send':
# No CSRF check here, duh
msg = postParams.get('message', '')
if msg == '':
self.send_error(400, 'Missing or empty message')
else:
self.vulnState.csrfMessage(msg)
self._redirect('/csrf/')
elif reqp.path == '/reflected_xss/send':
if self._csrfCheck(postParams):
msg = postParams.get('message', '')
if msg == '':
self.send_error(400, 'Missing or empty message')
else:
self.vulnState.reflected_xss_message(msg)
self._redirect('/reflected_xss/?username=Sender%2C')
elif reqp.path == '/stored_xss/send':
if self._csrfCheck(postParams):
msg = postParams.get('message', '')
if msg == '':
self.send_error(400, 'Missing or empty message')
else:
self.vulnState.stored_xss_message(msg)
self._redirect('/stored_xss/')
elif reqp.path == '/stored_xss/clear':
if self._csrfCheck(postParams):
self.vulnState.remove_stored_xss_messages()
self._redirect('/stored_xss/')
elif reqp.path == '/mac/login':
if self._csrfCheck(postParams):
val = _uc('user=Gast&time=' + str(int(time.time())))
mac = hashlib.sha256(self.vulnState.macSecret + val.encode('ascii')).hexdigest()
cookieval = mac + '!' + val
c = {'mac_session': cookieval}
self._redirect('/mac/', c)
elif reqp.path == '/reset':
if self._csrfCheck(postParams):
self.vulnState.reset()
self._redirect('/')
else:
self.send_error(404, 'No POST handler defined')
def do_GET(self):
reqp = _urlparse(self.path)
try:
getParams = query2dict(reqp.query.encode('ascii'))
except ValueError:
_type, e, _traceback = sys.exc_info()
self.send_error(400, 'Invalid query format: ' + str(e))
return
sessionID = self._getSessionID()
if reqp.path == '/':
self._writeHtmlDoc(_uc('''
<ol class="mainMenu">
<li><a href="clientauth/">Client-Side Authorization Check</a></li>
<li><a href="mac/">MAC Length Extension</a></li>
<li><a href="csrf/">Cross-Site Request Forgery (CSRF)</a></li>
<li><a href="reflected_xss/?username=Benutzer%21">Reflected Cross-Site Scripting (XSS)</a></li>
<li><a href="stored_xss/?username=Benutzer%21">Stored Cross-Site Scripting (XSS)</a></li>
<li><a href="sqlinjection/">SQL Injection</a></li>
<li><a href="pathtraversal/">Path Traversal</a></li>
</ol>'''), 'vulnsrv', sessionID)
elif reqp.path == '/clientauth/':
js_code = html.escape('if (\'you\' != \'admin\') {alert(\'Zugriff verweigert!\'); return false;} else return true;', True)
self._writeHtmlDoc(
_uc('''
<p>Finden Sie das Geheimnis heraus!</p>
<form action="secret" method="post">
<input type="submit" value="Geheimnis herausfinden"
onclick="%s" />
%s
</form>
''') % (js_code, self._getCsrfTokenField(sessionID)),
'Client-Side Authorization Check', sessionID)
elif reqp.path == '/csrf/':
self._writeHtmlDoc(
_uc('''
<p>Mit dem untenstehendem Formular können Sie Nachrichten schreiben.
Erstellen Sie eine HTML-Datei <code>evil-csrf.html</code>, bei deren Aufruf der arglose Benutzer hier unfreiwillig eine übelgesinnte Nachricht hinterlässt.
</p>
<form action="send" enctype="application/x-www-form-urlencoded" method="post">
<input type="text" name="message" autofocus="autofocus" required="required" placeholder="Eine freundliche Nachricht" size="50" />
<input type="submit" value="Senden" />
</form>
''') + msgsToHtml(self.vulnState.csrfMessages), 'CSRF', sessionID)
elif reqp.path == '/reflected_xss/':
username = getParams.get('username', 'Unbekannter')
self._writeHtmlDoc(_uc(
'''<div>Hallo %s</div>
<p>Das untenstehende Formular ist gegen Cross-Site Request Forgery geschützt.
Erstellen Sie eine HTML-Datei <code>evil-reflected-xss.html</code>, bei deren Aufruf der arglose Benutzer hier trotzdem unfreiwillig eine übelgesinnte Nachricht hinterlässt.
</p>
<form action="send" enctype="application/x-www-form-urlencoded" method="post">
<input type="text" name="message" autofocus="autofocus" required="required" placeholder="Eine freundliche Nachricht" size="50" />
%s
<input type="submit" value="Senden" />
</form>
''') % (_uc(username), self._getCsrfTokenField(sessionID)) + msgsToHtml(self.vulnState.reflected_xss_messages), 'Reflected XSS', sessionID)
elif reqp.path == '/stored_xss/':
self._writeHtmlDoc(_uc(
'''<div>Hallo <span class="userid">%s</span></div>
<p>Das untenstehende Formular ist gegen Cross-Site Request Forgery geschützt.
Sorgen Sie dafür, dass jeder Benutzer der diese Seite aufruft unfreiwillig eine Nachricht hinterlässt, die IP und Port des Benutzers beinhaltet.
</p>
<form action="send" enctype="application/x-www-form-urlencoded" method="post">
<input type="text" name="message" autocomplete="off" autofocus="autofocus" required="required" placeholder="Eine freundliche Nachricht" size="50" />
%s
<input type="submit" value="Senden" />
</form>
%s
<script>
function show(messages_json) {
var messages = JSON.parse(messages_json);
var list = document.querySelector('.messages');
messages.forEach(function(m) {
var li = document.createElement('li');
li.appendChild(document.createTextNode(m));
list.appendChild(li);
});
}
function download() {
var xhr = new XMLHttpRequest();
xhr.dataType = 'text';
xhr.onload = function(e) {
show(xhr.responseText);
};
xhr.open('GET', 'json');
xhr.send();
}
function send(msg) {
var xhr = new XMLHttpRequest();
var token = document.querySelector('input[name="csrfToken"]').value;
var params = 'csrfToken=' + encodeURIComponent(token) + '&message=' +encodeURIComponent(msg);
xhr.open('POST', 'send');
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send(params);
}
function user() {
return document.querySelector('.userid').textContent;
}
</script>
<script>
// JSON direkt einbinden
var messages_json = '%s';
show(messages_json);
// Vorheriger Code:
// download();
</script>
<form action="clear" enctype="application/x-www-form-urlencoded" method="post">
%s
<button role="submit">Alle Nachrichten löschen</button
</form>
''') % (_uc(':').join(map(_uc, self.client_address)), self._getCsrfTokenField(sessionID), msgsToHtml([]), json.dumps(self.vulnState.stored_xss_messages), self._getCsrfTokenField(sessionID)), 'Stored XSS', sessionID)
elif reqp.path == '/sqlinjection/':
webMessages = self.vulnState.sqlQuery("SELECT id,msg FROM messages WHERE user='web'")
self._writeHtmlDoc(_uc('''
<p>In der untenstehenden Tabelle sehen Sie die Nachrichten an den Benutzer <code>web</code>. Welche Nachrichten hat der Benutzer <code>admin</code> bekommen?</p>
<h2>Nachrichten an <code>web</code></h2>
<ul class="messages">
%s
</ul>''') % '\n'.join('<li><a href="/sqlinjection/msg?id=' + html.escape(str(row[0])) + '">' + html.escape(row[1]) + '</a></li>' for row in webMessages), 'SQL Injection', sessionID)
elif reqp.path == '/sqlinjection/msg':
msgNum = getParams.get('id', '')
sql = "SELECT id,user,msg FROM messages WHERE user='web' AND id='" + msgNum + "'"
try:
msgs = self.vulnState.sqlQuery(sql)
if len(msgs) == 0:
msg_html = '<td colspan="3">Keine web-Nachrichten gefunden</td>'
else:
msg_html = '\n'.join('<tr>' + ''.join('<td>' + html.escape(str(cell)) + '</td>' for cell in row) + '</tr>' for row in msgs)
except:
_type, e, _traceback = sys.exc_info()
msg_html = '<td colspan="3" class="error">' + html.escape(str(e)) + '</td>'
self._writeHtmlDoc(('''
<table class="messages">
<thead><tr><th>ID</th><th>Benutzer</th><th>Nachricht</th></tr></thead>
%s
</table>
<p><a href="/sqlinjection/">Zurück zur Übersicht</a></p>
''' % msg_html), 'Detailansicht: Nachricht ' + msgNum, sessionID)
elif reqp.path == '/pathtraversal/':
fileHtml = _uc('').join(
_uc('<li><a href="get?') + html.escape(urlencode([('file', fn)])) + _uc('">') + html.escape(fn) + _uc('</a></li>\n')
for fn in FILES['/var/www/img']['content'])
self._writeHtmlDoc(_uc('''
<p>Welchen Unix-Account sollte ein Angreifer näher untersuchen?</p>
<p><em>Bonus-Aufgabe</em>: Was ist das Passwort des Accounts?</p>
<p>Dateien zum Download:</p>
<ul>
%s
</ul>''' % fileHtml), 'Path Traversal', sessionID)
elif reqp.path == '/pathtraversal/get':
fn = '/var/www/img/' + getParams.get('file', '')
# Resolve the path.
# If we were using a real filesystem, this would be done automatically by the OS filesystem functions, of course
curPath = []
for pel in fn.split('/'):
if pel == '' or pel == '.':
continue
if pel == '..':
if len(curPath) > 0:
curPath.pop()
# else: We're at the root, and /../ is /
else:
curPath.append(pel)
finalPath = '/' + '/'.join(curPath)
if finalPath.endswith('/'):
finalPath = finalPath[:-1]
if finalPath in FILES:
fdata = FILES[finalPath]
if fdata['type'] == '__directory__':
self.send_error(404, 'Is a directory')
else:
fileBlob = base64.b64decode(fdata['blob_b64'].encode('ascii'))
self.send_response(200)
self.send_header('Content-Type', fdata['type'])
self.send_header('Content-Length', str(len(fileBlob)))
self.end_headers()
self.wfile.write(fileBlob)
else:
self.send_error(404)
elif reqp.path == '/mac/':
cookies = self._readCookies()
raw_cookie = cookies.get('mac_session')
if raw_cookie is not None:
if isinstance(raw_cookie, compat_bytes): # Python 2.x
raw_cookie = raw_cookie.decode('latin1')
mac, _, session_data_str = raw_cookie.rpartition(_uc('!'))
session_data = session_data_str.encode('latin1')
secret = self.vulnState.macSecret
if hashlib.sha256(secret + session_data).hexdigest() == mac:
session = query2dict(session_data)
user = session['user']
timestamp = session['time']
else:
user = timestamp = _uc('(Falscher MAC)')
else:
raw_cookie = _uc('')
user = timestamp = _uc('(Nicht gesetzt)')
assert isinstance(raw_cookie, _uc)
raw_cookie_hex = binascii.b2a_hex(raw_cookie.encode('utf-8')).decode('ascii')
assert isinstance(raw_cookie_hex, _uc)
self._writeHtmlDoc(_uc('''
<p>Loggen Sie sich als Benutzer admin ein (ohne das Geheimnis aus dem Server-Prozess auszulesen).
Schreiben Sie dafür ein Programm, das den korrekten Cookie-Wert berechnet.</p>
<form method="post" action="login">
%s
<input type="submit" value="Gast-Login" />
</form>
<h3>Aktuelle Session-Daten:</h3>
<p>Cookie (roh): <code>%s</code> (%s Bytes)</p>
<dl>
<dt>Benutzername:</dt><dd>%s</dd>
<dt>Login-Zeit:</dt><dd>%s</dd>
</dl>
<p>Für den Angriff können Sie <a href="mac_attack.py">dieses Python-Skript</a> verwenden.
Das Skript erwartet, dass im lokalen Verzeichnis eine ausführbare Datei ./mac_extension liegt, die mit den Argumenten <code>[Bekannter Hash]</code> <code>[Bekannte Eingabe]</code> <code>[Einzufügende Daten]</code> <code>[Länge des secrets in Bytes (32)]</code> aufgerufen werden kann und das exploit zurückgibt.
</p>
''' % (
self._getCsrfTokenField(sessionID),
html.escape(raw_cookie),
html.escape(_uc(len(raw_cookie))),
html.escape(user),
html.escape(timestamp)
)), 'Length Extension-Angriffe gegen MAC', sessionID)
elif reqp.path == '/mac/mac_attack.py':
fdata = FILES['/mac/mac_attack.py']
fileBlob = base64.b64decode(fdata['blob_b64'].encode('ascii'))
self.send_response(200)
self.send_header('Content-Type', fdata['type'])
self.send_header('Content-Length', str(len(fileBlob)))
self.end_headers()
self.wfile.write(fileBlob)
elif reqp.path == '/favicon.ico':
self.send_response(200)
self.send_header('Content-Type', 'image/png')
self.send_header('Content-Length', str(len(FAVICON)))
self.end_headers()
self.wfile.write(FAVICON)
elif reqp.path == '/stored_xss/json':
self._write_json(self.vulnState.stored_xss_messages)
else:
self.send_error(404)
def _writeHtmlDoc(self, htmlContent, title, sessionID, add_headers=None):
if add_headers is None:
add_headers = DEFAULT_HEADERS
title = _uc(title)
mimeType = _uc('text/html; charset=utf-8')
htmlCode = (_uc('''<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="%s" />
<title>%s</title>
<style type="text/css">
body {margin: 0; padding: 0 2em;}
.mainMenu {font-size: 160%%;}
h1 {text-align: center;}
.secret{font-size: 180%%; /*background: #000;*/}
.error {background: #ffd4d4;}
td[colspan] {text-align: center;}
nav {position: fixed; left: 0; bottom: 0; padding: 0.5em; background: #eef}
nav>a {display: inline-block; margin: 0 0.4em;}
nav>.sep{display: inline-block; width: 1em;}
nav>form {display: inline-block;}
.messages{padding-left: 0;}
.messages>li{list-style-type: none; padding: 0.3em 0.5em;}
.messages>li:nth-child(even){background: #f4f4f4;}
.messages>li:nth-child(odd){background: #ddd;}
</style>
</head>
<body>
<h1>%s</h1>
%s
<nav>
<a href="/clientauth/">Client-Side Authorization Check</a>
<a href="/mac/">MAC Length Extension</a>
<a href="/csrf/">Cross-Site Request Forgery (CSRF)</a>
<a href="/reflected_xss/?username=Benutzer%%21">Reflected XSS</a>
<a href="/stored_xss/"">Stored XSS</a>
<a href="/sqlinjection/">SQL Injection</a>
<a href="/pathtraversal/">Path Traversal</a>
<span class="sep"></span>
<form class="reset" method="post" action="/reset">
%s
<input type="submit" value="clear data" />
</form>
</nav>
</body>
</html>
''' % (
html.escape(mimeType),
html.escape(title),
html.escape(title),
htmlContent,
self._getCsrfTokenField(sessionID)
)))
htmlBytes = htmlCode.encode('utf-8')
self.send_response(200)
self._writeCookies()
self.send_header('Content-Length', str(len(htmlBytes)))
self.send_header('Content-Type', mimeType)
for k, v in add_headers:
self.send_header(k, v)
self.end_headers()
self.wfile.write(htmlBytes)
def _write_json(self, data):
json_bytes = json.dumps(data).encode('utf-8')
self.send_response(200)
self._writeCookies()
self.send_header('Content-Length', str(len(json_bytes)))
self.send_header('Content-Type', 'application/json')
for k, v in DEFAULT_HEADERS:
self.send_header(k, v)
self.end_headers()
self.wfile.write(json_bytes)
def _redirect(self, target, cookieData=None):
self.send_response(302)
self.send_header('Location', target)
self._writeCookies(cookieData)
self.send_header('Content-Length', '0')
self.end_headers()
def _writeCookies(self, add=None):
cookies = {
'sessionID': self._getSessionID()
}
if add:
cookies.update(add)
c = _cookies.SimpleCookie()
for k, v in cookies.items():
assert re.match(r'^[a-zA-Z0-9_-]+$', k)
c[k] = v
c[k]['path'] = '/'
if sys.version_info >= (2, 6):
c[k]['httponly'] = True
outp = c.output(sep=_uc('\r\n')) + _uc('\r\n')
assert isinstance(outp, _uc)
if hasattr(self, '_headers_buffer'):
self._headers_buffer.append(outp.encode('latin1', 'strict'))
else:
self.wfile.write(outp.encode('latin1', 'strict'))
def _readCookies(self):
hdr = self.headers.get('cookie', '')
c = _cookies.SimpleCookie(hdr)
res = {}
for morsel in c.values():
res[morsel.key] = morsel.value
return res
def _readPostParams(self):
contentLen = int(self.headers['content-length'])
postBody = self.rfile.read(contentLen)
contentType = self.headers['content-type']
if not contentType.startswith('application/x-www-form-urlencoded'):
raise ValueError('Invalid content type')
res = query2dict(postBody)
return res
@property
def vulnState(self):
return self.server.vulnState
def _getSessionID(self, autogenerate=True):
cookies = self._readCookies()
if 'sessionID' in cookies:
return cookies['sessionID']
elif autogenerate:
return base64.b64encode(os.urandom(16)).decode('ascii')
else:
return None
def _csrfCheck(self, postParams):
if 'csrfToken' not in postParams:
self.send_error(400, 'CSRF Token missing')
return False
elif postParams['csrfToken'] != self._getSessionID(False):
self.send_error(400, 'CSRF Token not matching')
return False
else:
return True
def _getCsrfTokenField(self, sessionID):
return _uc('<input type="hidden" name="csrfToken" value="') + html.escape(sessionID) + _uc('" />')
error_message_format = _uc("""<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>%(code)d %(codestr)s</title>
</head>
<body>
<h1>Error %(code)d: %(codestr)s</h1>
<p>%(explain)s</p>
<p>Message: %(message)s</p>
</body>
</html>
""")
error_content_type = 'text/html; charset=utf-8'
def send_error(self, code, message=_uc('')):
codestr, explain = self.responses.get(code, (str(code), _uc('Unknown code')))
self.log_error("code %d, message %s", code, message)
content = (self.error_message_format %
{'code': code, 'message': html.escape(message),
'explain': explain, 'codestr': codestr})
self.send_response(code, codestr)
self.send_header('Content-Type', self.error_content_type)
self.send_header('X-Frame-Options', 'deny')
self.end_headers()
if self.command != 'HEAD' and code >= 200 and code not in (204, 304):
self.wfile.write(content.encode('utf-8'))
class VulnServer(ThreadingMixIn, HTTPServer):
def __init__(self, config):
self.vulnState = VulnState()
addr = (config.get('addr', 'localhost'), config.get('port', 8666))
HTTPServer.__init__(self, addr, VulnHandler)
def main():
args = sys.argv[1:]
if len(args) == 0:
config = {}
elif len(args) == 1:
with open(args[0], 'rb') as f:
config = json.load(f)
else:
help()
vsrv = VulnServer(config)
try:
vsrv.serve_forever()
except KeyboardInterrupt:
print('Killed by keyboard interrupt')
sys.exit(99)
def help():
sys.stdout.write('Usage: ' + sys.argv[0] + ' [configfile]\n')
sys.exit(2)
if __name__ == '__main__':
main()