-
Notifications
You must be signed in to change notification settings - Fork 0
/
vuln.vm
901 lines (742 loc) · 11.4 KB
/
vuln.vm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
main:
stk 0 b
stk 0 c
stk 0 d
imm a main_menu
jmp 0 a
stk d 0
stk c 0
stk b 0
stk i 0
main_menu:
stk 0 b
stk 0 c
stk 0 d
imm a welcome
stk 0 a
imm a welcome_len
ldm a a
stk 0 a
imm a _main_after_print_welcome
stk 0 a
imm a print_str
jmp 0 a
_main_after_print_welcome:
stk a 0
stk a 0
imm a _main_after_menu
stk 0 a
imm a menu_loop
jmp 0 a
_main_after_menu:
imm a end
jmp 0 a
end:
sys exit c
menu_loop:
stk 0 b
stk 0 c
stk 0 d
_menu_start:
imm a menu
stk 0 a
imm a menu_len
ldm a a
stk 0 a
imm a _menu_loop_after_print
stk 0 a
imm a print_str
jmp 0 a
_menu_loop_after_print:
# need to read in the input,
stk a 0
stk a 0
# Read in two bytes, first is input, second is newline
imm a 0
imm b menu_input
imm c 2
sys read d
# zero out the last byte (by overwriting all the rest
imm a 1
add b a
imm a 0
stm b a
imm b menu_input
ldm b b
# 0x31 is 1
imm a 0x31
cmp a b
imm a _menu_call_new_log
jmp == a
# 0x32 is 2
imm a 0x32
cmp a b
imm a _menu_list_logs
jmp == a
# 0x33 is 4
imm a 0x33
cmp a b
imm a _menu_view_log
jmp == a
# fall through, say goodbye then bounce
imm a menu_goodbye
stk 0 a
imm a menu_goodbye_len
ldm a a
stk 0 a
imm a _main_after_print_goodbye
stk 0 a
imm a print_str
jmp 0 a
_main_after_print_goodbye:
stk b 0
stk c 0
imm a _menu_return
jmp 0 a
_menu_call_new_log:
# call new log
imm a _menu_start
stk 0 a
imm a new_log
jmp 0 a
_menu_list_logs:
# call list logs
imm b _menu_start
stk 0 b
imm a list_logs
jmp 0 a
_menu_view_log:
# call view log
imm c _menu_start
stk 0 c
imm c view_log
jmp 0 c
_menu_return:
stk d 0
stk c 0
stk b 0
stk i 0
new_log:
stk 0 b
stk 0 c
stk 0 d
# store where the stack was in d
stm s s
ldm d s
# Make room on the stack
# imm a 0
# stk 0 a
# stk 0 a
# stk 0 a
# stk 0 a
# stk 0 a
# stk 0 a
# stk 0 a
# stk 0 a
# stk 0 a
# stk 0 a
imm b negative_log_struct_size
ldm a b
add s a
# First, check and exit early if there's no space for more logs
imm a current_num_logs
ldm a a
imm b max_num_logs
ldm b b
cmp a b
imm a _new_log_continue
jmp < a
imm a no_space
stk 0 a
imm a no_space_len
ldm a a
stk 0 a
imm a _new_log_return
stk 0 a
imm a print_str
jmp 0 a
_new_log_continue:
# ask for the log content
# ask for the name
imm a log_name
stk 0 a
imm a log_name_len
ldm a a
stk 0 a
imm a _new_log_after_log_name
stk 0 a
imm a print_str
jmp 0 a
_new_log_after_log_name:
stk b 0
stk b 0
# b is buf
imm a name_offset
ldm a a
add a s
imm b 0
add b a
# fd to read from (stdin)
imm a 0
stk 0 a
# buf to read into (s+name_offset)
stk 0 b
# num to read (name_len max due to struct)
imm a name_len
ldm a a
stk 0 a
imm a _new_log_after_read_until_newline
stk 0 a
imm a read_until_newline
jmp 0 a
_new_log_after_read_until_newline:
stk b 0
stk b 0
stk b 0
# a should have the size, store on stack at (s+name_len_offset)
imm b name_len_offset
ldm b b
add b s
stm b a
# Name successfully read
# # DEBUGGING OUTPUT, TODO: REMOVE
# # buf into b
# imm a name_offset
# ldm a a
# add a s
# imm b 0
# add b a
# # length into c
# imm a name_len_offset
# ldm a a
# add a s
# ldm c a
# # push char* name
# stk 0 b
# # push length of name
# stk 0 c
# imm a _new_log_test_print_after
# stk 0 a
# imm a print_str
# jmp 0 a
# _new_log_test_print_after:
# stk b 0
# stk b 0
## ask for the content
imm a log_content
stk 0 a
imm a log_content_len
ldm a a
stk 0 a
imm a _new_log_after_log_content
stk 0 a
imm a print_str
jmp 0 a
_new_log_after_log_content:
stk b 0
stk b 0
# b is buf
imm a content_offset
ldm a a
add a s
imm b 0
add b a
# fd to read from (stdin)
imm a 0
stk 0 a
# buf to read into (s+name_offset)
stk 0 b
# num to read (32 max due to struct)
imm a content_len
ldm a a
stk 0 a
imm a _new_log_after_read_until_newline_content
stk 0 a
imm a read_until_newline
jmp 0 a
_new_log_after_read_until_newline_content:
stk b 0
stk b 0
stk b 0
# a should have the size, store on stack at (s+content_len_offset)
imm b content_len_offset
ldm b b
add b s
stm b a
# Content successfully read
# # DEBUGGING OUTPUT, TODO: REMOVE
# # buf into b
# imm a content_offset
# ldm a a
# add a s
# imm b 0
# add b a
# # length into c
# imm a content_len_offset
# ldm a a
# add a s
# ldm c a
# # push char* name
# stk 0 b
# # push length of name
# stk 0 c
# imm a _new_log_test_print_after_content
# stk 0 a
# imm a print_str
# jmp 0 a
# _new_log_test_print_after_content:
# stk b 0
# stk b 0
# store it to global array
imm b log_struct_size
ldm b b
imm a current_num_logs
ldm a a
# calc the index = mult(log_struct_size, current_num_logs)
stk 0 a
stk 0 b
imm a _new_log_after_mult
stk 0 a
imm a mult
jmp 0 a
_new_log_after_mult:
stk b 0
stk b 0
# a has the index into logs
imm b logs
add b a
# b is the buf we want to write to, d is on the stack
# save d, which is the frame pointer
stk 0 d
# Copy from stack into buf
imm d 8
add d s
ldm c d
stm b c
# increment both dest and buff
imm a 8
add b a
add d a
# copy
ldm c d
stm b c
# increment
add b a
add d a
# copy
ldm c d
stm b c
# increment
add b a
add d a
# copy
ldm c d
stm b c
# increment
add b a
add d a
# copy
ldm c d
stm b c
# increment
add b a
add d a
# copy
ldm c d
stm b c
# increment
add b a
add d a
# copy
ldm c d
stm b c
# Should have copied 7*8 (56) total bytes to the global buffer
# DEBUGGING OUTPUT, straight output the first 56 bytes of logs
# imm a logs
# stk 0 a
# imm a 56
# stk 0 a
# imm a _new_log_after_debug_output
# stk 0 a
# imm a print_str
# jmp 0 a
# _new_log_after_debug_output:
# stk a 0
# stk a 0
# restore the fp
stk d 0
# increment the current_num_logs
imm a current_num_logs
ldm a a
imm b 1
add b a
imm a current_num_logs
stm a b
_new_log_return:
# restore the previous stack
imm s 0
add s d
stk d 0
stk c 0
stk b 0
stk i 0
# void list_logs()
list_logs:
stk 0 b
stk 0 c
stk 0 d
# Print out "Logs:\n"
imm a list_message
stk 0 a
imm a list_message_len
ldm a a
stk 0 a
imm a _list_logs_after_print_logs
stk 0 a
imm a print_str
jmp 0 a
_list_logs_after_print_logs:
stk a 0
stk a 0
# COUNTER IS b, DO NOT CHANGE
imm b 0
_list_logs_start:
imm a current_num_logs
ldm a a
cmp a b
imm a _list_logs_end
jmp == a
# print "("
imm a 0x28
stk 0 a
imm a 0
add a s
stk 0 a
imm a 1
stk 0 a
imm a _list_logs_after_open_paren
stk 0 a
imm a print_str
jmp 0 a
_list_logs_after_open_paren:
stk a 0
stk a 0
stk a 0
# print counter + 1
imm a 1
add a b
stk 0 a
imm a _list_logs_after_print_counter
stk 0 a
imm a print_int
jmp 0 a
_list_logs_after_print_counter:
stk a 0
# print ") "
imm a end_paren
stk 0 a
imm a end_paren_len
ldm a a
stk 0 a
imm a _list_logs_after_close_paren
stk 0 a
imm a print_str
jmp 0 a
_list_logs_after_close_paren:
stk a 0
stk a 0
# calc counter * log_struct_size
imm a log_struct_size
ldm a a
stk 0 b
stk 0 a
imm a _list_logs_after_mult
stk 0 a
imm a mult
jmp 0 a
_list_logs_after_mult:
stk c 0
stk c 0
# a has the offset
imm c logs
add c a
# c is now logs+offset
stk 0 c
imm a _list_logs_after_print
stk 0 a
imm a print_log_name
jmp 0 a
_list_logs_after_print:
stk a 0
# print newline
imm a 0xa
stk 0 a
imm a 0
add a s
stk 0 a
imm a 1
stk 0 a
imm a _list_logs_after_newline
stk 0 a
imm a print_str
jmp 0 a
_list_logs_after_newline:
stk a 0
stk a 0
stk a 0
# increment the counter
imm a 1
add b a
imm a _list_logs_start
jmp 0 a
_list_logs_end:
stk d 0
stk c 0
stk b 0
stk i 0
view_log:
stk 0 b
stk 0 c
stk 0 d
# print request for index
imm a index_log_message
stk 0 a
imm a index_log_message_len
ldm a a
stk 0 a
imm a _view_log_after_print_log_message
stk 0 a
imm a print_str
jmp 0 a
_view_log_after_print_log_message:
stk a 0
stk a 0
# read in index
# Read in two bytes, first is input, second is newline
imm a 0
imm b menu_input
imm c 2
sys read d
# zero out the last byte (by overwriting all the rest
imm a 1
add b a
imm a 0
stm b a
imm b menu_input
ldm b b
# b is the character that they input, turn it into an offset
imm a negative_ascii_zero
ldm a a
add b a
# b is the log number, turn into offset
imm a negative_one
ldm a a
add b a
# check against the bounds
imm a 0
cmp b a
imm a _view_logs_no_log
jmp < a
imm a current_num_logs
ldm a a
cmp b a
imm a _view_logs_no_log
jmp > a
jmp == a
# Print out the log at the index b
# calc the index = mult(log_struct_size, current_num_logs)
imm a log_struct_size
ldm a a
stk 0 b
stk 0 a
imm a _view_log_after_mult
stk 0 a
imm a mult
jmp 0 a
_view_log_after_mult:
stk b 0
stk b 0
# a has the offset into the logs
imm b logs
add b a
# b is the buff
# print name
stk 0 b
imm a _view_log_after_print_name
stk 0 a
imm a print_log_name
jmp 0 a
_view_log_after_print_name:
stk a 0
# print ": "
imm a view_separator
stk 0 a
imm a view_separator_len
ldm a a
stk 0 a
imm a _view_log_after_print_separator
stk 0 a
imm a print_str
jmp 0 a
_view_log_after_print_separator:
stk a 0
stk a 0
# print content
stk 0 b
imm a _view_log_after_print_content
stk 0 a
imm a print_log_content
jmp 0 a
_view_log_after_print_content:
stk a 0
# print newline
imm a 0xa
stk 0 a
imm a 0
add a s
stk 0 a
imm a 1
stk 0 a
imm a _view_log_after_newline
stk 0 a
imm a print_str
jmp 0 a
_view_log_after_newline:
stk a 0
stk a 0
stk a 0
imm a _view_logs_end
jmp 0 a
_view_logs_no_log:
imm a no_log_message
stk 0 a
imm a no_log_message_len
ldm a a
stk 0 a
imm a _view_log_after_no_log
stk 0 a
imm a print_str
jmp 0 a
_view_log_after_no_log:
stk a 0
stk a 0
_view_logs_end:
stk d 0
stk c 0
stk b 0
stk i 0
# void print_log_name(void* log)
print_log_name:
stk 0 b
stk 0 c
stk 0 d
# log is s+(4*8)
imm a 32
add a s
ldm c a
imm a name_offset
ldm a a
add a c
# a is now log+name_offset
imm d name_len_offset
ldm d d
add d c
# d is now log+name_len_offset
# push buf
stk 0 a
# push len
ldm d d
stk 0 d
imm a _print_name_after_print_str
stk 0 a
imm a print_str
jmp 0 a
_print_name_after_print_str:
stk a 0
stk a 0
stk d 0
stk c 0
stk b 0
stk i 0
# void print_log_content(void* log)
print_log_content:
stk 0 b
stk 0 c
stk 0 d
# log is s+(4*8)
imm a 32
add a s
ldm c a
imm a content_offset
ldm a a
add a c
# a is now log+content_offset
imm d content_len_offset
ldm d d
add d c
# d is now log+content_len_offset
# push buf
stk 0 a
# push len
ldm d d
stk 0 d
imm a _print_content_after_print_str
stk 0 a
imm a print_str
jmp 0 a
_print_content_after_print_str:
stk a 0
stk a 0
stk d 0
stk c 0
stk b 0
stk i 0
.include print_str.vm
.include print_int.vm
.include read_until_newline.vm
.include mult.vm
# Log "struct" format
# name len (8 bytes)
# eight bytes name
# content len (8 bytes)
# 32 bytes content
# sizeof(struct log) = 56 bytes
.data
welcome "Welcome to the droid logging functionality.\n"
welcome_len len(welcome)
menu "Choose:\n(1) New log\n(2) List logs\n(3) View log\n(4) Quit\n"
menu_len len(menu)
menu_input "\x00"*9
menu_input_len len(menu_input)
menu_goodbye "So long\n"
menu_goodbye_len len(menu_goodbye)
log_struct_size 56
negative_log_struct_size -(log_struct_size)
content_len 32
name_offset 8
name_len_offset 0
content_offset 24
content_len_offset 16
log_name "What is the name of the log?\n"
log_name_len len(log_name)
log_content "What is the content of the log?\n"
log_content_len len(log_content)
negative_ascii_zero -0x30
negative_one -1
current_num_logs 0
no_space "No space for more logs\n"
no_space_len len(no_space)
index_log_message "What is the log number?\n"
index_log_message_len len(index_log_message)
no_log_message "Cannot find that log\n"
no_log_message_len len(no_log_message)
view_separator ": "
view_separator_len len(view_separator)
# Due to input parsing constraints, max_num_logs cannot be more than 9
logs "\x00"*((5*log_struct_size)-1)
max_num_logs 5
end_paren ") "
end_paren_len len(end_paren)
list_message "Logs:\n"
list_message_len len(list_message)
name_len 8