BASH的一个例子,显示了我想在NASM中实现的类似功能。
read -p "Enter the group you would like to create: " newgroup
groupadd $newgroup
我创建此代码的原因是为了显示在Linux中使用的不同语言的比较,这是我的研究项目的一部分。所以我知道,这可以用其他语言来完成,例如Python BASH和C等。在任何人问之前。
我相信我在正确的位置,这是下面的代码。
;---------------------------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.
错误是:
ld -s -o myasm myasm.o
myasm.o: In function `arg1':
myasm.asm:(.data+0x3e): relocation truncated to fit: R_386_8 against `.data'
使用dd修复了此错误,但不会将用户组添加到系统中。
以下代码用于显示所有用户,但不会将用户输入作为参数。这是工作命令代码但没有用户输入的示例。那么如何使用命令行代码实现用户输入。
command: db '/bin/cat', 0h ; command to execute.
arg1: db '/etc/passwd', 0h ; Shows all users on the system.
答案 0 :(得分:1)
这是一个固定版本。请注意,groupadd
可能不在/bin
中,如果exec
失败,您应该打印错误消息。
;---------------------------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.