linux - 将参数传递给 execve 系统调用的最简单方法

标签 linux assembly x86 nasm system-calls

据推测,将参数传递给 execve 系统调用非常简单。

在教程中,讲师说它只有一行,把它留作练习。

下面的代码执行“ls”命令。我正在尝试执行类似“ls -la”的操作。 找了又找,还是不知道在什么地方加上“-la”!

我知道它在 ecx 寄存器指向的结构中,并且必须以 null 终止。目前,ecx 包含一个到/bin/ls 的地址。参数应该是另一个地址吗? argv 是一个数组,第一个元素是“/bin/ls”...

global _start

section .text
_start:
        xor eax, eax
        push eax

        push 0x736c2f6e 
        push 0x69622f2f ; //bin/ls

        mov ebx, esp

        push eax
        mov edx, esp

        push ebx
        mov ecx, esp

        mov al, 11
        int 0x80

这是行不通的:

xor eax, eax
push eax
push 0x2a632020
push 0x736c2f6e 
push 0x69622f2f ; /bin/ls  c*
mov ecx, esp

最佳答案

您必须将-la参数保存在ecx寄存器中,并将其复制到esp寄存器(我的意思是在堆栈中)

push eax
push byte 0x61
push word 0x6c2d 
mov ecx, esp ; -la

以下是您修改后的代码:

global _start

section .text
_start:

xor eax, eax

push eax
push byte 0x61
push word 0x6c2d    
mov ecx, esp ; -la

push eax
push 0x736c2f6e
push 0x69622f2f ; //bin/ls
mov ebx, esp

push edx
push ecx
push ebx
mov ecx, esp

mov al, 11
int 0x80

代码运行良好:)

% ./list
total 4
-rwxrwxr-x 1 febri febri 512 Oct  5 07:45 list
% 

关于linux - 将参数传递给 execve 系统调用的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58244168/

相关文章:

c - waitpid 调用后如何将自己的 shell 置于前台?

linux - 无法使用 mkdir 理解 perror 的消息

汇编编码 strdup. Malloc 在共享库中调用

assembly - MIPS 跳转和分支指令范围

c - 在 C 中模拟 thiscall 以实现无需自引用的结构函数

x86 - 流水线中的软件中断会如何处理?

linux - gammu 不保存已发送的消息

c - C 中的动态加载

linux - syscall getpid,打印返回值?

assembly - 我应该为 Intel 8086 DOS 程序集使用什么代码框架?