Jump to content
Nytro

[NASM] Linux Sockets

Recommended Posts

[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. :P

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 10

section .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 _exit

section .data
cerrmsg db 'failed to connect :(',0xa
cerrlen equ $-cerrmsg
msg db 'DIE DIE DIE!',0xa
msglen equ $-msg

szIp db '127.0.0.1',0
szPort db '256',0

section .bss
sock 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 2
buff resb 1024

Assemble:

nasm -o socket.o -f elf32 -g socket.asm

Link:

ld -o socket socket.o

It 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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...