我开始在asm(NASM)中编程网络程序,技术上,accept函数阻止程序(被动套接字)。好吧,在我的程序中,我执行程序并完成程序。我已经测试过将积压设置为1(监听功能),但这不是问题......会发生什么?
BITS 32
section .text
global _start
_start:
; Create the socket file descriptor
; int socket(int domain, int type, int protocol);
mov eax, 102 ; __NR_socketcall
mov ebx, 1 ; socketcall type (socket)
; socket parameters
push 0 ; IPPROTO_TCP
push 1 ; SOCK_STREAM
push 2 ; AF_INET
int 0x80 ; socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
mov edx, eax ; edx = socketfd
; Bind the socket with an address type
; int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
mov eax, 102 ; __NR_socketcall
mov ebx, 2 ; socketcall type (bind)
; build the sockaddr_in struct
push 0 ; INADDR_ANY
push WORD 0x0457 ; port 1111
push WORD 2 ; AF_INET
mov ecx, esp ; struct ptr
; bind parameters
push 16 ; sockaddr struct size = sizeof(struct sockaddr) = 16
push ecx ; sockaddr_in struct ptr
push edx ; socket fd
int 0x80 ; bind(sockfd, {AF_INET, 11111, INADDR_ANY}, 16)
; Prepare to listen the incoming connection (passive socket)
; int listen(int sockfd, int backlog);
mov eax, 102 ; __NR_socketcall
mov ebx, 4 ; socketcall type (listen)
; listen parameters
push 0 ; nº connections in the waiting queue (0 = accept and we haven't queue)
push edx ; socket fd
int 0x80 ; listen(sockfd, 0);
; Accept the incoming connection
; int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
mov eax, 102 ; __NR_socketcall
mov ebx, 5 ; socketcall type (accept)
; accept parameters
push 0
push 0
push edx ; socket fd
int 0x80 ; accept(sockfd, NULL, NULL)
; Exit
; int exit(int status)
mov eax, 1 ; __NR_exit
mov ebx, 0 ; exit code
int 0x80
答案 0 :(得分:2)
在最后一次推送每个参数推送后,你错过了mov ecx, esp
,以及htons
'端口号。您的代码的固定版本应如下所示:
BITS 32
section .text
global _start
_start:
; Create the socket file descriptor
; int socket(int domain, int type, int protocol);
mov eax, 102 ; __NR_socketcall
mov ebx, 1 ; socketcall type (socket)
; socket parameters
push 6 ; IPPROTO_TCP
push 1 ; SOCK_STREAM
push 2 ; AF_INET
mov ecx, esp ; <<== uargs* in ecx
int 0x80 ; socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
mov edx, eax ; edx = socketfd
; Bind the socket with an address type
; int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
mov eax, 102 ; __NR_socketcall
mov ebx, 2 ; socketcall type (bind)
; build the sockaddr_in struct
push 0 ; INADDR_ANY
push WORD 0x5704 ; port 1111 == htons(1111)
push WORD 2 ; AF_INET
mov ecx, esp ; struct ptr
; bind parameters
push 16 ; sockaddr struct size = sizeof(struct sockaddr) = 16
push ecx ; sockaddr_in struct ptr
push edx ; socket fd
mov ecx, esp ; <<== uargs* in ecx
int 0x80 ; bind(sockfd, {AF_INET, 11111, INADDR_ANY}, 16)
; Prepare to listen the incoming connection (passive socket)
; int listen(int sockfd, int backlog);
mov eax, 102 ; __NR_socketcall
mov ebx, 4 ; socketcall type (listen)
; listen parameters
push 0 ; nº connections in the waiting queue (0 = accept and we haven't queue)
push edx ; socket fd
mov ecx, esp ; <<== uargs* in ecx
int 0x80 ; listen(sockfd, 0);
; Accept the incoming connection
; int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
mov eax, 102 ; __NR_socketcall
mov ebx, 5 ; socketcall type (accept)
; accept parameters
push 0
push 0
push edx ; socket fd
mov ecx, esp ; struct ptr
int 0x80 ; accept(sockfd, NULL, NULL)
; Exit
; int exit(int status)
mov eax, 1 ; __NR_exit
mov ebx, 0 ; exit code
int 0x80
在这种情况下,在程序中执行strace
以验证您是否在系统调用中看到正在处理的正确参数非常重要。
如果我们strace
我们的原始程序:
socket(PF_UNSPEC, 0, 0) = -1 EFAULT (Bad address)
bind(1459879938, NULL, 2) = -1 EBADF (Bad file descriptor)
listen(1459879938, 0) = -1 EBADF (Bad file descriptor)
accept(1459879938, 0, 0x2) = -1 EBADF (Bad file descriptor)
所有这些看起来都非常糟糕。
如果您查看compat_sys_socketcall
的来源,则会显示:
asmlinkage long compat_sys_socketcall(int call, u32 __user *args)
表示EBX是调用,ECX指向其余参数。