-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_client.asm
726 lines (421 loc) · 16.9 KB
/
http_client.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
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
%include "utils.asm"
; ==============================================
; External Functions
; ==============================================
extern printf
extern sprintf
extern strstr
extern strlen
extern strcmp
extern strcpy
extern strncpy
extern strchr
extern get_sockaddr_in_for_hostname
; ==============================================
; Structs
; ==============================================
struc addrinfo_struct
.a_flags resd 1
.a_family resd 1
.a_socktype resd 1
.a_protocol resd 1
.a_addrlen resd 1
.a_sockaddr resq 1
.a_canonname resq 1
.a_next resq 1
endstruc
; ==============================================
; Data Section
; ==============================================
section .data
; ==============================================
; Variables
; ==============================================
; Syscalls + options
SYSCALL_SOCKET equ 41
SYSCALL_CONNECT equ 42
SYSCALL_READ equ 0
SYSCALL_WRITE equ 1
SOCKET_AF_INET equ 2
SOCKET_SOCK_STREAM equ 1
SOCKET_NO_FLAGS equ 0
; Misc
NL equ 10
; Debug strings
str_socket_calling db " - Creating socket...",NL,0
str_socket_result db " - Socket result: %d",NL,0
str_connect_calling db " - Connecting (FD: %d)...",NL,0
str_connect_result db " - Connect result: %d",NL,0
str_write_calling db " - Sending request (FD: %d)...",NL,0
str_write_result db " - Write result: %d",NL,0
str_read_calling db " - Receiving response (FD: %d)...",NL,0
str_read_result db "%s",0
; addrinfo_hints struct instance
addrinfo_hints istruc addrinfo_struct
at addrinfo_struct.a_flags, dd 0
at addrinfo_struct.a_family, dd SOCKET_AF_INET
at addrinfo_struct.a_socktype, dd SOCKET_SOCK_STREAM
at addrinfo_struct.a_protocol, dd 0
at addrinfo_struct.a_addrlen, dd 0
at addrinfo_struct.a_sockaddr, dq 0
at addrinfo_struct.a_canonname, dq 0
at addrinfo_struct.a_next, dq 0
iend
; Request
request_body_template db "%s %s HTTP/1.0",NL,"Host: %s",NL,"User-Agent: HttpClient",NL,"Accept: */*",NL,NL,0
request_default_uri db "/",0
; Response
response_buf_len equ 8192
; HTTP Methods (most common ones supported only :D)
http_get_method db "GET",0
http_post_method db "POST",0
http_put_method db "PUT",0
http_delete_method db "DELETE",0
http_options_method db "OPTIONS",0
; Protocols
http_service db "http",0
http_protocol db "http://",0
http_protocol_len equ $-http_protocol-1
https_protocol db "https://",0
; Options
option_prefix db "--",0
http_get_method_option db "--GET",0
http_post_method_option db "--POST",0
http_put_method_option db "--PUT",0
http_delete_method_option db "--DELETE",0
http_options_method_option db "--OPTIONS",0
; Errors
str_err_invalid_url_received db " - ERROR: Invalid URL received. It has to be in the following format (HTTPS not supported yet): http://somehost/some-uri-if-needed",NL,0
str_err_cant_create_socket db " - ERROR: Could NOT create socket. Result: %d",NL,0
str_err_cant_connect db " - ERROR: Could NOT connect to the desired host. Result: %d",NL,0
str_err_cant_write db " - ERROR: Could NOT send request to the desired host. Result: %d",NL,0
str_err_cant_read db " - ERROR: Could NOT receive response from the desired host. Result: %d",NL,0
str_err_invalid_argument db " - ERROR: An argument received is invalid. Check that it's not an empty string or an unknown option flag.",NL,0
str_err_could_not_resolve_hostname db " - ERROR: Could NOT resolve hostname. Result: %d",NL,0
str_err_could_not_prepare_request_body db " - ERROR: Could NOT prepare request body. Result: %d",NL,0
; Other messages
str_done db " - Done!",NL,0
str_option_received db " - Option received: %s",NL,0
str_uri_received db " - URI received: %s",NL,0
str_request_to_send db " - Request to send: %s",NL,0
; Misc
slash db '/'
; ==============================================
; BSS Section
; ==============================================
section .bss
request_body resb 2000 ; Arbitrary length
fd resd 1
; URL
hostname resb 253 ; Reserve maximum length for the hostname
hostname_on_request resb 253
uri_only resb 2000 ; Arbitrary max length for the URI :)
url_without_protocol resb 2253 ; Everything without the protocol
; Options
http_method resq 1
; sockaddr_in struct (required for the "connect" syscall
sockaddr resq 1
response_buf resb response_buf_len
; ==============================================
; Text Section
; ==============================================
section .text
; ==============================================
; Functions
; ==============================================
global main
main:
push rbp
mov rbp, rsp
; Parse arguments
call _parse_arguments
; Create socket
call _socket
mov [fd], rax
; Connect
mov rdi, [fd]
call _connect
; Send request
mov rdi, [fd]
call _write
; Receive response
mov rdi, [fd]
call _read
; Done!
exit str_done, 0
_parse_arguments:
push rbp
mov rbp, rsp
; Initialize variables
mov r15, http_get_method
mov [http_method], r15 ; GET == Default HTTP Method
mov r12, rdi ; argc
mov r13, rsi ; argv
; Skip first argument, which is the binary file being called
mov r14, 1 ; counter
_parse_argument:
cmp r14, r12
jge _leave
mov r15, [r13+8*r14]
inc r14
; Check length
mov rdi, r15
call strlen
cmp rax, 0
je _error_invalid_argument
; Check if this is an option
push r15
mov rdi, r15
call _parse_option
mov rdi, r15
pop r15
cmp rax, 1
je _parse_argument
; If not an option, check if this is the URL
call _parse_url
jmp _parse_argument
_leave:
cmp byte [sockaddr], 0
je _error_invalid_url_received
custom_printf str_uri_received, uri_only
leave
ret
_parse_url:
push rbp
mov rbp, rsp
; Save URL
mov r15, rdi
; Check protocol first (rdi is already the URL)
mov rsi, http_protocol
call strstr
cmp rax, 0
je _error_invalid_url_received
; Copy URL without protocol
mov rdi, url_without_protocol ; Destination
lea rsi, [r15+http_protocol_len] ; URL without protocol
call strcpy
; Calculate URL without protocol length
mov rdi, url_without_protocol
call strlen
mov r15, rax ; Save it in r15
; Find first occurrence of "/" (if any). If it has it, then that's the end of the hostname
mov rdi, url_without_protocol ; URL without protocol
mov rsi, [slash] ; Find "/"
call strchr
cmp rax, 0
je _set_default_uri
mov rdi, uri_only
mov rsi, rax
call strcpy
; Calculate hostname length (url_without_protocol length - uri_only length)
mov rdi, uri_only
call strlen
sub r15, rax
jmp _extract_hostname
_set_default_uri:
mov rdi, [request_default_uri]
mov [uri_only], rdi
mov rdi, url_without_protocol
mov [hostname], rdi
_extract_hostname:
mov rdi, hostname
mov rsi, url_without_protocol
mov rdx, r15
call strncpy
; @TODO: Somewhere my "hostname" is being deleted. For now, let's just backup in another variable
; this value... :facepalm
mov rdi, hostname_on_request
mov rsi, hostname
mov rdx, r15
call strncpy
_resolve_hostname:
; Resolve hostname
mov rdi, hostname
mov rsi, 80 ; @TODO: Extract port from url!
call get_sockaddr_in_for_hostname
cmp rax, 0
je _error_could_not_resolve_hostname
mov qword [sockaddr], rax ; sockaddr_in , which has the resolved IP address
_leave_parse_url:
leave
ret
_parse_option:
push rbp
mov rbp, rsp
; rdi is already the argument
mov rsi, option_prefix
call strstr
cmp rax, 0
je _leave_parse_option
mov r15, rdi
custom_printf str_option_received, r15
; Check which option was received
; Is it a GET?
mov rdi, r15
mov rsi, http_get_method_option
call strcmp
cmp rax, 0
je _set_http_get_method
; Is it a POST?
mov rdi, r15
mov rsi, http_post_method_option
call strcmp
cmp rax, 0
je _set_http_post_method
; Is it a PUT?
mov rdi, r15
mov rsi, http_put_method_option
call strcmp
cmp rax, 0
je _set_http_put_method
; Is it a DELETE?
mov rdi, r15
mov rsi, http_delete_method_option
call strcmp
cmp rax, 0
je _set_http_delete_method
; Is it an OPTIONS?
mov rdi, r15
mov rsi, http_options_method_option
call strcmp
cmp rax, 0
je _set_http_options_method
; Unknown flag? Return error
jmp _error_invalid_argument
_set_http_get_method:
mov qword [http_method], http_get_method
mov rax, 1
jmp _leave_parse_option
_set_http_post_method:
mov qword [http_method], http_post_method
mov rax, 1
jmp _leave_parse_option
_set_http_put_method:
mov qword [http_method], http_put_method
mov rax, 1
jmp _leave_parse_option
_set_http_delete_method:
mov qword [http_method], http_delete_method
mov rax, 1
jmp _leave_parse_option
_set_http_options_method:
mov qword [http_method], http_options_method
mov rax, 1
jmp _leave_parse_option
_leave_parse_option:
leave
ret
_socket:
push rbp
mov rbp, rsp
; Create socket
mov rax, SYSCALL_SOCKET
mov rdi, SOCKET_AF_INET
mov rsi, SOCKET_SOCK_STREAM
mov rdx, SOCKET_NO_FLAGS
syscall
; If error, exit with error code
cmp rax, 0
jl _error_socket
leave
ret
_connect:
push rbp
mov rbp, rsp
; Save rdi
mov r12, rdi
; Print what we're doing
custom_printf str_connect_calling, r12
; Connect
mov rax, SYSCALL_CONNECT
mov rdi, r12 ; FD
mov rsi, [sockaddr]
mov rdx, 16
syscall
; If error, exit with error code
cmp rax, 0
jl _error_connect
; Print result
push rax
custom_printf str_connect_result, rax
pop rax
leave
ret
_write:
push rbp
mov rbp, rsp
; Save rdi
mov r12, rdi
; Print what we're doing
custom_printf str_write_calling, r12
; Prepare request body
mov rdi, request_body
mov rsi, request_body_template
mov rdx, [http_method]
mov rcx, uri_only
mov r8, hostname_on_request
call sprintf
cmp rax, 0
jl _error_could_not_prepare_request_body
cmp r11, rax
custom_printf str_request_to_send, request_body
; Write
mov rax, SYSCALL_WRITE
mov rdi, r12 ; FD
mov rsi, request_body
mov rdx, r11
syscall
; If error, exit with error code
cmp rax, 0
jl _error_write
; Print result
push rax
custom_printf str_write_result, rax
pop rax
leave
ret
_read:
push rbp
mov rbp, rsp
; Save rdi
mov r12, rdi
; Print what we're doing
custom_printf str_read_calling, r12
_read_loop:
; Read
mov rax, SYSCALL_READ
mov rdi, r12 ; FD
mov rsi, response_buf ; Buffer
mov rdx, response_buf_len ; Buffer size
sub rdx, 1 ; Leave space for the null byte
syscall
; If error, exit with error code
cmp rax, 0
jl _error_read
; Add null byte at the end of the data
mov byte [response_buf + rax], 0
; Print current buffer contents
push rax
custom_printf str_read_result, response_buf, rax
pop rax
; If number of bytes read 0, we've read everything
cmp rax, 0
jne _read_loop
leave
ret
_error_invalid_url_received:
exit str_err_invalid_url_received, 1
_error_invalid_argument:
exit str_err_invalid_argument, 1
_error_could_not_prepare_request_body:
exit str_err_could_not_prepare_request_body, rax, 1
_error_could_not_resolve_hostname:
exit str_err_could_not_resolve_hostname, rax, 1
_error_socket:
exit str_err_cant_create_socket, rax, 1
_error_connect:
exit str_err_cant_connect, rax, 1
_error_write:
exit str_err_cant_write, rax, 1
_error_read:
exit str_err_cant_read, rax, 1