├── Assembly ├── crawler.asm ├── create.asm ├── execute.asm ├── fork.asm ├── functions.asm ├── helloworld-args.asm ├── helloworld-input.asm ├── helloworld.asm ├── namespace.asm ├── read.asm ├── unlink.asm └── write.asm ├── Images ├── binance.jpg ├── privilege_levels.png ├── privilege_levels_linux.png └── systemcalls_linux_kernel.png ├── LICENSE └── README.md /Assembly/crawler.asm: -------------------------------------------------------------------------------- 1 | ; Crawler 2 | ; Compile with: nasm -f elf crawler.asm 3 | ; Link with (64 bit systems require elf_i386 option): ld -m elf_i386 crawler.o -o crawler 4 | ; Run with: ./crawler 5 | 6 | %include 'functions.asm' 7 | 8 | SECTION .data 9 | ; our request string 10 | request db 'GET / HTTP/1.1', 0Dh, 0Ah, 'Host: 139.162.39.66:80', 0Dh, 0Ah, 0Dh, 0Ah, 0h 11 | 12 | SECTION .bss 13 | buffer resb 1, ; variable to store response 14 | 15 | SECTION .text 16 | global _start 17 | 18 | _start: 19 | 20 | xor eax, eax ; init eax 0 21 | xor ebx, ebx ; init ebx 0 22 | xor edi, edi ; init edi 0 23 | 24 | _socket: 25 | 26 | push byte 6 ; push 6 onto the stack (IPPROTO_TCP) 27 | push byte 1 ; push 1 onto the stack (SOCK_STREAM) 28 | push byte 2 ; push 2 onto the stack (PF_INET) 29 | mov ecx, esp ; move address of arguments into ecx 30 | mov ebx, 1 ; invoke subroutine SOCKET (1) 31 | mov eax, 102 ; invoke SYS_SOCKETCALL (kernel opcode 102) 32 | int 80h ; call the kernel 33 | 34 | _connect: 35 | 36 | mov edi, eax ; move return value of SYS_SOCKETCALL into edi (file descriptor for new socket, or -1 on error) 37 | push dword 0x4227a28b ; push 139.162.39.66 onto the stack IP ADDRESS (reverse byte order) 38 | push word 0x5000 ; push 80 onto stack PORT (reverse byte order) 39 | push word 2 ; push 2 dec onto stack AF_INET 40 | mov ecx, esp ; move address of stack pointer into ecx 41 | push byte 16 ; push 16 dec onto stack (arguments length) 42 | push ecx ; push the address of arguments onto stack 43 | push edi ; push the file descriptor onto stack 44 | mov ecx, esp ; move address of arguments into ecx 45 | mov ebx, 3 ; invoke subroutine CONNECT (3) 46 | mov eax, 102 ; invoke SYS_SOCKETCALL (kernel opcode 102) 47 | int 80h ; call the kernel 48 | 49 | _write: 50 | 51 | mov edx, 43 ; move 43 dec into edx (length in bytes to write) 52 | mov ecx, request ; move address of our request variable into ecx 53 | mov ebx, edi ; move file descriptor into ebx (created socket file descriptor) 54 | mov eax, 4 ; invoke SYS_WRITE (kernel opcode 4) 55 | int 80h ; call the kernel 56 | 57 | _read: 58 | 59 | mov edx, 1 ; number of bytes to read (we will read 1 byte at a time) 60 | mov ecx, buffer ; move the memory address of our buffer variable into ecx 61 | mov ebx, edi ; move edi into ebx (created socket file descriptor) 62 | mov eax, 3 ; invoke SYS_READ (kernel opcode 3) 63 | int 80h ; call the kernel 64 | 65 | cmp eax, 0 ; if return value of SYS_READ in eax is zero, we have reached the end of the file 66 | jz _close ; jmp to _close if we have reached the end of the file (zero flag set) 67 | 68 | mov eax, buffer ; move the memory address of our buffer variable into eax for printing 69 | call sprint ; call our string printing function 70 | jmp _read ; jmp to _read 71 | 72 | _close: 73 | 74 | mov ebx, edi ; move edi into ebx (connected socket file descriptor) 75 | mov eax, 6 ; invoke SYS_CLOSE (kernel opcode 6) 76 | int 80h ; call the kernel 77 | 78 | _exit: 79 | 80 | call quit ; call our quit function -------------------------------------------------------------------------------- /Assembly/create.asm: -------------------------------------------------------------------------------- 1 | ; Create 2 | ; Compile with: nasm -f elf create.asm 3 | ; Link with (64 bit systems require elf_i386 option): ld -m elf_i386 create.o -o create 4 | ; Run with: ./create 5 | 6 | %include 'functions.asm' 7 | 8 | SECTION .data 9 | filename db 'readme.txt', ; the filename to create 10 | 11 | SECTION .text 12 | global _start 13 | 14 | _start: 15 | 16 | mov ecx, 0777 ; set all permissions to read, write, execute 17 | mov ebx, filename ; filename we will create 18 | mov eax, 8 ; invoke SYS_CREAT (kernel opcode 8) 19 | int 80h ; call the kernel 20 | 21 | call quit ; call our quit function -------------------------------------------------------------------------------- /Assembly/execute.asm: -------------------------------------------------------------------------------- 1 | ; Execute 2 | ; Compile with: nasm -f elf execute.asm 3 | ; Link with (64 bit systems require elf_i386 option): ld -m elf_i386 execute.o -o execute 4 | ; Run with: ./execute 5 | 6 | %include 'functions.asm' 7 | 8 | SECTION .data 9 | command db '/bin/echo', 0h ; command to execute 10 | arg1 db 'Hello World!', 0h 11 | arguments dd command 12 | dd arg1 ; arguments to pass to commandline (in this case just one) 13 | dd 0h ; end the struct 14 | environment dd 0h ; arguments to pass as environment variables (inthis case none) end the struct 15 | 16 | SECTION .text 17 | global _start 18 | 19 | _start: 20 | 21 | mov edx, environment ; address of environment variables 22 | mov ecx, arguments ; address of the arguments to pass to the commandline 23 | mov ebx, command ; address of the file to execute 24 | mov eax, 11 ; invoke SYS_EXECVE (kernel opcode 11) 25 | int 80h 26 | 27 | call quit ; call our quit function -------------------------------------------------------------------------------- /Assembly/fork.asm: -------------------------------------------------------------------------------- 1 | ; Fork 2 | ; Compile with: nasm -f elf fork.asm 3 | ; Link with (64 bit systems require elf_i386 option): ld -m elf_i386 fork.o -o fork 4 | ; Run with: ./fork 5 | 6 | %include 'functions.asm' 7 | 8 | SECTION .data 9 | childMsg db 'This is the child process', 0h ; a message string 10 | parentMsg db 'This is the parent process', 0h ; a message string 11 | 12 | SECTION .text 13 | global _start 14 | 15 | _start: 16 | 17 | mov eax, 2 ; invoke SYS_FORK (kernel opcode 2) 18 | int 80h 19 | 20 | cmp eax, 0 ; if eax is zero we are in the child process 21 | jz child ; jump if eax is zero to child label 22 | 23 | parent: 24 | mov eax, parentMsg ; inside our parent process move parentMsg into eax 25 | call sprintLF ; call our string printing with linefeed function 26 | 27 | call quit ; quit the parent process 28 | 29 | child: 30 | mov eax, childMsg ; inside our child process move childMsg into eax 31 | call sprintLF ; call our string printing with linefeed function 32 | 33 | call quit ; quit the child process -------------------------------------------------------------------------------- /Assembly/functions.asm: -------------------------------------------------------------------------------- 1 | ;------------------------------------------ 2 | ; void iprint(Integer number) 3 | ; Integer printing function (itoa) 4 | iprint: 5 | push eax ; preserve eax on the stack to be restored after function runs 6 | push ecx ; preserve ecx on the stack to be restored after function runs 7 | push edx ; preserve edx on the stack to be restored after function runs 8 | push esi ; preserve esi on the stack to be restored after function runs 9 | mov ecx, 0 ; counter of how many bytes we need to print in the end 10 | 11 | divideLoop: 12 | inc ecx ; count each byte to print - number of characters 13 | mov edx, 0 ; empty edx 14 | mov esi, 10 ; mov 10 into esi 15 | idiv esi ; divide eax by esi 16 | add edx, 48 ; convert edx to it's ascii representation - edx holds the remainder after a divide instruction 17 | push edx ; push edx (string representation of an intger) onto the stack 18 | cmp eax, 0 ; can the integer be divided anymore? 19 | jnz divideLoop ; jump if not zero to the label divideLoop 20 | 21 | printLoop: 22 | dec ecx ; count down each byte that we put on the stack 23 | mov eax, esp ; mov the stack pointer into eax for printing 24 | call sprint ; call our string print function 25 | pop eax ; remove last character from the stack to move esp forward 26 | cmp ecx, 0 ; have we printed all bytes we pushed onto the stack? 27 | jnz printLoop ; jump is not zero to the label printLoop 28 | 29 | pop esi ; restore esi from the value we pushed onto the stack at the start 30 | pop edx ; restore edx from the value we pushed onto the stack at the start 31 | pop ecx ; restore ecx from the value we pushed onto the stack at the start 32 | pop eax ; restore eax from the value we pushed onto the stack at the start 33 | ret 34 | 35 | 36 | ;------------------------------------------ 37 | ; void iprintLF(Integer number) 38 | ; Integer printing function with linefeed (itoa) 39 | iprintLF: 40 | call iprint ; call our integer printing function 41 | 42 | push eax ; push eax onto the stack to preserve it while we use the eax register in this function 43 | mov eax, 0Ah ; move 0Ah into eax - 0Ah is the ascii character for a linefeed 44 | push eax ; push the linefeed onto the stack so we can get the address 45 | mov eax, esp ; move the address of the current stack pointer into eax for sprint 46 | call sprint ; call our sprint function 47 | pop eax ; remove our linefeed character from the stack 48 | pop eax ; restore the original value of eax before our function was called 49 | ret 50 | 51 | 52 | ;------------------------------------------ 53 | ; int slen(String message) 54 | ; String length calculation function 55 | slen: 56 | push ebx 57 | mov ebx, eax 58 | 59 | nextchar: 60 | cmp byte [eax], 0 61 | jz finished 62 | inc eax 63 | jmp nextchar 64 | 65 | finished: 66 | sub eax, ebx 67 | pop ebx 68 | ret 69 | 70 | 71 | ;------------------------------------------ 72 | ; void sprint(String message) 73 | ; String printing function 74 | sprint: 75 | push edx 76 | push ecx 77 | push ebx 78 | push eax 79 | call slen 80 | 81 | mov edx, eax 82 | pop eax 83 | 84 | mov ecx, eax 85 | mov ebx, 1 86 | mov eax, 4 87 | int 80h 88 | 89 | pop ebx 90 | pop ecx 91 | pop edx 92 | ret 93 | 94 | 95 | ;------------------------------------------ 96 | ; void sprintLF(String message) 97 | ; String printing with line feed function 98 | sprintLF: 99 | call sprint 100 | 101 | push eax 102 | mov eax, 0AH 103 | push eax 104 | mov eax, esp 105 | call sprint 106 | pop eax 107 | pop eax 108 | ret 109 | 110 | 111 | ;------------------------------------------ 112 | ; void exit() 113 | ; Exit program and restore resources 114 | quit: 115 | mov ebx, 0 116 | mov eax, 1 117 | int 80h 118 | ret -------------------------------------------------------------------------------- /Assembly/helloworld-args.asm: -------------------------------------------------------------------------------- 1 | ; Hello World Program (Passing arguments (args) from the command line) 2 | ; Compile with: nasm -f elf helloworld-args.asm 3 | ; Link with (64 bit systems require elf_i386 option): ld -m elf_i386 helloworld-args.o -o helloworld-args 4 | ; Run with: ./helloworld-args 5 | 6 | %include 'functions.asm' 7 | 8 | SECTION .text 9 | global _start 10 | 11 | _start: 12 | 13 | pop ecx ; first value on the stack is the number of arguments 14 | 15 | nextArg: 16 | cmp ecx, 0h ; check to see if we have any arguments left 17 | jz noMoreArgs ; if zero flag is set jump to noMoreArgs label (jumping over the end of the loop) 18 | pop eax ; pop the next argument off the stack 19 | call sprintLF ; call our print with linefeed function 20 | dec ecx ; decrease ecx (number of arguments left) by 1 21 | jmp nextArg ; jump to nextArg label 22 | 23 | noMoreArgs: 24 | call quit -------------------------------------------------------------------------------- /Assembly/helloworld-input.asm: -------------------------------------------------------------------------------- 1 | ; Hello World Program (Getting input) 2 | ; Compile with: nasm -f elf helloworld-input.asm 3 | ; Link with (64 bit systems require elf_i386 option): ld -m elf_i386 helloworld-input.o -o helloworld-input 4 | ; Run with: ./helloworld-input 5 | 6 | %include 'functions.asm' 7 | 8 | SECTION .data 9 | msg1 db 'Please enter your name: ', 0h ; message string asking user for input 10 | msg2 db 'Hello, ', 0h ; message string to use after user has entered their name 11 | 12 | SECTION .bss 13 | sinput: resb 255 ; reserve a 255 byte space in memory for the users input string 14 | 15 | SECTION .text 16 | global _start 17 | 18 | _start: 19 | 20 | mov eax, msg1 21 | call sprint 22 | 23 | mov edx, 255 ; number of bytes to read 24 | mov ecx, sinput ; reserved space to store our input (known as a buffer) 25 | mov ebx, 0 ; write to the STDIN file 26 | mov eax, 3 ; invoke SYS_READ (kernel opcode 3) 27 | int 80h 28 | 29 | mov eax, msg2 30 | call sprint 31 | 32 | mov eax, sinput ; move our buffer into eax (Note: input contains a linefeed) 33 | call sprint ; call our print function 34 | 35 | call quit -------------------------------------------------------------------------------- /Assembly/helloworld.asm: -------------------------------------------------------------------------------- 1 | ; Hello World Program 2 | ; Compile with: nasm -f elf helloworld.asm 3 | ; Link with (64 bit systems require elf_i386 option): ld -m elf_i386 helloworld.o -o helloworld 4 | ; Run with: ./helloworld 5 | 6 | SECTION .data 7 | msg db 'Hello World!', 0Ah 8 | 9 | SECTION .text 10 | global _start 11 | 12 | _start: 13 | 14 | mov edx, 13 15 | mov ecx, msg 16 | mov ebx, 1 17 | mov eax, 4 18 | int 80h 19 | 20 | mov ebx, 0 ; return 0 status on exit - 'No Errors' 21 | mov eax, 1 ; invoke SYS_EXIT (kernel opcode 1) 22 | int 80h -------------------------------------------------------------------------------- /Assembly/namespace.asm: -------------------------------------------------------------------------------- 1 | ; Namespace 2 | ; Compile with: nasm -f elf namespace.asm 3 | ; Link with (64 bit systems require elf_i386 option): ld -m elf_i386 namespace.o -o namespace 4 | ; Run with: ./namespace 5 | 6 | %include 'functions.asm' 7 | 8 | SECTION .data 9 | msg1 db 'Jumping to finished label.', 0h ; a message string 10 | msg2 db 'Inside subroutine number: ', 0h ; a message string 11 | msg3 db 'Inside subroutine "finished".', 0h ; a message string 12 | 13 | SECTION .text 14 | global _start 15 | 16 | _start: 17 | 18 | subrountineOne: 19 | mov eax, msg1 ; move the address of msg1 into eax 20 | call sprintLF ; call our string printing with linefeed function 21 | jmp .finished ; jump to the local label under the subrountineOne scope 22 | 23 | .finished: 24 | mov eax, msg2 ; move the address of msg2 into eax 25 | call sprint ; call our string printing function 26 | mov eax, 1 ; move the value one into eax (for subroutine number one) 27 | call iprintLF ; call our integer printing function with linefeed function 28 | 29 | subrountineTwo: 30 | mov eax, msg1 ; move the address of msg1 into eax 31 | call sprintLF ; call our string print with linefeed function 32 | jmp .finished ; jump to the local label under the subrountineTwo scope 33 | 34 | .finished: 35 | mov eax, msg2 ; move the address of msg2 into eax 36 | call sprint ; call our string printing function 37 | mov eax, 2 ; move the value two into eax (for subroutine number two) 38 | call iprintLF ; call our integer printing function with linefeed function 39 | 40 | mov eax, msg1 ; move the address of msg1 into eax 41 | call sprintLF ; call our string printing with linefeed function 42 | jmp finished ; jump to the global label finished 43 | 44 | finished: 45 | mov eax, msg3 ; move the address of msg3 into eax 46 | call sprintLF ; call our string printing with linefeed function 47 | call quit ; call our quit function -------------------------------------------------------------------------------- /Assembly/read.asm: -------------------------------------------------------------------------------- 1 | ; Read 2 | ; Compile with: nasm -f elf read.asm 3 | ; Link with (64 bit systems require elf_i386 option): ld -m elf_i386 read.o -o read 4 | ; Run with: ./read 5 | 6 | %include 'functions.asm' 7 | 8 | SECTION .data 9 | filename db 'readme.txt', 0h ; the filename to create 10 | contents db 'Hello world!', 0h ; the contents to write 11 | 12 | SECTION .bss 13 | fileContents resb 255, ; variable to store file contents 14 | 15 | SECTION .text 16 | global _start 17 | 18 | _start: 19 | 20 | mov ecx, 0777 21 | mov ebx, filename 22 | mov eax, 8 23 | int 80h 24 | 25 | mov edx, 12 26 | mov ecx, contents 27 | mov ebx, eax 28 | mov eax, 4 29 | int 80h 30 | 31 | mov ecx, 0 32 | mov ebx, filename 33 | mov eax, 5 34 | int 80h 35 | 36 | mov edx, 12 ; number of bytes to read - one for each letter of the file contents 37 | mov ecx, fileContents ; move the memory address of our file contents variable into ecx 38 | mov ebx, eax ; move the opened file descriptor into EBX 39 | mov eax, 3 ; invoke SYS_READ (kernel opcode 3) 40 | int 80h ; call the kernel 41 | 42 | mov eax, fileContents ; move the memory address of our file contents variable into eax for printing 43 | call sprintLF ; call our string printing function 44 | 45 | call quit ; call our quit function -------------------------------------------------------------------------------- /Assembly/unlink.asm: -------------------------------------------------------------------------------- 1 | ; Unlink 2 | ; Compile with: nasm -f elf unlink.asm 3 | ; Link with (64 bit systems require elf_i386 option): ld -m elf_i386 unlink.o -o unlink 4 | ; Run with: ./unlink 5 | 6 | %include 'functions.asm' 7 | 8 | SECTION .data 9 | filename db 'readme.txt', 0h ; the filename to delete 10 | 11 | SECTION .text 12 | global _start 13 | 14 | _start: 15 | 16 | mov ebx, filename ; filename we will delete 17 | mov eax, 10 ; invoke SYS_UNLINK (kernel opcode 10) 18 | int 80h ; call the kernel 19 | 20 | call quit ; call our quit function -------------------------------------------------------------------------------- /Assembly/write.asm: -------------------------------------------------------------------------------- 1 | ; Write 2 | ; Compile with: nasm -f elf write.asm 3 | ; Link with (64 bit systems require elf_i386 option): ld -m elf_i386 write.o -o write 4 | ; Run with: ./write 5 | 6 | %include 'functions.asm' 7 | 8 | SECTION .data 9 | filename db 'readme.txt', 0h ; the filename to create 10 | contents db 'Hello world!', 0h ; the contents to write 11 | 12 | SECTION .text 13 | global _start 14 | 15 | _start: 16 | 17 | mov ecx, 0777 18 | mov ebx, filename 19 | mov eax, 8 20 | int 80h 21 | 22 | mov edx, 12 ; number of bytes to write - one for each letter of our contents string 23 | mov ecx, contents ; move the memory address of our contents string into ecx 24 | mov ebx, eax ; move the file descriptor of the file we created into ebx 25 | mov eax, 4 ; invoke SYS_WRITE (kernel opcode 4) 26 | int 80h ; call the kernel 27 | 28 | call quit ; call our quit function -------------------------------------------------------------------------------- /Images/binance.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IDouble/NASM-Assembly-Linux-Notes/6e7d5870ccd1f4d700f53ba44ddb3f45d5254161/Images/binance.jpg -------------------------------------------------------------------------------- /Images/privilege_levels.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IDouble/NASM-Assembly-Linux-Notes/6e7d5870ccd1f4d700f53ba44ddb3f45d5254161/Images/privilege_levels.png -------------------------------------------------------------------------------- /Images/privilege_levels_linux.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IDouble/NASM-Assembly-Linux-Notes/6e7d5870ccd1f4d700f53ba44ddb3f45d5254161/Images/privilege_levels_linux.png -------------------------------------------------------------------------------- /Images/systemcalls_linux_kernel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IDouble/NASM-Assembly-Linux-Notes/6e7d5870ccd1f4d700f53ba44ddb3f45d5254161/Images/systemcalls_linux_kernel.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Alpay Yildirim 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | --------------------------------------------------------------------------------