Nytro Posted November 17, 2010 Report Posted November 17, 2010 [NASM] Linux Sockets Author: DemonEmporer:Right so, It's been a while since I actually contributed anything. Declans Wind0ze hatin'(j/k j/k lol) got me thinkin' about somethin' I could do in linux. And reading through a few pages I noticed a lack of linux based ASM or NASM for that matter. You may find this educational, silly or totally random or all 3. Either way. So I thought in my spare time, I'd start doing some NASM linux examples(They are linux specific, not *nix. BSD has a different interpret style). Hello worlds aside I thought I'd do something slightly more interesting and throw in a socket connection.%assign SOCK_STREAM 1%assign AF_INET 2%assign SYS_socketcall 102%assign SYS_SOCKET 1%assign SYS_CONNECT 3%assign SYS_SEND 9%assign SYS_RECV 10section .text global _start;--------------------------------------------------;Functions to make things easier. :];--------------------------------------------------_socket: mov [cArray+0], dword AF_INET mov [cArray+4], dword SOCK_STREAM mov [cArray+8], dword 0 mov eax, SYS_socketcall mov ebx, SYS_SOCKET mov ecx, cArray int 0x80 ret_connect: call _socket mov dword [sock], eax mov dx, si mov byte [edi+3], dl mov byte [edi+2], dh mov [cArray+0], eax ;sock; mov [cArray+4], edi ;&sockaddr_in; mov edx, 16 mov [cArray+8], edx ;sizeof(sockaddr_in); mov eax, SYS_socketcall mov ebx, SYS_CONNECT mov ecx, cArray int 0x80 ret_send: mov edx, [sock] mov [sArray+0],edx mov [sArray+4],eax mov [sArray+8],ecx mov [sArray+12], dword 0 mov eax, SYS_socketcall mov ebx, SYS_SEND mov ecx, sArray int 0x80 ret_exit: mov eax, 1 int 0x80_print: mov ebx, 1 mov eax, 4 int 0x80 ret ;--------------------------------------------------;Main code body;--------------------------------------------------_start: mov esi, szIp mov edi, sockaddr_in xor eax,eax xor ecx,ecx xor edx,edx .cc: xor ebx,ebx .c: lodsb inc edx sub al,'0' jb .next imul ebx,byte 10 add ebx,eax jmp short .c .next: mov [edi+ecx+4],bl inc ecx cmp ecx,byte 4 jne .cc mov word [edi], AF_INET mov esi, szPort xor eax,eax xor ebx,ebx .nextstr1: lodsb test al,al jz .ret1 sub al,'0' imul ebx,10 add ebx,eax jmp .nextstr1 .ret1: xchg ebx,eax mov [sport], eax mov si, [sport] call _connect cmp eax, 0 jnz short _fail mov eax, msg mov ecx, msglen call _send call _exit_fail: mov edx, cerrlen mov ecx, cerrmsg call _print call _exit_recverr: call _exit_dced: call _exitsection .datacerrmsg db 'failed to connect ',0xacerrlen equ $-cerrmsgmsg db 'DIE DIE DIE!',0xamsglen equ $-msgszIp db '127.0.0.1',0szPort db '256',0section .bsssock resd 1;general 'array' for syscall_socketcall argument arg.cArray resd 1 resd 1 resd 1 resd 1;send 'array'.sArray resd 1 resd 1 resd 1 resd 1;duh?sockaddr_in resb 16;..sport resb 2buff resb 1024Assemble:nasm -o socket.o -f elf32 -g socket.asmLink:ld -o socket socket.oIt uses 0 external libraries, hence the ld link line. Next time, we might do something with GTK(QT has a CPP interface and is more annoying to call from ASM XD). If you have any questions, feel free to ask and I'll answer as best I can. Quote