I have had problems implementing user output into and argument with my below code.
An example of BASH showing, a similar example function of what i would like to implement in NASM.
read -p "Enter the group you would like to create: " newgroup groupadd $newgroup
The reason why I am creating this code is to show a comparison of different languages, used within Linux, This is one part of my research project. So i know, this can be done in other languages e.g. Python BASH and C etc. Before anyone asks.
I believe I’m somewhere on the right lines, here is the code below.
;---------------------------NSAM---------------------- section .data ; initialized data, can be var's userg: db "Type in the user group to add: ",10,0 ; 1st string, 10 is used to drop down a line. 0 is used to end the line. userg_L: equ $-userg ; string length, automatically updates how long the string is, so you don't have to type 10 etc. every time its changed respns: db "groupadded: ",10,0 ; 2nd string, 10 is used to drop down a line. 0 is used to end the line. respns_L equ $-respns ; 2nd string length, automatically updates how long the string is, so you don't have to type 10 etc. every time its changed command: db '/bin/groupadd', 0h ; command to execute. arg1: db userg, 0h ; Shows all users on the system. argu: dd command dd arg1 ; arguments passed to the command line. dd 0h ; struct. enviro: dd 0h ; no arguments are need for the environment variables. ;======================================================== section .bss ; uninitialised data, also can be var's userg_V resb 255 ; reserves 255 bytes of data. ;======================================================== section .text ; Asm core code. any of the sections can be in any order. global _start: ; Makes _start the global function _start: ; Makes _start the global function mov eax, 0x04 ; store the system call code = 4 e.g. = kernel function (sys_write) mov ebx, 0x01 ; 1 is the code to where to write the Asm out to. 1= The terminal mov ecx, userg ; contains the label of the string. mov edx, userg_L ; this is the length of the string. int 80h ; calls the Linux kernel mov eax, 0x03 ; 3 is the code to read user input. e.g. kernel function (sys_read) mov ebx, 0x00 ; This is the error code memory block. mov ecx, userg_V ; reserves 255 bytes of data name of string. mov edx, 255 ; reserves 255 bytes of data. int 80h ; calls the Linux kernel. mov eax, 0x04 ; store the system call code = 4 e.g. = kernel function (sys_write) mov ebx, 0x01 ; 1 is the code to where to write the Asm out to. 1= The terminal mov ecx, respns ; calls the var mov edx, respns_L ; calls the userg var length int 80h ; calls the Linux kernel. mov eax, 0x04 ; store the system call code = 4 e.g. = kernel function (sys_write) mov ebx, 0x01 ; 1 is the code to where to write the Asm out to. 1= The terminal mov ecx, userg_V ; reserves 255 bytes of data name of string. mov edx, 255 ; reserves 255 bytes of data. int 80h ; calls the Linux kernel. mov edx, enviro ; Environment variables mov ecx, argu ; Arguments to pass to the command line mov ebx, command ; address to execute mov eax, 11 ; SYS_EXECVE kernel opcode (11) int 80h call exit ; Calls the exit function. ;---------------------------------ASM exit code below!------------------------------------------- exit: mov eax, 0x01 ; [EAX] is 1 this is the exit code! e.g. = kernel function (sys_exit) mov ebx, 0x00 ; This is the error code memory block. int 80h ; calls the Linux kernel.
The error is:
ld -s -o myasm myasm.o myasm.o: In function `arg1': myasm.asm:(.data+0x3e): relocation truncated to fit: R_386_8 against `.data'
This error was fixed using dd but It will not add the user group to the system.
The below code works to show all users but will not take user input as the argument. This is an example of working command code but without user input. So how would one implement user input, with the command line code.
command: db '/bin/cat', 0h ; command to execute. arg1: db '/etc/passwd', 0h ; Shows all users on the system.
Advertisement
Answer
Here is a fixed version. Note that groupadd
might not be in /bin
, and you should probably print an error message if exec
fails.
;---------------------------NASM---------------------- section .data ; initialized data, can be var's userg: db "Type in the user group to add: ",10 ; 1st string, 10 is used to drop down a line. userg_L: equ $-userg ; string length, automatically updates how long the string is, so you don't have to type 10 etc. every time its changed respns: db "groupadded: ",10 ; 2nd string, 10 is used to drop down a line. 0 is used to end the line. respns_L equ $-respns ; 2nd string length, automatically updates how long the string is, so you don't have to type 10 etc. every time its changed command: db '/bin/groupadd', 0h ; command to execute. argu: dd command dd userg_V ; arguments passed to the command line. dd 0h ; struct. enviro: dd 0h ; no arguments are need for the environment variables. ;======================================================== section .bss ; uninitialised data, also can be var's userg_V resb 255 ; reserves 255 bytes of data. ;======================================================== section .text ; Asm core code. any of the sections can be in any order. global _start: ; Makes _start the global function _start: ; Makes _start the global function mov eax, 0x04 ; store the system call code = 4 e.g. = kernel function (sys_write) mov ebx, 0x01 ; 1 is the code to where to write the Asm out to. 1= The terminal mov ecx, userg ; contains the label of the string. mov edx, userg_L ; this is the length of the string. int 80h ; calls the Linux kernel mov eax, 0x03 ; 3 is the code to read user input. e.g. kernel function (sys_read) mov ebx, 0x00 ; This is the error code memory block. mov ecx, userg_V ; reserves 255 bytes of data name of string. mov edx, 255 ; reserves 255 bytes of data. int 80h ; calls the Linux kernel. push eax ; save length of input mov eax, 0x04 ; store the system call code = 4 e.g. = kernel function (sys_write) mov ebx, 0x01 ; 1 is the code to where to write the Asm out to. 1= The terminal mov ecx, respns ; calls the var mov edx, respns_L ; calls the userg var length int 80h ; calls the Linux kernel. mov eax, 0x04 ; store the system call code = 4 e.g. = kernel function (sys_write) mov ebx, 0x01 ; 1 is the code to where to write the Asm out to. 1= The terminal mov ecx, userg_V ; the input text mov edx, [esp] ; the length of the input text int 80h ; calls the Linux kernel. pop eax ; get back input length mov [eax + userg_V - 1], byte 0 ; chop off trailing newline mov edx, enviro ; Environment variables mov ecx, argu ; Arguments to pass to the command line mov ebx, command ; address to execute mov eax, 11 ; SYS_EXECVE kernel opcode (11) int 80h ;---------------------------------ASM exit code below!------------------------------------------- exit: mov eax, 0x01 ; [EAX] is 1 this is the exit code! e.g. = kernel function (sys_exit) mov ebx, 0x00 ; This is the error code memory block. int 80h ; calls the Linux kernel.