Сборка execve /bin/bash (x64)

Я новичок в asm и пытаюсь выполнить системный вызов в /bin/bash. Однако в настоящее время я сталкиваюсь со следующей проблемой:

Мой код работает для любого вызова execve, длина 1-го аргумента которого меньше 8 байт, то есть "/ bin / sh" или "/ bin / ls":

.section .data

    name: .string "/bin/sh"

.section .text

.globl _start

_start:
    #third argument of execve, set to NULL
    xor %rdx, %rdx 

    #push nullbyte to the stack
    pushq %rdx 

    #push /bin/sh to the stack
    pushq name 

    #copy stack to rdi, 1st arg of execve
    mov %rsp, %rdi 

    #copy 59 to rax, defining syscall number for execve  
    movq $59, %rax 

    #3rd arg of execve set to NULL
    movq $0, %rsi 

    syscall

Что меня удивляет, так это то, что я не могу заставить его работать

name: .string "/bin/bash"

Я попытался разбить строку на части, вставить в стек "/ bash", затем "/ bin", кажется, ничто не позволяет мне работать, и я каждый раз получаю ошибку "Недопустимая инструкция". Что я делаю неправильно?

Неработающий код:

.section .data

    name: .string "/bin/bash"

.section .text

.globl _start

_start:
    #third argument of execve, set to NULL
    xor %rdx, %rdx 

    #push nullbyte to the stack
    pushq %rdx 

    #push /bin/sh to the stack
    pushq name 

    #copy stack to rdi, 1st arg of execve
    mov %rsp, %rdi 

    #copy 59 to rax, defining syscall number for execve  
    movq $59, %rax 

    #3rd arg of execve set to NULL
    movq $0, %rsi 

    syscall

Другой не рабочий код:

.section .data

.section .text

.globl _start

_start:
    #third argument of execve, set to NULL
    xor %rdx, %rdx 

    #push nullbyte to the stack
    pushq %rdx 

    #push /bin/bash to the stack
    pushq $0x68
    pushq $0x7361622f
    pushq $0x6e69622f

    #copy stack to rdi, 1st arg of execve
    mov %rsp, %rdi 

    #copy 59 to rax, defining syscall number for execve  
    movq $59, %rax 

    #3rd arg of execve set to NULL
    movq $0, %rsi 

    syscall

1 ответ

Решение

Вы, кажется, полностью сбиты с толку, слишком много, чтобы перечислить все ошибки. Тем не менее, вот неполный список:

  1. Вы устанавливаете ESI в ноль argv является NULL
  2. push nullbyte to the stack на самом деле NULL указатель для завершения argv массив (это не нулевой байт, оканчивающий строку).
  3. Вам нужно указать адрес имени файла как argv[0], Вам не нужно копировать строку в стек.

Вот исправленная версия:

.section .data

    name: .string "/bin/bash"

.section .text

.globl _start

_start:
    # third argument of execve is envp, set to NULL
    xor %rdx, %rdx 

    # push NULL to the stack, argv terminator
    pushq %rdx 

    # first argument to execve is the file name
    leaq name, %rdi

    # also argv[0]
    push %rdi

    # second argument to execve is argv
    mov %rsp, %rsi

    #copy 59 to rax, defining syscall number for execve  
    movq $59, %rax 
    syscall

И версия, которая создает строку в стеке из кода без нулевых байтов:

.section .text

.globl _start

_start:
    # third argument of execve is envp, set to NULL
    xor %rdx, %rdx 

    # zero terminator
    push %rdx

    # space for string
    sub $16, %rsp

    # end is aligned to the zero terminator
    movb $0x2f, 7(%rsp)        # /
    movl $0x2f6e6962, 8(%rsp)  # bin/
    movl $0x68736162, 12(%rsp) # bash

    # first argument to execve is the file name
    leaq 7(%rsp), %rdi

    # push NULL to the stack, argv terminator
    pushq %rdx 

    # also argv[0]
    push %rdi

    # second argument to execve is argv
    mov %rsp, %rsi

    # copy 59 to rax, defining syscall number for execve
    # avoid zero byte
    xor %eax, %eax
    movb $59, %al 
    syscall
Другие вопросы по тегам