All pastes #462290 Raw Edit

Stuff

public text v1 · immutable
#462290 ·published 2007-04-28 04:53 UTC
rendered paste body
; irc client ;D
; tgo@anomalous-security.org

; TODO: 
;    ping/pong reply
;    version reply    

[bits 32]

global _start

[section .data]

	stdin            dd 0
	stdout           dd 1
	sys_socketcall   dd 102
	sys_socket       db 1
	sys_connect      dd 3
	sys_read         dd 3
	sys_write        dd 4
	sys_select       dd 142
	fd               dd 0
	maxlen           dd 510
	msg              times 510 db 0 ; irc messages are 510 + \r\n at most
	nick             db "NICK testing",0xa
	user             db 'USER test 0 0 :test',0xa
	server           dd 0x77841955 ; irc.anomalous-security.org
	port             dw 0xb1a           ; 6667
	channel          db "JOIN #test",0xa
	fdset            times 10 dd 0  

[section .text]

	_start:

	socket:
		push dword ebx
		add byte  bl,1
		push dword ebx
		mov byte dl,2
		push dword edx
		mov dword ecx,esp
		mov dword eax,[sys_socketcall]	
		int 0x80
	
		cmp byte al,1
		jl near quit

		mov dword [fd],eax		
	
	connect: 
		push dword [server]
		push word [port]
		push word 2
		mov dword ecx,esp

		push byte 0x10
		push dword ecx
		push dword [fd]
		mov dword ecx,esp		
		mov dword ebx,[sys_connect]
		mov dword eax,[sys_socketcall]
		int 0x80

		cmp eax,0
		jne near quit

	signin:
		mov ebx,[fd]

		mov dword ecx,user
		call write

		mov dword ecx,nick
		call write

		mov dword ecx,channel
		call write

		sub dword esp,4		; no idea dont ask why - gdb it and find out the problem!

	whileone:

		call set_fdset

		call select		
		test eax,eax
		je whileone

		mov dword ecx,[stdin]
		call fd_isset
		je notreadin

		mov ebx,[stdin]
		mov ecx,msg
		
	; read
	; on call:
	; ecx points to buffer to fill with what is read
	; ebx to point to fd

	read:
		mov dword edx,[maxlen]
		mov dword eax,[sys_read]
		int 0x80
		test eax,eax
		jl quit

		mov dword edx,eax
		mov dword ebx,[fd]
		mov dword ecx,msg

	; writelen
	; excepts ebx,ecx,edx to be set to their proper values

	writelen:
		mov dword eax,[sys_write]
		int 0x80
		cmp dword eax,0
		jl close
		retn

	notreadin:
		mov dword ecx,[fd]
		call fd_isset

	readsock:
		mov ebx,[fd]
		mov ecx,msg
		call read
	
		mov dword edx,eax
		mov dword ebx,[stdout]
		mov dword ecx,msg
		call writelen
		retn

		je whileone			

	close:
		mov  dword ebx,[fd]
		mov  dword eax,6
		int  0x80

	quit:
		xor dword eax,eax
		inc  byte  al
		int 0x80

	; write
	; expects ecx to point to string, ebx to file desc
	; how much was written is in eax on ret

	write:
		call strlen  ; sets edx to length
		mov dword eax,[sys_write]
		int 0x80	
		test eax,eax
		jl close
		retn

	; strlen	
	; call:
	;  ecx points to string
	; return:
	;  edx holds length since most syscalls use edx for the length

	strlen:
		push dword eax
		push dword ecx
		mov dword edi,ecx
		mov dword ecx,[maxlen]
		xor dword eax,eax
		mov byte  al,0xa
		cld
		repne scasb
		mov dword edx,[maxlen]
		sub dword edx,ecx
		pop dword ecx
		pop dword eax
		retn

	select:
		mov  dword eax,[sys_select]
		mov  dword ebx,10
		mov  dword ecx,fdset
		xor  dword edx,edx
		mov  dword esi,edx
		push dword 0
		push dword 1
		mov dword edi,esp
		int 0x80
		add dword esp,8
		test eax,eax
		jl close
		retn

	; set_fdset
	; sets master fd_set of stdin and remote socket

	set_fdset:

		mov dword [fdset],0  ; FD_ZERO

		mov dword ecx,[stdin]
		call fd_set

		mov dword ecx,[fd]

	; fd_set
	; takes number to set in ecx
	; |= (1<<(n)))

	fd_set:
		mov dword ebx,1		
		shl dword ebx,cl
		or dword [fdset],ebx		
		retn

	; fd_isset
	; checks if the fd (ecx) is set in fdset

	fd_isset:
		bt dword [fdset],ecx
		setb byte cl
		test cl,cl
		retn