-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtema2.asm
491 lines (377 loc) · 8.87 KB
/
tema2.asm
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
extern puts
extern printf
extern strlen
%define BAD_ARG_EXIT_CODE -1
section .data
filename: db "./input0.dat", 0
inputlen: dd 2263
fmtstr: db "Key: %d",0xa, 0
usage: db "Usage: %s <task-no> (task-no can be 1,2,3,4,5,6)", 10, 0
error_no_file: db "Error: No input file %s", 10, 0
error_cannot_read: db "Error: Cannot read input file %s", 10, 0
section .text
global main
convert_from_hex:
push ebp
mov ebp, esp
push ebx
push ecx
push edx
mov ecx, 0
mov edx, 0
mov eax, 0
mov ecx, [ebp + 8]
cmp cl, 97
jl add_to_eax
mov bl, 10
sub cl, 97
add bl, cl
mov cl, bl
jmp do_the_add
add_to_eax:
sub ecx, "0"
do_the_add:
add eax, ecx
mov edx, [ebp + 12]
cmp dl, 97
jl add_to_eax2
mov bl, 10
sub dl, 97
add bl, dl
mov dl, bl
jmp do_the_add2
add_to_eax2:
sub edx, "0"
do_the_add2:
shl edx, 4
add eax, edx
pop edx
pop ecx
pop ebx
leave
ret
xor_strings:
; TODO TASK 1
push ebp
mov ebp, esp
mov esi, [ebp + 8]
mov edi, [ebp + 12]
mov ecx, 0
parse_string:
mov al, [esi + ecx]
mov bl, [edi + ecx]
cmp al, 0
je done
xor al, bl
mov [esi + ecx], al
inc ecx
jmp parse_string
done:
leave
ret
rolling_xor:
; TODO TASK 2
push ebp
mov ebp, esp
mov esi, [ebp + 8]
mov ecx, 1
mov al, 0
decrypt:
xor al, [esi + ecx - 1]
mov bl, [esi + ecx]
cmp bl, 0
je end_of_string
xor bl, al
mov [esi + ecx], bl
inc ecx
jmp decrypt
end_of_string:
leave
ret
xor_hex_strings:
; TODO TASK 3
push ebp
mov ebp, esp
mov esi, [ebp + 8]
mov edi, [ebp + 12]
mov ecx, 0
mov edx, 0
parse_hex_string:
mov al, [esi + ecx]
cmp al, "."
je done_hex
mov bl, [esi + ecx + 1]
push edx
push eax
push ebx
call convert_from_hex
add esp, 8
mov ebx, eax
push ebx
mov al, [edi + ecx]
mov bl, [edi + ecx + 1]
push eax
push ebx
call convert_from_hex
add esp, 8
mov edx, eax
pop ebx
xor bl, dl
pop edx
mov [esi + edx], bl
add ecx, 2
inc edx
cmp edx, [ebp + 16]
jl parse_hex_string
done_hex:
mov byte [esi + edx], 0
leave
ret
base32decode:
; TODO TASK 4
push ebp
mov ebp, esp
mov esi, [ebp + 8]
push esi
call strlen
pop esi
mov edx, eax
mov eax, 0
mov ebx, 0
mov ecx, 0
parse_str:
mov al, byte [esi + ecx]
cmp al, "A"
jl convert_number
convert_letter:
sub al, "A"
jmp end_convert
convert_number:
sub al, "0"
add al, 24
end_convert:
shl ebx, 5
add ebx, eax
mov esp, ebp
pop ebp
ret
bruteforce_singlebyte_xor:
; TODO TASK 5
ret
decode_vigenere:
; TODO TASK 6
push ebp
mov ebp, esp
sub esp, 4
mov esi, [ebp + 8]
mov edi, [ebp + 12]
push edi
call strlen
pop edi
mov [ebp - 4], eax
mov eax, 0
mov ecx, 0
mov edx, 0
parse_encoded_string:
mov al, [esi + ecx]
cmp al, 0
je end_parse
mov bl, [edi + edx]
sub bl, "a"
sub al, bl
cmp al, "a"
jge continue_parsing
push ecx
mov cl, 96
sub cl, al
mov al, 122
sub al, cl
pop ecx
continue_parsing:
mov [esi + ecx], al
inc ecx
inc edx
cmp edx, [ebp - 8]
jl parse_encoded_string
mov edx, 0
jmp parse_encoded_string
end_parse:
mov esp, ebp
pop ebp
ret
main:
push ebp
mov ebp, esp
sub esp, 2300
; test argc
mov eax, [ebp + 8]
cmp eax, 2
jne exit_bad_arg
; get task no
mov ebx, [ebp + 12]
mov eax, [ebx + 4]
xor ebx, ebx
mov bl, [eax]
sub ebx, '0'
push ebx
; verify if task no is in range
cmp ebx, 1
jb exit_bad_arg
cmp ebx, 6
ja exit_bad_arg
; create the filename
lea ecx, [filename + 7]
add bl, '0'
mov byte [ecx], bl
; fd = open("./input{i}.dat", O_RDONLY):
mov eax, 5
mov ebx, filename
xor ecx, ecx
xor edx, edx
int 0x80
cmp eax, 0
jl exit_no_input
; read(fd, ebp - 2300, inputlen):
mov ebx, eax
mov eax, 3
lea ecx, [ebp-2300]
mov edx, [inputlen]
int 0x80
cmp eax, 0
jl exit_cannot_read
; close(fd):
mov eax, 6
int 0x80
; all input{i}.dat contents are now in ecx (address on stack)
pop eax
cmp eax, 1
je task1
cmp eax, 2
je task2
cmp eax, 3
je task3
cmp eax, 4
je task4
cmp eax, 5
je task5
cmp eax, 6
je task6
jmp task_done
task1:
; TASK 1: Simple XOR between two byte streams
; TODO TASK 1: find the address for the string and the key
; TODO TASK 1: call the xor_strings function
push ecx
call strlen
pop ecx
mov esi, ecx
inc eax
add ecx, eax
mov edi, ecx
push edi
push esi
call xor_strings
pop ecx
add esp, 4
push ecx
call puts ;print resulting string
add esp, 4
jmp task_done
task2:
; TASK 2: Rolling XOR
; TODO TASK 2: call the rolling_xor function
push ecx
call rolling_xor
pop ecx
push ecx
call puts
add esp, 4
jmp task_done
task3:
; TASK 3: XORing strings represented as hex strings
; TODO TASK 1: find the addresses of both strings
; TODO TASK 1: call the xor_hex_strings function
push ecx
call strlen
pop ecx
mov esi, ecx
inc eax
add ecx, eax
mov edi, ecx
dec eax
shr eax, 1
push eax
push edi
push esi
call xor_hex_strings
pop ecx
add esp, 4
push ecx ;print resulting string
call puts
add esp, 4
jmp task_done
task4:
; TASK 4: decoding a base32-encoded string
; TODO TASK 4: call the base32decode function
push ecx
call base32decode
pop ecx
push ecx
call puts ;print resulting string
pop ecx
jmp task_done
task5:
; TASK 5: Find the single-byte key used in a XOR encoding
; TODO TASK 5: call the bruteforce_singlebyte_xor function
push ecx ;print resulting string
call puts
pop ecx
push eax ;eax = key value
push fmtstr
call printf ;print key value
add esp, 8
jmp task_done
task6:
; TASK 6: decode Vignere cipher
; TODO TASK 6: find the addresses for the input string and key
; TODO TASK 6: call the decode_vigenere function
push ecx
call strlen
pop ecx
add eax, ecx
inc eax
push eax
push ecx ;ecx = address of input string
call decode_vigenere
pop ecx
add esp, 4
push ecx
call puts
add esp, 4
task_done:
xor eax, eax
jmp exit
exit_bad_arg:
mov ebx, [ebp + 12]
mov ecx , [ebx]
push ecx
push usage
call printf
add esp, 8
jmp exit
exit_no_input:
push filename
push error_no_file
call printf
add esp, 8
jmp exit
exit_cannot_read:
push filename
push error_cannot_read
call printf
add esp, 8
jmp exit
exit:
mov esp, ebp
pop ebp
ret