0

How do I print out a property from a sockaddr_in struct? For example, I want to print out the port or address of the socket:

;; Data definitions
struc sockaddr_in
    .sin_family resw 1
    .sin_port resw 1
    .sin_addr resd 1
    .sin_zero resb 8
endstruc

section .data

;; sockaddr_in structure for the address the listening socket binds to
pop_sa istruc sockaddr_in
    at sockaddr_in.sin_family, dw 2           ; AF_INET
    at sockaddr_in.sin_port, dw 0xce56        ; port 22222 in host byte order
    at sockaddr_in.sin_addr, dd 0             ; localhost - INADDR_ANY
    at sockaddr_in.sin_zero, dd 0, 0
iend
sockaddr_in_len     equ $ - pop_sa
sin_port_len     equ $ - pop_sa + sockaddr_in.sin_port

_start:

;; Print listen init message to stdout
mov       rax, 1             ; SYS_WRITE
mov       rdi, 1             ; STDOUT
mov       rsi, pop_sa + sockaddr_in.sin_port
mov       rdx, sin_port_len
syscall

mov        rax, 60
syscall

No matter which property I try to print, I get the output: "V�". Why is this happening, and how can I get the expected output (i.e. 22222 for sin_port)?

thetipsyhacker
  • 1,402
  • 4
  • 18
  • 39
  • 2
    The port is in binary. You need to convert to text either before or after printing (e.g. by piping into hexdump). Also your `sin_port_len` is wrong. `./a.out | hd` gives `00000000 56 ce` – Jester Oct 08 '20 at 22:23
  • Thank you @Jester . I see the hex, 56CE (which is 22222 in decimal). Is there any way to do this in the .asm file? – thetipsyhacker Oct 08 '20 at 22:48
  • 1
    Yes, you need to convert to text yourself either hex or decimal whichever you like. Also if you can use the C library you can just call `printf`. – Jester Oct 08 '20 at 23:02

0 Answers0