你可以把argv
数组入栈并将其地址加载到rsi
。第一个成员是argv
是指向程序名称的指针,因此我们可以使用加载到的相同地址rdi
.
xor edx, edx ; Load NULL to be used both as the third
; parameter to execve as well as
; to push 0 onto the stack later.
push "-aal" ; Put second argument string onto the stack.
mov rax, rsp ; Load the address of the second argument.
mov rcx, "/bin//ls" ; Load the file name string
push rdx ; and place a null character
push rcx ; and the string onto the stack.
mov rdi, rsp ; Load the address of "/bin//ls". This is
; used as both the first member of argv
; and as the first parameter to execve.
; Now create argv.
push rdx ; argv must be terminated by a NULL pointer.
push rax ; Second arg is a pointer to "-aal".
push rdi ; First arg is a pointer to "/bin//ls"
mov rsi, rsp ; Load the address of argv into the second
; parameter to execve.
这还纠正了问题中代码的其他一些问题。它使用 8 字节推送作为文件名,因为 x86-64 不支持 4 字节推送,并且它确保文件名具有空终止符。
此代码确实使用带有 4 字节立即数的 64 位推送来推送“-aal”,因为该字符串适合 4 个字节。这也使得它以空终止,而无需在代码中使用空字节。
我使用了带有双字符的字符串,因为它们在问题中是为了避免代码中出现空字节,但我的偏好是这样的:
mov ecx, "X-al" ; Load second argument string,
shr ecx, 8 ; shift out the dummy character,
push rcx ; and write the string to the stack.
mov rax, rsp ; Load the address of the second argument.
mov rcx, "X/bin/ls" ; Load file name string,
shr rcx, 8 ; shift out the dummy character,
push rcx ; and write the string onto the stack.
请注意,文件名字符串通过移位获得空终止符,从而避免了额外的推送。此模式适用于双字符不起作用的字符串,并且也可以用于较短的字符串。