-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlegacy-index.html
More file actions
6644 lines (5945 loc) · 291 KB
/
Copy pathlegacy-index.html
File metadata and controls
6644 lines (5945 loc) · 291 KB
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
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Memory Corruption Lab</title>
<style>
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
--bg: #0c0c0c;
--panel-bg: #141414;
--panel-border: #333;
--text: #ddd;
--text-dim: #999;
--green: #4ec9b0;
--blue: #569cd6;
--amber: #ce9178;
--red: #f44747;
--yellow: #dcdcaa;
--purple: #c586c0;
--comment: #7fbf68;
--font: 'Cascadia Code', 'Fira Code', 'JetBrains Mono', 'Consolas', monospace;
}
html, body { height: 100%; background: var(--bg); color: var(--text); font-family: var(--font); font-size: 13px; }
#app {
display: flex; flex-direction: column; height: 100vh; overflow: hidden;
}
header {
display: flex; align-items: center; gap: 1rem; padding: 0.5rem 1rem;
border-bottom: 1px solid var(--panel-border); background: var(--panel-bg);
flex-shrink: 0;
}
header h1 { font-size: 14px; color: var(--green); font-weight: normal; white-space: nowrap; }
nav { display: flex; gap: 0.15rem; flex-wrap: wrap; align-items: center; }
.nav-unit-label {
font-size: 10px; color: var(--text-dim); text-transform: uppercase;
letter-spacing: 0.05em; padding: 0.25rem 0.5rem; margin-left: 0.5rem;
border-left: 1px solid var(--panel-border);
}
.nav-unit-label:first-child { margin-left: 0; border-left: none; }
nav button {
background: transparent; border: 1px solid var(--panel-border); color: var(--text-dim);
padding: 0.25rem 0.5rem; font-family: var(--font); font-size: 11px; cursor: pointer;
transition: all 0.15s; min-width: 2em; text-align: center;
}
nav button:hover:not(:disabled) { border-color: var(--text-dim); color: var(--text); }
nav button.active { border-color: var(--green); color: var(--green); }
nav button.completed { border-color: var(--comment); color: var(--comment); }
nav button.completed::after { content: ' ✓'; font-size: 9px; }
nav button:disabled { opacity: 0.3; cursor: not-allowed; }
#badges { display: flex; gap: 0.5rem; margin-left: auto; }
.badge {
font-size: 11px; padding: 0.15rem 0.5rem; border: 1px solid var(--yellow);
color: var(--yellow); border-radius: 2px; white-space: nowrap;
}
main {
flex: 1; display: grid; overflow: hidden;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr;
gap: 1px; background: var(--panel-border);
}
.panel {
background: var(--panel-bg); display: flex; flex-direction: column; overflow: hidden;
}
.panel-hdr {
padding: 0.35rem 0.75rem; font-size: 11px; color: var(--text-dim);
border-bottom: 1px solid var(--panel-border); flex-shrink: 0;
text-transform: uppercase; letter-spacing: 0.05em;
}
.panel-body { flex: 1; overflow-y: auto; padding: 0.75rem; }
/* Source panel */
#source-panel { grid-column: 1; grid-row: 1; }
#source-code {
white-space: pre; line-height: 1.6; tab-size: 4;
}
.src-line { display: block; padding: 0 0.5rem; position: relative; }
.src-line .ln { color: var(--text-dim); display: inline-block; width: 2.5em; text-align: right; margin-right: 1em; user-select: none; }
.src-line.highlight { background: rgba(78, 201, 176, 0.1); border-left: 2px solid var(--green); }
.src-line.executing { background: rgba(220, 220, 170, 0.12); border-left: 2px solid var(--yellow); }
.src-line.executing .ln::before { content: '▶'; color: var(--yellow); position: absolute; left: 0.15rem; font-size: 10px; }
.src-kw { color: var(--blue); }
.src-type { color: var(--blue); }
.src-fn { color: var(--yellow); }
.src-str { color: var(--amber); }
.src-cmt { color: var(--comment); font-style: italic; }
.src-vuln { color: var(--red); text-decoration: underline wavy var(--red); }
/* Stack panel */
#stack-panel { grid-column: 2; grid-row: 1; }
#stack-viz { font-size: 12px; }
.stack-row {
display: flex; align-items: center; padding: 2px 0;
border-bottom: 1px solid rgba(255,255,255,0.03);
transition: background 0.3s;
}
.stack-row.region-start { border-top: 1px solid var(--panel-border); }
.stack-addr { color: var(--text-dim); width: 8em; text-align: right; padding-right: 0.75em; flex-shrink: 0; font-size: 11px; }
.stack-bytes { display: flex; gap: 2px; flex-shrink: 0; }
.stack-byte {
width: 2.2em; text-align: center; padding: 2px 0; border-radius: 2px;
transition: all 0.2s; font-size: 12px;
}
.stack-byte.empty { color: var(--text-dim); }
.stack-ascii { color: var(--text-dim); padding-left: 0.75em; width: 5em; flex-shrink: 0; font-size: 11px; }
.stack-label { color: var(--text-dim); padding-left: 0.75em; font-size: 11px; white-space: nowrap; }
.region-buffer .stack-byte { background: rgba(78, 201, 176, 0.15); color: var(--green); }
.region-canary .stack-byte { background: rgba(197, 134, 192, 0.15); color: var(--purple); }
.region-ebp .stack-byte { background: rgba(86, 156, 214, 0.15); color: var(--blue); }
.region-ret .stack-byte { background: rgba(206, 145, 120, 0.15); color: var(--amber); }
.stack-byte.overflow {
background: rgba(244, 71, 71, 0.3) !important; color: var(--red) !important;
animation: pulse 0.6s ease;
}
.stack-byte.just-written { animation: flash 0.4s ease; }
.stack-byte.current {
outline: 2px solid var(--yellow); outline-offset: -1px;
animation: flash 0.3s ease;
}
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.5; } }
@keyframes flash { 0% { background: rgba(255,255,255,0.4); } 100% {} }
/* Input panel */
#input-panel { grid-column: 1; grid-row: 2; }
#input-area { margin-bottom: 0.75rem; }
#input-area label { display: block; font-size: 11px; color: var(--text-dim); margin-bottom: 0.35rem; }
#payload-input {
width: 100%; background: #0a0a0a; border: 1px solid var(--panel-border); color: var(--text);
font-family: var(--font); font-size: 13px; padding: 0.5rem; resize: none; min-height: 2.5em;
}
#payload-input:focus { outline: none; border-color: var(--green); }
#payload-input::placeholder { color: var(--text-dim); }
.input-mode-toggle {
display: flex; gap: 0.5rem; margin-bottom: 0.5rem;
}
.input-mode-toggle button {
background: transparent; border: 1px solid var(--panel-border); color: var(--text-dim);
font-family: var(--font); font-size: 11px; padding: 0.2rem 0.5rem; cursor: pointer;
}
.input-mode-toggle button.active { border-color: var(--green); color: var(--green); }
.controls {
display: flex; gap: 0.5rem; flex-wrap: wrap;
}
.controls button {
background: transparent; border: 1px solid var(--panel-border); color: var(--text);
font-family: var(--font); font-size: 12px; padding: 0.35rem 1rem; cursor: pointer;
transition: all 0.15s;
}
.controls button:hover:not(:disabled) { border-color: var(--green); color: var(--green); }
.controls button.primary { border-color: var(--green); color: var(--green); }
.controls button:disabled { opacity: 0.3; cursor: not-allowed; }
/* Payload builder */
#payload-builder {
margin-top: 0.75rem; padding: 0.5rem; border: 1px solid var(--panel-border);
font-size: 11px;
}
#payload-builder h4 { color: var(--text-dim); font-weight: normal; margin-bottom: 0.5rem; text-transform: uppercase; letter-spacing: 0.05em; }
.builder-row { display: flex; align-items: center; gap: 0.5rem; margin-bottom: 0.35rem; }
.builder-row label { color: var(--text-dim); width: 10em; }
.builder-row input {
background: #0a0a0a; border: 1px solid var(--panel-border); color: var(--text);
font-family: var(--font); font-size: 11px; padding: 0.25rem 0.4rem; width: 10em;
}
.builder-row input:focus { outline: none; border-color: var(--green); }
.builder-row .hint { color: var(--text-dim); font-size: 10px; }
#built-payload { color: var(--yellow); word-break: break-all; margin-top: 0.5rem; padding: 0.35rem; background: #0a0a0a; min-height: 1.5em; }
/* Hex calculator */
#hex-calc {
margin-top: 0.75rem; padding: 0.5rem; border: 1px solid var(--panel-border);
font-size: 11px;
}
#hex-calc h4 { color: var(--text-dim); font-weight: normal; margin-bottom: 0.5rem; text-transform: uppercase; letter-spacing: 0.05em; }
.calc-row { display: flex; align-items: center; gap: 0.35rem; margin-bottom: 0.35rem; flex-wrap: wrap; }
.calc-row input {
background: #0a0a0a; border: 1px solid var(--panel-border); color: var(--text);
font-family: var(--font); font-size: 12px; padding: 0.25rem 0.4rem; width: 10em;
}
.calc-row input:focus { outline: none; border-color: var(--green); }
.calc-op { color: var(--yellow); font-size: 14px; font-weight: bold; width: 1.5em; text-align: center; }
.calc-eq { color: var(--green); font-size: 14px; font-weight: bold; width: 1.5em; text-align: center; }
#calc-result {
color: var(--green); font-size: 12px; font-weight: bold; padding: 0.25rem 0.4rem;
background: #0a0a0a; border: 1px solid var(--green); min-width: 10em; display: inline-block;
}
.calc-hint { color: var(--text-dim); font-size: 10px; margin-top: 0.25rem; }
/* Symbols table */
#symbols-table { margin-top: 0.75rem; font-size: 11px; }
#symbols-table h4 { color: var(--text-dim); font-weight: normal; margin-bottom: 0.35rem; text-transform: uppercase; letter-spacing: 0.05em; }
.sym-row { display: flex; gap: 1rem; padding: 0.15rem 0; }
.sym-name { color: var(--yellow); width: 8em; }
.sym-addr { color: var(--amber); }
.sym-note { color: var(--text-dim); }
.sym-row.target .sym-name { color: var(--green); }
.sym-row.target .sym-addr { color: var(--green); }
/* Log panel */
#log-panel { grid-column: 2; grid-row: 2; }
#exec-log { font-size: 12px; line-height: 1.6; }
.log-entry { padding: 0.1rem 0; border-bottom: 1px solid rgba(255,255,255,0.02); }
.log-info { color: var(--text-dim); }
.log-action { color: var(--text); }
.log-warn { color: var(--amber); }
.log-error { color: var(--red); }
.log-success { color: var(--green); font-weight: bold; }
.log-addr { color: var(--amber); }
.log-label { color: var(--text-dim); font-size: 10px; display: inline; margin-left: 0.5em; }
/* Exercise description */
#exercise-desc {
padding: 0.5rem 0.75rem; font-size: 12px; line-height: 1.5;
border-bottom: 1px solid var(--panel-border); flex-shrink: 0;
color: var(--text); background: rgba(78, 201, 176, 0.03);
}
#exercise-desc strong { color: var(--green); font-weight: normal; }
/* Sandbox controls */
.sandbox-controls {
display: flex; gap: 1rem; flex-wrap: wrap; margin-bottom: 0.75rem;
padding: 0.5rem; border: 1px solid var(--panel-border);
}
.sandbox-controls label { font-size: 11px; color: var(--text-dim); display: flex; align-items: center; gap: 0.4rem; }
.sandbox-controls input[type="range"] { width: 80px; }
.sandbox-controls input[type="checkbox"] { accent-color: var(--green); }
/* Success banner */
#success-banner {
display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
background: #111; border: 2px solid var(--green); padding: 2rem 3rem;
text-align: center; z-index: 100; box-shadow: 0 0 60px rgba(78,201,176,0.2);
}
#success-banner.show { display: block; animation: fadeIn 0.3s ease; }
#success-banner h2 { color: var(--green); font-size: 16px; margin-bottom: 0.5rem; }
#success-banner p { color: var(--text); font-size: 13px; }
#success-banner button {
margin-top: 1rem; background: transparent; border: 1px solid var(--green); color: var(--green);
font-family: var(--font); font-size: 12px; padding: 0.35rem 1rem; cursor: pointer;
}
@keyframes fadeIn { from { opacity: 0; transform: translate(-50%, -50%) scale(0.95); } to { opacity: 1; transform: translate(-50%, -50%) scale(1); } }
/* CRT scanline overlay */
.crt::after {
content: ''; position: fixed; top: 0; left: 0; width: 100%; height: 100%;
background: repeating-linear-gradient(0deg, rgba(0,0,0,0.03) 0px, rgba(0,0,0,0.03) 1px, transparent 1px, transparent 3px);
pointer-events: none; z-index: 1000;
}
/* Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: var(--panel-bg); }
::-webkit-scrollbar-thumb { background: var(--panel-border); }
#gadget-table { margin-top: 0.75rem; font-size: 11px; }
#gadget-table h4 { color: var(--text-dim); font-weight: normal; margin-bottom: 0.35rem; text-transform: uppercase; letter-spacing: 0.05em; }
.gadget-row { display: flex; gap: 0.75rem; padding: 0.15rem 0; cursor: pointer; }
.gadget-row:hover { background: rgba(255,255,255,0.03); }
.gadget-addr { color: var(--amber); width: 9em; }
.gadget-asm { color: var(--text); }
.gadget-row.used { opacity: 0.5; }
#register-display {
margin-top: 0.75rem; padding: 0.5rem; border: 1px solid var(--panel-border);
font-size: 11px; display: grid; grid-template-columns: repeat(4, 1fr); gap: 0.25rem;
}
.reg-entry { display: flex; gap: 0.35rem; }
.reg-name { color: var(--blue); width: 3em; }
.reg-val { color: var(--amber); }
.reg-val.changed { color: var(--green); animation: flash 0.4s ease; }
#sigframe-builder {
margin-top: 0.75rem; padding: 0.5rem; border: 1px solid var(--panel-border);
font-size: 11px;
}
#sigframe-builder h4 { color: var(--text-dim); font-weight: normal; margin-bottom: 0.5rem; text-transform: uppercase; }
.sigframe-row { display: flex; align-items: center; gap: 0.5rem; margin-bottom: 0.25rem; }
.sigframe-row label { color: var(--blue); width: 5em; }
.sigframe-row input {
background: #0a0a0a; border: 1px solid var(--panel-border); color: var(--text);
font-family: var(--font); font-size: 11px; padding: 0.2rem 0.4rem; width: 10em;
}
#heap-viz { font-size: 12px; }
.heap-memory-strip { display: flex; flex-wrap: wrap; gap: 2px; margin-bottom: 1rem; padding: 0.5rem; border: 1px solid var(--panel-border); }
.heap-chunk { display: flex; flex-direction: column; border: 1px solid var(--panel-border); border-radius: 2px; overflow: hidden; min-width: 4em; }
.heap-chunk.allocated { border-color: var(--green); }
.heap-chunk.freed { border-color: var(--text-dim); opacity: 0.7; }
.heap-chunk.corrupted { border-color: var(--red); }
.heap-chunk.highlighted { outline: 2px solid var(--yellow); outline-offset: 1px; }
.heap-chunk-header { display: flex; font-size: 10px; padding: 2px 4px; background: rgba(255,255,255,0.05); }
.heap-chunk-header .prev-size { color: var(--text-dim); margin-right: 0.5em; }
.heap-chunk-header .size-field { color: var(--amber); }
.heap-chunk-header .flags { color: var(--purple); margin-left: 0.25em; }
.heap-chunk-data { padding: 4px; font-size: 11px; min-height: 1.5em; display: flex; flex-wrap: wrap; gap: 1px; }
.heap-chunk-data .byte { width: 2em; text-align: center; font-size: 11px; padding: 1px 0; border-radius: 1px; }
.heap-chunk-data .byte.allocated { color: var(--green); background: rgba(78,201,176,0.1); }
.heap-chunk-data .byte.freed { color: var(--text-dim); background: rgba(255,255,255,0.03); }
.heap-chunk-data .byte.pointer { color: var(--amber); background: rgba(206,145,120,0.15); }
.heap-chunk-data .byte.corrupted { color: var(--red); background: rgba(244,71,71,0.2); }
.heap-chunk-label { font-size: 10px; color: var(--text-dim); padding: 2px 4px; text-align: center; border-top: 1px solid var(--panel-border); }
.free-lists { margin-top: 0.75rem; font-size: 11px; }
.free-lists h4 { color: var(--text-dim); font-weight: normal; margin-bottom: 0.35rem; text-transform: uppercase; letter-spacing: 0.05em; }
.free-list-row { display: flex; align-items: center; gap: 0.25rem; padding: 0.15rem 0; flex-wrap: wrap; }
.free-list-name { color: var(--blue); width: 10em; }
.free-list-node { display: inline-flex; align-items: center; gap: 0.25rem; }
.free-list-addr { color: var(--amber); padding: 1px 4px; border: 1px solid var(--panel-border); border-radius: 2px; }
.free-list-arrow { color: var(--text-dim); }
.free-list-nil { color: var(--text-dim); font-style: italic; }
.func-ptr-table { margin-top: 0.75rem; font-size: 11px; }
.func-ptr-table h4 { color: var(--text-dim); font-weight: normal; margin-bottom: 0.35rem; text-transform: uppercase; letter-spacing: 0.05em; }
.func-ptr-row { display: flex; gap: 0.75rem; padding: 0.15rem 0; }
.func-ptr-name { color: var(--yellow); width: 10em; }
.func-ptr-val { color: var(--amber); }
.func-ptr-val.overwritten { color: var(--red); animation: flash 0.4s ease; }
.heap-top-chunk { border: 1px dashed var(--text-dim); border-radius: 2px; padding: 4px 8px; font-size: 10px; color: var(--text-dim); text-align: center; flex: 1; min-width: 4em; }
</style>
</head>
<body class="crt">
<div id="app">
<header>
<h1>// MEMORY CORRUPTION LAB</h1>
<nav id="exercise-nav"></nav>
<div id="badges"></div>
</header>
<main>
<div class="panel" id="source-panel">
<div class="panel-hdr">source.c</div>
<div id="exercise-desc"></div>
<div class="panel-body"><div id="source-code"></div></div>
</div>
<div class="panel" id="stack-panel">
<div class="panel-hdr">stack <span id="stack-info"></span></div>
<div class="panel-body"><div id="stack-viz"></div><div id="heap-viz"></div></div>
</div>
<div class="panel" id="input-panel">
<div class="panel-hdr">input</div>
<div class="panel-body">
<div id="input-area"></div>
<div id="payload-builder" style="display:none"></div>
<div id="hex-calc" style="display:none"></div>
<div id="symbols-table" style="display:none"></div>
<div id="gadget-table" style="display:none"></div>
<div id="register-display" style="display:none"></div>
<div id="sigframe-builder" style="display:none"></div>
</div>
</div>
<div class="panel" id="log-panel">
<div class="panel-hdr">execution log</div>
<div class="panel-body"><div id="exec-log"></div></div>
</div>
</main>
</div>
<div id="success-banner">
<h2 id="success-title"></h2>
<p id="success-msg"></p>
<button onclick="dismissSuccess()">Continue</button>
</div>
<script>
// ──────────────────────────────────────────────
// Stack Simulator
// ──────────────────────────────────────────────
class StackSim {
constructor(bufSize, retAddr, savedEbp, baseAddr, useCanary) {
this.bufSize = bufSize;
this.canarySize = useCanary ? 4 : 0;
this.ebpSize = 4;
this.retSize = 4;
this.totalSize = bufSize + this.canarySize + this.ebpSize + this.retSize;
this.baseAddr = baseAddr || 0xbfff0100;
this.memory = new Uint8Array(this.totalSize);
this.written = new Uint8Array(this.totalSize);
this.overflowAt = new Uint8Array(this.totalSize);
this.inputLen = 0;
this.useCanary = !!useCanary;
this.origRetAddr = retAddr;
this.origSavedEbp = savedEbp;
this.canaryValue = 0;
if (this.useCanary) {
this.canaryValue = (Math.random() * 0xffffffff) >>> 0;
this._writeLE(this.bufSize, this.canaryValue, 4);
}
this._writeLE(this.bufSize + this.canarySize, savedEbp, 4);
this._writeLE(this.bufSize + this.canarySize + 4, retAddr, 4);
this.written.fill(0);
}
_writeLE(offset, value, size) {
for (let i = 0; i < size; i++) {
this.memory[offset + i] = (value >>> (i * 8)) & 0xff;
}
}
_readLE(offset, size) {
let v = 0;
for (let i = size - 1; i >= 0; i--) {
v = (v * 256) + this.memory[offset + i];
}
return v >>> 0;
}
writeInput(bytes) {
this.inputLen = bytes.length;
if (this.useCanary) this._writeLE(this.bufSize, this.canaryValue, 4);
this._writeLE(this.bufSize + this.canarySize, this.origSavedEbp, 4);
this._writeLE(this.bufSize + this.canarySize + 4, this.origRetAddr, 4);
this.written.fill(0);
this.overflowAt.fill(0);
for (let i = 0; i < bytes.length && i < this.totalSize; i++) {
this.memory[i] = bytes[i];
this.written[i] = 1;
if (i >= this.bufSize) {
this.overflowAt[i] = 1;
}
}
}
resetForInput() {
if (this.useCanary) this._writeLE(this.bufSize, this.canaryValue, 4);
this._writeLE(this.bufSize + this.canarySize, this.origSavedEbp, 4);
this._writeLE(this.bufSize + this.canarySize + 4, this.origRetAddr, 4);
this.written.fill(0);
this.overflowAt.fill(0);
this.inputLen = 0;
this.hlStart = -1;
this.hlEnd = -1;
}
clearBlank() {
this.memory.fill(0);
this.written.fill(0);
this.overflowAt.fill(0);
this.hlStart = -1;
this.hlEnd = -1;
}
writeWord(offset, bytes) {
for (let i = 0; i < bytes.length && (offset + i) < this.totalSize; i++) {
this.memory[offset + i] = bytes[i];
this.written[offset + i] = 1;
if ((offset + i) >= this.bufSize) this.overflowAt[offset + i] = 1;
}
this.inputLen = Math.max(this.inputLen, offset + bytes.length);
this.hlStart = offset;
this.hlEnd = offset + bytes.length;
}
markRegion(start, end) {
for (let i = start; i < end && i < this.totalSize; i++) this.written[i] = 1;
this.hlStart = start;
this.hlEnd = end;
}
clearHighlight() { this.hlStart = -1; this.hlEnd = -1; }
getRetAddr() { return this._readLE(this.bufSize + this.canarySize + 4, 4); }
getSavedEbp() { return this._readLE(this.bufSize + this.canarySize, 4); }
getCanary() { return this.useCanary ? this._readLE(this.bufSize, 4) : null; }
canaryIntact() { return !this.useCanary || this.getCanary() === this.canaryValue; }
regionOf(byteIndex) {
if (byteIndex < this.bufSize) return 'buffer';
if (this.useCanary && byteIndex < this.bufSize + this.canarySize) return 'canary';
if (byteIndex < this.bufSize + this.canarySize + this.ebpSize) return 'ebp';
if (byteIndex < this.bufSize + this.canarySize + this.ebpSize + this.retSize) return 'ret';
return 'ret'; // extra bytes past ret (for ret2libc/ROP/SROP payloads)
}
}
// ──────────────────────────────────────────────
// HeapSim — glibc-like heap allocator simulation
// ──────────────────────────────────────────────
class HeapSim {
constructor(size = 1024) {
this.memorySize = size;
this.memory = new Uint8Array(size);
this.written = new Uint8Array(size);
this.highlight = { start: -1, end: -1 };
this.chunks = new Map(); // addr → {size, allocated, dataStart}
this.HEADER_SIZE = 8; // prev_size(4) + size(4)
this.MIN_CHUNK = 16; // header + at least 8 data (or fd+bk)
this.tcache = {}; // size → [addr, ...] (max 7 per bin)
this.fastbins = {}; // size → [addr, ...]
this.unsorted = []; // [addr, ...]
this.topAddr = 0;
this.topSize = size;
this.funcPtrs = {};
this.mainArena = 0x0804b100;
this.baseAddr = 0x08050000;
}
_writeLE(offset, value, size) {
for (let i = 0; i < size; i++) {
this.memory[offset + i] = (value >>> (i * 8)) & 0xff;
}
}
_readLE(offset, size) {
let v = 0;
for (let i = size - 1; i >= 0; i--) {
v = (v * 256) + this.memory[offset + i];
}
return v >>> 0;
}
_alignSize(requested) {
// Add header, align to 8 bytes, enforce minimum
let total = requested + this.HEADER_SIZE;
total = (total + 7) & ~7;
if (total < this.MIN_CHUNK) total = this.MIN_CHUNK;
return total;
}
malloc(requested) {
const chunkSize = this._alignSize(requested);
const dataSize = chunkSize - this.HEADER_SIZE;
// 1. Check tcache
if (this.tcache[chunkSize] && this.tcache[chunkSize].length > 0) {
const addr = this.tcache[chunkSize].pop();
const chunk = this.chunks.get(addr);
if (chunk) {
chunk.allocated = true;
this._writeChunkHeader(addr, chunkSize, true);
return { addr: addr, dataAddr: addr + this.HEADER_SIZE, dataSize: dataSize };
}
}
// 2. Check fastbin (for sizes <= 64)
if (chunkSize <= 64 && this.fastbins[chunkSize] && this.fastbins[chunkSize].length > 0) {
const addr = this.fastbins[chunkSize].pop();
const chunk = this.chunks.get(addr);
if (chunk) {
chunk.allocated = true;
this._writeChunkHeader(addr, chunkSize, true);
return { addr: addr, dataAddr: addr + this.HEADER_SIZE, dataSize: dataSize };
}
}
// 3. Check unsorted bin
for (let i = 0; i < this.unsorted.length; i++) {
const addr = this.unsorted[i];
const chunk = this.chunks.get(addr);
if (chunk && chunk.size >= chunkSize) {
this.unsorted.splice(i, 1);
chunk.allocated = true;
this._writeChunkHeader(addr, chunk.size, true);
return { addr: addr, dataAddr: addr + this.HEADER_SIZE, dataSize: chunk.size - this.HEADER_SIZE };
}
}
// 4. Carve from top chunk
if (this.topSize >= chunkSize) {
const addr = this.topAddr;
this.topAddr += chunkSize;
this.topSize -= chunkSize;
this.chunks.set(addr, { size: chunkSize, allocated: true, dataStart: addr + this.HEADER_SIZE });
this._writeChunkHeader(addr, chunkSize, true);
return { addr: addr, dataAddr: addr + this.HEADER_SIZE, dataSize: dataSize };
}
return null; // out of memory
}
free(addr) {
// addr is the chunk start (header), not the data pointer
// If caller passes data pointer, adjust
let chunkAddr = addr;
if (!this.chunks.has(addr) && this.chunks.has(addr - this.HEADER_SIZE)) {
chunkAddr = addr - this.HEADER_SIZE;
}
const chunk = this.chunks.get(chunkAddr);
if (!chunk || !chunk.allocated) return false;
chunk.allocated = false;
const chunkSize = chunk.size;
const dataStart = chunkAddr + this.HEADER_SIZE;
// Mark chunk as freed in header (clear in-use bit)
this._writeChunkHeader(chunkAddr, chunkSize, false);
// 1. Try tcache first (max 7 per bin)
if (!this.tcache[chunkSize]) this.tcache[chunkSize] = [];
if (this.tcache[chunkSize].length < 7) {
// Write fd pointer (points to previous tcache entry or 0)
const prevFd = this.tcache[chunkSize].length > 0
? this.tcache[chunkSize][this.tcache[chunkSize].length - 1] + this.HEADER_SIZE
: 0;
this._writeLE(dataStart, prevFd, 4);
this.written[dataStart] = 1;
this.written[dataStart + 1] = 1;
this.written[dataStart + 2] = 1;
this.written[dataStart + 3] = 1;
this.tcache[chunkSize].push(chunkAddr);
return true;
}
// 2. Fastbin (if size <= 64)
if (chunkSize <= 64) {
if (!this.fastbins[chunkSize]) this.fastbins[chunkSize] = [];
const prevFd = this.fastbins[chunkSize].length > 0
? this.fastbins[chunkSize][this.fastbins[chunkSize].length - 1] + this.HEADER_SIZE
: 0;
this._writeLE(dataStart, prevFd, 4);
this.written[dataStart] = 1;
this.written[dataStart + 1] = 1;
this.written[dataStart + 2] = 1;
this.written[dataStart + 3] = 1;
this.fastbins[chunkSize].push(chunkAddr);
return true;
}
// 3. Unsorted bin
const prevFd = this.unsorted.length > 0
? this.unsorted[this.unsorted.length - 1] + this.HEADER_SIZE
: 0;
this._writeLE(dataStart, prevFd, 4);
this.written[dataStart] = 1;
this.written[dataStart + 1] = 1;
this.written[dataStart + 2] = 1;
this.written[dataStart + 3] = 1;
this.unsorted.push(chunkAddr);
return true;
}
_writeChunkHeader(addr, size, inUse) {
// prev_size (4 bytes) — leave as-is for now
// size field (4 bytes): actual size | PREV_INUSE flag (bit 0)
const sizeField = inUse ? (size | 1) : (size & ~1);
this._writeLE(addr + 4, sizeField, 4);
}
write(offset, bytes) {
for (let i = 0; i < bytes.length && (offset + i) < this.memorySize; i++) {
this.memory[offset + i] = bytes[i];
this.written[offset + i] = 1;
}
}
read(offset, size) {
const result = [];
for (let i = 0; i < size && (offset + i) < this.memorySize; i++) {
result.push(this.memory[offset + i]);
}
return result;
}
getChunksForDisplay() {
const sorted = Array.from(this.chunks.entries())
.sort((a, b) => a[0] - b[0])
.map(([addr, info]) => ({
addr: addr,
size: info.size,
allocated: info.allocated,
dataStart: addr + this.HEADER_SIZE,
dataSize: info.size - this.HEADER_SIZE,
highlighted: addr >= this.highlight.start && addr < this.highlight.end
}));
return sorted;
}
getFreeLists() {
return {
tcache: { ...this.tcache },
fastbins: { ...this.fastbins },
unsorted: [...this.unsorted]
};
}
markRegion(start, end) {
this.highlight = { start: start, end: end };
}
clearHighlight() {
this.highlight = { start: -1, end: -1 };
}
}
// ──────────────────────────────────────────────
// Hex/address helpers
// ──────────────────────────────────────────────
function hex8(v) { return '0x' + (v >>> 0).toString(16).padStart(8, '0'); }
function hex2(v) { return (v & 0xff).toString(16).padStart(2, '0'); }
function toAscii(v) { return (v >= 0x20 && v <= 0x7e) ? String.fromCharCode(v) : '.'; }
function addrToLE(addrStr) {
let v = parseInt(addrStr, 16);
if (isNaN(v)) return null;
return [(v >>> 0) & 0xff, (v >>> 8) & 0xff, (v >>> 16) & 0xff, (v >>> 24) & 0xff];
}
function hexStrToBytes(str) {
const clean = str.replace(/\s+/g, '').replace(/^0x/i, '');
const bytes = [];
for (let i = 0; i < clean.length; i += 2) {
const b = parseInt(clean.substr(i, 2), 16);
if (isNaN(b)) break;
bytes.push(b);
}
return bytes;
}
function strToBytes(str) {
return Array.from(str).map(c => c.charCodeAt(0) & 0xff);
}
// ──────────────────────────────────────────────
// State
// ──────────────────────────────────────────────
const state = {
currentEx: 0,
sim: null,
completed: new Set(),
logMessages: [],
inputMode: 'text',
stepIndex: 0,
symbols: {},
aslrBase: 0,
running: false,
execSteps: null,
execIndex: 0,
};
// ──────────────────────────────────────────────
// Exercises
// ──────────────────────────────────────────────
const BASE_SYMBOLS = {
main: 0x08048000,
vuln: 0x08048054,
win: 0x08048150,
normal: 0x080480a0,
system: 0x08048200,
binsh: 0x08049300,
};
function retAddrInMain() { return (state.symbols.main || BASE_SYMBOLS.main) + 0x25; }
const EXERCISES = [
{
unit: 1,
title: '01: The Stack Frame',
desc: 'Watch how a function call sets up memory. Click <strong>Step</strong> to see each piece get placed on the stack — the computer saves where to go back to (return address), saves a bookmark to the previous frame, then makes room for local variables.',
code: [
{ text: '#include <stdio.h>', cls: '' },
{ text: '', cls: '' },
{ text: 'void vuln() {', cls: '', fn: true },
{ text: ' char buf[16];', cls: 'highlight' },
{ text: ' // memory layout on the stack:', cls: 'cmt' },
{ text: ' // [buf 16 bytes] [saved bookmark] [go-back address]', cls: 'cmt' },
{ text: '}', cls: '' },
{ text: '', cls: '' },
{ text: 'int main() {', cls: '' },
{ text: ' vuln();', cls: 'highlight' },
{ text: ' return 0;', cls: '' },
{ text: '}', cls: '' },
],
mode: 'step',
bufSize: 16,
showSymbols: false,
showBuilder: false,
aslr: false,
steps: [
{ region: 'none', log: ['info', 'main() is about to call vuln()...'] },
{ region: 'ret', log: ['action', 'The computer saves the go-back address so it knows where to return after vuln() finishes'] },
{ region: 'ebp', log: ['action', 'Save a bookmark to the previous frame (saved EBP) — used to clean up later'] },
{ region: 'buffer', log: ['action', 'Make room for buf[] — 16 bytes of space for our input'] },
{ region: 'all', log: ['info', 'Done! The stack now has: our buffer (green), the saved bookmark (blue), and the go-back address (orange). The go-back address is the key — it controls where the program goes next.'] },
],
check() { return false; },
},
{
unit: 1,
title: '02: The Overflow',
desc: 'This function reads your input with <strong>gets()</strong> — a dangerous function that doesn\'t check how much you type. Fill the 16-byte buffer, then <strong>keep typing</strong>. Watch what happens when your input spills into the saved bookmark and go-back address above it.',
code: [
{ text: '#include <stdio.h>', cls: '' },
{ text: '', cls: '' },
{ text: 'void vuln() {', cls: '', fn: true },
{ text: ' char buf[16];', cls: '' },
{ text: ' gets(buf);', cls: 'highlight vuln' },
{ text: ' printf("You entered: %s\\n", buf);', cls: '' },
{ text: '}', cls: '' },
{ text: '', cls: '' },
{ text: 'int main() {', cls: '' },
{ text: ' printf("Enter your name: ");', cls: '' },
{ text: ' vuln();', cls: '' },
{ text: ' return 0;', cls: '' },
{ text: '}', cls: '' },
],
mode: 'input-text',
bufSize: 16,
showSymbols: false,
showBuilder: false,
aslr: false,
check(sim) {
const ret = sim.getRetAddr();
return ret !== sim.origRetAddr && !Object.values(state.symbols).includes(ret);
},
winTitle: 'Crash! Segmentation Fault',
winMsg: 'The program crashed because you overwrote the go-back address with garbage. The computer tried to jump to a nonsense location and exploded. Nice work — in exercise 3, you\'ll control WHERE it jumps instead of just crashing it.',
},
{
unit: 1,
title: '03: Hijack Execution',
desc: 'Now instead of crashing, make the program jump to <strong>win()</strong>. Use the payload builder to craft your input: 16 bytes of filler (fills the buffer), 4 bytes of junk (overwrites saved bookmark), then the address of win() written <strong>backwards</strong> (little-endian — the least significant byte goes first).',
code: [
{ text: '#include <stdio.h>', cls: '' },
{ text: '#include <stdlib.h>', cls: '' },
{ text: '', cls: '' },
{ text: 'void win() {', cls: '' },
{ text: ' printf("FLAG{you_own_the_eip}\\n");', cls: '' },
{ text: ' // you redirected execution here!', cls: 'cmt' },
{ text: '}', cls: '' },
{ text: '', cls: '' },
{ text: 'void vuln() {', cls: '', fn: true },
{ text: ' char buf[16];', cls: '' },
{ text: ' gets(buf);', cls: 'highlight vuln' },
{ text: '}', cls: '' },
{ text: '', cls: '' },
{ text: 'int main() {', cls: '' },
{ text: ' vuln();', cls: '' },
{ text: ' return 0;', cls: '' },
{ text: '}', cls: '' },
],
mode: 'input-hex',
bufSize: 16,
showSymbols: true,
showBuilder: true,
aslr: false,
check(sim) {
return sim.getRetAddr() === state.symbols.win;
},
winTitle: 'FLAG{you_own_the_eip}',
winMsg: 'You made the program run a function it was never supposed to call! In a real attack, this is how hackers take control of a computer — they overwrite the go-back address to run whatever they want.',
},
{
unit: 1,
title: '04: Randomized Addresses',
desc: 'Now the computer shuffles where everything is in memory each time the program runs (<strong>ASLR</strong>). Your old address for win() won\'t work anymore! But the program accidentally prints where <strong>main()</strong> is. Since win() is always <strong>+0x150</strong> bytes after main(), you can do the math. Use the hex calculator to add the leaked address + offset.',
code: [
{ text: '#include <stdio.h>', cls: '' },
{ text: '', cls: '' },
{ text: 'void win() {', cls: '' },
{ text: ' printf("FLAG{aslr_defeated}\\n");', cls: '' },
{ text: '}', cls: '' },
{ text: '', cls: '' },
{ text: 'void vuln() {', cls: '', fn: true },
{ text: ' char buf[16];', cls: '' },
{ text: ' // info leak — but not the function we want', cls: 'cmt' },
{ text: ' printf("DEBUG: main=%p\\n", main);', cls: 'highlight' },
{ text: ' gets(buf);', cls: 'highlight vuln' },
{ text: '}', cls: '' },
{ text: '', cls: '' },
{ text: 'int main() {', cls: '' },
{ text: ' vuln();', cls: '' },
{ text: ' return 0;', cls: '' },
{ text: '}', cls: '' },
],
mode: 'input-hex',
bufSize: 16,
showSymbols: false,
showBuilder: true,
showCalc: true,
aslr: true,
check(sim) {
return sim.getRetAddr() === state.symbols.win;
},
winTitle: 'FLAG{aslr_defeated}',
winMsg: 'Even though the addresses were randomized, you figured out win()\'s address by reading the leaked main() address and doing simple math. This is exactly how real hackers beat ASLR — find one leaked address, calculate the rest.',
},
{
unit: 1,
title: '05: The Tripwire',
desc: 'The program now places a secret value (a <strong>stack canary</strong>) between your buffer and the important stuff above it — like a tripwire. If you overwrite it, the program catches you and aborts. But there\'s a bug that leaks the secret value. Put the correct canary bytes in your payload at the right spot to sneak past the tripwire.',
code: [
{ text: '#include <stdio.h>', cls: '' },
{ text: '', cls: '' },
{ text: 'void win() {', cls: '' },
{ text: ' printf("FLAG{canary_bypassed}\\n");', cls: '' },
{ text: '}', cls: '' },
{ text: '', cls: '' },
{ text: 'void vuln() {', cls: '', fn: true },
{ text: ' char buf[16];', cls: '' },
{ text: ' // format string leak — prints stack contents', cls: 'cmt' },
{ text: ' printf(buf);', cls: 'highlight' },
{ text: ' gets(buf);', cls: 'highlight vuln' },
{ text: '}', cls: '' },
{ text: '', cls: '' },
{ text: 'int main() {', cls: '' },
{ text: ' vuln();', cls: '' },
{ text: ' return 0;', cls: '' },
{ text: '}', cls: '' },
],
mode: 'input-hex',
bufSize: 16,
showSymbols: true,
showBuilder: true,
aslr: false,
canary: true,
check(sim) {
return sim.canaryIntact() && sim.getRetAddr() === state.symbols.win;
},
winTitle: 'FLAG{canary_bypassed}',
winMsg: 'You read the leaked secret value and carefully placed it in your payload so the tripwire stayed intact — while still overwriting the go-back address. Sneaky! This is how real attackers bypass stack canaries.',
},
{
title: '06: Integer Overflow',
unit: 2,
desc: 'The program adds a header size (64) to your input length. But what happens when your number is so big that the addition <strong>wraps around</strong> past the maximum value? The buffer ends up tiny, but the program still reads your full input — overflow!',
code: [
{ text: '#include <stdio.h>', cls: '' },
{ text: '', cls: '' },
{ text: 'void win() { printf("FLAG\\n"); }', cls: '' },
{ text: '', cls: '' },
{ text: 'void process(unsigned int user_len) {', cls: '', fn: true },
{ text: ' unsigned int total = user_len + 64;', cls: 'highlight vuln' },
{ text: ' char buf[total];', cls: '' },
{ text: ' read_input(buf, user_len);', cls: 'highlight' },
{ text: '}', cls: '' },
{ text: '', cls: '' },
{ text: 'int main() {', cls: '' },
{ text: ' unsigned int len;', cls: '' },
{ text: ' scanf("%u", &len);', cls: '' },
{ text: ' process(len);', cls: '' },
{ text: ' return 0;', cls: '' },
{ text: '}', cls: '' },
],
mode: 'input-int-overflow',
vizMode: 'stack',
bufSize: 16,
showSymbols: true,
showBuilder: true,
aslr: false,
intOverflow: true,
headerSize: 64,
check(sim) {
return sim.getRetAddr() === state.symbols.win;
},
winTitle: 'FLAG{integer_wrap}',
winMsg: 'The math wrapped around! A huge number + 64 became a tiny number, so the buffer was too small. Your input overflowed it and hijacked the go-back address. This is how integer overflows lead to memory corruption.',
},
{
title: '07: Format String: Reading',
unit: 2,
desc: 'The program passes your input directly to <strong>printf()</strong> as the format string. Normally printf expects a fixed string like "hello %s". But if YOU control the format string, you can use <strong>%x</strong> to read values from the stack — like a secret password stored in memory.',
code: [
{ text: '#include <stdio.h>', cls: '' },
{ text: '', cls: '' },
{ text: 'void vuln() {', cls: '', fn: true },
{ text: ' int secret = 0xCAFEBABE;', cls: '' },
{ text: ' char buf[64];', cls: '' },
{ text: ' fgets(buf, 64, stdin);', cls: '' },
{ text: ' printf(buf);', cls: 'highlight vuln' },
{ text: ' // BUG: user input IS the format string!', cls: 'cmt' },
{ text: '}', cls: '' },
{ text: '', cls: '' },
{ text: 'int main() {', cls: '' },
{ text: ' vuln();', cls: '' },
{ text: ' return 0;', cls: '' },
{ text: '}', cls: '' },
],
mode: 'input-fmt-read',
vizMode: 'stack',
bufSize: 64,
showSymbols: false,
showBuilder: false,
aslr: false,
fmtRead: true,
secretValue: 0xCAFEBABE,
secretOffset: 3,
check(sim) {
return state.fmtLeakedSecret === true;
},
winTitle: 'Secret Leaked!',
winMsg: 'You used %x to walk up the stack and read the secret value. In real exploits, this technique leaks canaries, return addresses, and heap pointers — anything stored on the stack.',
},
// ── Exercise 08: Format String Writing (%n) ──
{
title: '08: Format String: Writing',
unit: 2,
desc: 'The <strong>%n</strong> format specifier doesn\'t print — it <strong>writes</strong>. It stores the number of characters printed so far into a memory address on the stack. Put the right address in your input, advance with %x, then %n writes to it. Change <strong>authorized</strong> from 0 to anything else.',
code: [
{ text: '#include <stdio.h>', cls: '' },
{ text: '', cls: '' },
{ text: 'void win() { printf("FLAG\\n"); }', cls: '' },
{ text: '', cls: '' },
{ text: 'int authorized = 0; // at 0x0804a020', cls: 'highlight' },