在Linux x86_64程序集中生成外壳

时间:2019-05-18 14:19:52

标签: linux assembly x86-64 shellcode

我正在尝试创建一个在Linux x86_64程序集中生成'/ bin / sh'shell的shellcode,当我将其作为可执行文件执行时,它就可以正常工作。问题是当我转储代码的二进制文件并将其作为字符串放入时,出现错误:

  

'分段错误:核心已转储'

    global _start

    section .text

    _start:

    push    59   ;sys_execve
    pop rax
    xor rdi,    rdi
    push rdi
    mov rdi,    0x68732F2f6e69622F ;/bin//sh in reverse
    push rdi
    mov rdi,    rsp ;pointer to the /bin//sh
    xor rsi,    rsi ;NULL
    xor rdx,    rdx ;NULL
    syscall

不带二进制的C中的shellcode:

  #include <stdio.h>

  char sh[]="\x6a\x3b\x58\x48\x31\xff\x57\x48\xbf\x2f\x62\x69\x6e\x2f\x2f\x73\x68\x57\x48\x89\xe7\x48\x31\xf6\x48\x31\xd2\x0f\x05 ";

  void main(int argc, char **argv)
 {
     int (*func)();
     func = (int (*)()) sh;
     (int)(*func)();
 }

我用来生成shellcode的命令:

nasm -felf64 shellcode.nasm -o shellcode.o
ld shellcode.o -o shellcode

我用来生成要利用的程序的命令:

gcc -fno-stack-protector -z execstack shellcode.c

strace ./shellcode输出:

    execve("./shellcode", ["./shellcode"], 0x7ffe19f431f0 /* 59 vars */) = 0
brk(NULL)                               = 0x5651f32c3000
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=96319, ...}) = 0
mmap(NULL, 96319, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7ff0d7a8d000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\260\34\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=2030544, ...}) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7ff0d7a8b000
mmap(NULL, 4131552, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7ff0d748d000
mprotect(0x7ff0d7674000, 2097152, PROT_NONE) = 0
mmap(0x7ff0d7874000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1e7000) = 0x7ff0d7874000
mmap(0x7ff0d787a000, 15072, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7ff0d787a000
close(3)                                = 0
arch_prctl(ARCH_SET_FS, 0x7ff0d7a8c4c0) = 0
mprotect(0x7ff0d7874000, 16384, PROT_READ) = 0
mprotect(0x5651f168d000, 4096, PROT_READ) = 0
mprotect(0x7ff0d7aa5000, 4096, PROT_READ) = 0
munmap(0x7ff0d7a8d000, 96319)           = 0
--- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_ACCERR, si_addr=0x5651f168e020} ---
+++ killed by SIGSEGV (core dumped) +++
Segmentation fault (core dumped)

1 个答案:

答案 0 :(得分:2)

gcc -fno-stack-protector -z execstack shellcode.c不会创建名为shellcode的文件。 因为您没有使用a.out,它会创建-o

因此,运行strace ./shellcode将运行您用NASM + ld生成的二进制文件。但是您显示的strace输出与静态可执行文件不匹配。也许是由于先前的gcc调用导致您忘记了-z execstack

运行strace ./a.out ,以运行来自当前源版本的gcc构建的文件。


如果您将错误的args传递给execve,则strace将显示它返回-EFAULT。但是,仅在没有这种情况的情况下进行段错误可能就意味着您运行的可执行文件试图跳至不可执行的页面。如果没有-zexecstack,这将完全符合二进制版本。