daily pastebin goal
59%
SHARE
TWEET

Untitled

a guest Jan 21st, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .globl _main
  2.  
  3. .bss
  4. ####################
  5. .lcomm remote, 16
  6. .lcomm rbuff, 512
  7. .lcomm wbuff, 512
  8.  
  9. .data
  10. ####################
  11. err:
  12. .ascii "ERR"
  13. val:
  14. .byte 0x01
  15. sockaddr:
  16. .short 0x0200
  17. .short 0x5000
  18. .long 0x0100007f
  19. remlen:
  20. .short 0x10
  21.  
  22. .text
  23. #####################
  24.  
  25. #socket(RDI: int domain, RSI: int type, RDX: int protocol)
  26. # domain   -> AF_UNSPEC   = 0
  27. # type     -> SOCK_STREAM = 1
  28. # protocol -> IPPROTO_TCP = 6
  29. socket:
  30.   pushq %rbp
  31.   movq %rsp, %rbp
  32.   movq $(0x00), %rdi
  33.   movq $(0x01), %rsi
  34.   movq $(0x06), %rdx
  35.   movq $(0x02000061), %rax
  36.   syscall
  37.   leave
  38.   ret
  39.  
  40. #setsockopt(RDI: int socket, RSI: int level, RDX: int name, RCX: caddr_t val, R8: socklen_t valsize)
  41. # socket -> returned from socket call
  42. # level  -> SOL_SOCKET   = 0xffff
  43. # name   -> SO_REUSEADDR = 0x0004
  44. # val    -> address of val
  45. # valsize = 4
  46.  
  47. setsockopt:
  48.   pushq %rbp
  49.   movq %rsp, %rbp
  50.   movq %rax, %rdi
  51.   movq $(0xffff), %rsi
  52.   movq $(0x04), %rdx
  53.   leaq val(%rip), %rcx
  54.   movq $(0x01), %r8
  55.   movq $(0x02000069), %rax
  56.   syscall
  57.   leave
  58.   ret
  59.  
  60. # bind(RDI: int socket, RSI: caddr_t sockaddr, RDX: socklen_t sockaddrlen)
  61. # socket      -> in rax already
  62. # sockaddr    -> starting address where first 2 bytes are the family, the next 2 bytes the port, the next 4 bytes the address.
  63. # sockaddrlen -> 0x10 bytes (16 bytes decimal)
  64. bind:
  65.   push %rbp
  66.   movq %rsp, %rbp
  67.   lea sockaddr(%rip), %rsi
  68.   movq $(0x08), %rdx
  69.   movq $(0x02000064), %rax
  70.   syscall
  71.   leave
  72.   ret
  73.  
  74. # listen(RDI: int socket, RSI: backlog)
  75.  
  76. listen:
  77.   push %rbp
  78.   movq %rsp, %rbp
  79.   movq %rax, %rdi
  80.   movq $(0x05), %rsi
  81.   movq $(0x0200006a), %rax
  82.   syscall
  83.   leave
  84.   ret
  85.  
  86. # accept(RDI: int socket, RSI: caddr_t name, RDX: socklen_t* anamelen)
  87.  
  88. accept:
  89.   push %rbp
  90.   movq %rsp, %rbp
  91.   movq %rax, %rdi
  92.   movq remote(%rip), %rsi
  93.   movq $(0x10), %rdx
  94.   movq $(0x0200001d), %rax
  95.   syscall
  96.   leave
  97.   ret
  98.  
  99. # read(RDI: int fd, RSI: user_addr_t cbuf, RDX: user_size_t nbyte)
  100.  
  101. read:
  102.   push %rbp
  103.   movq %rsp, %rbp
  104.   movq %rax, %rdi
  105.   movq rbuff(%rip), %rsi
  106.   movq $(0x010000), %rdx
  107.   movq $(0x02000003), %rax
  108.   syscall
  109.   leave
  110.   ret
  111.  
  112. # print read buffer
  113. pr:
  114.   push %rbp
  115.   movq %rsp, %rbp
  116.   movq $(0x01), %rdi
  117.   movq rbuff(%rip), %rsi
  118.   movq $(0x010000), %rdx
  119.   movq $(0x02000004), %rax
  120.   syscall
  121.   leave
  122.   ret
  123.  
  124. #print ERR if return value is -1
  125. pe:
  126.   push %rbp
  127.   movq %rsp, %rbp
  128.   movq $(0x01), %rdi
  129.   movq err(%rip), %rsi
  130.   movq $(0x03), %rdx
  131.   movq $(0x02000004), %rax
  132.   syscall
  133.   leave
  134.   ret
  135.  
  136. #check return value for -1
  137. chk:
  138.   pushq %rbp
  139.   movq %rsp, %rbp
  140.   cmpq $(-0x01), %rax
  141.   je pe
  142.   leave
  143.   ret
  144.  
  145. ################################# Start
  146.  
  147. _main:
  148.   push %rbp
  149.   movq %rsp, %rbp
  150.   movq $(-0x01), %rax
  151.   call chk
  152.   call socket
  153.   movq %rax, %r8
  154.   movq %r8, %rdi
  155.   call setsockopt
  156.   movq %r8, %rdi
  157.   call bind
  158.   call chk
  159.   call listen
  160.   call chk
  161.  
  162. #take connections
  163. loop:
  164.   call accept
  165.   cmp $(0x00), %rax
  166.   jl pe
  167.  
  168.   call read
  169.   call chk
  170.  
  171.   jmp loop
  172.  
  173. #unused yet
  174. end:
  175.   leave
  176.   ret
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top