Skip to content

Commit 5e90cd8

Browse files
committed
Socket programming.
1 parent e0a0ec3 commit 5e90cd8

File tree

5 files changed

+205
-3
lines changed

5 files changed

+205
-3
lines changed

AES-Encryption.asm

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
%include "IO.inc" ; Custom Written IO library
55
%include "Encryption.inc" ; Encryption functions
66
%include "Decryption.inc" ; Decryption functions
7+
%include "Socket.inc" ; Socket functions
78
global _start
89

910
section .data
@@ -12,6 +13,18 @@ key db 0x2b, 0x28, 0xab, 0x09, 0x7e, 0xae, 0xf7, 0xcf, 0x15, 0xd2, 0x15, 0x4f,
1213

1314
section .text
1415
_start:
16+
; mov eax, [esp+8]
17+
; call SetSocketFromArg
18+
; mov esi, prompt
19+
; mov ecx, 30
20+
; call WriteString
21+
; mov bx, [socket]
22+
; xchg bl, bh
23+
; movzx eax, bx
24+
; call WriteDec
25+
; call WriteLine
26+
27+
; call InitialiseServer
1528

1629
mov esi, message
1730
mov edi, key

AuxillaryFunctions.inc

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,36 @@ XOR4x4Matrices:
1616
loop ._xor
1717
pop ecx
1818
pop eax
19-
ret
19+
ret
20+
21+
; Takes:
22+
; eax: Hex Value
23+
; Returns:
24+
; edx: Decimal Value with the same digits
25+
GetDecimalValueFromHex:
26+
xor edx, edx
27+
xor ebx, ebx
28+
mov ebx, 1
29+
.cont:
30+
mov cl, al
31+
and cl, 0x0F
32+
push eax
33+
push edx
34+
movzx eax, cl
35+
mul ebx
36+
pop edx
37+
add edx, eax
38+
pop eax
39+
shr eax, 4
40+
push eax
41+
xchg eax, ebx
42+
mov ebx, 10
43+
push edx
44+
mul ebx
45+
pop edx
46+
xchg eax, ebx
47+
pop eax
48+
cmp eax, 0
49+
jne .cont
50+
ret
51+

IO.inc

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,48 @@ Print4x4Matrix:
168168
call WriteChar
169169
.done:
170170
loop .print
171-
ret
171+
ret
172+
173+
sprintLF:
174+
call sprint
175+
push eax
176+
mov eax, 0xA
177+
push eax
178+
mov eax, esp
179+
call sprint
180+
pop eax
181+
pop eax
182+
ret
183+
184+
sprint:
185+
push edx
186+
push ecx
187+
push ebx
188+
push eax
189+
call strlen
190+
191+
mov edx, eax
192+
pop eax
193+
194+
mov ecx, eax
195+
mov ebx, 1
196+
mov eax, 4
197+
int 80h
198+
199+
pop ebx
200+
pop ecx
201+
pop edx
202+
ret
203+
204+
strlen:
205+
push ebx
206+
mov ebx, eax
207+
.nextchar:
208+
cmp BYTE [eax], 0
209+
jz .finished
210+
inc eax
211+
jmp .nextchar
212+
.finished:
213+
sub eax, ebx
214+
pop ebx
215+
ret

