Advertisement
Guest User

Untitled

a guest
Aug 30th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; --------------------------------------------------------------
  2. ; Pre-processor area
  3.  
  4. ; Converts little-endian to big-endian
  5. %define htons(x) (((x & 0xFF) << 8) | (x >> 8))
  6.  
  7. %define crlf $0d, $0a
  8.  
  9. ; --------------------------------------------------------------
  10. section .data
  11.  
  12. PORT equ 80
  13. ;HOST equ 16777343               ; 127.0.0.1
  14. HOST equ 4124127939            ; neti.ee
  15.  
  16. sockerr:  db "Socket failed", crlf
  17. sockerr_len equ $-sockerr
  18.  
  19. connerr:  db "Unable to connect", crlf
  20. connerr_len equ $-connerr
  21.  
  22. get:   db "GET /", crlf
  23. get_len equ $-get
  24.  
  25.  
  26. ; --------------------------------------------------------------
  27. section .bss
  28.  
  29. buffer: resb 2048
  30.  
  31. ; --------------------------------------------------------------
  32. section .text
  33. global _start
  34.  
  35.  
  36. _start:
  37.         test eax, eax
  38.         jz main
  39.  
  40.         jmp exit2
  41.        
  42.  
  43. main:   call socket
  44.         call connect
  45.         call http_get
  46.         call read_loop
  47.  
  48.  
  49. exit:   mov eax, 6              ; SYS_CLOSE = 6
  50.         mov ebx, esi            ; close fd of socket
  51.         int $80
  52.  
  53.  
  54. exit2:  mov eax, 1              ; SYS_EXIT = 1
  55.         xor ebx, ebx            ; exit(0)
  56.         int $80
  57.  
  58.  
  59. fail_sock:
  60.         mov eax, 4
  61.         mov ebx, 2
  62.         mov ecx, sockerr
  63.         mov edx, sockerr_len
  64.         int $80
  65.         jmp exitf
  66.  
  67.  
  68. fail_conn:
  69.         mov eax, 4
  70.         mov ebx, 2
  71.         mov ecx, connerr
  72.         mov edx, connerr_len
  73.         int $80
  74.         jmp exitf
  75.  
  76.  
  77. exitf:
  78.         mov eax, 1              ; SYS_CLOSE = 1
  79.         mov ebx, -1             ; exit(-1)
  80.         int $80
  81.  
  82.  
  83.         ;; socket(PF_INET, SOCK_STREAM, 0)
  84. socket:
  85.     push ebp
  86.         mov ebp, esp
  87.  
  88.         push 0                  ; protocol = 0
  89.         push 1                  ; SOCK_STREAM = 1
  90.         push 2                  ; AF_INET = 2
  91.  
  92.         mov eax, $66            ; SYS_SOCKETCALL = $66
  93.         mov ebx, 1              ; SYS_SOCKET = 1
  94.         mov ecx, esp
  95.         int $80                 ; eax contains a socket fd, or -1
  96.  
  97.         cmp eax, -1             ; if ERROR
  98.         je fail_sock            ; then exit(-1)
  99.  
  100.         mov esi, eax            ; save the fd
  101.  
  102.         leave
  103.         ret
  104.  
  105.    
  106.  
  107.         ;; connect(fd, [AF_INET, port, IPv4], size)
  108. connect:
  109.         push ebp
  110.         mov ebp, esp
  111.  
  112.         ;; sockaddr strcuture
  113.         push dword 0
  114.         push dword 0
  115.        
  116.         push dword HOST         ; IP in decimal
  117.         push word htons(PORT)   ; port, tcp/ip uses bigendian
  118.         push word 2             ; AF_INET = 2
  119.         mov ecx, esp
  120.  
  121.         push 16                 ; size
  122.         push ecx                ; [AF_INET, port, IPv4]
  123.         push dword esi          ; fd of socket
  124.  
  125.         mov eax, $66            ; SYS_SOCKETCALL
  126.         mov ebx, 3              ; SYS_CONNECT = 3
  127.         mov ecx, esp            ; connect(args)
  128.         int $80
  129.  
  130.         cmp eax, -1
  131.         je fail_conn
  132.  
  133.         leave
  134.         ret
  135.    
  136.  
  137. http_get:
  138.         mov eax, 4
  139.         mov ebx, esi
  140.         mov ecx, get
  141.         mov edx, get_len
  142.         int $80
  143.  
  144.         ret
  145.  
  146.  
  147. read_loop:
  148.        
  149.     .read:
  150.         mov eax, 3              ; SYS_READ = 3
  151.         mov ebx, esi
  152.         mov ecx, buffer
  153.         mov edx, 1024
  154.         int $80
  155.  
  156.         call parse
  157.     jmp .read
  158.  
  159. parse:
  160.         push ebp
  161.         mov ebp, esp
  162.         mov ebx, buffer
  163.  
  164.     mov edx, 0
  165.     .loop:
  166.     inc edx
  167.     inc ebx
  168.     cmp dword [ebx], '<tit'
  169.         jne .loop
  170.    
  171.     .loop1:
  172.     inc ebx
  173.     inc edx
  174.     cmp byte [ebx], '>'
  175.     jne .loop1
  176.    
  177.     mov edi, 0
  178.     inc edx
  179.     .loop2:
  180.     inc ebx
  181.     inc edi
  182.     cmp byte [ebx], '<'
  183.     jne .loop2
  184.  
  185.     jmp print_title
  186.  
  187.  
  188. print_title:
  189.     dec edi     ; decrement to exclude <
  190.     mov eax, 4
  191.     mov ebx, 1
  192.     mov ecx, buffer
  193.     add ecx, edx
  194.     mov edx, edi
  195.     int 0x80
  196.     call exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement