-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwnadventure_sourcery2.html
896 lines (785 loc) · 100 KB
/
pwnadventure_sourcery2.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="author" content="F3real" />
<meta name="keywords" content="ctf,reversing,assembly" />
<meta name="description" content="How to solve Pwn Adventure Sourcery swamp challenges" />
<title>Pwn Adventure Sourcery part 2 - EnSec blog</title>
<link href="https://f3real.github.io/theme/css/combined.css" rel="stylesheet" />
<!-- Feeds -->
</head>
<body data-spy="scroll" data-target="#scrollspy">
<div id="wrapper">
<!-- Sidebar -->
<nav id="sidebar-wrapper-small" class="twitchy-background">
<ul id="accordion-small" class="sidebar-nav sidebar-nav-small">
<li>
<a href="https://f3real.github.io" title="EnSec blog" class="collapsed">
<span class="fas fa-home"></span>
</a>
</li>
<li class="nav-divider"></li>
<li>
<a href="https://f3real.github.io/archives.html" title="Recent Articles" class="collapsed">
<span class="fas fa-th-list"></span>
</a>
</li>
<li class="nav-divider"></li>
<li>
<a data-toggle="collapse" data-parent="#accordion-small" href="#collapse-social-small" title="Social" class="collapsed">
<i class="fas fa-users padding-small"></i>
</a>
</li>
<li class="panel anti-panel"><ul id="collapse-social-small" class="collapse ">
<li>
<a href="https://github.com/F3real" title="Github"><i class="fab fa-github-square padding-small"></i></a>
</li>
<li>
<a href="https://www.linkedin.com/in/stefan-ili%C4%87-61a004111" title="Linkedin"><i class="fab fa-linkedin padding-small"></i></a>
</li>
</ul></li>
<li class="nav-divider"></li>
<li>
<a href="#" title="Back to top" class="collapsed">
<span class="fas fa-arrow-up"></span>
</a>
</li>
</ul>
</nav>
<nav id="sidebar-wrapper" class="twitchy-background">
<ul id="accordion" class="sidebar-nav">
<li class="sidebar-brand">
<a href="https://f3real.github.io/">
<span class="fas fa-home padding-small"></span>
EnSec blog
</a>
</li>
<li>
<a href="https://f3real.github.io/archives.html">
<span class="fas fa-th-list padding-small"></span>
Archives
</a>
</li>
<li class="nav-divider"></li>
<li>
<a data-toggle="collapse" data-parent="#accordion" href="#collapse-social">
<i class="fas fa-users padding-small"></i>
Contact
</a>
</li>
<li class="panel anti-panel"><ul id="collapse-social" class="sidebar_submenu collapse ">
<li>
<a href="https://github.com/F3real" title="Github">
<i class="fab fa-github-square padding-small"></i>
Github
</a>
</li>
<li>
<a href="https://www.linkedin.com/in/stefan-ili%C4%87-61a004111" title="Linkedin">
<i class="fab fa-linkedin padding-small"></i>
Linkedin
</a>
</li>
</ul></li>
<li class="nav-divider"></li>
<li class="panel anti-panel"><ul id="collapse-pages" class="sidebar_submenu collapse ">
</ul></li>
<li class="nav-divider"></li>
<li>
<a data-toggle="collapse" data-parent="#accordion" href="#collapse-categories">
<i class="fas fa-folder-open padding-small"></i>
Categories
</a>
</li>
<li class="panel anti-panel"><ul id="collapse-categories" class="sidebar_submenu collapse ">
<li class="active">
<a href="https://f3real.github.io/category/ctf.html">
<i class="fas fa-folder-open padding-small"></i>
ctf
<span class="badge badge-secondary float-right categorybadge">28</span>
</a>
</li>
<li >
<a href="https://f3real.github.io/category/misc.html">
<i class="fas fa-folder-open padding-small"></i>
misc
<span class="badge badge-secondary float-right categorybadge">15</span>
</a>
</li>
<li >
<a href="https://f3real.github.io/category/reversing.html">
<i class="fas fa-folder-open padding-small"></i>
reversing
<span class="badge badge-secondary float-right categorybadge">6</span>
</a>
</li>
<li >
<a href="https://f3real.github.io/category/tutorial.html">
<i class="fas fa-folder-open padding-small"></i>
tutorial
<span class="badge badge-secondary float-right categorybadge">5</span>
</a>
</li>
</ul></li>
</ul>
</nav>
<!-- /#sidebar-wrapper -->
<!-- open/close sidebar -->
<button onclick="toggleMenu();return false;" class="btn btn-primary" id="menu-toggle">
<span id="right-arrow" class="fas fa-chevron-right" title="expand sidebar"></span>
<span id="left-arrow" class="fas fa-chevron-left" title="minimize sidebar"></span>
</button>
<!-- /open/close sidebar -->
<!-- Page Content -->
<div id="page-content-wrapper">
<div class="container-fluid">
<section id="content">
<article>
<div class="row">
<div class="col-lg-10">
<header class="page-header">
<h1>
<a href="https://f3real.github.io/pwnadventure_sourcery2.html"
rel="bookmark"
title="Permalink to Pwn Adventure Sourcery part 2">
Pwn Adventure Sourcery part 2
</a>
<small>
<div class="post-info">
<div class="publish-info-block">
<small>
<span class="published">
<i class="fa fa-calendar padding-small"></i><time datetime="2019-01-04T10:01:00+01:00"> Fri 04 January 2019</time>
</span>
<span class="category">
<i class="fa fa-folder-open padding-small"></i><a href="https://f3real.github.io/category/ctf.html">ctf</a>
</span>
<span class="tags">
<i class="fa fa-tags padding-small"></i>
<a href="https://f3real.github.io/tag/ctf.html">ctf</a> / <a href="https://f3real.github.io/tag/reversing.html">reversing</a> / <a href="https://f3real.github.io/tag/assembly.html">assembly</a> </span>
</small>
</div>
</div><!-- /.post-info --> </small>
</h1>
</header>
</div>
</div>
<div class="row">
<div class="col-lg-10">
<div class="entry-content">
<p>In this writeup, we will take a look at the second lab door and swap maze entrance challenges.</p>
<div class="toc">
<ul>
<li><a href="#lab-door-2">Lab door 2</a></li>
<li><a href="#lab-door-3">Lab door 3</a></li>
<li><a href="#swamp-maze-entrace">Swamp maze entrace</a></li>
</ul>
</div>
<h2 id="lab-door-2">Lab door 2</h2>
<p>Lets get the source code:</p>
<div class="highlight"><pre><span></span><code><span class="nf">TERMINAL_INPUT</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="mi">0</span>
<span class="nf">TERMINAL_OUTPUT</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="mi">1</span>
<span class="nf">DOOR_CONTROL</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="mi">2</span>
<span class="nl">main:</span>
<span class="na">.correct_pin</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="mi">0</span>
<span class="nl">.main_loop:</span>
<span class="w"> </span><span class="nf">call</span><span class="w"> </span><span class="no">ask_and_verify_code</span>
<span class="w"> </span><span class="nf">test</span><span class="w"> </span><span class="no">al</span><span class="p">,</span><span class="w"> </span><span class="no">al</span>
<span class="w"> </span><span class="nf">jz</span><span class="w"> </span><span class="no">.invalid</span>
<span class="w"> </span><span class="nf">call</span><span class="w"> </span><span class="no">open_door</span>
<span class="w"> </span><span class="nf">jmp</span><span class="w"> </span><span class="no">.main_loop</span>
<span class="nl">.invalid:</span>
<span class="w"> </span><span class="c1">; Show denied message</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">esi</span><span class="p">,</span><span class="w"> </span><span class="no">invalid</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">ecx</span><span class="p">,</span><span class="w"> </span><span class="no">end_invalid</span><span class="w"> </span><span class="p">-</span><span class="w"> </span><span class="no">invalid</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">dx</span><span class="p">,</span><span class="w"> </span><span class="no">TERMINAL_OUTPUT</span>
<span class="w"> </span><span class="na">rep</span><span class="w"> </span><span class="nf">outsb</span>
<span class="w"> </span><span class="nf">call</span><span class="w"> </span><span class="no">sleep</span>
<span class="w"> </span><span class="nf">jmp</span><span class="w"> </span><span class="no">.main_loop</span>
<span class="nl">ask_and_verify_code:</span>
<span class="c1">; Variables</span>
<span class="na">.input</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="mi">-32</span>
<span class="na">.len</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="mi">-36</span>
<span class="w"> </span><span class="nf">push</span><span class="w"> </span><span class="no">esi</span>
<span class="w"> </span><span class="nf">push</span><span class="w"> </span><span class="no">edi</span>
<span class="w"> </span><span class="nf">push</span><span class="w"> </span><span class="no">ebp</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">ebp</span><span class="p">,</span><span class="w"> </span><span class="no">esp</span>
<span class="w"> </span><span class="nf">sub</span><span class="w"> </span><span class="no">esp</span><span class="p">,</span><span class="w"> </span><span class="mi">36</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">dword</span><span class="w"> </span><span class="p">[</span><span class="no">ebp</span><span class="w"> </span><span class="err">+</span><span class="w"> </span><span class="no">.len</span><span class="p">],</span><span class="w"> </span><span class="mi">0</span>
<span class="w"> </span><span class="c1">; Display initial message</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">esi</span><span class="p">,</span><span class="w"> </span><span class="no">message</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">ecx</span><span class="p">,</span><span class="w"> </span><span class="no">end_message</span><span class="w"> </span><span class="p">-</span><span class="w"> </span><span class="no">message</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">dx</span><span class="p">,</span><span class="w"> </span><span class="no">TERMINAL_OUTPUT</span>
<span class="w"> </span><span class="na">rep</span><span class="w"> </span><span class="nf">outsb</span>
<span class="nl">.input_loop:</span>
<span class="w"> </span><span class="c1">; Grab next input</span>
<span class="w"> </span><span class="nf">in</span><span class="w"> </span><span class="no">al</span><span class="p">,</span><span class="w"> </span><span class="no">TERMINAL_INPUT</span>
<span class="w"> </span><span class="c1">; If enter is pressed, done</span>
<span class="w"> </span><span class="nf">cmp</span><span class="w"> </span><span class="no">al</span><span class="p">,</span><span class="w"> </span><span class="err">'\</span><span class="no">n</span><span class="err">'</span>
<span class="w"> </span><span class="nf">je</span><span class="w"> </span><span class="no">.end_input</span>
<span class="w"> </span><span class="c1">; Look for backspace</span>
<span class="w"> </span><span class="nf">cmp</span><span class="w"> </span><span class="no">al</span><span class="p">,</span><span class="w"> </span><span class="mi">8</span>
<span class="w"> </span><span class="nf">je</span><span class="w"> </span><span class="no">.backspace</span>
<span class="w"> </span><span class="c1">; Printable only</span>
<span class="w"> </span><span class="nf">cmp</span><span class="w"> </span><span class="no">al</span><span class="p">,</span><span class="w"> </span><span class="err">'</span><span class="w"> </span><span class="err">'</span>
<span class="w"> </span><span class="nf">jb</span><span class="w"> </span><span class="no">.input_loop</span>
<span class="w"> </span><span class="nf">cmp</span><span class="w"> </span><span class="no">al</span><span class="p">,</span><span class="w"> </span><span class="err">'~'</span>
<span class="w"> </span><span class="nf">ja</span><span class="w"> </span><span class="no">.input_loop</span>
<span class="w"> </span><span class="c1">; Add character to input</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">ecx</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="no">ebp</span><span class="w"> </span><span class="err">+</span><span class="w"> </span><span class="no">.len</span><span class="p">]</span>
<span class="w"> </span><span class="nf">cmp</span><span class="w"> </span><span class="no">ecx</span><span class="p">,</span><span class="w"> </span><span class="mi">31</span>
<span class="w"> </span><span class="nf">jae</span><span class="w"> </span><span class="no">.input_loop</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="p">[</span><span class="no">ebp</span><span class="w"> </span><span class="err">+</span><span class="w"> </span><span class="no">.input</span><span class="w"> </span><span class="err">+</span><span class="w"> </span><span class="no">ecx</span><span class="p">],</span><span class="w"> </span><span class="no">al</span>
<span class="w"> </span><span class="nf">inc</span><span class="w"> </span><span class="no">dword</span><span class="w"> </span><span class="p">[</span><span class="no">ebp</span><span class="w"> </span><span class="err">+</span><span class="w"> </span><span class="no">.len</span><span class="p">]</span>
<span class="w"> </span><span class="nf">out</span><span class="w"> </span><span class="no">TERMINAL_OUTPUT</span><span class="p">,</span><span class="w"> </span><span class="no">al</span>
<span class="w"> </span><span class="nf">jmp</span><span class="w"> </span><span class="no">.input_loop</span>
<span class="nl">.backspace:</span>
<span class="w"> </span><span class="nf">cmp</span><span class="w"> </span><span class="no">dword</span><span class="w"> </span><span class="p">[</span><span class="no">ebp</span><span class="w"> </span><span class="err">+</span><span class="w"> </span><span class="no">.len</span><span class="p">],</span><span class="w"> </span><span class="mi">0</span>
<span class="w"> </span><span class="nf">je</span><span class="w"> </span><span class="no">.input_loop</span>
<span class="w"> </span><span class="c1">; Erase last character</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">al</span><span class="p">,</span><span class="w"> </span><span class="mi">8</span>
<span class="w"> </span><span class="nf">out</span><span class="w"> </span><span class="no">TERMINAL_OUTPUT</span><span class="p">,</span><span class="w"> </span><span class="no">al</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">al</span><span class="p">,</span><span class="w"> </span><span class="err">'</span><span class="w"> </span><span class="err">'</span>
<span class="w"> </span><span class="nf">out</span><span class="w"> </span><span class="no">TERMINAL_OUTPUT</span><span class="p">,</span><span class="w"> </span><span class="no">al</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">al</span><span class="p">,</span><span class="w"> </span><span class="mi">8</span>
<span class="w"> </span><span class="nf">out</span><span class="w"> </span><span class="no">TERMINAL_OUTPUT</span><span class="p">,</span><span class="w"> </span><span class="no">al</span>
<span class="w"> </span><span class="nf">dec</span><span class="w"> </span><span class="no">dword</span><span class="w"> </span><span class="p">[</span><span class="no">ebp</span><span class="w"> </span><span class="err">+</span><span class="w"> </span><span class="no">.len</span><span class="p">]</span>
<span class="w"> </span><span class="nf">jmp</span><span class="w"> </span><span class="no">.input_loop</span>
<span class="nl">.end_input:</span>
<span class="w"> </span><span class="nf">out</span><span class="w"> </span><span class="no">TERMINAL_OUTPUT</span><span class="p">,</span><span class="w"> </span><span class="no">al</span>
<span class="w"> </span><span class="c1">; Null terminate input</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">ecx</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="no">ebp</span><span class="w"> </span><span class="err">+</span><span class="w"> </span><span class="no">.len</span><span class="p">]</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">byte</span><span class="w"> </span><span class="p">[</span><span class="no">ebp</span><span class="w"> </span><span class="err">+</span><span class="w"> </span><span class="no">.input</span><span class="w"> </span><span class="err">+</span><span class="w"> </span><span class="no">ecx</span><span class="p">],</span><span class="w"> </span><span class="mi">0</span>
<span class="w"> </span><span class="c1">; Check code</span>
<span class="w"> </span><span class="nf">lea</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="no">ebp</span><span class="w"> </span><span class="err">+</span><span class="w"> </span><span class="no">.input</span><span class="p">]</span>
<span class="w"> </span><span class="nf">push</span><span class="w"> </span><span class="no">eax</span>
<span class="w"> </span><span class="nf">call</span><span class="w"> </span><span class="no">verify_code</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">esp</span><span class="p">,</span><span class="w"> </span><span class="no">ebp</span>
<span class="w"> </span><span class="nf">pop</span><span class="w"> </span><span class="no">ebp</span>
<span class="w"> </span><span class="nf">pop</span><span class="w"> </span><span class="no">edi</span>
<span class="w"> </span><span class="nf">pop</span><span class="w"> </span><span class="no">esi</span>
<span class="w"> </span><span class="nf">ret</span>
<span class="nl">verify_code:</span>
<span class="w"> </span><span class="nf">push</span><span class="w"> </span><span class="no">esi</span>
<span class="w"> </span><span class="nf">push</span><span class="w"> </span><span class="no">ebp</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">ebp</span><span class="p">,</span><span class="w"> </span><span class="no">esp</span>
<span class="w"> </span><span class="nf">sub</span><span class="w"> </span><span class="no">esp</span><span class="p">,</span><span class="w"> </span><span class="mi">8</span>
<span class="w"> </span><span class="nf">push</span><span class="w"> </span><span class="no">dword</span><span class="w"> </span><span class="p">[</span><span class="no">ebp</span><span class="w"> </span><span class="err">+</span><span class="w"> </span><span class="mi">12</span><span class="p">]</span>
<span class="w"> </span><span class="nf">call</span><span class="w"> </span><span class="no">strlen</span>
<span class="w"> </span><span class="nf">cmp</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="w"> </span><span class="mi">16</span><span class="w"> </span><span class="c1">;length has to be 16</span>
<span class="w"> </span><span class="nf">jne</span><span class="w"> </span><span class="no">.bad</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">esi</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="no">ebp</span><span class="w"> </span><span class="err">+</span><span class="w"> </span><span class="mi">12</span><span class="p">]</span><span class="w"> </span><span class="c1">;load input to esi</span>
<span class="w"> </span><span class="c1">;initialization</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">edx</span><span class="p">,</span><span class="w"> </span><span class="mi">0xfa</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">al</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="no">esi</span><span class="p">]</span><span class="w"> </span><span class="c1">;load first char from input</span>
<span class="w"> </span><span class="nf">rol</span><span class="w"> </span><span class="no">edx</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="c1">;like shift but shifted bits </span>
<span class="w"> </span><span class="nf">xor</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="no">al</span><span class="w"> </span><span class="c1">;are rotated to the other end</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="mi">0xab</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">al</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="no">esi</span><span class="err">+</span><span class="mi">1</span><span class="p">]</span>
<span class="w"> </span><span class="nf">rol</span><span class="w"> </span><span class="no">edx</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span>
<span class="w"> </span><span class="nf">xor</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="no">al</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="mi">0x45</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">al</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="no">esi</span><span class="err">+</span><span class="mi">2</span><span class="p">]</span>
<span class="w"> </span><span class="nf">rol</span><span class="w"> </span><span class="no">edx</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span>
<span class="w"> </span><span class="nf">xor</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="no">al</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="mi">0x12</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">al</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="no">esi</span><span class="err">+</span><span class="mi">3</span><span class="p">]</span>
<span class="w"> </span><span class="nf">rol</span><span class="w"> </span><span class="no">edx</span><span class="p">,</span><span class="w"> </span><span class="mi">9</span>
<span class="w"> </span><span class="nf">xor</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="no">al</span>
<span class="w"> </span><span class="c1">;calculate remaining characters based on first four</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="mi">0xcd</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="no">dl</span>
<span class="w"> </span><span class="nf">and</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="mi">15</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="err">'</span><span class="no">a</span><span class="err">'</span>
<span class="w"> </span><span class="nf">cmp</span><span class="w"> </span><span class="p">[</span><span class="no">esi</span><span class="err">+</span><span class="mi">4</span><span class="p">],</span><span class="w"> </span><span class="no">cl</span>
<span class="w"> </span><span class="nf">jne</span><span class="w"> </span><span class="no">.bad</span>
<span class="w"> </span><span class="nf">rol</span><span class="w"> </span><span class="no">edx</span><span class="p">,</span><span class="w"> </span><span class="mi">12</span>
<span class="w"> </span><span class="nf">xor</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="no">cl</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="mi">0x87</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="no">dl</span>
<span class="w"> </span><span class="nf">and</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="mi">15</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="err">'</span><span class="no">a</span><span class="err">'</span>
<span class="w"> </span><span class="nf">cmp</span><span class="w"> </span><span class="p">[</span><span class="no">esi</span><span class="err">+</span><span class="mi">5</span><span class="p">],</span><span class="w"> </span><span class="no">cl</span>
<span class="w"> </span><span class="nf">jne</span><span class="w"> </span><span class="no">.bad</span>
<span class="w"> </span><span class="nf">rol</span><span class="w"> </span><span class="no">edx</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span>
<span class="w"> </span><span class="nf">xor</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="no">cl</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="mi">0xef</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="no">dl</span>
<span class="w"> </span><span class="nf">and</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="mi">15</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="err">'</span><span class="no">C</span><span class="err">'</span>
<span class="w"> </span><span class="nf">cmp</span><span class="w"> </span><span class="p">[</span><span class="no">esi</span><span class="err">+</span><span class="mi">6</span><span class="p">],</span><span class="w"> </span><span class="no">cl</span>
<span class="w"> </span><span class="nf">jne</span><span class="w"> </span><span class="no">.bad</span>
<span class="w"> </span><span class="nf">rol</span><span class="w"> </span><span class="no">edx</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span>
<span class="w"> </span><span class="nf">xor</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="no">cl</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="mi">0x10</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="no">dl</span>
<span class="w"> </span><span class="nf">and</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="mi">15</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="err">'</span><span class="no">f</span><span class="err">'</span>
<span class="w"> </span><span class="nf">cmp</span><span class="w"> </span><span class="p">[</span><span class="no">esi</span><span class="err">+</span><span class="mi">7</span><span class="p">],</span><span class="w"> </span><span class="no">cl</span>
<span class="w"> </span><span class="nf">jne</span><span class="w"> </span><span class="no">.bad</span>
<span class="w"> </span><span class="nf">rol</span><span class="w"> </span><span class="no">edx</span><span class="p">,</span><span class="w"> </span><span class="mi">13</span>
<span class="w"> </span><span class="nf">xor</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="no">cl</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="mi">0x9a</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="no">dl</span>
<span class="w"> </span><span class="nf">and</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="mi">15</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="err">'</span><span class="no">e</span><span class="err">'</span>
<span class="w"> </span><span class="nf">cmp</span><span class="w"> </span><span class="p">[</span><span class="no">esi</span><span class="err">+</span><span class="mi">8</span><span class="p">],</span><span class="w"> </span><span class="no">cl</span>
<span class="w"> </span><span class="nf">jne</span><span class="w"> </span><span class="no">.bad</span>
<span class="w"> </span><span class="nf">rol</span><span class="w"> </span><span class="no">edx</span><span class="p">,</span><span class="w"> </span><span class="mi">9</span>
<span class="w"> </span><span class="nf">xor</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="no">cl</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="mi">0xa8</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="no">dl</span>
<span class="w"> </span><span class="nf">and</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="mi">15</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="err">'</span><span class="no">D</span><span class="err">'</span>
<span class="w"> </span><span class="nf">cmp</span><span class="w"> </span><span class="p">[</span><span class="no">esi</span><span class="err">+</span><span class="mi">9</span><span class="p">],</span><span class="w"> </span><span class="no">cl</span>
<span class="w"> </span><span class="nf">jne</span><span class="w"> </span><span class="no">.bad</span>
<span class="w"> </span><span class="nf">rol</span><span class="w"> </span><span class="no">edx</span><span class="p">,</span><span class="w"> </span><span class="mi">7</span>
<span class="w"> </span><span class="nf">xor</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="no">cl</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="mi">0xca</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="no">dl</span>
<span class="w"> </span><span class="nf">and</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="mi">15</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="err">'</span><span class="no">D</span><span class="err">'</span>
<span class="w"> </span><span class="nf">cmp</span><span class="w"> </span><span class="p">[</span><span class="no">esi</span><span class="err">+</span><span class="mi">10</span><span class="p">],</span><span class="w"> </span><span class="no">cl</span>
<span class="w"> </span><span class="nf">jne</span><span class="w"> </span><span class="no">.bad</span>
<span class="w"> </span><span class="nf">rol</span><span class="w"> </span><span class="no">edx</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span>
<span class="w"> </span><span class="nf">xor</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="no">cl</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="mi">0x91</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="no">dl</span>
<span class="w"> </span><span class="nf">and</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="mi">15</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="err">'</span><span class="no">c</span><span class="err">'</span>
<span class="w"> </span><span class="nf">cmp</span><span class="w"> </span><span class="p">[</span><span class="no">esi</span><span class="err">+</span><span class="mi">11</span><span class="p">],</span><span class="w"> </span><span class="no">cl</span>
<span class="w"> </span><span class="nf">jne</span><span class="w"> </span><span class="no">.bad</span>
<span class="w"> </span><span class="nf">rol</span><span class="w"> </span><span class="no">edx</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span>
<span class="w"> </span><span class="nf">xor</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="no">cl</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="mi">0x86</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="no">dl</span>
<span class="w"> </span><span class="nf">and</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="mi">15</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="err">'</span><span class="no">A</span><span class="err">'</span>
<span class="w"> </span><span class="nf">cmp</span><span class="w"> </span><span class="p">[</span><span class="no">esi</span><span class="err">+</span><span class="mi">12</span><span class="p">],</span><span class="w"> </span><span class="no">cl</span>
<span class="w"> </span><span class="nf">jne</span><span class="w"> </span><span class="no">.bad</span>
<span class="w"> </span><span class="nf">rol</span><span class="w"> </span><span class="no">edx</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span>
<span class="w"> </span><span class="nf">xor</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="no">cl</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="mi">0xf1</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="no">dl</span>
<span class="w"> </span><span class="nf">and</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="mi">15</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="err">'</span><span class="no">e</span><span class="err">'</span>
<span class="w"> </span><span class="nf">cmp</span><span class="w"> </span><span class="p">[</span><span class="no">esi</span><span class="err">+</span><span class="mi">13</span><span class="p">],</span><span class="w"> </span><span class="no">cl</span>
<span class="w"> </span><span class="nf">jne</span><span class="w"> </span><span class="no">.bad</span>
<span class="w"> </span><span class="nf">rol</span><span class="w"> </span><span class="no">edx</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span>
<span class="w"> </span><span class="nf">xor</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="no">cl</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="mi">0x1f</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="no">dl</span>
<span class="w"> </span><span class="nf">and</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="mi">15</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="err">'</span><span class="no">B</span><span class="err">'</span>
<span class="w"> </span><span class="nf">cmp</span><span class="w"> </span><span class="p">[</span><span class="no">esi</span><span class="err">+</span><span class="mi">14</span><span class="p">],</span><span class="w"> </span><span class="no">cl</span>
<span class="w"> </span><span class="nf">jne</span><span class="w"> </span><span class="no">.bad</span>
<span class="w"> </span><span class="nf">rol</span><span class="w"> </span><span class="no">edx</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span>
<span class="w"> </span><span class="nf">xor</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="no">cl</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="mi">0x90</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="no">dl</span>
<span class="w"> </span><span class="nf">and</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="mi">15</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">cl</span><span class="p">,</span><span class="w"> </span><span class="err">'</span><span class="no">f</span><span class="err">'</span>
<span class="w"> </span><span class="nf">cmp</span><span class="w"> </span><span class="p">[</span><span class="no">esi</span><span class="err">+</span><span class="mi">15</span><span class="p">],</span><span class="w"> </span><span class="no">cl</span>
<span class="w"> </span><span class="nf">jne</span><span class="w"> </span><span class="no">.bad</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">al</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">esp</span><span class="p">,</span><span class="w"> </span><span class="no">ebp</span>
<span class="w"> </span><span class="nf">pop</span><span class="w"> </span><span class="no">ebp</span>
<span class="w"> </span><span class="nf">pop</span><span class="w"> </span><span class="no">esi</span>
<span class="w"> </span><span class="nf">ret</span><span class="w"> </span><span class="mi">4</span>
<span class="nl">.bad:</span>
<span class="w"> </span><span class="nf">xor</span><span class="w"> </span><span class="no">al</span><span class="p">,</span><span class="w"> </span><span class="no">al</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">esp</span><span class="p">,</span><span class="w"> </span><span class="no">ebp</span>
<span class="w"> </span><span class="nf">pop</span><span class="w"> </span><span class="no">ebp</span>
<span class="w"> </span><span class="nf">pop</span><span class="w"> </span><span class="no">esi</span>
<span class="w"> </span><span class="nf">ret</span><span class="w"> </span><span class="mi">4</span>
<span class="nl">strlen:</span>
<span class="w"> </span><span class="nf">xor</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="w"> </span><span class="no">eax</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">ecx</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="no">esp</span><span class="w"> </span><span class="err">+</span><span class="w"> </span><span class="mi">4</span><span class="p">]</span>
<span class="nl">.loop:</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="no">ecx</span><span class="p">]</span>
<span class="w"> </span><span class="nf">test</span><span class="w"> </span><span class="no">dl</span><span class="p">,</span><span class="w"> </span><span class="no">dl</span>
<span class="w"> </span><span class="nf">jz</span><span class="w"> </span><span class="no">.end</span>
<span class="w"> </span><span class="nf">inc</span><span class="w"> </span><span class="no">eax</span>
<span class="w"> </span><span class="nf">inc</span><span class="w"> </span><span class="no">ecx</span>
<span class="w"> </span><span class="nf">jmp</span><span class="w"> </span><span class="no">.loop</span>
<span class="nl">.end:</span>
<span class="w"> </span><span class="nf">ret</span><span class="w"> </span><span class="mi">4</span>
<span class="nl">open_door:</span>
<span class="w"> </span><span class="nf">push</span><span class="w"> </span><span class="no">esi</span>
<span class="w"> </span><span class="c1">; Send command to unlock door</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">al</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span>
<span class="w"> </span><span class="nf">out</span><span class="w"> </span><span class="no">DOOR_CONTROL</span><span class="p">,</span><span class="w"> </span><span class="no">al</span>
<span class="w"> </span><span class="c1">; Show open message</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">esi</span><span class="p">,</span><span class="w"> </span><span class="no">open</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">ecx</span><span class="p">,</span><span class="w"> </span><span class="no">end_open</span><span class="w"> </span><span class="p">-</span><span class="w"> </span><span class="no">open</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">dx</span><span class="p">,</span><span class="w"> </span><span class="no">TERMINAL_OUTPUT</span>
<span class="w"> </span><span class="na">rep</span><span class="w"> </span><span class="nf">outsb</span>
<span class="w"> </span><span class="nf">call</span><span class="w"> </span><span class="no">sleep</span>
<span class="w"> </span><span class="nf">pop</span><span class="w"> </span><span class="no">esi</span>
<span class="w"> </span><span class="nf">ret</span>
<span class="nl">sleep:</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">ecx</span><span class="p">,</span><span class="w"> </span><span class="mi">120</span>
<span class="nl">.sleep_loop:</span>
<span class="w"> </span><span class="nf">pause</span>
<span class="w"> </span><span class="nf">loop</span><span class="w"> </span><span class="no">.sleep_loop</span>
<span class="w"> </span><span class="nf">ret</span>
<span class="nl">message:</span>
<span class="w"> </span><span class="nf">db</span><span class="w"> </span><span class="err">"\</span><span class="no">fCLEARENCE</span><span class="w"> </span><span class="no">LEVEL</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="no">REQUIRED</span><span class="err">\</span><span class="no">nAuthorization</span><span class="w"> </span><span class="no">code</span><span class="p">:</span><span class="err">\</span><span class="no">n</span><span class="err">"</span>
<span class="nl">end_message:</span>
<span class="nl">open:</span>
<span class="w"> </span><span class="nf">db</span><span class="w"> </span><span class="err">"</span><span class="no">ACCESS</span><span class="w"> </span><span class="no">GRANTED</span><span class="err">"</span>
<span class="nl">end_open:</span>
<span class="nl">invalid:</span>
<span class="w"> </span><span class="nf">db</span><span class="w"> </span><span class="err">"</span><span class="no">ACCESS</span><span class="w"> </span><span class="no">DENIED</span><span class="err">"</span>
<span class="nl">end_invalid:</span>
</code></pre></div>
<p>If we look at the <code>verify_code</code> function carefully we see, that the key is calculated based on the first 4 characters. There is a lot of code, but basically there are just 2 unrolled for loops. If you don't want to look at assembly, C solver will give you a good overview of the way in which the key is being calculated.</p>
<p>But still, this challenge took some time to solve mostly due to bug caused by not properly emulating 8 bit addition. To track it down I actually had to write inline assembly version of the same code.</p>
<p>C solver:</p>
<div class="highlight"><pre><span></span><code><span class="cp">#include</span><span class="w"> </span><span class="cpf"><stdio.h></span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf"><stdint.h></span>
<span class="kt">uint8_t</span><span class="w"> </span><span class="n">input</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="sc">'a'</span><span class="p">,</span><span class="w"> </span><span class="sc">'a'</span><span class="p">,</span><span class="w"> </span><span class="sc">'a'</span><span class="p">,</span><span class="w"> </span><span class="sc">'a'</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">};</span>
<span class="kt">uint8_t</span><span class="w"> </span><span class="n">hardcoded_add</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="mh">0xfa</span><span class="p">,</span><span class="w"> </span><span class="mh">0xab</span><span class="p">,</span><span class="w"> </span><span class="mh">0x45</span><span class="p">,</span><span class="w"> </span><span class="mh">0x12</span><span class="p">,</span><span class="w"> </span><span class="mh">0xcd</span><span class="p">,</span><span class="w"> </span><span class="mh">0x87</span><span class="p">,</span><span class="w"> </span><span class="mh">0xef</span><span class="p">,</span><span class="w"> </span><span class="mh">0x10</span><span class="p">,</span><span class="w"> </span><span class="mh">0x9a</span><span class="p">,</span><span class="w"> </span>
<span class="mh">0xa8</span><span class="p">,</span><span class="w"> </span><span class="mh">0xca</span><span class="p">,</span><span class="w"> </span><span class="mh">0x91</span><span class="p">,</span><span class="w"> </span><span class="mh">0x86</span><span class="p">,</span><span class="w"> </span><span class="mh">0xf1</span><span class="p">,</span><span class="w"> </span><span class="mh">0x1f</span><span class="p">,</span><span class="w"> </span><span class="mh">0x90</span><span class="p">};</span>
<span class="kt">uint8_t</span><span class="w"> </span><span class="n">hardcoded_roll</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">9</span><span class="p">,</span><span class="w"> </span><span class="mi">12</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">13</span><span class="p">,</span><span class="w"> </span><span class="mi">9</span><span class="p">,</span><span class="w"> </span><span class="mi">7</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">};</span>
<span class="kt">uint8_t</span><span class="w"> </span><span class="n">hardcoded_add2</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="sc">'a'</span><span class="p">,</span><span class="w"> </span><span class="sc">'a'</span><span class="p">,</span><span class="w"> </span><span class="sc">'C'</span><span class="p">,</span><span class="w"> </span><span class="sc">'f'</span><span class="p">,</span><span class="w"> </span><span class="sc">'e'</span><span class="p">,</span><span class="w"> </span><span class="sc">'D'</span><span class="p">,</span><span class="w"> </span><span class="sc">'D'</span><span class="p">,</span><span class="w"> </span><span class="sc">'c'</span><span class="p">,</span><span class="w"> </span><span class="sc">'A'</span><span class="p">,</span><span class="w"> </span><span class="sc">'e'</span><span class="p">,</span><span class="w"> </span><span class="sc">'B'</span><span class="p">,</span><span class="w"> </span><span class="sc">'f'</span><span class="p">};</span>
<span class="kt">uint32_t</span><span class="w"> </span><span class="nf">rotl32</span><span class="w"> </span><span class="p">(</span><span class="kt">uint32_t</span><span class="w"> </span><span class="n">n</span><span class="p">,</span><span class="w"> </span><span class="kt">unsigned</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">c</span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">unsigned</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">mask</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="mi">8</span><span class="o">*</span><span class="k">sizeof</span><span class="p">(</span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
<span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">&=</span><span class="w"> </span><span class="n">mask</span><span class="p">;</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="o"><<</span><span class="n">c</span><span class="p">)</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="o">>></span><span class="p">(</span><span class="w"> </span><span class="p">(</span><span class="o">-</span><span class="n">c</span><span class="p">)</span><span class="o">&</span><span class="n">mask</span><span class="w"> </span><span class="p">));</span>
<span class="p">}</span>
<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">()</span>
<span class="p">{</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="kt">uint32_t</span><span class="w"> </span><span class="n">tmp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o"><</span><span class="mi">4</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">){</span>
<span class="w"> </span><span class="n">tmp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tmp</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">hardcoded_add</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
<span class="w"> </span><span class="n">tmp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">rotl32</span><span class="p">(</span><span class="n">tmp</span><span class="p">,</span><span class="w"> </span><span class="n">hardcoded_roll</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span><span class="w"> </span>
<span class="w"> </span><span class="n">tmp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tmp</span><span class="w"> </span><span class="o">^</span><span class="w"> </span><span class="n">input</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">4</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o"><</span><span class="mi">16</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">){</span>
<span class="w"> </span><span class="n">tmp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="mh">0xFFFFFF00</span><span class="w"> </span><span class="o">&</span><span class="w"> </span><span class="n">tmp</span><span class="p">)</span><span class="o">+</span><span class="w"> </span><span class="p">((</span><span class="n">tmp</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">hardcoded_add</span><span class="p">[</span><span class="n">i</span><span class="p">])</span><span class="w"> </span><span class="o">&</span><span class="w"> </span><span class="mh">0xFF</span><span class="p">);</span>
<span class="w"> </span><span class="kt">uint8_t</span><span class="w"> </span><span class="n">test1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tmp</span><span class="w"> </span><span class="o">&</span><span class="w"> </span><span class="mi">15</span><span class="p">;</span>
<span class="w"> </span><span class="n">test1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">test1</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">hardcoded_add2</span><span class="p">[</span><span class="n">i</span><span class="mi">-4</span><span class="p">];</span>
<span class="w"> </span><span class="n">input</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">test1</span><span class="p">;</span>
<span class="w"> </span><span class="n">tmp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">rotl32</span><span class="p">(</span><span class="n">tmp</span><span class="p">,</span><span class="w"> </span><span class="n">hardcoded_roll</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
<span class="w"> </span><span class="n">tmp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tmp</span><span class="w"> </span><span class="o">^</span><span class="w"> </span><span class="n">test1</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o"><</span><span class="mi">16</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">){</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"%c"</span><span class="p">,</span><span class="w"> </span><span class="n">input</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div>
<p>Solution: <code>aaaaohFgqNRnHeBt</code></p>
<p>Inline assembly version:</p>
<div class="highlight"><pre><span></span><code><span class="cp">#include</span><span class="w"> </span><span class="cpf"><stdio.h></span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf"><stdint.h></span>
<span class="kt">uint8_t</span><span class="w"> </span><span class="n">input</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="sc">'a'</span><span class="p">,</span><span class="w"> </span><span class="sc">'a'</span><span class="p">,</span><span class="w"> </span><span class="sc">'a'</span><span class="p">,</span><span class="w"> </span><span class="sc">'a'</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">};</span>
<span class="kt">uint8_t</span><span class="w"> </span><span class="n">hardcoded_add</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="mh">0xfa</span><span class="p">,</span><span class="w"> </span><span class="mh">0xab</span><span class="p">,</span><span class="w"> </span><span class="mh">0x45</span><span class="p">,</span><span class="w"> </span><span class="mh">0x12</span><span class="p">,</span><span class="w"> </span><span class="mh">0xcd</span><span class="p">,</span><span class="w"> </span><span class="mh">0x87</span><span class="p">,</span><span class="w"> </span><span class="mh">0xef</span><span class="p">,</span><span class="w"> </span><span class="mh">0x10</span><span class="p">,</span><span class="w"> </span><span class="mh">0x9a</span><span class="p">,</span><span class="w"> </span>
<span class="mh">0xa8</span><span class="p">,</span><span class="w"> </span><span class="mh">0xca</span><span class="p">,</span><span class="w"> </span><span class="mh">0x91</span><span class="p">,</span><span class="w"> </span><span class="mh">0x86</span><span class="p">,</span><span class="w"> </span><span class="mh">0xf1</span><span class="p">,</span><span class="w"> </span><span class="mh">0x1f</span><span class="p">,</span><span class="w"> </span><span class="mh">0x90</span><span class="p">};</span>
<span class="kt">uint8_t</span><span class="w"> </span><span class="n">hardcoded_roll</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">9</span><span class="p">,</span><span class="w"> </span><span class="mi">12</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">13</span><span class="p">,</span><span class="w"> </span><span class="mi">9</span><span class="p">,</span><span class="w"> </span><span class="mi">7</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">};</span>
<span class="kt">uint8_t</span><span class="w"> </span><span class="n">hardcoded_add2</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="sc">'a'</span><span class="p">,</span><span class="w"> </span><span class="sc">'a'</span><span class="p">,</span><span class="w"> </span><span class="sc">'C'</span><span class="p">,</span><span class="w"> </span><span class="sc">'f'</span><span class="p">,</span><span class="w"> </span><span class="sc">'e'</span><span class="p">,</span><span class="w"> </span><span class="sc">'D'</span><span class="p">,</span><span class="w"> </span><span class="sc">'D'</span><span class="p">,</span><span class="w"> </span><span class="sc">'c'</span><span class="p">,</span><span class="w"> </span><span class="sc">'A'</span><span class="p">,</span><span class="w"> </span><span class="sc">'e'</span><span class="p">,</span><span class="w"> </span><span class="sc">'B'</span><span class="p">,</span><span class="w"> </span><span class="sc">'f'</span><span class="p">};</span>
<span class="kt">uint32_t</span><span class="w"> </span><span class="nf">rotl32</span><span class="w"> </span><span class="p">(</span><span class="kt">uint32_t</span><span class="w"> </span><span class="n">n</span><span class="p">,</span><span class="w"> </span><span class="kt">unsigned</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">c</span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">unsigned</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">mask</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="mi">8</span><span class="o">*</span><span class="k">sizeof</span><span class="p">(</span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
<span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">&=</span><span class="w"> </span><span class="n">mask</span><span class="p">;</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="o"><<</span><span class="n">c</span><span class="p">)</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="o">>></span><span class="p">(</span><span class="w"> </span><span class="p">(</span><span class="o">-</span><span class="n">c</span><span class="p">)</span><span class="o">&</span><span class="n">mask</span><span class="w"> </span><span class="p">));</span>
<span class="p">}</span>
<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">()</span>
<span class="p">{</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="kt">uint32_t</span><span class="w"> </span><span class="n">tmp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o"><</span><span class="mi">4</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">){</span>
<span class="w"> </span><span class="k">asm</span><span class="p">(</span>
<span class="w"> </span><span class="s">"add %1, %%dl;"</span>
<span class="w"> </span><span class="s">"mov %2, %%al;"</span>
<span class="w"> </span><span class="s">"roll %3, %%edx;"</span>
<span class="w"> </span><span class="s">"xor %%al, %%dl;"</span>
<span class="w"> </span><span class="o">:</span><span class="s">"+d"</span><span class="p">(</span><span class="n">tmp</span><span class="p">)</span>
<span class="w"> </span><span class="o">:</span><span class="s">"r"</span><span class="p">(</span><span class="n">hardcoded_add</span><span class="p">[</span><span class="n">i</span><span class="p">]),</span><span class="w"> </span><span class="s">"r"</span><span class="p">(</span><span class="n">input</span><span class="p">[</span><span class="n">i</span><span class="p">]),</span><span class="w"> </span><span class="s">"cI"</span><span class="p">(</span><span class="n">hardcoded_roll</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
<span class="w"> </span><span class="o">:</span><span class="s">"eax"</span>
<span class="w"> </span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">4</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o"><</span><span class="mi">16</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">){</span>
<span class="w"> </span><span class="k">asm</span><span class="p">(</span>
<span class="w"> </span><span class="s">"add %1, %%dl;"</span>
<span class="w"> </span><span class="o">:</span><span class="s">"+d"</span><span class="p">(</span><span class="n">tmp</span><span class="p">)</span>
<span class="w"> </span><span class="o">:</span><span class="s">"r"</span><span class="p">(</span><span class="n">hardcoded_add</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
<span class="w"> </span><span class="p">);</span>
<span class="w"> </span><span class="kt">uint8_t</span><span class="w"> </span><span class="n">test1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="k">asm</span><span class="p">(</span>
<span class="w"> </span><span class="s">"mov %2, %%edx;"</span>
<span class="w"> </span><span class="s">"mov %%dl, %%cl;"</span>
<span class="w"> </span><span class="s">"and $15, %%cl;"</span>
<span class="w"> </span><span class="s">"add %1, %%cl;"</span>
<span class="w"> </span><span class="s">"mov %%cl, %0;"</span>
<span class="w"> </span><span class="o">:</span><span class="s">"=r"</span><span class="p">(</span><span class="n">test1</span><span class="p">)</span>
<span class="w"> </span><span class="o">:</span><span class="s">"r"</span><span class="p">(</span><span class="n">hardcoded_add2</span><span class="p">[</span><span class="n">i</span><span class="mi">-4</span><span class="p">]),</span><span class="w"> </span><span class="s">"r"</span><span class="p">(</span><span class="n">tmp</span><span class="p">)</span>
<span class="w"> </span><span class="o">:</span><span class="s">"eax"</span><span class="p">,</span><span class="w"> </span><span class="s">"ecx"</span><span class="p">,</span><span class="w"> </span><span class="s">"edx"</span>
<span class="w"> </span><span class="p">);</span>
<span class="w"> </span><span class="n">input</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">test1</span><span class="p">;</span><span class="w"> </span>
<span class="w"> </span><span class="n">tmp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">rotl32</span><span class="p">(</span><span class="n">tmp</span><span class="p">,</span><span class="w"> </span><span class="n">hardcoded_roll</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
<span class="w"> </span><span class="k">asm</span><span class="p">(</span>
<span class="w"> </span><span class="s">"xor %1, %%dl;"</span>
<span class="w"> </span><span class="o">:</span><span class="s">"+d"</span><span class="p">(</span><span class="n">tmp</span><span class="p">)</span>
<span class="w"> </span><span class="o">:</span><span class="s">"r"</span><span class="p">(</span><span class="n">test1</span><span class="p">)</span>
<span class="w"> </span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o"><</span><span class="mi">16</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">){</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"%c"</span><span class="p">,</span><span class="w"> </span><span class="n">input</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div>
<p>Btw, there is also cave filled with spiders north from the desert with boss to beat :D</p>
<p><img alt="Spider boss" class="img-fluid centerimage" src="https://f3real.github.io/images/2019_1_4_sourcery.png"></p>
<h2 id="lab-door-3">Lab door 3</h2>
<p>The source code of the third lab door has ~6k lines of code. But fortunately, most of it is in <code>decipher</code> function and represents the unrolled loop.</p>
<p>Full source code can be found <a href="https://github.com/F3real/ctf_solutions/blob/master/2018/pwn_adventure_sourcery/LabDoor3/LabDoor3.asm">here</a></p>
<p>Let's look at important few code snippets:</p>
<div class="highlight"><pre><span></span><code><span class="w"> </span><span class="c1">; read in the input, expect "XXXXXXXX-XXXXXXXX"</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">edi</span><span class="p">,</span><span class="w"> </span><span class="no">inbuf</span>
<span class="w"> </span><span class="nf">call</span><span class="w"> </span><span class="no">input</span>
<span class="w"> </span><span class="c1">; check input format</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">esi</span><span class="p">,</span><span class="w"> </span><span class="no">inbuf</span>
<span class="w"> </span><span class="nf">cmp</span><span class="w"> </span><span class="no">byte</span><span class="w"> </span><span class="p">[</span><span class="no">esi</span><span class="err">+</span><span class="mi">8</span><span class="p">],</span><span class="w"> </span><span class="err">'</span><span class="p">-</span><span class="err">'</span>
<span class="w"> </span><span class="nf">jne</span><span class="w"> </span><span class="no">.fail</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">byte</span><span class="w"> </span><span class="p">[</span><span class="no">esi</span><span class="err">+</span><span class="mi">8</span><span class="p">],</span><span class="w"> </span><span class="mi">0</span>
<span class="w"> </span><span class="c1">; parse first ctext</span>
<span class="w"> </span><span class="nf">push</span><span class="w"> </span><span class="no">v0</span>
<span class="w"> </span><span class="nf">push</span><span class="w"> </span><span class="no">inbuf</span>
<span class="w"> </span><span class="nf">call</span><span class="w"> </span><span class="no">parse_uint32_hex</span>
<span class="w"> </span><span class="nf">cmp</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="w"> </span><span class="mi">-1</span>
<span class="w"> </span><span class="nf">je</span><span class="w"> </span><span class="no">.fail</span>
<span class="w"> </span><span class="c1">; parse second ctext</span>
<span class="w"> </span><span class="nf">push</span><span class="w"> </span><span class="no">v1</span>
<span class="w"> </span><span class="nf">push</span><span class="w"> </span><span class="no">inbuf</span><span class="err">+</span><span class="mi">9</span>
<span class="w"> </span><span class="nf">call</span><span class="w"> </span><span class="no">parse_uint32_hex</span>
<span class="w"> </span><span class="nf">cmp</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="w"> </span><span class="mi">-1</span>
<span class="w"> </span><span class="nf">je</span><span class="w"> </span><span class="no">.fail</span>
<span class="w"> </span><span class="c1">; decipher</span>
<span class="w"> </span><span class="nf">push</span><span class="w"> </span><span class="no">v0</span>
<span class="w"> </span><span class="nf">call</span><span class="w"> </span><span class="no">decipher</span>
<span class="w"> </span><span class="c1">; check</span>
<span class="w"> </span><span class="nf">cld</span><span class="w"> </span><span class="c1">; clear direction flag so that string pointers auto increment after each string operation</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">esi</span><span class="p">,</span><span class="w"> </span><span class="no">v0</span><span class="w"> </span>
<span class="w"> </span><span class="no">lodsd</span><span class="w"> </span><span class="c1">; load string instructions (loads to EAX)</span>
<span class="w"> </span><span class="nf">cmp</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="w"> </span><span class="mi">0x57415343</span><span class="w"> </span><span class="c1">; 'CSAW'</span>
<span class="w"> </span><span class="nf">jne</span><span class="w"> </span><span class="no">.fail</span>
<span class="w"> </span><span class="nf">lodsd</span>
<span class="w"> </span><span class="nf">cmp</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="w"> </span><span class="mi">0x41484148</span><span class="w"> </span><span class="c1">; 'HAHA'</span>
<span class="w"> </span><span class="nf">jne</span><span class="w"> </span><span class="no">.fail</span>
<span class="na">...</span>
<span class="nl">decipher:</span>
<span class="w"> </span><span class="nf">push</span><span class="w"> </span><span class="no">ebp</span><span class="w"> </span><span class="c1">; esp - 4</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">ebp</span><span class="p">,</span><span class="no">esp</span>
<span class="w"> </span><span class="nf">push</span><span class="w"> </span><span class="no">esi</span>
<span class="w"> </span><span class="nf">push</span><span class="w"> </span><span class="no">ebx</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">ecx</span><span class="p">,</span><span class="no">dword</span><span class="w"> </span><span class="p">[</span><span class="no">ebp</span><span class="err">+</span><span class="mi">0x8</span><span class="p">]</span><span class="w"> </span><span class="c1">; input address</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">edx</span><span class="p">,</span><span class="no">dword</span><span class="w"> </span><span class="p">[</span><span class="no">ecx</span><span class="p">]</span><span class="w"> </span><span class="c1">; first part of input i0 = y1</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">esi</span><span class="p">,</span><span class="no">dword</span><span class="w"> </span><span class="p">[</span><span class="no">ecx</span><span class="err">+</span><span class="mi">0x4</span><span class="p">]</span><span class="w"> </span><span class="c1">; second part of input i1 = y0</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="no">edx</span><span class="w"> </span>
<span class="w"> </span><span class="no">mov</span><span class="w"> </span><span class="no">ebx</span><span class="p">,</span><span class="no">edx</span>
<span class="w"> </span><span class="nf">shr</span><span class="w"> </span><span class="no">ebx</span><span class="p">,</span><span class="mi">0x5</span><span class="w"> </span><span class="c1">; y0 >> 5 </span>
<span class="w"> </span><span class="nf">shl</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="mi">0x4</span><span class="w"> </span><span class="c1">; y0 << 4 </span>
<span class="w"> </span><span class="nf">xor</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="no">ebx</span><span class="w"> </span>
<span class="w"> </span><span class="no">add</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="no">edx</span>
<span class="w"> </span><span class="nf">xor</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="mi">0x2913260a</span><span class="w"> </span>
<span class="w"> </span><span class="no">sub</span><span class="w"> </span><span class="no">esi</span><span class="p">,</span><span class="no">eax</span><span class="w"> </span><span class="c1">; y2 = y0 - ((y1 >> 5) ^ (y1 << 4) + y1 ) ^ v0</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">ebx</span><span class="p">,</span><span class="no">esi</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="no">esi</span>
<span class="w"> </span><span class="nf">shr</span><span class="w"> </span><span class="no">esi</span><span class="p">,</span><span class="mi">0x5</span>
<span class="w"> </span><span class="nf">shl</span><span class="w"> </span><span class="no">ebx</span><span class="p">,</span><span class="mi">0x4</span>
<span class="w"> </span><span class="nf">xor</span><span class="w"> </span><span class="no">ebx</span><span class="p">,</span><span class="no">esi</span>
<span class="w"> </span><span class="nf">add</span><span class="w"> </span><span class="no">ebx</span><span class="p">,</span><span class="no">eax</span>
<span class="w"> </span><span class="nf">xor</span><span class="w"> </span><span class="no">ebx</span><span class="p">,</span><span class="mi">0x37dbdd6f</span><span class="w"> </span>
<span class="w"> </span><span class="no">sub</span><span class="w"> </span><span class="no">edx</span><span class="p">,</span><span class="no">ebx</span><span class="w"> </span><span class="c1">; y3 = y1 - ((y2 >> 5) ^ (y2 << 4) + y2 ) ^ v1</span>
</code></pre></div>
<p>Looking at the algorithm used for pin derivation, we see that it calculates each new step based on two previous ones. Since in the end results are compared to <code>0x57415343</code> and <code>0x41484148</code>, the results of two last steps, we have everything we need to run derivation backwards and get required inputs.</p>
<p>Python solver:</p>
<div class="highlight"><pre><span></span><code><span class="n">x</span> <span class="o">=</span> <span class="mh">0x57415343</span>
<span class="n">y</span> <span class="o">=</span> <span class="mh">0x41484148</span>
<span class="k">def</span> <span class="nf">step</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="k">return</span> <span class="p">(((</span><span class="n">x</span> <span class="o">>></span> <span class="mi">5</span><span class="p">)</span><span class="o">^</span><span class="p">(</span><span class="n">x</span><span class="o"><<</span><span class="mi">4</span><span class="p">))</span> <span class="o">+</span> <span class="n">x</span><span class="p">)</span> <span class="o">&</span> <span class="mh">0xffffffff</span>
<span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">values</span><span class="p">[::</span><span class="o">-</span><span class="mi">1</span><span class="p">]:</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="p">(</span><span class="n">step</span><span class="p">(</span><span class="n">y</span><span class="p">)</span> <span class="o">^</span> <span class="n">val</span><span class="p">))</span> <span class="o">&</span> <span class="mh">0xffffffff</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">y</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">tmp</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"0x</span><span class="si">%X</span><span class="s2"> 0x</span><span class="si">%X</span><span class="s2">"</span> <span class="o">%</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">))</span>
</code></pre></div>
<p>Full code can be found <a href="https://github.com/F3real/ctf_solutions/blob/master/2018/pwn_adventure_sourcery/LabDoor3/sol.py">here</a></p>
<p>Some things to note:</p>
<ul>
<li>inputs are taken in reverse order, the first part of the input is used as y1 and second as y0</li>
<li>program treats inputted string as a hex number</li>
</ul>
<p>Calculated pin: <code>9b916917-b6117336</code></p>
<h2 id="swamp-maze-entrace">Swamp maze entrace</h2>
<p>After we enter ruins we see robot we can program. Source code of robot:</p>
<div class="highlight"><pre><span></span><code><span class="nf">INPUT</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="mi">0</span>
<span class="nl">main:</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">esi</span><span class="p">,</span><span class="w"> </span><span class="no">program</span>
<span class="nl">.loop:</span>
<span class="w"> </span><span class="nf">in</span><span class="w"> </span><span class="no">al</span><span class="p">,</span><span class="w"> </span><span class="no">INPUT</span>
<span class="w"> </span><span class="nf">cmp</span><span class="w"> </span><span class="no">al</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span>
<span class="w"> </span><span class="nf">je</span><span class="w"> </span><span class="no">.done</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="p">[</span><span class="no">esi</span><span class="p">],</span><span class="w"> </span><span class="no">al</span>
<span class="w"> </span><span class="nf">inc</span><span class="w"> </span><span class="no">esi</span>
<span class="w"> </span><span class="nf">jmp</span><span class="w"> </span><span class="no">.loop</span>
<span class="nl">.done:</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="w"> </span><span class="no">SYS_DISCONNECT</span>
<span class="w"> </span><span class="nf">int</span><span class="w"> </span><span class="mi">0x80</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="w"> </span><span class="no">SYS_ROM_UPDATE</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">ebx</span><span class="p">,</span><span class="w"> </span><span class="no">program</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">ecx</span><span class="p">,</span><span class="w"> </span><span class="no">esi</span>
<span class="w"> </span><span class="nf">sub</span><span class="w"> </span><span class="no">ecx</span><span class="p">,</span><span class="w"> </span><span class="no">ebx</span><span class="w"> </span><span class="c1">;get length of program</span>
<span class="w"> </span><span class="nf">int</span><span class="w"> </span><span class="mi">0x80</span>
<span class="na">.data</span>
<span class="nl">program:</span>
</code></pre></div>
<p>It executes input we pass to it. To solve this challenge we can use <code>SYS_WALK</code> syscall. </p>
<div class="highlight"><pre><span></span><code><span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="w"> </span><span class="no">SYS_WALK</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">ebx</span><span class="p">,</span><span class="w"> </span><span class="no">x</span><span class="w"> </span><span class="c1">; x-vel</span>
<span class="w"> </span><span class="nf">mov</span><span class="w"> </span><span class="no">ecx</span><span class="p">,</span><span class="w"> </span><span class="no">y</span><span class="w"> </span><span class="c1">; y-vel</span>
<span class="w"> </span><span class="nf">int</span><span class="w"> </span><span class="mi">0x80</span>
<span class="w"> </span><span class="nf">hlt</span>
</code></pre></div>
<p>But we can't use null byte since it is used to trigger execution.
So we have to change <code>mov eax, SYS_WALK</code> with <code>xor eax, eax; mov al, SYS_WALK</code>. Also, we don't need y-vel so we will set ecx to 0 with <code>xor ecx, ecx</code> and in ebx we will write max value (-1)</p>
<p>Solution:</p>
<div class="highlight"><pre><span></span><code><span class="nf">mov</span><span class="w"> </span><span class="no">esi</span><span class="p">,</span><span class="w"> </span><span class="no">data</span>
<span class="nf">mov</span><span class="w"> </span><span class="no">ecx</span><span class="p">,</span><span class="w"> </span><span class="no">end-data</span>
<span class="nf">mov</span><span class="w"> </span><span class="no">dx</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span>
<span class="na">rep</span><span class="w"> </span><span class="nf">outsb</span>
<span class="nf">hlt</span>
<span class="nl">data:</span>
<span class="nf">xor</span><span class="w"> </span><span class="no">eax</span><span class="p">,</span><span class="w"> </span><span class="no">eax</span>
<span class="nf">xor</span><span class="w"> </span><span class="no">ecx</span><span class="p">,</span><span class="w"> </span><span class="no">ecx</span>
<span class="nf">mov</span><span class="w"> </span><span class="no">al</span><span class="p">,</span><span class="w"> </span><span class="no">SYS_WALK</span>
<span class="nf">mov</span><span class="w"> </span><span class="no">ebx</span><span class="p">,</span><span class="w"> </span><span class="mi">-1</span>
<span class="nf">int</span><span class="w"> </span><span class="mi">0x80</span>
<span class="nf">hlt</span>
<span class="nf">db</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="c1">;starts execution</span>
<span class="nl">end:</span>
</code></pre></div>
</div>
<footer class="text-right">
<p>- F3real</p>
</footer>
<div id="show-comments" class="span7 text-center">
<a href="https://f3real.github.io/pwnadventure_sourcery2.html#disqus_thread"
data-disqus-identifier="pwnadventure_sourcery2"
class="btn btn-primary twitchy-background">Show Comments</a>
</div>
<section id="comments" class="comments hidden">
<hr/>
<h2>Comments</h2>
<div id="disqus_thread"></div>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by
Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
</section>
</div>
</div>
</article>
</section>
<footer>
<hr>
<div class="row">
<div class="col-lg-10 text-center">
<p><small>
Built by <a href="http://docs.getpelican.com/en/latest">Pelican</a> / <a href="https://github.com/F3real/pelican-twitchy">pelican-twitchy</a>
· © 2024 F3real
</small></p>
</div>
</div>
</footer> </div>
</div>
<!-- /#page-content-wrapper -->
</div>
<!-- /#wrapper -->
<!-- disqus -->
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'https-f3real-github-io'; // required: replace example with your forum shortname
var disqus_identifier = 'pwnadventure_sourcery2';
var disqus_url = 'https://f3real.github.io/pwnadventure_sourcery2.html';
var disqus_config = function () {
this.language = "en";
};
var commentsDiv = document.getElementById('show-comments');
commentsDiv.onclick = function() {
/* * * DON'T EDIT BELOW THIS LINE * * */
(function () {
var dsq = document.createElement('script');
dsq.type = 'text/javascript';
dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
this.style.display = 'none';
};
</script>
<!-- /disqus -->
<script>
const wrapper = document.getElementById('wrapper');
const sidebarBig = document.getElementById('sidebar-wrapper');
const sidebarSmall = document.getElementById('sidebar-wrapper-small');
const triggers = Array.from(document.querySelectorAll('[data-toggle="collapse"]'));
for (var i = 0; i < triggers.length; i++) {
triggers[i].addEventListener('click', (ev) => {
const elm = ev.currentTarget;
ev.preventDefault();
const selector = elm.getAttribute('href').replace('#','');
elm.classList.toggle('collapsed');
document.getElementById(selector).classList.toggle('show');
}, false);
}
function showBigNav() {;
sidebarBig.style.display = 'block';
sidebarSmall.style.display = 'none';
}
function showSmallNav() {
sidebarBig.style.display = 'none';
sidebarSmall.style.display = 'block';
}
const mediaQuery = window.matchMedia('(min-width:768px)');
mediaQuery.onchange = e => {
if (wrapper.classList.contains('toggled')) {
wrapper.classList.remove('toggled');
} else {
if (e.matches) {
showBigNav();
} else {
showSmallNav();
}
}
}
function setNavbar() {
var condition = wrapper.classList.contains('toggled');
if (!mediaQuery.matches) {
condition = !condition;
}
if (condition) {
showSmallNav();
} else {
showBigNav();
}
}
function toggleMenu(){
wrapper.classList.toggle('toggled');
setNavbar();
}
window.onload = setNavbar;
</script>
<script data-goatcounter="https://f3real.goatcounter.com/count"
async src="//gc.zgo.at/count.js"></script>
</body>
</html>