我对汇编很新,我正在尝试创建一个客户端,它将连接到服务器并传达用户的ID。我使用getuid系统调用。 我也尝试在客户端直接打印它,但这也不起作用。
global _start
struc sockaddr_in
.sin_family resw 1
.sin_port resw 1
.sin_addr resd 1
.sin_zero resb 8
endstruc
; Convert numbers (constants!) to network byte order
%define hton(x) ((x & 0xFF000000) >> 24) | ((x & 0x00FF0000) >> 8) | ((x & 0x0000FF00) << 8) | ((x & 0x000000FF) << 24)
%define htons(x) ((x >> 8) & 0xFF) | ((x & 0xFF) << 8)
AF_INET equ 2
SOCK_STREAM equ 1
INADDR_ANY equ 0 ; /usr/include/linux/in.h
STDIN equ 0
STDOUT equ 1
__NR_exit equ 1
__NR_read equ 3
__NR_write equ 4
__NR_close equ 6
__NR_socketcall equ 102
__NR_getuid equ 24
; commands for sys_socketcall
; /usr/include/linux/in.h
SYS_SOCKET equ 1
SYS_BIND equ 2
SYS_CONNECT equ 3
SYS_LISTEN equ 4
SYS_ACCEPT equ 5
SYS_SEND equ 9
SYS_RECV equ 10
;------------------------
_ip equ 0x7F000001 ; loopback - 127.0.0.1
_port equ 31337
; Convert 'em to network byte order
IP equ hton(_ip)
PORT equ htons(_port)
BUFLEN equ 1000
section .data
my_sa istruc sockaddr_in
at sockaddr_in.sin_family, dw AF_INET
at sockaddr_in.sin_port, dw PORT
at sockaddr_in.sin_addr, dd INADDR_ANY
at sockaddr_in.sin_zero, dd 0, 0
iend
socket_args dd AF_INET, SOCK_STREAM, 0
connect_args dd 0, my_sa, sockaddr_in_size
section .bss
my_buf resb BUFLEN
fd_socket resd 1
fd_conn resd 1
id resd 1
section .text
_start:
; socket(AF_INET, SOCK_STREAM, 0)
mov ecx, socket_args ; address of args structure
mov ebx, SYS_SOCKET ; subfunction or "command"
mov eax, __NR_socketcall ;c.f. /usr/src/linux/net/socket.c
int 80h
cmp eax, -4096
ja exit
mov [fd_socket], eax
; and fill in connect_args, etc.
mov [connect_args], eax
mov ecx, connect_args
mov ebx, SYS_CONNECT ; subfunction or "command"
mov eax, __NR_socketcall
int 80h
cmp eax, -4096
ja exit
mov [fd_conn], eax
xor eax, eax
; call getuid()
mov eax, __NR_getuid
int 80h
cmp eax, -4096
ja exit
;mov eax, '3'
mov [id], eax
; write(sock, buf, len)
mov edx, 4 ; arg 3: max count
mov ecx, id ; arg 2: buffer
mov ebx, [fd_socket] ; arg 1: fd
mov eax, __NR_write ; sys_write
int 80h
cmp eax, -4096
ja exit
goodexit:
xor eax, eax ; success
exit:
mov ebx, eax ; exitcode
neg ebx
mov eax, __NR_exit
int 80h
;-----------------------
基本上我没有在服务器端收到任何东西。但是如果我通过取消注释mov eax线直接发送值3,那么我可以看到3来到服务器。 有人可以告诉我我做错了什么吗? 如果需要,我也可以发布服务器代码。 (它只是c中的一个基本服务器,它打印出从客户端收到的所有内容)。