Socket.inc

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
section .data
2+
response db 'HTTP/1.1 200 OK', 0Dh, 0Ah, 'Content-Type: text/html', 0Dh, 0Ah, 'Content-Length: 16', 0Dh, 0Ah, 0Dh, 0Ah, 'ABCDABCDABCDABCD', 0Dh, 0Ah, 0h
3+
prompt db 'Running on localhost at port: '
4+
section .bss
5+
buffer resb 255, ; variable to store request headers
6+
socket resw 0 ; variable to store the socket number in hex, reverse bytes
7+
8+
section .text
9+
InitialiseServer:
10+
xor eax, eax
11+
xor ebx, ebx
12+
xor edi, edi
13+
xor esi, esi
14+
;Initiliase the socket
15+
push BYTE 6 ; push 6 onto the stack (IPPROTO_TCP)
16+
push BYTE 1 ; push 1 onto the stack (SOCK_STREAM)
17+
push BYTE 2 ; push 2 onto the stack (PF_INET)
18+
mov ecx, esp ; move address of arguments into ecx
19+
mov ebx, 1 ; invoke subroutine SOCKET (1)
20+
mov eax, 102 ; invoke SYS_SOCKETCALL (kernel opcode 102)
21+
int 80h ; call the kernel
22+
23+
;Bing the the server to the socket at localhost
24+
mov edi, eax ; move return value of SYS_SOCKETCALL into edi (file descriptor for new socket, or -1 on error)
25+
push DWORD 0x00000000 ; move 0 dec onto the stack IP ADDRESS
26+
push WORD [socket] ; move 9001 dec onto stack PORT
27+
push WORD 2 ; move 2 dec onto stack AF_INET
28+
mov ecx, esp ; move address of stack pointer into ecx
29+
push BYTE 16 ; move 16 dec onto stack (arguments length)
30+
push ecx ; push the address of arguments onto stack
31+
push edi ; push the file descriptor onto stack
32+
mov ecx, esp ; move address of arguments into ecx
33+
mov ebx, 2 ; invoke subroutine BIND (2)
34+
mov eax, 102 ; invoke SYS_SOCKETCALL (kernel opcode 102)
35+
int 80h ; call the kernel
36+
37+
;Listen on the port for an encoming request
38+
push BYTE 1 ; move 1 onto stack (max queue length argument)
39+
push edi ; push the file descriptor onto stack
40+
mov ecx, esp ; move address of arguments into ecx
41+
mov ebx, 4 ; invoke subroutine LISTEN (4)
42+
mov eax, 102 ; invoke SYS_SOCKETCALL (kernel opcode 102)
43+
int 80h ; call the kernel
44+
45+
;Accept an incoming request
46+
.accept:
47+
push BYTE 0 ; push 0 dec onto stack (address length argument)
48+
push BYTE 0 ; push 0 dec onto stack (address argument)
49+
push edi ; push the file descriptor onto stack
50+
mov ecx, esp ; move address of arguments into ecx
51+
mov ebx, 5 ; invoke subroutine ACCEPT (5)
52+
mov eax, 102 ; invoke SYS_SOCKETCALL (kernel opcode 102)
53+
int 80h ; call the kernel
54+
55+
;Fork the server into 2 child processes, one to read the buffer, one to accept the request
56+
mov esi, eax ; move return value of SYS_SOCKETCALL into esi (file descriptor for accepted socket, or -1 on error)
57+
mov eax, 2 ; invoke SYS_FORK (kernel opcode 2)
58+
int 80h ; call the kernel
59+
60+
cmp eax, 0 ; if return value of SYS_FORK in eax is zero we are in the child process
61+
jz .read ; jmp in child process to _read
62+
63+
jmp .accept ; jmp in parent process to _accept
64+
65+
;Read the sent request
66+
.read:
67+
mov edx, 255 ; number of bytes to read (we will only read the first 255 bytes for simplicity)
68+
mov ecx, buffer ; move the memory address of our buffer variable into ecx
69+
mov ebx, esi ; move esi into ebx (accepted socket file descriptor)
70+
mov eax, 3 ; invoke SYS_READ (kernel opcode 3)
71+
int 80h ; call the kernel
72+
73+
mov eax, buffer ; move the memory address of our buffer variable into eax for printing
74+
call sprintLF ; call our string printing function
75+
76+
;Send the value of the request to the socket
77+
mov edx, 80 ; move 80 dec into edx (length in bytes to write)
78+
mov ecx, response ; move address of our response variable into ecx
79+
mov ebx, esi ; move file descriptor into ebx (accepted socket id)
80+
mov eax, 4 ; invoke SYS_WRITE (kernel opcode 4)
81+
int 80h ; call the kernel
82+
83+
;Close the socket
84+
mov ebx, esi ; move esi into ebx (accepted socket file descriptor)
85+
mov eax, 6 ; invoke SYS_CLOSE (kernel opcode 6)
86+
int 80h ; call the kernel
87+
ret
88+
89+
; Takes:
90+
; eax: address of argv[1]
91+
; Sets the value of the socket varibale to argv[1] in reverse byte order
92+
SetSocketFromArg:
93+
mov eax, [eax]
94+
push eax
95+
sub al, 0x30
96+
sub ah, 0x30
97+
shl al, 4
98+
add al, ah
99+
movzx bx, al
100+
pop eax
101+
shr eax, 16
102+
sub al, 0x30
103+
sub ah, 0x30
104+
shl al, 4
105+
add al, ah
106+
shl bx, 8
107+
and ax, 0xFF
108+
add bx, ax
109+
movzx eax, bx
110+
call GetDecimalValueFromHex
111+
xchg dh, dl
112+
mov [socket], dx
113+
ret

makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
AES-Encryption: AES-Encryption.asm SBOX.inc IO.inc Substitution.inc Rotation.inc ColumnMixing.inc RoundKey.inc Encryption.inc SBOXInverse.inc Decryption.inc AuxillaryFunctions.inc
1+
AES-Encryption: AES-Encryption.asm SBOX.inc IO.inc Substitution.inc Rotation.inc ColumnMixing.inc RoundKey.inc Encryption.inc SBOXInverse.inc Decryption.inc AuxillaryFunctions.inc Socket.inc
22
nasm -f elf AES-Encryption.asm
33
ld -m elf_i386 AES-Encryption.o -o AES-Encryption

0 commit comments

Comments
 (0)