assembly 正在尝试在NASM中执行bash脚本

tv6aics1  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(121)

你好,我是一个nasm的初学者。我试图写一个程序来执行一个脚本,它需要一个参数,使用/bin/bash。

SECTION .data
      command db '/bin/bash', 0
      script  db 'path/to/script', 0
      script_arg db 'my_arg', 0
      arguments dd command
                dd script  ; arguments to pass to commandline, in this case just the path to the script
                dd script_arg
                dd  0

SECTION .text
global  _start

_start:

    mov edx, 0 ; no environment variables are being used
    mov ecx, arguments ; array of pointers has to be passed
    mov ebx, command    ; bash
    mov eax, 11   ; invoke SYS_EXECVE 
    int     80h

上面的代码只是用bash执行脚本,但没有向脚本本身添加任何参数。我试图将其作为附加参数传递,但没有任何作用。如果我将参数添加到脚本字符串的路径(path/to/script arg 1),它会破坏终端(颜色主题设置为白色文本),除此之外什么也不做。
还有,改变参数指针数组内容的最简单的方法是什么?我如何在.bss部分定义它,并在程序运行时改变它的内容?至少有一个关于这方面的文档的点会很好...

qqrboqgw

qqrboqgw1#

当我输入run-bash.asm时:

SECTION .data
      command db '/bin/bash', 0
      script  db './test.sh', 0
      script_arg db 'my_arg', 0
      arguments dd command
                dd script  ; arguments to pass to commandline, in this case just the path to the script
                dd script_arg
                dd  0

SECTION .text
global  _start

_start:

    mov edx, 0 ; no environment variables are being used
    mov ecx, arguments ; array of pointers has to be passed
    mov ebx, command    ; bash
    mov eax, 11   ; invoke SYS_EXECVE 
    int     80h

并在www.example.com中输入test.sh:

#!/usr/bin/env bash
  
echo "First argument is : $1"

运行它的方式:

nasm -f elf run-bash.asm
ld -m elf_i386 run-bash.o -o run-bash
chmod +x run-bash
./run-bash
# Output : 
# First argument is : my_arg

相关问题